Convert IEEE float to TI TMS320C30 32bits float in python

穿精又带淫゛_ 提交于 2021-01-29 07:50:52

问题


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, but I can't seem to wrap my head around the proposed algorithm. I also found a C version of the algorithm, but it looks like it is a version that runs in the TI DSP, so there are operations that I can't figure out (reverse the sign, for instance).

I have a very naive implementation below, but it doesn't work...

# see http://www.ti.com/lit/an/spra400/spra400.pdf

import math

def twos_comp(val, bits):
    """compute the 2's complement of int value val"""
    if( (val&(1<<(bits-1))) != 0 ):
        val = val - (1<<bits)
    return val

def float_to_ti( f ):
    m,e = math.frexp( f )
    # print m, e,
    mantissa = int(str(m)[2:])
    exponent = twos_comp((e - 127), 8) if e != 0 else (-128)
    sign = 1 if f < 0 else 0
    # print sign, mantissa, exponent
    return ((sign << 30) + (exponent << 24) + mantissa) & 0xffffffff

Some examples of expected values:

# Float     TI Decimal value of the resulting 32bits
#################################################
# 0.0         2147483648
# 1.0         0
# 100         105381888
# 0.000021    4029688104
# 10          52428800
# -1.0        4286578688
# -10.0       65011712
# -0.0021     4160118745
# -84.5487    114747153

I think it boils down to the way python returns the mantissa/significand, but I'm not sure.
How would you start things here?

Note: I found this question that might be related, I'll look into the struct pack and unpack..

FYI: I retrieved the theoretical values with a C program that I load in the DSP like this:

{
  float f = -1.0; printf("F: %f -> %u", f, *(unsigned int*)&f);
  f = -10.0;      printf("F: %f -> %u", f, *(unsigned int*)&f);
  f = -0.0021;    printf("F: %f -> %u", f, *(unsigned int*)&f);
  f = -84.5487;   printf("F: %f -> %u", f, *(unsigned int*)&f);
}

Working implementation

Following Armin's answer, I got it working for negative numbers:

def float_to_ti(f):
    m, e = math.frexp(f)
    ma = m
    m = abs(m * 2)
    e -= 1
    sign = (math.copysign(1.0, f) < 0)
    man = int((m - 1.0) * (1 << 23))
    if sign:
        man *= -1
    if e == 0 and sign == 1:
        e = 255
    if f == 0.0:
        return (128 << 24) | (sign << 23)
    return ((e & 0xff) << 24) | (sign << 23) | man & 0x7fffff

回答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)
    if f == 0.0:
        return (128 << 24) | (sign << 23)
    if sign:
        m += 2.0
        if m == 1.0:
            m = 0.0
            e -= 1
    else:
        m -= 1.0
    assert 0.0 <= m < 1.0
    return ((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).



来源:https://stackoverflow.com/questions/17833469/convert-ieee-float-to-ti-tms320c30-32bits-float-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!