home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!usc!sdd.hp.com!uakari.primate.wisc.edu!ames!agate!benrg
- From: benrg@ocf.berkeley.edu (Ben Rudiak-Gould)
- Newsgroups: comp.lang.c
- Subject: How can I multiply and divide 32-bit fixed-point numbers?
- Date: 1 Sep 1992 10:40:53 GMT
- Organization: U.C. Berkeley Open Computing Facility
- Lines: 27
- Message-ID: <17vhblINNi9d@agate.berkeley.edu>
- NNTP-Posting-Host: tornado.berkeley.edu
-
- I'm using ints as fixed-point numbers with 16 bits of integer and 16
- bits of fraction. What's the best way to multiply and divide two of
- these and return a third? For multiplication, I could do
-
- (a>>8) * (b>>8)
-
- but that loses (ignores) the lower eight bits of precision of both
- variables (which might be significant). Division is even worse; the best
- I can come up with is
-
- (a / (b>>8)) << 8
-
- but then not only do I lose eight bits of b, I also always end up with a
- result that is truncated to 8 bits of fraction, far less than the 1/65536
- precision I should be able to get.
-
- What's most annoying about this is that I know that the uP I'm using
- (i486) returns 64-bit results to 32-bit multiplies and divides, but
- C won't let me access them.
-
- Is there any solution short of inline assembly?
-
- Thanks.
-
- --
- Ben Rudiak-Gould
- benrg@ocf.berkeley.edu
-