I wrote a basic fixed-point variant of the sine function which utilizes a lookup table (targeting an AVR micro controller w/o FPU). My implementation also accepts negative values and values which exceed 2π, like its floating point pendant in math.h does.
So I need to map the given value to a range between 0 and 2π (i.e. their fixed point counterparts). For positive arguments it's easy to trim them with C's build-in remainder operator %. As this is not an option for negative values, I'm using the following (obvious) approach:
unsigned int modulus = (a - (INT_FLOOR(a / b) * b) + b) % b;
a and b are integer typed values and INT_FLOOR() is just meant as a hint that the fractional part of (a/b) is truncated. This formula ensures that the calculated modulus (which is used as an index for the table array) is always positive and that also negative arguments are mapped their positive counterparts (maintaining phase shifts in both directions).
My problem with this approach is that it seems to be overly complex as it involves no less than five arithmetic operations. Is there a more efficient approach I am missing?