Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!mips!apple!turk From: turk@Apple.COM (Ken "Turk" Turkowski) Newsgroups: comp.graphics Subject: Re: Fixed Point Arithmetic Message-ID: <5889@internal.Apple.COM> Date: 20 Dec 89 05:17:08 GMT References: Distribution: comp Organization: Advanced Technology Graphics, Apple Computer, Cupertino, CA, USA Lines: 41 In article 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