DSPRelated.com
Forums

floating pt. to fixed pt. conversion

Started by shekhar85 August 16, 2010
Hi,

are there any good tutorials to convert floating point C code to fixed
point C code.

Regards,
Shaekhar
shekhar85 <chandrashekharla@n_o_s_p_a_m.gmail.com> wrote:
 
> are there any good tutorials to convert floating point C > code to fixed point C code.
What kind of tutorial do you want? Converting the code is easy. Making sure that the algoritm works the same way in fixed point is a completely different question. What kind of problems are you asking about? -- glen
Hi Shaekhar

> are there any good tutorials to convert floating point C code to fixed > point C code.
I have some routine for conversion ... unsigned int MaskBit(unsigned char Bit) { // Local Variable unsigned int x; // Init Mask x = 0; // Loop for Mask dimension while(Bit) { // Rotate Left x <<= 1; // Add Mask Bit x |= 1; // Decrement bit number Bit--; } // Return with MaskBit return x; } float Fixed2Float(unsigned int Value, unsigned char Data, unsigned char Binary, unsigned char Signed) { // Signed conversione ... if((Signed != 0) && (Value >> (Data - 1))) { // Positive Value Value = (~Value + 1) & MaskBit(Data); // Negative Value return (float)(-1.0 * ((float)(Value) / (float)(1 << Binary))); } else { // Positive Value return (float)(+1.0 * ((float)(Value) / (float)(1 << Binary))); } } unsigned int Float2Fixed(float Value, unsigned char Data, unsigned char Binary) { // Convert to Fixed Point return ((unsigned int)((Value) * (float)(1 << Binary) + ((Value) < 0 ? -0.5 : 0.5))) & MaskBit(Data); } Reagrds Kappasm
On Aug 16, 2:43&#4294967295;am, glen herrmannsfeldt <g...@ugcs.caltech.edu> wrote:
> shekhar85 <chandrashekharla@n_o_s_p_a_m.gmail.com> wrote: > > are there any good tutorials to convert floating point C > > code to fixed point C code. > > Converting the code is easy. &#4294967295;Making sure that the algorithm > works the same way in fixed point is a completely different > question.
and you might have an ice cube's chance in hell in getting them to work exactly the same. sometimes algs written in float have great difficulty working in fixed (like with FFT in it) without a great deal of roundoff error or the risk of overflow deep in side. on the other hand, sometimes an alg works in fixed but not so well in float (like a CIC filter). to answer the OP's concern, doing algs in fixed-point in C requires care in the size of word and what happens to the size of the result of an operation. and then there are the scaling factors you need to worry about. i thought maybe Randy Yates had a tutorial. no? r b-j r b-j
On 08/15/2010 10:15 PM, shekhar85 wrote:
> Hi, > > are there any good tutorials to convert floating point C code to fixed > point C code.
I probably over-complicate things, but this is a pretty broad subject, and one that would be hard to write a tutorial _for_. As RBJ pointed out, some algorithms almost demand floating point, or huge increases in numerical precision (such as the FFT), while some algorithms depend on fixed-point tricks (such as CIC). I do most of my signal processing for control systems and for low-level communications, where the incoming data path size is known and the filters are simple; these applications boil down to IIR and FIR filtering for the most part, and for the most part do just fine in fixed-point math. Here, the big difference between a 32-bit floating point number and a 32-bit fixed point number is that when I'm paying proper attention to scaling the floating point number throws away 7 perfectly good and valuable bits worth of precision, and gives me back a 10^76 variation in magnitude that I don't need in the least. But I've always converted between floating point and fixed point algorithms by understanding the underlying math, and rewriting the algorithm to match the different representation. I suppose you could come up with specific recipes for specific situations (IIR, FIR, etc.), but I'm not sure if you could easily cover all the useful ones in a document that's smaller and easier than one that just teaches you the math. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com Do you need to implement control loops in software? "Applied Control Theory for Embedded Systems" was written for you. See details at http://www.wescottdesign.com/actfes/actfes.html

robert bristow-johnson wrote:

> On Aug 16, 2:43 am, glen herrmannsfeldt <g...@ugcs.caltech.edu> wrote: > >>shekhar85 <chandrashekharla@n_o_s_p_a_m.gmail.com> wrote: >> >>>are there any good tutorials to convert floating point C >>>code to fixed point C code. >> >>Converting the code is easy. Making sure that the algorithm >>works the same way in fixed point is a completely different >>question. > > > and you might have an ice cube's chance in hell in getting them to > work exactly the same. sometimes algs written in float have great > difficulty working in fixed (like with FFT in it) without a great deal > of roundoff error or the risk of overflow deep in side. on the other > hand, sometimes an alg works in fixed but not so well in float (like a > CIC filter). > > to answer the OP's concern, doing algs in fixed-point in C requires > care in the size of word and what happens to the size of the result of > an operation. and then there are the scaling factors you need to > worry about. > > i thought maybe Randy Yates had a tutorial. no?
I think a conversion of an algorithm from floating point to integer implementation could be done as a routine fully automated procedure. You have to track all variables, their variances and derivatives of the result from the variables, numerically. Of course that won't avoid the special cases, however the converted algorithm will be generally working. I am surprised if this is not yet implemented in matlabi, etc. Vladimir Vassilevsky DSP and Mixed Signal Design Consultant http://www.abvolt.com
On Aug 16, 2:22&#4294967295;pm, Vladimir Vassilevsky <nos...@nowhere.com> wrote:
> robert bristow-johnson wrote: > > On Aug 16, 2:43 am, glen herrmannsfeldt <g...@ugcs.caltech.edu> wrote: > > >>shekhar85 <chandrashekharla@n_o_s_p_a_m.gmail.com> wrote: > > >>>are there any good tutorials to convert floating point C > >>>code to fixed point C code. > > >>Converting the code is easy. &#4294967295;Making sure that the algorithm > >>works the same way in fixed point is a completely different > >>question. > > > and you might have an ice cube's chance in hell in getting them to > > work exactly the same. &#4294967295;sometimes algs written in float have great > > difficulty working in fixed (like with FFT in it) without a great deal > > of roundoff error or the risk of overflow deep in side. &#4294967295;on the other > > hand, sometimes an alg works in fixed but not so well in float (like a > > CIC filter). > > > to answer the OP's concern, doing algs in fixed-point in C requires > > care in the size of word and what happens to the size of the result of > > an operation. &#4294967295;and then there are the scaling factors you need to > > worry about. > > > i thought maybe Randy Yates had a tutorial. &#4294967295;no? > > I think a conversion of an algorithm from floating point to integer > implementation could be done as a routine fully automated procedure. You > have to track all variables, their variances and derivatives of the > result from the variables, numerically. Of course that won't avoid the > special cases, however the converted algorithm will be generally > working. I am surprised if this is not yet implemented in matlabi, etc. > > Vladimir Vassilevsky > DSP and Mixed Signal Design Consultanthttp://www.abvolt.com
What happens when you come across a sqrt, atan2, log, pow, etc? Would fixed point versions be installed automatically? If automatic conversion code exists, please post a link. John
On 08/16/2010 11:22 AM, Vladimir Vassilevsky wrote:
> > > robert bristow-johnson wrote: > >> On Aug 16, 2:43 am, glen herrmannsfeldt <g...@ugcs.caltech.edu> wrote: >> >>> shekhar85 <chandrashekharla@n_o_s_p_a_m.gmail.com> wrote: >>> >>>> are there any good tutorials to convert floating point C >>>> code to fixed point C code. >>> >>> Converting the code is easy. Making sure that the algorithm >>> works the same way in fixed point is a completely different >>> question. >> >> >> and you might have an ice cube's chance in hell in getting them to >> work exactly the same. sometimes algs written in float have great >> difficulty working in fixed (like with FFT in it) without a great deal >> of roundoff error or the risk of overflow deep in side. on the other >> hand, sometimes an alg works in fixed but not so well in float (like a >> CIC filter). >> >> to answer the OP's concern, doing algs in fixed-point in C requires >> care in the size of word and what happens to the size of the result of >> an operation. and then there are the scaling factors you need to >> worry about. >> >> i thought maybe Randy Yates had a tutorial. no? > > I think a conversion of an algorithm from floating point to integer > implementation could be done as a routine fully automated procedure. You > have to track all variables, their variances and derivatives of the > result from the variables, numerically. Of course that won't avoid the > special cases, however the converted algorithm will be generally > working. I am surprised if this is not yet implemented in matlabi, etc.
I don't know -- I think there'd be too many variables for it to work well in every case. Perhaps it could be done to work well in the linear shift-invariant case, however. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com Do you need to implement control loops in software? "Applied Control Theory for Embedded Systems" was written for you. See details at http://www.wescottdesign.com/actfes/actfes.html
Tim Wescott <tim@seemywebsite.com> wrote:
> On 08/15/2010 10:15 PM, shekhar85 wrote:
>> are there any good tutorials to convert floating point C code >> to fixed point C code.
> I probably over-complicate things, but this is a pretty broad > subject, and one that would be hard to write a tutorial _for_.
Well, there are tutorials on fixed point (non-integer) math. It does seem that some have a hard time with it, even though we all learned it in about fourth grade. For addition, align the radix point, for multiplication add the number of places to the right of the radix point, and use that for the product. It works in decimal, binary, and any other base you want to use. PL/I is one of the few languages with direct support for fixed point with the radix point other than to the immediate right of the least significant digit. (both binary and decimal.) PL/I allows the scale factor (radix point position) to be either positive or negative. In 32 bit binary, it is usual to allow between -31 and 31 bits to the right of the binary point, or between -15 and +15 to the right of the decimal point (for fixed decimal arithmetic).
> As RBJ pointed out, some algorithms almost demand floating point, or > huge increases in numerical precision (such as the FFT), while some > algorithms depend on fixed-point tricks (such as CIC).
As I have said here before, floating point for quantities with relative uncertainty, fixed point for absolute uncertainty. Knuth says fixed point for typesetting and financial calculations, two that, more or less obviously, have absolute uncertainty. Knuth has an example of a typesetting machine that he was told was at 5333 DPI, but turned out to be 5333+1/3 DPI. Programming for 5333 was visibly wrong, though you might think that the error is small. (snip)
> But I've always converted between floating point and fixed point > algorithms by understanding the underlying math, and rewriting the > algorithm to match the different representation. I suppose you could > come up with specific recipes for specific situations (IIR, FIR, etc.), > but I'm not sure if you could easily cover all the useful ones in a > document that's smaller and easier than one that just teaches you > the math.
Understand the math, understand the uncertainties in the input and output data, and what happens in between. -- glen

John wrote:

> On Aug 16, 2:22 pm, Vladimir Vassilevsky <nos...@nowhere.com> wrote: >
>>I think a conversion of an algorithm from floating point to integer >>implementation could be done as a routine fully automated procedure. You >>have to track all variables, their variances and derivatives of the >>result from the variables, numerically. Of course that won't avoid the >>special cases, however the converted algorithm will be generally >>working. I am surprised if this is not yet implemented in matlabi, etc. >> > > What happens when you come across a sqrt, atan2, log, pow, etc? Would > fixed point versions be installed automatically?
The trivial approach is borrow the function from a software floating point library as is. The better solution could be having approximations for standard functions accurate to a given number of bits.
> If automatic > conversion code exists, please post a link.
I don't know if there is such a tool, however I don't think that float to fixed point conversion is more difficult problem, then, say, analytical integration. Vladimir Vassilevsky DSP and Mixed Signal Design Consultant http://www.abvolt.com