8000 py/binary: Use _Float16 for half-precision when available · micropython/micropython@05916c6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 05916c6

Browse files
committed
py/binary: Use _Float16 for half-precision when available
Signed-off-by: Matthias Urlichs <matthias@urlichs.de>
1 parent f397a18 commit 05916c6

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

py/binary.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ size_t mp_binary_get_size(char struct_type, char val_type, size_t *palign) {
155155
return size;
156156
}
157157

158-
#if MICROPY_PY_BUILTINS_FLOAT
158+
#if MICROPY_PY_BUILTINS_FLOAT && !defined(FLT16_MAX)
159+
159160
static float mp_decode_half(uint16_t hf) {
160161
union {
161162
uint32_t i;
@@ -207,7 +208,6 @@ static uint16_t mp_encode_half(float x) {
207208
return bits;
208209
}
209210

210-
211211
#endif
212212

213213
mp_obj_t mp_binary_get_val_array(char typecode, void *p, size_t index) {
@@ -309,7 +309,15 @@ mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte *p_base, byte *
309309
return mp_obj_new_str(s_val, strlen(s_val));
310310
#if MICROPY_PY_BUILTINS_FLOAT
311311
} else if (val_type == 'e') {
312+
#ifdef FLT16_MAX
313+
union {
314+
uint16_t i;
315+
_Float16 f;
316+
} fpu = {val};
317+
return mp_obj_new_float_from_f(fpu.f);
318+
#else
312319
return mp_obj_new_float_from_f(mp_decode_half(val));
320+
#endif
313321
} else if (val_type == 'f') {
314322
union {
315323
uint32_t i;
@@ -380,7 +388,17 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte *p
380388
break;
381389
#if MICROPY_PY_BUILTINS_FLOAT
382390
case 'e':
391+
#ifdef FLT16_MAX
392+
union {
393+
uint16_t i;
394+
_Float16 f;
395+
} fp_sp;
396+
fp_sp.f = mp_obj_get_float_to_f(val_in);
397+
val = fp_sp.i;
398+
break;
399+
#else
383400
val = mp_encode_half(mp_obj_get_float_to_f(val_in));
401+
#endif
384402
break;
385403
case 'f': {
386404
union {

0 commit comments

Comments
 (0)
0