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 <matthias@urlichs.de>
Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Matthias Urlichs
2023-10-25 19:17:47 +02:00
committed by Damien George
parent 77f08b72ca
commit e520fa2e0f
4 changed files with 119 additions and 3 deletions

View File

@@ -8,7 +8,7 @@ except ImportError:
i = 1.0 + 1 / 2
# TODO: it looks like '=' format modifier is not yet supported
# for fmt in ('f', 'd', '>f', '>d', '<f', '<d', '=f', '=d'):
for fmt in ("f", "d", ">f", ">d", "<f", "<d"):
for fmt in ("e", "f", "d", ">e", ">f", ">d", "<e", "<f", "<d"):
x = struct.pack(fmt, i)
v = struct.unpack(fmt, x)[0]
print("%2s: %.17f - %s" % (fmt, v, (i == v) and "passed" or "failed"))