This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Compiler: C5515 - PRINTF A FLOAT32 IN INT16 FORMAT

Tool/software: TI C/C++ Compiler

Hi,

i need to convert a Float32 array in C5515 in Int16 Array.

How can I do?

Thanks a lot

Paolo

  • Will all the values fit into a 16 bit signed int?
  • Yes, they are.
    I need to print the array in Int16 format
  • A cast should work if truncation is OK, or if you want special rounding the ceiling() or floor() functions.
  • This is not the solution
    Example: i have float f = 1.256647
    I want it in the form Int16 i = 0xXYZT
  • Then be more clear, just what do you want In the "Int16"?

    What is wrong with 0x0001?

    Obviously, floats are not integers.

    Do you want a 16 bit float representation stuffed into a 16bit integer?
    Do you want fixed point?
  • Hi, i thing this is the solution:

    float h;

    printf("0x%x,\n",*(Int16*)&h);

    this should print the number in desired format...what do you think? I'm not sure it is correct...

    Thanks a lot

    Paolo

  • AFAIK floats are 32 bits* on the C5155, this is from the compiler manual:

    float 32 bits IEEE 32-bit
    double 32 bits IEEE 32-bit
    long double 32 bits IEEE 32-bit

    So, your float is the same size as double. There *is* an IEEE 16 bit "half precision" float, but it allows only 3 digits of precision and is not supported by the compiler, anyway.

    So, you are trying to fit 32 pounds of sugar into a 16 pound bag. I think your solution will just truncate things, and leave you with a mess. Again, what do you want to do?

    If you want to check, just do sizeof(float), but be warned that:
    "By ISO C definition, the size of operator yields the number of bytes required to store an
    object. ISO further stipulates that when sizeof is applied to char, the result is 1. Since the
    C55x char is 16 bits (to make it separately addressable), a byte is also 16 bits. This yields
    results you may not expect; for example, sizeof (int) == 1 (not 2). C55x bytes and words are
    equivalent (16 bits)."

    So, your sizeof(float) will be 2 if it is 32 bits.
  • Hi Keith,

    for example, in VC5505 FFT Filter Demo there is 

    Int16 coeffs[LPF_7_TAP] = {
    0x00c5, 0x0328, 0x081f, 0x0aab, 0x081f, 0x0328, 0x00c5

    };

    that became from a mathematical expression containing trigonometric functions in order to calculate the Hamming window.

    I would like to recreate a new filter and have these values like the example above in Int16 format.

    Thanks a lot, Keith.

    Bye

    Paolo

  • You need to dive into the code and see what those represent. I suspect it is straight integer or fixed point, and not a float.
  • Ok Keith, i found the solution.
    I need to convert float to Q15 format.

    Thanks
    Paolo