Skip to content

Commit 72ae3c7

Browse files
committed
tools/mpy-tool.py: Support freezing float literals with obj-repr C.
The tool now generates code for freezing floats in obj-repr A, B or C, with the specific representation detected at compile time using macros.
1 parent 8a15e0b commit 72ae3c7

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

tools/mpy-tool.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
# end compatibility code
4343

4444
import sys
45+
import struct
4546
from collections import namedtuple
4647

4748
sys.path.append('../py')
@@ -315,9 +316,10 @@ def freeze(self, parent_name):
315316
'{.neg=%u, .fixed_dig=1, .alloc=%u, .len=%u, .dig=(uint%u_t[]){%s}}};'
316317
% (obj_name, neg, ndigs, ndigs, bits_per_dig, digs))
317318
elif type(obj) is float:
318-
# works for REPR A and B only
319+
print('#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A || MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B')
319320
print('STATIC const mp_obj_float_t %s = {{&mp_type_float}, %.16g};'
320321
% (obj_name, obj))
322+
print('#endif')
321323
else:
322324
# TODO
323325
raise FreezeError(self, 'freezing of object %r is not implemented' % (obj,))
@@ -328,7 +330,18 @@ def freeze(self, parent_name):
328330
for qst in self.qstrs:
329331
print(' (mp_uint_t)MP_OBJ_NEW_QSTR(%s),' % global_qstrs[qst].qstr_id)
330332
for i in range(len(self.objs)):
331-
print(' (mp_uint_t)&const_obj_%s_%u,' % (self.escaped_name, i))
333+
if type(self.objs[i]) is float:
334+
print('#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A || MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B')
335+
print(' (mp_uint_t)&const_obj_%s_%u,' % (self.escaped_name, i))
336+
print('#elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C')
337+
n = struct.unpack('<I', struct.pack('<f', self.objs[i]))[0]
338+
n = ((n & ~0x3) | 2) + 0x80800000
339+
print(' (mp_uint_t)0x%08x,' % (n,))
340+
print('#else')
341+
print('#error "MICROPY_OBJ_REPR_D not supported with floats in frozen mpy files"')
342+
print('#endif')
343+
else:
344+
print(' (mp_uint_t)&const_obj_%s_%u,' % (self.escaped_name, i))
332345
for rc in self.raw_codes:
333346
print(' (mp_uint_t)&raw_code_%s,' % rc.escaped_name)
334347
print('};')

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy