Whole Credentials in C

C code is usually written

a = b*b;

instead

a = pow(b, 2.0);

for variables double. I get this, since it powis a general function that can handle non-integer metrics, it should be naively assumed that the first version is faster. Interestingly, however, the compiler (gcc) converts calls to powwith integer exponents for direct multiplication as part of any additional optimization.

Assuming that this optimization is not performed, what is the largest integer exponent for which it is faster to write out the manual multiplication, as in b*b* ... *b?

I know that I could conduct performance tests on this machine to find out if I need to even worry, but I would like to have a deeper understanding of what is “right” to do.

+6
source share
2 answers

You want -ffinite-math-only -ffast-mathand maybe #include <tgmath.h> This is the same as -Ofastwithout specifying optimization -O3.

This not only helps these optimizations when turned on -ffinite-math-only and -ffast-math, but the general mathematical model of the type also allows you to compensate when you forget to add the appropriate suffix to the (non-double) mathematical function.

For example:

#include <tgmath.h>
float pow4(float f){return pow(f,4.0f);}
//compiles to
pow4:
    vmulss  xmm0, xmm0, xmm0
    vmulss  xmm0, xmm0, xmm0
    ret

clang 32, gcc , , 2 147 483 647 (, ), -Os ( a jmp pow ) - -, 2.

-ffast-math - , . , -fno-math-errno -funsafe-math-optimizations -ffinite-math-only

+2

- , . , . , pow . pow, , - ( ).

0

All Articles