RE: [Algorithms] Fixed point maths.
Brought to you by:
vexxed72
From: Tom F. <tom...@ee...> - 2004-08-09 17:42:33
|
Most 32-bit CPUs have a 32x32->32:32 instruction. Best thing is to use that, then shift down by 16 bits. If you insist on C, then let's represent our 32-bit number A with 0x10000*A1+A2, i.e. top and bottom shorts: (0x10000*A1 + A2) * (0x10000*B1 + B2) = 0x100000000*(A1*B1) + 0x10000*(A2*B1 + A1*B2) + (A2*B2) ...and you want to shift the result down by 16 bits (coz it's fixed-point 16:16), so you want: = 0x10000*(A1*B1) + (A2*B1 + A1*B2) + ((A2*B2)>>16) Careful with the signs - A1 and B1 are signed, while A2 and B2 are unsigned (for a signed mul - obviously for an unsigned one, everything is unsigned). But as I say, much quicker to use the CPU's 32x32=32:32 instruction. It's about two lines of assembly per platform. TomF. > -----Original Message----- > From: gda...@li... > [mailto:gda...@li...] On > Behalf Of Richard E Collins > Sent: 09 August 2004 09:01 > To: gda...@li... > Subject: [Algorithms] Fixed point maths. > > > Anyone got a link to a proper implementaion of fixed > point maths. I'm > intrested in the 16.16 format. I understand the basics but > can't decide the > best way to do mul and div. Also need some help on the other > trig funcs. > I've been wasting the last few days searching google and all > I come up with > is ethier asm solutions that are no good ( I need to port to > a system with > 32bit regs only) or unreadable macros that are just designed > to make the > auther look cleaver. Most implementations are just hacks that > will not work > on the systems that really need to used fixed point maths. > One method for > the mul i've seen is to cast the two values to 64bits do the > divide and then > put back to 32 bits, another converted the numbers to floats > did the div and > back to ints, thats just crazzyness! > > So, in short, links to 32 fixed point maths methods that > only ever use > 32 bits no floats and is written in working mans maths. ( non > of that j = > p^2 - (g -1 * f) f = some fancy word for 16 and g = a big formula that > really means shift it by your small thing. ) > > > > Ta from a maths notation illiterate. > > Richard e Collins > www.maddoggames.com > > > > > ------------------------------------------------------- > This SF.Net email is sponsored by OSTG. Have you noticed the > changes on > Linux.com, ITManagersJournal and NewsForge in the past few weeks? Now, > one more big change to announce. We are now OSTG- Open Source > Technology > Group. Come see the changes on the new OSTG site. www.ostg.com > _______________________________________________ > GDAlgorithms-list mailing list > GDA...@li... > https://lists.sourceforge.net/lists/listinfo/gdalgorithms-list > Archives: > http://sourceforge.net/mailarchive/forum.php?forum_id=6188 > |