Compute x ^ y with GCC intrinsic properties

Suppose I have a 2-element vector that is defined as follows (using the GCC syntax for packed vectors)

// packed vector of 2-elements typedef double v2d __attribute__((vector_size(sizeof(double)*2))); v2d x = ...; double y = ...; x[0] = pow(x[0], y) x[1] = pow(x[1], y) 

I would like to know if there is a faster way to do two power calculations using vector operations. The GCC architecture is on x86-64, and the platform code is fine.

+6
source share
2 answers

Yes, it should be possible if you have no special cases (negative numbers, 0, 1, NaN, etc.), so the code path is linear.

Here is the general code for the pow function to double IEEE754, it does not have loop cycles, so if you create all the special cases, vectorization seems simple. Enjoy.

+5
source

You can directly contact elements with the correct parameters. GCC and ICC will use the pow vector function

 #include <math.h> typedef double vnd __attribute__((vector_size(sizeof(double)*2))); vnd foo(vnd x, vnd y) { #pragma omp simd for(int i=0; i<2; i++) x[i] = pow(x[i], y[i]); return x; } 

Only with -O2 call __svml_pow2 ICC generate just call __svml_pow2 . SVML (Brief Vector Math Library) is Intel's vectorized math library. With -Ofast -fopenmp GCC generates just call _ZGVbN2vv___pow_finite .

Klang does not vectorize it.

https://godbolt.org/g/pjpzFX

+1
source

All Articles