diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2020-08-28 17:33:16 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2020-10-12 18:20:06 +0900 |
| commit | 471479e723157c1a7df2023ab2eea24fa4ca2246 (patch) | |
| tree | 8d5c18642f44ca2f47832b66b6a49e5f0250afb7 /src/dump.c | |
| parent | 397b005715b443ff8175ffe3549f13ebfc6a9e7f (diff) | |
| download | mruby-471479e723157c1a7df2023ab2eea24fa4ca2246.tar.gz mruby-471479e723157c1a7df2023ab2eea24fa4ca2246.zip | |
Change float representation in `mrb` binary files.
From human readable (ASCII) string representation to binary dump of
IEEE754 in little endian.
Diffstat (limited to 'src/dump.c')
| -rw-r--r-- | src/dump.c | 35 |
1 files changed, 21 insertions, 14 deletions
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 <mruby/irep.h> #include <mruby/numeric.h> #include <mruby/debug.h> +#include <mruby/endian.h> #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; i<sizeof(double); i++) { + buf[i] = u.s[sizeof(double)-i-1]; + } } - return mrb_float_to_str(mrb, mrb_float_value(mrb, f), MRB_FLOAT_FMT); } #endif @@ -133,10 +146,7 @@ get_pool_block_size(mrb_state *mrb, const mrb_irep *irep) case IREP_TT_FLOAT: #ifndef MRB_NO_FLOAT { - mrb_value str = float_to_str(mrb, irep->pool[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 |
