Convert Ieee Float To Ti Tms320c30 32bits Float In Python
I need to convert a python float to a TI DSP TMS320C30 float representation, following this convention: http://www.ti.com/lit/an/spra400/spra400.pdf#page=13 I've tried a few things
Solution 1:
The following code passes your tests:
def float_to_ti( f ):
m, e = math.frexp(f)
m *= 2
e -= 1
sign = (math.copysign(1.0, f) < 0)
iff== 0.0:
return (128 << 24) | (sign << 23)
if sign:
m += 2.0ifm== 1.0:
m = 0.0
e -= 1else:
m -= 1.0assert0.0 <= m < 1.0return ((e & 0xff) << 24) | (sign << 23) | int(m * (1 << 23) + 0.5)
Note the different order (exponent, sign, mantissa). Note also that math.frexp() doesn't return anything in the IEEE format, so this code doesn't worry about any IEEE details: the (e & 0xff)
converts the exponent (as a signed char) to an unsigned number. Finally, note that the C30 format doesn't support denormals, which means that its mantissa's top bit is implied (hence the m - 1.0
).
Post a Comment for "Convert Ieee Float To Ti Tms320c30 32bits Float In Python"