Gcc hardware square root

I am trying to use hardware instructions to calculate some math functions. For example, the square root (sqrtpd instruction). I am compiling my C code using GCC.

Does anyone know which gcc options are forcibly compiled using hardware instructions and not use libc? Or if I need to do something special in my source code? (Without entering the asm code).

+6
source share
4 answers

In gcc, you should use __builtin_ia32_sqrtpd .

+3
source

The easiest way is to use optimization flags. -O1 generates

sqrtsd %xmm1, %xmm0

in assembly code. Try using the -S flag with gcc to build the assembly and see how optimization flags work.

+3
source

Even if you do not want to write 'asm code', it is single-line with a built-in GCC assembly, and it is worth considering whether you want to force the use of an instruction. For some double value (u) :

 double sqrt_val; __asm__ ("sqrtsd %1, %0" : "=x" (sqrt_val) : "x" (u)); 

A memory source operand is also a legal alternative:

 __asm__ ("sqrtsd %1, %0" : "=x" (sqrt_val) : "xm" (u)); 

This is great for GCC, which (as a rule) will use case when it will be more efficient for this, but otherwise may load the value from memory. This is not so good for clang, which (always!) Spills a register into memory when it is given an alternative restriction of "m" . I would just go with the first form.

If you actually find packed square roots in type __m128d (u) :

 __m128d sqrt_val; __asm__ ("sqrtpd %1, %0" : "=x" (sqrt_val) : "x" (u)); 
0
source

Why don't you just write the right hardware instruction in assembler directly.

As far as I know, it is possible to write assembly code directly in c-code. It is called Inline Assembly. [Cm. Here: http://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html ]

-2
source

All Articles