Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSE integer 2^n powers of 2 for 32-bit integers without AVX2

I can't find an SSE instruction for computing 2^n for a vector __m128i of 32-bit integers.

Is there an instruction or function that does the following pseudocode?

__m128i power_of_two(__m128i b) {
    __m128 r;
    for (int i = 0; i < 4; i++)
        r[i] = 1 << b[i];
    return r;
}

The _mm_sll_epi32 instruction only computes r[i] = a[i] << b[0].

like image 449
Vortico Avatar asked Jan 26 '23 23:01

Vortico


1 Answers

There is no single instruction pre-AVX2, but even with just SSE2 there is a trick that abuses the floating point format to generate powers of two by generating the exponent field with integer arithmetic and then converting that from a float to an integer. There may be faster options.

__m128i power_of_two(__m128i b) {
    __m128i exp = _mm_add_epi32(b, _mm_set1_epi32(127));
    __m128 f = _mm_castsi128_ps(_mm_slli_epi32(exp, 23));
    return _mm_cvtps_epi32(f);
}
like image 99
harold Avatar answered Jan 29 '23 10:01

harold