Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.
7014 Discussions

fast test for double < 0.0 and round it to 0.0


Hi, (Intel 10.1, VS2005, min target build Core2 microarch SSE3)

I have a very tight loop decrementing a double that looks like:

double time, dt;
for (int t=N-1; t>=0; t--)
time -= dt;

with dt variable, depending on t, but such that at the end of the loop (t==0), time shouldbe "exactly" 0.0.. Due naturally to floating calculations, time sometimes becomes negative-1e-170 or so...

1) I believe there is some flag to "round down" to 0 if a double is small "enough"...

2) Otherwise, i could do a test to see if time is negative....
time -= dt;
if (time<0.0) time=0.0;
Is this fast enough? Or is there some other function from mathimf.h to do it faster? ( i mean that gets assembled to some more appropriate instruction )....


0 Kudos
1 Reply
Honored Contributor III
In such a situation, you may get better results from something similar to
time = t * dt;
Possibly faster might be to keep a double copy of t to use in this calculation.

Setting abrupt underflow (e.g. by setting /Qftz in your compilation) might avoid accidental generation of such small non-zero numbers.

When you use SSE2 or SSE3, your
if (time<0.0) time=0.0;
should compile to fast code (maxsd instruction, no branching).
0 Kudos