mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Change float representation in mrb binary files.
From human readable (ASCII) string representation to binary dump of IEEE754 in little endian.
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
** @file mruby/endian.h - detect endian-ness
|
||||
**
|
||||
** See Copyright Notice in mruby.h
|
||||
*/
|
||||
|
||||
#ifndef MRUBY_ENDIAN_H
|
||||
#define MRUBY_ENDIAN_H
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
MRB_BEGIN_DECL
|
||||
|
||||
#if !defined(BYTE_ORDER) && defined(__BYTE_ORDER__)
|
||||
# define BYTE_ORDER __BYTE_ORDER__
|
||||
#endif
|
||||
#if !defined(BIG_ENDIAN) && defined(__ORDER_BIG_ENDIAN__)
|
||||
# define BIG_ENDIAN __ORDER_BIG_ENDIAN__
|
||||
#endif
|
||||
#if !defined(LITTLE_ENDIAN) && defined(__ORDER_LITTLE_ENDIAN__)
|
||||
# define LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
|
||||
#endif
|
||||
|
||||
#ifdef BYTE_ORDER
|
||||
# if BYTE_ORDER == BIG_ENDIAN
|
||||
# define littleendian 0
|
||||
# elif BYTE_ORDER == LITTLE_ENDIAN
|
||||
# define littleendian 1
|
||||
# endif
|
||||
#endif
|
||||
#ifndef littleendian
|
||||
/* can't distinguish endian in compile time */
|
||||
static inline int
|
||||
check_little_endian(void)
|
||||
{
|
||||
unsigned int n = 1;
|
||||
return (*(unsigned char *)&n == 1);
|
||||
}
|
||||
# define littleendian check_little_endian()
|
||||
#endif
|
||||
|
||||
MRB_END_DECL
|
||||
|
||||
#endif /* MRUBY_ENDIAN_H */
|
||||
@@ -9,10 +9,10 @@
|
||||
#include "mruby/numeric.h"
|
||||
#include "mruby/string.h"
|
||||
#include "mruby/variable.h"
|
||||
#include "mruby/endian.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
|
||||
struct tmpl {
|
||||
@@ -63,36 +63,6 @@ const static unsigned char base64chars[] =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
static unsigned char base64_dec_tab[128];
|
||||
|
||||
#if !defined(BYTE_ORDER) && defined(__BYTE_ORDER__)
|
||||
# define BYTE_ORDER __BYTE_ORDER__
|
||||
#endif
|
||||
#if !defined(BIG_ENDIAN) && defined(__ORDER_BIG_ENDIAN__)
|
||||
# define BIG_ENDIAN __ORDER_BIG_ENDIAN__
|
||||
#endif
|
||||
#if !defined(LITTLE_ENDIAN) && defined(__ORDER_LITTLE_ENDIAN__)
|
||||
# define LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
|
||||
#endif
|
||||
|
||||
#ifdef BYTE_ORDER
|
||||
# if BYTE_ORDER == BIG_ENDIAN
|
||||
# define littleendian 0
|
||||
# define check_little_endian() (void)0
|
||||
# elif BYTE_ORDER == LITTLE_ENDIAN
|
||||
# define littleendian 1
|
||||
# define check_little_endian() (void)0
|
||||
# endif
|
||||
#endif
|
||||
#ifndef littleendian
|
||||
/* can't distinguish endian in compile time */
|
||||
static int littleendian = 0;
|
||||
static void
|
||||
check_little_endian(void)
|
||||
{
|
||||
unsigned int n = 1;
|
||||
littleendian = (*(unsigned char *)&n == 1);
|
||||
}
|
||||
#endif
|
||||
|
||||
static unsigned int
|
||||
hex2int(unsigned char ch)
|
||||
{
|
||||
@@ -1426,7 +1396,6 @@ mrb_pack_unpack1(mrb_state *mrb, mrb_value str)
|
||||
void
|
||||
mrb_mruby_pack_gem_init(mrb_state *mrb)
|
||||
{
|
||||
check_little_endian();
|
||||
make_base64_dec_tab();
|
||||
|
||||
mrb_define_method(mrb, mrb->array_class, "pack", mrb_pack_pack, MRB_ARGS_REQ(1));
|
||||
|
||||
+21
-14
@@ -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
|
||||
|
||||
+17
-7
@@ -15,6 +15,7 @@
|
||||
#include <mruby/debug.h>
|
||||
#include <mruby/error.h>
|
||||
#include <mruby/data.h>
|
||||
#include <mruby/endian.h>
|
||||
|
||||
#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;
|
||||
/* dump IEEE754 little endian binary */
|
||||
union {
|
||||
char s[sizeof(double)];
|
||||
double f;
|
||||
} u;
|
||||
|
||||
/* `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);
|
||||
mrb_assert(sizeof(double)==len);
|
||||
if (littleendian) {
|
||||
memcpy(u.s, p, sizeof(double));
|
||||
}
|
||||
else {
|
||||
int i;
|
||||
for (i=0; i<sizeof(double); i++) {
|
||||
u.s[i] = p[sizeof(double)-i-1];
|
||||
}
|
||||
}
|
||||
return u.f;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user