ram@jane.Jpl.Nasa.Gov (Bob Mortensen) (12/20/89)
Does anyone have some good fixed point arithmetic routines (or macros) for C? Or is there at least any good references on this subject. Specifically, I have an application that is written in floating point but could just as well use fixed, and I'm sure that it would be sped up dramatically. I would like to use 32-bit integers with 16-bits of integer and 16-bits of fraction. I hate to reinvent the wheel. 8-) Thanks in advance, Bob Mortensen Visualization and Earth Science Applications Jet Propulsion Laboratory ram@jane.jpl.nasa.gov 4800 Oak Grove Drive bob@zest.jpl.nasa.gov Pasadena, CA 91109
turk@Apple.COM (Ken "Turk" Turkowski) (12/20/89)
In article <RAM.89Dec19170118@jane.Jpl.Nasa.Gov> bob@zest.jpl.nasa.gov writes: >Does anyone have some good fixed point arithmetic routines (or macros) for C? >Or is there at least any good references on this subject. Specifically, I >have an application that is written in floating point but could just as well >use fixed, and I'm sure that it would be sped up dramatically. I would like >to use 32-bit integers with 16-bits of integer and 16-bits of fraction. C does not support fixed-point multiplication and division, but it does support fixed-point addition and subtraction. You can easily implement fixed-point multiplication on any 32-bit machine easily, and fixed-point division is relatively easy for any 32-bit machine (except for MIPS). The basic concept is that when you multiply two 32-bit integers, you get a 64-bit integer. When the integers are signed, you get a duplicate sign bit in the 64-bit product. Similarly, when you divide a 64-bit integer by a 32-bit integer, you get a 32-bit quotient and a 32-bit remainder. When the numbers are signed, you've got to also worry about duplicate sign bits. Here's multiplication in symbolic C: typedef long longlong[2]; // A 64-bit number typedef long fixed; // A 32-bit number fixed FixMul(fixed a, fixed b) { return( ((longlong)(a * b)) >> 15); } fixed FixDiv(fixed a, fixed b) { return( (((longlong)(a)) << 15) / b ); } -- Ken Turkowski @ Apple Computer, Inc., Cupertino, CA Internet: turk@apple.com Applelink: TURKOWSKI1 UUCP: sun!apple!turk
turk@apple.com@canremote.uucp (turk@Apple.COM) (12/21/89)
From: turk@Apple.COM (Ken "Turk" Turkowski) Orga: Advanced Technology Graphics, Apple Computer, Cupertino, CA, USA In article <RAM.89Dec19170118@jane.Jpl.Nasa.Gov> bob@zest.jpl.nasa.gov writes: >Does anyone have some good fixed point arithmetic routines (or macros) for C? >Or is there at least any good references on this subject. Specifically, I >have an application that is written in floating point but could just as well >use fixed, and I'm sure that it would be sped up dramatically. I would like >to use 32-bit integers with 16-bits of integer and 16-bits of fraction. C does not support fixed-point multiplication and division, but it does support fixed-point addition and subtraction. You can easily implement fixed-point multiplication on any 32-bit machine easily, and fixed-point division is relatively easy for any 32-bit machine (except for MIPS). The basic concept is that when you multiply two 32-bit integers, you get a 64-bit integer. When the integers are signed, you get a duplicate sign bit in the 64-bit product. Similarly, when you divide a 64-bit integer by a 32-bit integer, you get a 32-bit quotient and a 32-bit remainder. When the numbers are signed, you've got to also worry about duplicate sign bits. Here's multiplication in symbolic C: typedef long longlong[2]; // A 64-bit number typedef long fixed; // A 32-bit number fixed FixMul(fixed a, fixed b) { return( ((longlong)(a * b)) >> 15); } fixed FixDiv(fixed a, fixed b) { return( (((longlong)(a)) << 15) / b ); } -- Ken Turkowski @ Apple Computer, Inc., Cupertino, CA Internet: turk@apple.com Applelink: TURKOWSKI1 UUCP: sun!apple!turk --- * Via MaSNet/HST96/HST144/V32 - UN Graphics * Via Usenet Newsgroup comp.graphics