float to fixed_point 8_24 function?

Technical questions regarding the XTC tools and programming with XMOS.
EnSerg
Junior Member
Posts: 4
Joined: Wed Dec 04, 2013 7:16 am

float to fixed_point 8_24 function?

Post by EnSerg »

I try to work with fixed-point library for XMOS.
In file sc_lib_fixed_point-master.zip i see function for work with format fixed_point 8_24 .

My beginners question is
- float IEE-754 to 8_24 function? (how?)
- 8_24 to float IEE-754 function? (how?)

Thank you.
User avatar
TSC
Experienced Member
Posts: 111
Joined: Sun Mar 06, 2011 11:39 pm

Post by TSC »

Have you had a look at the example applications in that project? They seem quite helpful.

E.g. app_example_fixed_point

Code: Select all

//:: Example code to print a sine wave
#include "mathf8_24.h"

void printsine(void) {
    for(f8_24 rad = 0; rad <= PI2; rad += PI2/20) {
        printstr("sin ");
        printf8_24(rad);
        printstr(" = ");
        printf8_24ln(cosf8_24(rad));
    }
}
The mathf8_24.h header file is also quite well documented.

I don't think there's any way in XC to convert between float and f8_24, because any sort of float can't exist. If you have existing code that used floats, you'll have to refactor it so that any place a float is declared, a f8_24 is declared instead, and any place a floating point calculation takes place, the equivalent f8_24 function is called instead.

Code: Select all

f8_24 fpNum = divf8_24(1, 3);  //  fpNum gets 1/3 = 0.333...
Depending on your requirements, it might be easiest to do all the floating point stuff in C files, but performance would be quite slow because any sort of floating point functionality on XMOS parts would be emulated.
richard
Respected Member
Posts: 318
Joined: Tue Dec 15, 2009 12:46 am

Post by richard »

TSC wrote:I don't think there's any way in XC to convert between float and f8_24, because any sort of float can't exist.
xC supports float and double from version 12 onwards. It's still a good idea to use fixed point instead since it avoids increasing code size by pulling in the software floating point emulation library.

According to the header f8_24 is defined as the the signed integer value multiplied by 2^-24, so you should be able to convert to and from floating point as follows:

Code: Select all

double f8_24_to_double(f8_24 x) {
  return (double)x / (1 << 24);
}

f8_24 double_to_f8_24(double x) {
  return (f8_24)(x * (1 << 24));
}
The code to convert to / from float would be identical with double replaced by float.