Limegarden.net Personal site of Wouter Lindenhof

11Mar/090

Update: Fixed point math

It has been a long time since I updated my blog so here is yet another piece of free code. For those who don't know what fixed point math is: Fixed point math is math without using floating point (no floats or doubles allowed) and in general fixed point math is more precise and faster than floating point math. A way to simulate floating numbers like 0.5 is by shifting the numbers to something bigger like 500 (0.5 times 1000), however when you multiply 500 by 500 you want the result to be 250 (0.5 times 0.5 is 0.25).

For a recent assignment I had to create a fixed point rasterizer, think of DirectX but then everything code by yours truly without using anything of DirectX. However I'm not too happy with the result as there are a lot of things that could be improved or added and for that reason I won't upload the rasterizer. When will I upload it? When I'm happy with it. When will I be happy with it? Once the feature set is on par with DirectX and rendering a full screen quad will be above the 200 fps.

I'm pretty strict on when something is good so don't expect the fixed point rasterizer being uploaded anytime soon.

What I do upload is the fixed point math part. It's a collection of classes. You have FX32 and FX32S which are types who have a different fractional part. And there are two converter classes (FX32Convertor and FX32SConvertor) who can be used if you want to combine FX32 and FX32S. Of all the four classes you have a 'fake mode'. This fake mode is the exact same class but instead of holding an integer it holds a float. By changing the define in config.h you can simply switch between fake and real fixed point math.

Warning: Don't test performance in debug mode but in release mode. Fixed point math is more instructions then floating point, but these instructions are cheaper than floating point. However in debug mode the compiler will add 'optional breaks' (which allows you to pause execution on every line) and this has a lot of impact on the speed of the fixed point mode.

If you are a person who worries about the fact that all the code is in classes which means function call overhead, then I want to point out that the compiler should allow inlining or if possible force it for those files. If that happens then even the overloaded operators will be inlined removing the function calls and this should generate similar code as if you were doing it manual (without using classes). However this is all depended on how well your compiler inlines.

The downloads can be found on my portfolio or if you are lazy you can always grab the direct link: fixed point math.rar

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.