• Advertisement

# Need a fixed-point math library (C++)

This topic is 2515 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

Hello, sorry this is on a similar topic to my last thread, but I only just realized I needed this (the other was more for future reference). Is there an x86 optimized C++ fixed-point math library? Beyond basic arithmetic, it at least needs a square root function. Conversion functions would be nice. I've searched the forums and the internet, sorry if I missed something. I need it to ensure determinism for the pseudo-random parts of my map generation. The fixed-point stuff Allegro looks good, but it seems non-trivial to make it independent of the rest of the library.

#### Share this post

##### Share on other sites
Advertisement
trenki.net has one. I haven't used it, but guessing from who wrote it, I'd guess it is optimized enough.

#### Share this post

##### Share on other sites
If you need a fixed point library, you should write your own. It's something like asking for library that could compute a = b*c + e. If you need it you should write your own (and learn all important things in the process).

The major usage of the fixed point looks like directly manipulation of the data type in your source code. In addition you said you need it for determinism, this means you can't trust any external library.

#### Share this post

##### Share on other sites
I was using this one:
http://wiki.yak.net/675

If you need the SQRT for computing the vector length then you better do it like this (rewritten from the Graphics Gems):
  public static int vectorLength(int x, int y) {    int X = (x < 0 ? -x : x); /* absolute values */    int Y = (y < 0 ? -y : y);    if (X == 0)      return Y;    if (Y == 0)      return X;    if (X < Y) /* swap X and Y if (X < Y) */    { /* See WYvill (G1, 436)        */      X ^= Y;      Y ^= X;      X ^= Y;    }    int t = Y + (Y >> 1);    return (X - (X >> 5) - (X >> 7) + (t >> 2) + (t >> 6));  }

And finally there is a fixed-point SQRT algorithm:
http://www.worldserver.com/turk/computergraphics/FixedSqrt.pdf

#### Share this post

##### Share on other sites
Quote:
 Original post by samothtrenki.net has one. I haven't used it, but guessing from who wrote it, I'd guess it is optimized enough.

I had come across it before, but the author said it was optimized for the GP2X (ARM), so I feared use of assembly. Now that I've actually bothered to check I see that it's pure C++. I'll probably go with this one, thanks.
Quote:
 Original post by samothIf you need a fixed point library, you should write your own. It's something like asking for library that could compute a = b*c + e. If you need it you should write your own (and learn all important things in the process).

Fair point, but I shouldn't write my own at this stage of development- it's just not important or case-specific enough. And I can trust external libraries insofar as their features are advertised, otherwise I'd never be able to use other people's code. Granted determinism isn't advertised for the trenki.net lib, but I saw no signs of non-determinism, and it's hard to see how it could arise for a fixed-point library anyway.

Thanks Hardwire, but I was after a library that had sqrt implemented.

#### Share this post

##### Share on other sites
I know this thread is old but it is still one of the first results for "fixed point math library c".

There is an open-source library for handling fixed point 16.16 numbers which includes a C++ class interface.

It supports most of the features from math.h and some extra, such as:
Trig functions: sin, cos, tan, asin, acos, atan, atan2
Saturated Arithmetic: sadd, ssub, smul, sdiv
Linear Interpolation: lerp8, lerp16, lerp32
Other functions: sqrt, exp

The project is actively developed and open-source (and looking for contributors).

The project is called libfixmath, more information can be found on the following pages:
[url="http://code.google.com/p/libfixmath/"]libfixmath Project Page[/url]
[url="http://en.wikipedia.org/wiki/Libfixmath"]libfixmath Wikipedia Article[/url]

#### Share this post

##### Share on other sites

• Advertisement