Greg Ercolano wrote:
> MacArthur, Ian (SELEX) (UK) wrote:
>> awake_ring_head_ = (awake_ring_head_ + 1) & (AWAKE_RING_SIZE - 1);
>
> Maybe I'm missing something, but surely a mod (%) would be better:
>
> awake_ring_head_ = (++awake_ring_head_) % AWAKE_RING_SIZE;
>
> No power of 2 restriction (which is a bomb just waiting to go off).
>
> Integer divides are low overhead, and gets rid of the -1.
> The only restriction is that AWAKE_RING_SIZE never be zero ;)
Sometimes I deal with circular buffers, so if find this interesting. I
always wrap around using an if-statement mainly for clarity:
++idx;
if(idx >= RING_SIZE)
idx = 0;
But, from what is being said here, the conditional version is more expensive
than the arithmetic version? I guess that makes sense since the condition
version results in a CMP/setting flags/JGE where the arithmetic version is
one instruction (IDIV).
Along those lines, how does this apply to fmod()?
static const double TWO_PI = 6.2831853;
double radians = do_something();
// force radians between [0, 2PI)
if(radians >= TWO_PI)
radians = fmod(radians, TWO_PI);
I've used the if-statement as a way to minimise calling fmod(), which I have
thought would be expensive. Now, I'm wondering if it would be better to do
this:
double radians = fmod(do_something(), TWO_PI);
What is your experiences with the efficiency of fmod()? In your opinion,
which would be "better"? I develop using the GNU C/C++ libraries, if that
makes a difference.
Thanks
--
Alvin
_______________________________________________
fltk-dev mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk-dev