pouët.net

Go to bottom

c++ optimization

category: code [glöplog]
 
from this interesting article I found this sentence:
Quote:
The rest are normal compiler optimizations. But, there is something you can still do in the C code to reduce the code size: use allways the f subscript for the floaring point constants, otherwise a double will be stored in the executable, and a innecesary conversion will occur. As the size of a double is two times the size of a float, lot of space can be wasted if not carrefully adding the f to all the constants. Normally, if you follow the rule, you should be able to parse all the assembly listing code and find zero QWORDs in the code.


My question:
I generally write "0" instead of "0.0f", so I have to add the "f" everywhere like for all the others float values or since "0" is a special value it's ok to keep it without ".0f"?

thank you!
added on the 2011-09-12 13:20:29 by rez rez
Most compilers I know infer the correct type if there's a 1:1 conversion (which is possible for 0 but eg. not for 0.1). But how about this: right click on project -> Properties -> C/C++ -> Output Files -> Set "Assembler output" to "Assembly with Source Code" and look for yourself :)

(or the -S option in gcc (or use objdump), or look under Product -> Generate Output in XCode :)
added on the 2011-09-12 13:29:04 by kb_ kb_
What kb said. thread closed.
added on the 2011-09-12 13:57:53 by las las
For some _real_ material on optimization: http://www.agner.org/optimize/
added on the 2011-09-12 14:12:02 by superplek superplek
"0" is a special case because it's actually an integer.
Generally the compiler is either able to truncate floating point values at compile time (which gives a warning):
Code:float bla[3]= {0.1, 0.2, 0.3};

...or stores the value at double precision:
Code:float bla= 3.14159265359 * blub;

If you're using floats as intermediate values make sure to enable fp:fast, otherwise the compiler will truncate every assignment.
added on the 2011-09-12 14:19:10 by hfr hfr
thank for your answers messieurs :)
added on the 2011-09-12 14:20:09 by rez rez
BEAUREGARD: Interesting read, thanks!
you could also download iq's float header which contains a collection of "packable" floats :)
links or it didnt happen you beer-swilling viking
added on the 2011-09-12 19:47:22 by superplek superplek
(yes that's right, i don't happen to have iq's site in my bookmarks)
added on the 2011-09-12 19:47:47 by superplek superplek

login

Go to top