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.

How to convert a certain function from float point to fixed point

Other Parts Discussed in Thread: SPRC542

Hi everyone:

In my project I wanted to port my image-processing-algorithm on DM648.Because DM648 was fixed point DSP and my image-processing-algorithm used many float

point vars(变量),it run so slow that couldn't meet my need. I must convert these vars and functions from float point to fixed point.


for example ,my source code below:

//////

#include <math.h>

void mian()
{
float varA,varB;

varB = tanh(0.66666667*varA); // how to fixed point tanh();

}

////


Firstly,I tried to create a table to fixed point math function - tanh(x), but the range of x was (-∞,+∞).

it meant that I must create a very large table? so my question was:


1.

what was the length of table totally?


2.
Likely sin(x),cos(x) and so on? how to create a fixed point sinTbl or cosTbl? what was the length of sinTbl and cosTbl?

  • HI Steve,

    You can use the IQmath library from TI readily available for download which implements most of things that you require.

    http://www.ti.com/tool/sprc542

  • Radhesh:

    Thx, I knew IQmath, but I wanted to create a look-up table rather than used IQmath.

    In other words,I didn't want to call a black box - IQmath lib ,  I wanted to know the truth of  IQmath lib.

    could you gvie me some basic examples about  the technique of converting a function from float point to fixed point rather than called IQmath lib.

    for example,a expression like below :

    float dst  src1 src2;

    dst  = src1 + src2; (src1-src2)

    dst = src1*src2;

    dst = src1/src2;

    I felt confused that I couldn't find any reference material about the technique of convertint float point to fixed point systematically on TI  website.

    I just only found fragmentary thread from internet such as google.I felt so helpless .

     

  • HI Zhang,

    I referred this for my development. It should answer all your queries and will be useful for what you are looking for.

    http://www.cs.washington.edu/education/courses/cse467/08au/labs/l5/fp.pdf

  • Hi steve,

    steve zhang said:
    varB = tanh(0.66666667*varA); // how to fixed point tanh();

    your example includes tanh() only. Do you need to compute other trigonometric routines such as sines, cosines and transcendental functions also? Do you know the CORDIC arithmetic technique? CORDIC, which is short for "COordinate Rotation DIgital Computer," is an iterative technique proposed by Volder in 1956. CORDIC algorithms let you use one core routine to compute sines, cosines, exponentials, logarithms and other transcendentals. Furthermore, there are implementations available with routines that use fixed-point arithmetic only. See "Implementing CORDIC Algorithms", by Pitts Jarvis here.

    There is another article "Optimizing Math-Intensive Applications with Fixed-Point Arithmetic", by here.

    Best regards,
    Christian

  • Hello,

    You may first optimize the floating-point version of your algorithm. Recalling some basic ways to speed up code with floats:

    - Use float versions of standard functions (ie expf, sinf, cosf,...instead of exp, sin, cos,...), avoiding double calculations and float/double conversions

    - Always prefer multiplies to divides ( x / constant => x * (reciprocal constant), a / b / c => a / (b*c), ...).

    - Simplify and rewrite functions if full precision is not required

    Example that may be useful: reciprocal square root (without divides):

    float rsqrtf( float x )

     { float x0;

       int i, expo;

       frexpf( x, &expo);

       x0 = ldexpf( 0.75F, -(expo/2) );  // initial guess

       i = 5;  // 5 for full IEEE754 32-bit precision

       while (i--)

          x0 *= ( 1.5F - 0.5F * x *x0 * x0 ); 

       return x0;

     }

    For the specific case of tanh (or tanhf), if this function is only required to be a sigmoid function (thresholding in neural nets for ex.), you may use the simpler function: sigmo(x) = x * rsqrtf( 1 + x*x ) instead. If full precision is not required, it can be replaced by:

    float sigmo( float x )

     { float x0, xd;

       int expo;

       xd = 1.0F + x * x;

       frexpf( xd, &expo );

       x0 = ldexpf( 0.75F, -(expo/2) ); // initial guess

       x0 *= ( 1.5F - 0.5F * xd *x0 * x0 );  // add up to 4 identical lines stages for full float precision

       return x * x0;

     }

    Provided with no guaranty,

    Jakez