Source code for irfpy.ica.floats
"""Handling of minifloats (f8) as used by ICA
.. codeauthor:: Martin Wieser
Module: irfpy.ica.floats
Functions to convert between f8 minifloats and integers.
ICA minifloats are 8 bit in size and have the following structure:
s: sign bits
e: exponent bits
m: mantissa bits
s.e.m excess integer range covered
0.4.4 -4(?) 0 to 507904
.. autosummary::
"""
import logging
logging.basicConfig()
_logger = logging.getLogger('ica.floats')
_logger.setLevel(logging.DEBUG)
[docs]def u24_f8(u24):
""" Convert an 24 bit integer to a f8 minifloat.
In case of overflow 0xFF is returned
"""
if u24 > 507904:
return 0xFF
elif u24 < 0x10:
return u24
else:
e = 1
m = u24
while m >= 0x20:
m = m >> 1
e = e + 1
# x is now 0x10 .. 0x1F
m = m & 0xF # mask away 0x10
return (e << 4) + m
[docs]def f8_u16(f8):
""" Convert a f8 minifloat to 16-bit integer. In case of overflow 0xFFFF is returned
"""
e = f8 & 0xF0
if e < 0x10:
return f8
e = (e >> 4) - 1
if e > 11:
return 0xFFFF
return ((f8 & 0xF) | 0x10) << e
[docs]def f8_u24(f8):
""" Convert a f8 minifloat to 24-bit integer. no overflow possible
"""
e = f8 & 0xF0
if e < 0x10:
return f8
e = (e >> 4) - 1
return ((f8 & 0xF) | 0x10) << e