Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit72ae3c7

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 parent8a15e0b commit72ae3c7

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
importsys
45+
importstruct
4546
fromcollectionsimportnamedtuple
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
eliftype(obj)isfloat:
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
raiseFreezeError(self,'freezing of object %r is not implemented'% (obj,))
@@ -328,7 +330,18 @@ def freeze(self, parent_name):
328330
forqstinself.qstrs:
329331
print(' (mp_uint_t)MP_OBJ_NEW_QSTR(%s),'%global_qstrs[qst].qstr_id)
330332
foriinrange(len(self.objs)):
331-
print(' (mp_uint_t)&const_obj_%s_%u,'% (self.escaped_name,i))
333+
iftype(self.objs[i])isfloat:
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
forrcinself.raw_codes:
333346
print(' (mp_uint_t)&raw_code_%s,'%rc.escaped_name)
334347
print('};')

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp