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?
Unless your integer arguments have been scaled such that they're in terms of multiples of π (e.g. 65536 means 2π), trying to do argument reduction is probably misguided, as 2π is irrational and any reduction mod 2π will introduce error that scales up with the number of periods until the entire result of the reduction becomes error. This is actually a serious problem in many floating point trig implementations.
I would recommend either not doing argument reduction at all, or using an angle scale based on a power of two rather than radians (so, for example, 0x10000 or 0x1000000 corresponds to 2π or 360 degrees). Then argument reduction becomes a single bitwise and operation.