From 471479e723157c1a7df2023ab2eea24fa4ca2246 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Fri, 28 Aug 2020 17:33:16 +0900 Subject: Change float representation in `mrb` binary files. From human readable (ASCII) string representation to binary dump of IEEE754 in little endian. --- src/dump.c | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) (limited to 'src/dump.c') diff --git a/src/dump.c b/src/dump.c index 489e70f8a..243278103 100644 --- a/src/dump.c +++ b/src/dump.c @@ -12,6 +12,7 @@ #include #include #include +#include #ifndef MRB_NO_FLOAT #ifdef MRB_USE_FLOAT32 @@ -89,13 +90,25 @@ write_iseq_block(mrb_state *mrb, const mrb_irep *irep, uint8_t *buf, uint8_t fla } #ifndef MRB_NO_FLOAT -static mrb_value -float_to_str(mrb_state *mrb, mrb_float f) +static void +dump_float(mrb_state *mrb, uint8_t *buf, mrb_float f) { - if (isinf(f)) { - return f < 0 ? mrb_str_new_lit(mrb, "I") : mrb_str_new_lit(mrb, "i"); + /* dump IEEE754 binary in little endian */ + union { + double f; + char s[sizeof(double)]; + } u = {.f = (double)f}; + + if (littleendian) { + memcpy(buf, u.s, sizeof(double)); + } + else { + int i; + + for (i=0; ipool[pool_no].u.f); - mrb_int len = RSTRING_LEN(str); - mrb_assert_int_fit(mrb_int, len, size_t, SIZE_MAX); - size += (size_t)len; + size += sizeof(double); } #endif break; @@ -194,12 +204,9 @@ write_pool_block(mrb_state *mrb, const mrb_irep *irep, uint8_t *buf) cur += uint8_to_bin(IREP_TT_FLOAT, cur); /* data type */ #ifndef MRB_NO_FLOAT { - mrb_value str = float_to_str(mrb, irep->pool[pool_no].u.f); - ptr = RSTRING_PTR(str); - len = RSTRING_LEN(str); - mrb_assert_int_fit(mrb_int, len, uint16_t, UINT16_MAX); + len = sizeof(double); cur += uint16_to_bin((uint16_t)len, cur); /* data length */ - memcpy(cur, ptr, (size_t)len); + dump_float(mrb, cur,irep->pool[pool_no].u.f); cur += len; } #else -- cgit v1.2.3