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 +++++++++++++++++++++-------------- src/load.c | 26 ++++++++++++++++++-------- 2 files changed, 39 insertions(+), 22 deletions(-) (limited to 'src') 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 diff --git a/src/load.c b/src/load.c index 2cc0f7557..392a38456 100644 --- a/src/load.c +++ b/src/load.c @@ -15,6 +15,7 @@ #include #include #include +#include #if SIZE_MAX < UINT32_MAX # error size_t must be at least 32 bits wide @@ -40,17 +41,26 @@ offset_crc_body(void) } #ifndef MRB_NO_FLOAT -double mrb_str_len_to_dbl(mrb_state *mrb, const char *s, size_t len, mrb_bool badcheck); - static double str_to_double(mrb_state *mrb, const char *p, size_t len) { - /* `i`, `inf`, `infinity` */ - if (len > 0 && p[0] == 'i') return INFINITY; - - /* `I`, `-inf`, `-infinity` */ - if (p[0] == 'I' || (len > 1 && p[0] == '-' && p[1] == 'i')) return -INFINITY; - return mrb_str_len_to_dbl(mrb, p, len, TRUE); + /* dump IEEE754 little endian binary */ + union { + char s[sizeof(double)]; + double f; + } u; + + mrb_assert(sizeof(double)==len); + if (littleendian) { + memcpy(u.s, p, sizeof(double)); + } + else { + int i; + for (i=0; i