c++ optimization
category: code [glöplog]
from this interesting article I found this sentence:
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!
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!
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 :)
(or the -S option in gcc (or use objdump), or look under Product -> Generate Output in XCode :)
What kb said. thread closed.
For some _real_ material on optimization: http://www.agner.org/optimize/
"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):
...or stores the value at double precision:
If you're using floats as intermediate values make sure to enable fp:fast, otherwise the compiler will truncate every assignment.
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.
thank for your answers messieurs :)
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
(yes that's right, i don't happen to have iq's site in my bookmarks)