poslathian and I were exchanging posts about Fixed Point maths libaries on http://forums.leaflabs.com/topic.php?id=259&page=2, and I thought we should break FixedPoint out into a new thread.
This is my initial stab at what would be good, i.e. more than adequate:
I think one FixedPoint32 class would be fine to start with, and we can flesh it out as we go.
I am happy to start with a fixed 16.16 format, or a single common format for all instances of the Fixed32 class, to make it easy to get going.
I do think that template classes are the 'right' final solution, but we might find one on the net we can use, and hence avoid ever having to do that ourselves :-)
I would like to get to operator overloading. I did a little bit of programming on PowerPC Macs using the AltiVec function wrappers, and it was pretty ugly.
To start with, we probably only need a constructor, +, -, * and /. If we use int32, we should get ==, <, >, (etc) for free. We'll need a way to get a text string representation of the number.
I assume we are representing the number as an int32, and there are no virtual functions.
An adequate solution is to have some macros which wrap stuff up.
So, typing straight out of my head, with no real planning (no compiler), and no clue ...
typedef int FixedPoint32;
#define DECPOS (16)
#define FRACNORM (32-DECPOS)
#define INTNORM (DECPOS)
#define INTPART (0xFFFFFFFF<<DECPOS)
#define FRACPART (0xFFFFFFFF>>FRACNORM)
#define INT(a) (((a)&INTPART)>>DECPOS)
#define FRAC(a) ((a)&FRACPART)
#define FIX(i,f) ((i)<<DECPOS) | ((f)&FRACPART)
#define add(a, b) ((a)+(b))
#define sub(a, b) ((a)-(b))
#define mul(a, b) (FIX(INT(a)*INT(b),(FRAC(a)*FRAC(b))>>FRACNORM))
This is highly likely to be utter nonsense, it's just to illustrate that it should be a relatively small amount of work to get something working.
I am more than happy if someone can point out a reasonably compact, Open Source C/C++ library that would work nicely on the STM32F.
Is this interesting as a project?
If it is, should we add a little github repo to work on it?