Skip to content

Commit b8cfb0d

Browse files
committed
py: Add support for 64-bit NaN-boxing object model, on 32-bit machine.
To use, put the following in mpconfigport.h: #define MICROPY_OBJ_REPR (MICROPY_OBJ_REPR_D) #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_DOUBLE) typedef int64_t mp_int_t; typedef uint64_t mp_uint_t; #define UINT_FMT "%llu" #define INT_FMT "%lld" Currently does not work with native emitter enabled.
1 parent 999cedb commit b8cfb0d

File tree

6 files changed

+94
-3
lines changed

6 files changed

+94
-3
lines changed

py/compile.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2439,7 +2439,12 @@ STATIC void compile_bytes(compiler_t *comp, mp_parse_node_struct_t *pns) {
24392439
}
24402440

24412441
STATIC void compile_const_object(compiler_t *comp, mp_parse_node_struct_t *pns) {
2442+
#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
2443+
// nodes are 32-bit pointers, but need to extract 64-bit object
2444+
EMIT_ARG(load_const_obj, (uint64_t)pns->nodes[0] | ((uint64_t)pns->nodes[1] << 32));
2445+
#else
24422446
EMIT_ARG(load_const_obj, (mp_obj_t)pns->nodes[0]);
2447+
#endif
24432448
}
24442449

24452450
typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);

py/mpconfig.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@
7676

7777
#define MICROPY_OBJ_REPR_C (2)
7878

79+
// A MicroPython object is a 64-bit word having the following form (called R):
80+
// - seeeeeee eeeeffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff 64-bit fp, e != 0x7ff
81+
// - s1111111 11110000 00000000 00000000 00000000 00000000 00000000 00000000 +/- inf
82+
// - 01111111 11111000 00000000 00000000 00000000 00000000 00000000 00000000 normalised nan
83+
// - 01111111 11111101 00000000 00000000 iiiiiiii iiiiiiii iiiiiiii iiiiiii1 small int
84+
// - 01111111 11111110 00000000 00000000 qqqqqqqq qqqqqqqq qqqqqqqq qqqqqqq1 str
85+
// - 01111111 11111100 00000000 00000000 pppppppp pppppppp pppppppp pppppp00 ptr (4 byte alignment)
86+
// Stored as O = R + 0x8004000000000000, retrieved as R = O - 0x8004000000000000.
87+
// This makes pointers have all zeros in the top 32 bits.
88+
// Small-ints and strs have 1 as LSB to make sure they don't look like pointers
89+
// to the garbage collector.
90+
#define MICROPY_OBJ_REPR_D (3)
91+
7992
#ifndef MICROPY_OBJ_REPR
8093
#define MICROPY_OBJ_REPR (MICROPY_OBJ_REPR_A)
8194
#endif

py/obj.h

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,15 @@
3232
#include "py/mpprint.h"
3333

3434
// All Micro Python objects are at least this type
35-
// It must be of pointer size
35+
// The bit-size must be at least pointer size
3636

37+
#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
38+
typedef uint64_t mp_obj_t;
39+
typedef uint64_t mp_const_obj_t;
40+
#else
3741
typedef machine_ptr_t mp_obj_t;
3842
typedef machine_const_ptr_t mp_const_obj_t;
43+
#endif
3944

4045
// Anything that wants to be a Micro Python object must have
4146
// mp_obj_base_t as its first member (except small ints and qstrs)
@@ -158,6 +163,56 @@ static inline bool MP_OBJ_IS_QSTR(mp_const_obj_t o)
158163
static inline bool MP_OBJ_IS_OBJ(mp_const_obj_t o)
159164
{ return ((((mp_int_t)(o)) & 3) == 0); }
160165

166+
#elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
167+
168+
static inline bool MP_OBJ_IS_SMALL_INT(mp_const_obj_t o)
169+
{ return ((((mp_int_t)(o)) & 0xffff000000000000) == 0x0001000000000000); }
170+
#define MP_OBJ_SMALL_INT_VALUE(o) (((intptr_t)(o)) >> 1)
171+
#define MP_OBJ_NEW_SMALL_INT(small_int) ((mp_obj_t)(((uintptr_t)(small_int)) << 1) | 0x0001000000000001)
172+
173+
static inline bool MP_OBJ_IS_QSTR(mp_const_obj_t o)
174+
{ return ((((mp_int_t)(o)) & 0xffff000000000000) == 0x0002000000000000); }
175+
#define MP_OBJ_QSTR_VALUE(o) ((((mp_uint_t)(o)) >> 1) & 0xffffffff)
176+
#define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 1) | 0x0002000000000001))
177+
178+
#if MICROPY_PY_BUILTINS_FLOAT
179+
#define mp_const_float_e {((mp_obj_t)((uint64_t)0x4005bf0a8b125769 + 0x8004000000000000))}
180+
#define mp_const_float_pi {((mp_obj_t)((uint64_t)0x400921fb54442d18 + 0x8004000000000000))}
181+
182+
static inline bool mp_obj_is_float(mp_const_obj_t o) {
183+
return ((uint64_t)(o) & 0xfffc000000000000) != 0;
184+
}
185+
static inline mp_float_t mp_obj_float_get(mp_const_obj_t o) {
186+
union {
187+
mp_float_t f;
188+
uint64_t r;
189+
} num = {.r = o - 0x8004000000000000};
190+
return num.f;
191+
}
192+
static inline mp_obj_t mp_obj_new_float(mp_float_t f) {
193+
union {
194+
mp_float_t f;
195+
uint64_t r;
196+
} num = {.f = f};
197+
return num.r + 0x8004000000000000;
198+
}
199+
#endif
200+
201+
static inline bool MP_OBJ_IS_OBJ(mp_const_obj_t o)
202+
{ return ((((uint64_t)(o)) & 0xffff000000000000) == 0x0000000000000000); }
203+
#define MP_OBJ_TO_PTR(o) ((void*)(uintptr_t)(o))
204+
#define MP_OBJ_FROM_PTR(p) ((mp_obj_t)((uintptr_t)(p)))
205+
206+
// rom object storage needs special handling to widen 32-bit pointer to 64-bits
207+
typedef union _mp_rom_obj_t { uint64_t u64; struct { const void *lo, *hi; } u32; } mp_rom_obj_t;
208+
#define MP_ROM_INT(i) {MP_OBJ_NEW_SMALL_INT(i)}
209+
#define MP_ROM_QSTR(q) {MP_OBJ_NEW_QSTR(q)}
210+
#if MP_ENDIANNESS_LITTLE
211+
#define MP_ROM_PTR(p) {.u32 = {.lo = (p), .hi = NULL}}
212+
#else
213+
#define MP_ROM_PTR(p) {.u32 = {.lo = NULL, .hi = (p)}}
214+
#endif
215+
161216
#endif
162217

163218
// Macros to convert between mp_obj_t and concrete object types.

py/objfloat.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
#include <math.h>
4040
#include "py/formatfloat.h"
4141

42-
#if MICROPY_OBJ_REPR != MICROPY_OBJ_REPR_C
42+
#if MICROPY_OBJ_REPR != MICROPY_OBJ_REPR_C && MICROPY_OBJ_REPR != MICROPY_OBJ_REPR_D
4343

4444
typedef struct _mp_obj_float_t {
4545
mp_obj_base_t base;
@@ -125,7 +125,7 @@ const mp_obj_type_t mp_type_float = {
125125
.binary_op = float_binary_op,
126126
};
127127

128-
#if MICROPY_OBJ_REPR != MICROPY_OBJ_REPR_C
128+
#if MICROPY_OBJ_REPR != MICROPY_OBJ_REPR_C && MICROPY_OBJ_REPR != MICROPY_OBJ_REPR_D
129129

130130
mp_obj_t mp_obj_new_float(mp_float_t value) {
131131
mp_obj_float_t *o = m_new(mp_obj_float_t, 1);

py/parse.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,11 @@ void mp_parse_node_print(mp_parse_node_t pn, mp_uint_t indent) {
283283
} else if (MP_PARSE_NODE_STRUCT_KIND(pns) == RULE_bytes) {
284284
printf("literal bytes(%.*s)\n", (int)pns->nodes[1], (char*)pns->nodes[0]);
285285
} else if (MP_PARSE_NODE_STRUCT_KIND(pns) == RULE_const_object) {
286+
#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
287+
printf("literal const(%016llx)\n", (uint64_t)pns->nodes[0] | ((uint64_t)pns->nodes[1] << 32));
288+
#else
286289
printf("literal const(%p)\n", (mp_obj_t)pns->nodes[0]);
290+
#endif
287291
} else {
288292
mp_uint_t n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
289293
#ifdef USE_RULE_NAME
@@ -362,8 +366,15 @@ STATIC mp_parse_node_t make_node_const_object(parser_t *parser, mp_uint_t src_li
362366
return MP_PARSE_NODE_NULL;
363367
}
364368
pn->source_line = src_line;
369+
#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
370+
// nodes are 32-bit pointers, but need to store 64-bit object
371+
pn->kind_num_nodes = RULE_const_object | (2 << 8);
372+
pn->nodes[0] = (uint64_t)obj;
373+
pn->nodes[1] = (uint64_t)obj >> 32;
374+
#else
365375
pn->kind_num_nodes = RULE_const_object | (1 << 8);
366376
pn->nodes[0] = (mp_uint_t)obj;
377+
#endif
367378
return (mp_parse_node_t)pn;
368379
}
369380

py/smallint.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@
4646
// Mask to truncate mp_int_t to positive value
4747
#define MP_SMALL_INT_POSITIVE_MASK ~(WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1) | (WORD_MSBIT_HIGH >> 2))
4848

49+
#elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
50+
51+
#define MP_SMALL_INT_MIN ((mp_int_t)(((mp_int_t)0xffffffff80000000) >> 1))
52+
#define MP_SMALL_INT_FITS(n) ((((n) ^ ((n) << 1)) & 0xffffffff80000000) == 0)
53+
// Mask to truncate mp_int_t to positive value
54+
#define MP_SMALL_INT_POSITIVE_MASK ~(0xffffffff80000000 | (0xffffffff80000000 >> 1))
55+
4956
#endif
5057

5158
#define MP_SMALL_INT_MAX ((mp_int_t)(~(MP_SMALL_INT_MIN)))

0 commit comments

Comments
 (0)