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.

TMS320F28377S: there are functions to convert a floating point value into a UL also for the TMS320

Part Number: TMS320F28377S

Are there  functions to convert a floating point value into a UL also for the TMS320 available? I have seen such functios in the math.h and the _defs.h but the functions are not available for TMS320.

#elif defined(__ARM_ARCH)
#define __u32_bits_as_f32(x) (_itof(x))
#define __f32_bits_as_u32(x) (_ftoi(x))
#define __u64_bits_as_f64(x) (_itod((x)>>32,(x)&0xffffffff))
#define __f64_bits_as_u64(x) ((((__uint64_t)_hi(x) << 32 | _lo(x))))
#define __f64_upper_bits_as_u32(x) (_hi(x))
#define __f64_lower_bits_as_u32(x) (_lo(x))
#define __u32x2_bits_as_f64(x,y) (_itod((x),(y)))
#elif defined(__C7000__) /* Keep __C7000__ ordered before __TMS320C6X */
#define __u32_bits_as_f32(x) (__as_float(x))
#define __f32_bits_as_u32(x) (__as_uint(x))
#define __u64_bits_as_f64(x) (__as_double(x))
#define __f64_bits_as_u64(x) (__as_ulong(x))
#define __f64_upper_bits_as_u32(x) (__as_uint2(x).hi)
#define __f64_lower_bits_as_u32(x) (__as_uint2(x).lo)
#define __u32x2_bits_as_f64(x,y) (__as_double((__uint2)(y, x)))
#elif defined(__TMS320C6X__)
#define __u32_bits_as_f32(x) (_itof(x))
#define __f32_bits_as_u32(x) (_ftoi(x))
#define __u64_bits_as_f64(x) (_lltod(x))
#define __f64_bits_as_u64(x) (_dtoll(x))
#define __f64_upper_bits_as_u32(x) (_hi(x))
#define __f64_lower_bits_as_u32(x) (_lo(x))
#define __u32x2_bits_as_f64(x,y) (_itod((x),(y)))
#else
#define __f64_upper_bits_as_u32(x) (__f64_bits_as_u64(x) >> 32)
#define __f64_lower_bits_as_u32(x) (__f64_bits_as_u64(x) & 0xffffffff)
#define __u32x2_bits_as_f64(x,y) (__u64_bits_as_f64((__uint64_t)(x) << 32 | (__uint32_t)(y)))
#endif

  • Hi,

    Do you mean converting float to unsigned long, or indeed reinterpreting float as unsigned long.

    conversion f->UL  1.0->1

    reinterpreting f->UL 1.0->0x3F800000

    Defines like __f32_bits_as_u32(x) work on TMS320F28377S,  __f32_bits_as_u32(1.0) = 0x3F800000.

    For normal float <-> integer conversion you just assign float to UL or UL to float. If you need to round while converting to integer, then perhaps try using standard functions like lrint() or lround() defined in math.h

    Edward

  • Hi

    I want to reinterpret the Float value. I tried __f32_bits_as_u32(x) but for me it does not work? Do have to include anything else except math.h

    Best reagards 

    Jochen

  • Hi,

    It seems you don't need to include anything. I see no warning compiling this. Only move instruction to load store from/to f or ul.

    float f;

    unsigned long ul;

     

    void foo1(void)

    {

    ul = __f32_bits_as_u32(f);

    }

    _foo1:
     .dwcfi cfa_offset, -2
     .dwcfi save_reg_to_mem, 26, 0
     .dwpsn file "../test.c",line 15,column 5,is_stmt,isa 0
            MOVW      DP,#_f                ; [CPU_ARAU]
            MOVL      ACC,@_f               ; [CPU_ALU] |15|
            MOVL      @_ul,ACC              ; [CPU_ALU] |15|

    ..

    void foo2(void)

    {

    f = __u32_bits_as_f32(ul);

    }

    _foo2:
     .dwcfi cfa_offset, -2
     .dwcfi save_reg_to_mem, 26, 0
     .dwpsn file "../test.c",line 20,column 5,is_stmt,isa 0
            MOVW      DP,#_ul               ; [CPU_ARAU]
            MOVL      ACC,@_ul              ; [CPU_ALU] |20|
            MOVL      @_f,ACC               ; [CPU_ALU] |20|

    ..

    Edward

  • Jochen,

    There's an assembly instruction available to do this F32TOUI32. See Pg 58 of spruhs1c. Unfortunately, there isn't a corresponding intrinsic available for it that you could directly use in C code. It can be used in C code, with some effort. There are intrinsics for F32TOI16R and F32TOUI16R (signed and unsigned 16-bit integer with rounding). See Table 7-7 of spru514s.

    Thanks,

    Sira 

  • Also, A simple casting operation like (uint32_t (float_value) should result in the generation of the F32TOUI32 instruction.

    See attached.

    Thanks,

    Sira