Path: utzoo!utgpu!jarvis.csri.toronto.edu!torsqnt!tmsoft!masnet!canremote!turk@apple.com From: turk@apple.com@canremote.uucp (turk@Apple.COM) Newsgroups: comp.graphics Subject: Re: Fixed Point Arithmetic Message-ID: <89122504055727@masnet.uucp> Date: 21 Dec 89 05:05:00 GMT Organization: Canada Remote Systems Limited, Mississauga, ON, Canada Lines: 49 From: turk@Apple.COM (Ken "Turk" Turkowski) Orga: Advanced Technology Graphics, Apple Computer, Cupertino, CA, USA 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 --- * Via MaSNet/HST96/HST144/V32 - UN Graphics * Via Usenet Newsgroup comp.graphics