Archived

This topic is now archived and is closed to further replies.

glJunkie

comparative efficiency of floats / doubles

Recommended Posts

Hi everyone. In C++, which are faster: calculations using floats or calculations using doubles? Is it true that calculations using floats are actually done at double precision and then truncated - effectively meaning you might as well use doubles for calculations? I hope someone can help with this - Ive been confused about it for a while... Thanks! Tim.

Share this post


Link to post
Share on other sites
Yes, internally the fpu of x86 class chips does floating point math at 80 bits (long double). Of course you can change the fpu default setting to work at 32 bits (float) and you gain some speed. But mainly the advantages of using floats over doubles is that you save space, 32 bits for a float vs. 64 bits for a double. And in memory limited architectures like our good ol'' pc, we need all the memory bandwidth we can get.

Share this post


Link to post
Share on other sites
If you stick with floats, you may be able to use SSE; I think doubles need SSE2 which hasn''t got anything like the same market penetration.

Share this post


Link to post
Share on other sites
Thanks for the replies,

Premandrake,
By "change the fpu default setting to work at 32 bits (float)" do you mean simply decalring variables as float? And making sure to declare numbers with an "f", eg:

float i = x + 0.2f;

Also, what is SSE?

Share this post


Link to post
Share on other sites
Well... he doesn''t mean to explicitly FLOAT a value and suffixing and ''f''... he means setting the internal precision of the FPU by using the _control87 or _controlfp function. These functions will change the internal behaviour of the FPU unit in your processor.

Then... SSE is an instruction set for Intel Processors (I believe from Pentium III and upward, but I am not 100% sure) that gives some extra multimedia extensions, under which there are some fast floating point ops (but on this subject I am not 100% sure also... just look it up at www.intel.com)

Finally... to answer your question. I once had the same question... so I built a little benchmark. The results aren''t that surprising...

1) In software emulation of FP, 32 bits (ie floats) are much faster than 64 bits (ie double).

2) In the hardware FPU, 32 bits (ie floats) are a little bit faster than doubles... but not very significantly (about 7% or so)... this is because the FPU always has to copy the value from its internal register to some memory you own... and copying 4 bytes seems faster than 8 (which is logical). The fact that internal truncation occurs doesn''t seem to matter that much at all.

Conslusion... Note that everything I stated here MIGHT NOT WORK FOR YOU!! Ie I tested it all... but my tests could be wrong or the results could be processor specific (I used an AMD Athlon 900). But at least this might have answered your question a bit.

ICQ: 130925152
Email: e.j.folkertsma@student.utwente.nl

Share this post


Link to post
Share on other sites