Skip to content

Commit 59e1fe2

Browse files
matzclaude
andcommitted
mrbconf.h: rename MRB_WORDBOX_NO_FLOAT_TRUNCATE to MRB_WORDBOX_NO_INLINE_FLOAT
The old name referred to "truncation" of float precision, which no longer happens with rotation encoding. The new name describes the actual behavior: disabling inline float encoding in word boxing. The old name is kept as an obsolete alias for backward compatibility. Co-authored-by: Claude <noreply@anthropic.com>
1 parent 32f99a6 commit 59e1fe2

7 files changed

Lines changed: 29 additions & 23 deletions

File tree

doc/internal/boxing.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ The Word boxing packing bit patterns are like following:
2626
| undef | `00000000 00000000 00000000 00010100` |
2727
| symbol | `xxxxxxxx xxxxxxxx xxxxxxxx xxxxxx10` |
2828

29-
On 64-bit platforms (unless `MRB_WORDBOX_NO_FLOAT_TRUNCATE`), float values are also packed in the `mrb_value`. In that case, we drop least significant 2 bits from mantissa.
30-
If you need full precision for floating-point numbers, define `MRB_WORDBOX_NO_FLOAT_TRUNCATE`.
29+
On 64-bit platforms (unless `MRB_WORDBOX_NO_INLINE_FLOAT`), float values are also packed in the `mrb_value` using rotation encoding (lossless for exponents in range).
30+
To disable inline float encoding and heap-allocate all floats, define `MRB_WORDBOX_NO_INLINE_FLOAT`.
3131

3232
## NaN Boxing
3333

doc/mruby3.1.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Keyword arguments are basically separated from ordinal arguments.
2020

2121
Some configuration macros are available:
2222

23-
- `MRB_WORDBOX_NO_FLOAT_TRUNCATE`: by default, float values are packed in the word if possible, but define this macro to allocate float values in the heap.
23+
- `MRB_WORDBOX_NO_INLINE_FLOAT` (formerly `MRB_WORDBOX_NO_FLOAT_TRUNCATE`): by default, float values are packed in the word if possible, but define this macro to allocate float values in the heap.
2424
- `MRB_USE_RO_DATA_P_ETEXT`: define this macro if `_etext` is available on your platform.
2525
- `MRB_NO_DEFAULT_RO_DATA_P`: define this macro to avoid using predefined `mrb_ro_data_p()` function
2626

@@ -156,7 +156,7 @@ Now takes 2 operands and pushes multiple entries to an array.
156156
### Word Boxing
157157

158158
`MRB_WORD_BOXING` now packs floating-point numbers in the word, if the size of `mrb_float` is equal or smaller than the size of `mrb_int` by default.
159-
If the size of `mrb_float` and `mrb_int` are same, the last 2 bits in the `mrb_float` are trimmed and used as flags. If you need full precision, you need to define `MRB_WORDBOX_NO_FLOAT_TRUNCATE` as described above.
159+
If the size of `mrb_float` and `mrb_int` are same, the last 2 bits in the `mrb_float` are trimmed and used as flags. If you need full precision, you need to define `MRB_WORDBOX_NO_INLINE_FLOAT` (formerly `MRB_WORDBOX_NO_FLOAT_TRUNCATE`) as described above.
160160

161161
### NaN Boxing
162162

include/mrbconf.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,14 @@
6464
# define MRB_WORD_BOXING
6565
#endif
6666

67-
/* if defined mruby allocates Float objects in the heap to keep full precision if needed */
68-
//#define MRB_WORDBOX_NO_FLOAT_TRUNCATE
67+
/* if defined mruby does not inline float values in word boxing;
68+
all floats are heap-allocated as RFloat objects */
69+
//#define MRB_WORDBOX_NO_INLINE_FLOAT
70+
71+
/* obsolete configuration */
72+
#if defined(MRB_WORDBOX_NO_FLOAT_TRUNCATE)
73+
# define MRB_WORDBOX_NO_INLINE_FLOAT
74+
#endif
6975

7076
/* add -DMRB_INT32 to use 32-bit integer for mrb_int; conflict with MRB_INT64;
7177
Default for 32-bit CPU mode. */

include/mruby/boxing_word.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
#ifndef MRUBY_BOXING_WORD_H
88
#define MRUBY_BOXING_WORD_H
99

10-
#if defined(MRB_32BIT) && !defined(MRB_USE_FLOAT32) && !defined(MRB_WORDBOX_NO_FLOAT_TRUNCATE)
11-
# define MRB_WORDBOX_NO_FLOAT_TRUNCATE
10+
#if defined(MRB_32BIT) && !defined(MRB_USE_FLOAT32) && !defined(MRB_WORDBOX_NO_INLINE_FLOAT)
11+
# define MRB_WORDBOX_NO_INLINE_FLOAT
1212
#endif
1313

1414
#ifndef MRB_NO_FLOAT
1515
struct RFloat {
1616
MRB_OBJECT_HEADER;
17-
#ifdef MRB_WORDBOX_NO_FLOAT_TRUNCATE
17+
#ifdef MRB_WORDBOX_NO_INLINE_FLOAT
1818
/* avoid 8-byte alignment on 32-bit; use memcpy-based accessors */
1919
char f[sizeof(mrb_float)];
2020
#else
@@ -27,7 +27,7 @@ struct RFloat {
2727
static inline mrb_float
2828
mrb_rfloat_value(const struct RFloat *p)
2929
{
30-
#ifdef MRB_WORDBOX_NO_FLOAT_TRUNCATE
30+
#ifdef MRB_WORDBOX_NO_INLINE_FLOAT
3131
mrb_float f;
3232
memcpy(&f, p->f, sizeof(mrb_float));
3333
return f;
@@ -39,7 +39,7 @@ mrb_rfloat_value(const struct RFloat *p)
3939
static inline void
4040
mrb_rfloat_set(struct RFloat *p, mrb_float f)
4141
{
42-
#ifdef MRB_WORDBOX_NO_FLOAT_TRUNCATE
42+
#ifdef MRB_WORDBOX_NO_INLINE_FLOAT
4343
memcpy(p->f, &f, sizeof(mrb_float));
4444
#else
4545
p->f = f;
@@ -79,7 +79,7 @@ enum mrb_special_consts {
7979
#define WORDBOX_FIXNUM_FLAG (1 << (WORDBOX_FIXNUM_BIT_POS - 1))
8080
#define WORDBOX_FIXNUM_MASK ((1 << WORDBOX_FIXNUM_BIT_POS) - 1)
8181

82-
#if defined(MRB_WORDBOX_NO_FLOAT_TRUNCATE) || defined(MRB_NO_FLOAT)
82+
#if defined(MRB_WORDBOX_NO_INLINE_FLOAT) || defined(MRB_NO_FLOAT)
8383
/* floats are allocated in heaps */
8484
#define WORDBOX_IMMEDIATE_MASK 0x03
8585
#define WORDBOX_SYMBOL_BIT_POS 2
@@ -127,7 +127,7 @@ enum mrb_special_consts {
127127
* float : ...FFFF FF10 (float32 shifted left by 2)
128128
* (other values same as above)
129129
*
130-
* word boxing without inline float (MRB_WORDBOX_NO_FLOAT_TRUNCATE):
130+
* word boxing without inline float (MRB_WORDBOX_NO_INLINE_FLOAT):
131131
* nil : ...0000 0000 (all bits are 0)
132132
* false : ...0000 0100 (mrb_fixnum(v) != 0)
133133
* true : ...0000 1100
@@ -145,7 +145,7 @@ union mrb_value_ {
145145
struct RBasic *bp;
146146
#ifndef MRB_NO_FLOAT
147147
struct RFloat *fp;
148-
#if !defined(MRB_WORDBOX_NO_FLOAT_TRUNCATE) && defined(MRB_USE_FLOAT32)
148+
#if !defined(MRB_WORDBOX_NO_INLINE_FLOAT) && defined(MRB_USE_FLOAT32)
149149
mrb_float f;
150150
#endif
151151
#endif
@@ -180,7 +180,7 @@ MRB_API mrb_value mrb_boxing_int_value(struct mrb_state*, mrb_int);
180180
#define mrb_ptr(o) mrb_val_union(o).p
181181
#define mrb_cptr(o) mrb_val_union(o).vp->p
182182
#ifndef MRB_NO_FLOAT
183-
#ifndef MRB_WORDBOX_NO_FLOAT_TRUNCATE
183+
#ifndef MRB_WORDBOX_NO_INLINE_FLOAT
184184
MRB_API mrb_float mrb_word_boxing_value_float(mrb_value v);
185185
#define mrb_float(o) mrb_word_boxing_value_float(o)
186186
#else
@@ -205,7 +205,7 @@ mrb_integer_func(mrb_value o) {
205205
#define mrb_false_p(o) ((o).w == MRB_Qfalse)
206206
#define mrb_true_p(o) ((o).w == MRB_Qtrue)
207207
#ifndef MRB_NO_FLOAT
208-
#ifdef MRB_WORDBOX_NO_FLOAT_TRUNCATE
208+
#ifdef MRB_WORDBOX_NO_INLINE_FLOAT
209209
#define mrb_float_p(o) WORDBOX_OBJ_TYPE_P(o, FLOAT)
210210
#elif defined(MRB_USE_FLOAT32) && defined(MRB_64BIT)
211211
#define mrb_float_p(o) WORDBOX_SHIFT_VALUE_P(o, FLOAT)

mrbgems/mruby-test/driver.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ mrb_init_test_driver(mrb_state *mrb, mrb_bool verbose)
221221

222222
#ifndef MRB_NO_FLOAT
223223
#ifdef MRB_USE_FLOAT32
224-
#ifdef MRB_WORDBOX_NO_FLOAT_TRUNCATE
224+
#ifdef MRB_WORDBOX_NO_INLINE_FLOAT
225225
mrb_define_const(mrb, mrbtest, "FLOAT_TOLERANCE", mrb_float_value(mrb, 1e-5));
226226
#else
227227
mrb_define_const(mrb, mrbtest, "FLOAT_TOLERANCE", mrb_float_value(mrb, 1e-4));

src/etc.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ mrb_obj_id(mrb_value obj)
178178
#ifndef MRB_NO_FLOAT
179179
/*
180180
* Boxes a `mrb_float` into an `mrb_value` using word boxing.
181-
* - If `MRB_WORDBOX_NO_FLOAT_TRUNCATE` is defined, it allocates a new
181+
* - If `MRB_WORDBOX_NO_INLINE_FLOAT` is defined, it allocates a new
182182
* RFloat object on the heap.
183183
* - If `MRB_64BIT` and `MRB_USE_FLOAT32` are defined, it stores the float
184184
* in the lower bits of the word, shifted and tagged.
@@ -187,7 +187,7 @@ mrb_obj_id(mrb_value obj)
187187
* Floats outside the inline range are heap-allocated as RFloat.
188188
*/
189189

190-
#if !defined(MRB_WORDBOX_NO_FLOAT_TRUNCATE) && \
190+
#if !defined(MRB_WORDBOX_NO_INLINE_FLOAT) && \
191191
(!defined(MRB_USE_FLOAT32) || !defined(MRB_64BIT))
192192
/*
193193
* Rotation-based float encoding (shared between 64-bit float64 and
@@ -286,7 +286,7 @@ mrb_word_boxing_float_value(mrb_state *mrb, mrb_float f)
286286
{
287287
union mrb_value_ v;
288288

289-
#ifdef MRB_WORDBOX_NO_FLOAT_TRUNCATE
289+
#ifdef MRB_WORDBOX_NO_INLINE_FLOAT
290290
v.p = mrb_obj_alloc(mrb, MRB_TT_FLOAT, mrb->float_class);
291291
mrb_rfloat_set(v.fp, f);
292292
v.bp->frozen = 1;
@@ -366,7 +366,7 @@ mrb_word_boxing_float_value(mrb_state *mrb, mrb_float f)
366366
}
367367

368368

369-
#ifndef MRB_WORDBOX_NO_FLOAT_TRUNCATE
369+
#ifndef MRB_WORDBOX_NO_INLINE_FLOAT
370370
/*
371371
* Unboxes an `mrb_value` to an `mrb_float`.
372372
* - 64-bit + float32: right-shift by 2 to retrieve the float.

src/gc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ struct free_obj {
121121

122122
struct RVALUE_initializer {
123123
MRB_OBJECT_HEADER;
124-
#if defined(MRB_WORD_BOXING) && defined(MRB_32BIT) && defined(MRB_USE_FLOAT32) && !defined(MRB_WORDBOX_NO_FLOAT_TRUNCATE)
124+
#if defined(MRB_WORD_BOXING) && defined(MRB_32BIT) && defined(MRB_USE_FLOAT32) && !defined(MRB_WORDBOX_NO_INLINE_FLOAT)
125125
/* inline float word boxing needs 8-byte aligned objects;
126126
pad RVALUE to 24 bytes (multiple of 8) on 32-bit */
127127
char padding[sizeof(void*) * 4];
@@ -1680,7 +1680,7 @@ mrb_init_gc(mrb_state *mrb)
16801680
{
16811681
struct RClass *gc;
16821682

1683-
#if defined(MRB_WORD_BOXING) && defined(MRB_32BIT) && defined(MRB_USE_FLOAT32) && !defined(MRB_WORDBOX_NO_FLOAT_TRUNCATE)
1683+
#if defined(MRB_WORD_BOXING) && defined(MRB_32BIT) && defined(MRB_USE_FLOAT32) && !defined(MRB_WORDBOX_NO_INLINE_FLOAT)
16841684
/* 6 words: padded to 8-byte alignment for inline float word boxing */
16851685
mrb_static_assert(sizeof(RVALUE) <= sizeof(void*) * 6,
16861686
"RVALUE size must be within 6 words");

0 commit comments

Comments
 (0)