From e520fa2e0fb3cfafe27a1f9e7e9b230dd58d7a33 Mon Sep 17 00:00:00 2001 From: Matthias Urlichs Date: Wed, 25 Oct 2023 19:17:47 +0200 Subject: [PATCH 1/4] py/binary: Support half-float 'e' format in struct pack/unpack. This commit implements the 'e' half-float format: 10-bit mantissa, 5-bit exponent. It uses native _Float16 if supported by the compiler, otherwise uses custom bitshifting encoding/decoding routines. Signed-off-by: Matthias Urlichs Signed-off-by: Damien George --- docs/library/struct.rst | 2 + py/binary.c | 109 +++++++++++++++++++++++++++++++++++- py/mpconfig.h | 9 +++ tests/float/float_struct.py | 2 +- 4 files changed, 119 insertions(+), 3 deletions(-) diff --git a/docs/library/struct.rst b/docs/library/struct.rst index 026cb5e8ac3b4..c2357505960d7 100644 --- a/docs/library/struct.rst +++ b/docs/library/struct.rst @@ -45,6 +45,8 @@ The following data types are supported: +--------+--------------------+-------------------+---------------+ | Q | unsigned long long | integer (`1`) | 8 | +--------+--------------------+-------------------+---------------+ +| e | n/a (half-float) | float (`2`) | 2 | ++--------+--------------------+-------------------+---------------+ | f | float | float (`2`) | 4 | +--------+--------------------+-------------------+---------------+ | d | double | float (`2`) | 8 | diff --git a/py/binary.c b/py/binary.c index 4c8b6ffcdc49a..7c01cfa1c8284 100644 --- a/py/binary.c +++ b/py/binary.c @@ -74,11 +74,14 @@ size_t mp_binary_get_size(char struct_type, char val_type, size_t *palign) { case 'S': size = sizeof(void *); break; + case 'e': + size = 2; + break; case 'f': - size = sizeof(float); + size = 4; break; case 'd': - size = sizeof(double); + size = 8; break; } break; @@ -122,6 +125,10 @@ size_t mp_binary_get_size(char struct_type, char val_type, size_t *palign) { align = alignof(void *); size = sizeof(void *); break; + case 'e': + align = 2; + size = 2; + break; case 'f': align = alignof(float); size = sizeof(float); @@ -144,6 +151,99 @@ size_t mp_binary_get_size(char struct_type, char val_type, size_t *palign) { return size; } +#if MICROPY_PY_BUILTINS_FLOAT && MICROPY_FLOAT_USE_NATIVE_FLT16 + +static inline float mp_decode_half_float(uint16_t hf) { + union { + uint16_t i; + _Float16 f; + } fpu = { .i = hf }; + return fpu.f; +} + +static inline uint16_t mp_encode_half_float(float x) { + union { + uint16_t i; + _Float16 f; + } fp_sp = { .f = (_Float16)x }; + return fp_sp.i; +} + +#elif MICROPY_PY_BUILTINS_FLOAT + +static float mp_decode_half_float(uint16_t hf) { + union { + uint32_t i; + float f; + } fpu; + + uint16_t m = hf & 0x3ff; + int e = (hf >> 10) & 0x1f; + if (e == 0x1f) { + // Half-float is infinity. + e = 0xff; + } else if (e) { + // Half-float is normal. + e += 127 - 15; + } else if (m) { + // Half-float is subnormal, make it normal. + e = 127 - 15; + while (!(m & 0x400)) { + m <<= 1; + --e; + } + m -= 0x400; + ++e; + } + + fpu.i = ((hf & 0x8000) << 16) | (e << 23) | (m << 13); + return fpu.f; +} + +static uint16_t mp_encode_half_float(float x) { + union { + uint32_t i; + float f; + } fpu = { .f = x }; + + uint16_t m = (fpu.i >> 13) & 0x3ff; + if (fpu.i & (1 << 12)) { + // Round up. + ++m; + } + int e = (fpu.i >> 23) & 0xff; + + if (e == 0xff) { + // Infinity. + e = 0x1f; + } else if (e != 0) { + e -= 127 - 15; + if (e < 0) { + // Underflow: denormalized, or zero. + if (e >= -11) { + m = (m | 0x400) >> -e; + if (m & 1) { + m = (m >> 1) + 1; + } else { + m >>= 1; + } + } else { + m = 0; + } + e = 0; + } else if (e > 0x3f) { + // Overflow: infinity. + e = 0x1f; + m = 0; + } + } + + uint16_t bits = ((fpu.i >> 16) & 0x8000) | (e << 10) | m; + return bits; +} + +#endif + mp_obj_t mp_binary_get_val_array(char typecode, void *p, size_t index) { mp_int_t val = 0; switch (typecode) { @@ -240,6 +340,8 @@ mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte *p_base, byte * const char *s_val = (const char *)(uintptr_t)(mp_uint_t)val; return mp_obj_new_str(s_val, strlen(s_val)); #if MICROPY_PY_BUILTINS_FLOAT + } else if (val_type == 'e') { + return mp_obj_new_float_from_f(mp_decode_half_float(val)); } else if (val_type == 'f') { union { uint32_t i; @@ -309,6 +411,9 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte *p val = (mp_uint_t)val_in; break; #if MICROPY_PY_BUILTINS_FLOAT + case 'e': + val = mp_encode_half_float(mp_obj_get_float_to_f(val_in)); + break; case 'f': { union { uint32_t i; diff --git a/py/mpconfig.h b/py/mpconfig.h index cb0f050a74bd8..d9cff930d1485 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -830,6 +830,15 @@ typedef double mp_float_t; #define MICROPY_PY_BUILTINS_COMPLEX (MICROPY_PY_BUILTINS_FLOAT) #endif +// Whether to use the native _Float16 for 16-bit float support +#ifndef MICROPY_FLOAT_USE_NATIVE_FLT16 +#ifdef __FLT16_MAX__ +#define MICROPY_FLOAT_USE_NATIVE_FLT16 (1) +#else +#define MICROPY_FLOAT_USE_NATIVE_FLT16 (0) +#endif +#endif + // Whether to provide a high-quality hash for float and complex numbers. // Otherwise the default is a very simple but correct hashing function. #ifndef MICROPY_FLOAT_HIGH_QUALITY_HASH diff --git a/tests/float/float_struct.py b/tests/float/float_struct.py index 47fe405018038..6e282a6afe3ee 100644 --- a/tests/float/float_struct.py +++ b/tests/float/float_struct.py @@ -8,7 +8,7 @@ i = 1.0 + 1 / 2 # TODO: it looks like '=' format modifier is not yet supported # for fmt in ('f', 'd', '>f', '>d', 'f", ">d", "e", ">f", ">d", " Date: Tue, 19 Mar 2024 22:56:38 +1100 Subject: [PATCH 2/4] stm32/stm32.mk: Enable _Float16 support on MCUs with hardware floats. Signed-off-by: Damien George --- ports/stm32/stm32.mk | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/stm32/stm32.mk b/ports/stm32/stm32.mk index b9f70db73e0a1..718fa8cf0679d 100644 --- a/ports/stm32/stm32.mk +++ b/ports/stm32/stm32.mk @@ -44,14 +44,14 @@ ifneq ($(BUILDING_MBOOT),1) SUPPORTS_HARDWARE_FP_SINGLE = 0 SUPPORTS_HARDWARE_FP_DOUBLE = 0 ifeq ($(CMSIS_MCU),$(filter $(CMSIS_MCU),STM32F765xx STM32F767xx STM32F769xx STM32H743xx STM32H747xx STM32H750xx STM32H7A3xx STM32H7A3xxQ STM32H7B3xx STM32H7B3xxQ)) -CFLAGS_CORTEX_M += -mfpu=fpv5-d16 -mfloat-abi=hard +CFLAGS_CORTEX_M += -mfpu=fpv5-d16 -mfloat-abi=hard -mfp16-format=ieee SUPPORTS_HARDWARE_FP_SINGLE = 1 SUPPORTS_HARDWARE_FP_DOUBLE = 1 else ifeq ($(MCU_SERIES),$(filter $(MCU_SERIES),f0 g0 l0 l1 wl)) CFLAGS_CORTEX_M += -msoft-float else -CFLAGS_CORTEX_M += -mfpu=fpv4-sp-d16 -mfloat-abi=hard +CFLAGS_CORTEX_M += -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mfp16-format=ieee SUPPORTS_HARDWARE_FP_SINGLE = 1 endif endif From b80607ecafeb47664b35e4f255ed43171719ecf5 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 19 Mar 2024 23:09:11 +1100 Subject: [PATCH 3/4] unix/variants: Don't use native _Float16 type. Using it increases code size by about 2k. Signed-off-by: Damien George --- ports/unix/variants/mpconfigvariant_common.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ports/unix/variants/mpconfigvariant_common.h b/ports/unix/variants/mpconfigvariant_common.h index 981babca9f10b..2e34055bf773f 100644 --- a/ports/unix/variants/mpconfigvariant_common.h +++ b/ports/unix/variants/mpconfigvariant_common.h @@ -41,6 +41,11 @@ #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_DOUBLE) #endif +// Don't use native _Float16 because it increases code size by a lot. +#ifndef MICROPY_FLOAT_USE_NATIVE_FLT16 +#define MICROPY_FLOAT_USE_NATIVE_FLT16 (0) +#endif + // Enable arbitrary precision long-int by default. #ifndef MICROPY_LONGINT_IMPL #define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ) From 9d27183bde0a70521a0d189636e53975df2260eb Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 20 Mar 2024 13:30:22 +1100 Subject: [PATCH 4/4] tests/float/float_struct_e.py: Add specific test for struct 'e' type. Signed-off-by: Damien George --- tests/float/float_struct_e.py | 43 +++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 tests/float/float_struct_e.py diff --git a/tests/float/float_struct_e.py b/tests/float/float_struct_e.py new file mode 100644 index 0000000000000..403fbc5db4cde --- /dev/null +++ b/tests/float/float_struct_e.py @@ -0,0 +1,43 @@ +# Test struct pack/unpack with 'e' typecode. + +try: + import struct +except ImportError: + print("SKIP") + raise SystemExit + +test_values = ( + 1e-7, + 2e-7, + 1e-6, + 1e-5, + 1e-4, + 1e-3, + 1e-2, + 0.1, + 0, + 1, + 2, + 4, + 8, + 10, + 100, + 1e3, + 1e4, + 6e4, + float("inf"), +) + +for j in test_values: + for i in (j, -j): + x = struct.pack("