Multiplying SSE by 2 64-bit Integers

How to multiply two 64-bit integers by another 2 64-bit integers? I did not find instructions that can do this.

+7
source share
2 answers

I know this is an old question, but I really was looking for exactly that. Since there was no instruction for it yet, I implemented 64-bit multiplication myself using pmuldq, as Paul R. mentioned. Here's what I came up with:

// requires g++ -msse4.1 ... #include <emmintrin.h> #include <smmintrin.h> __m128i Multiply64Bit(__m128i a, __m128i b) { auto ax0_ax1_ay0_ay1 = a; auto bx0_bx1_by0_by1 = b; // i means ignored auto ax1_i_ay1_i = _mm_shuffle_epi32(ax0_ax1_ay0_ay1, _MM_SHUFFLE(3, 3, 1, 1)); auto bx1_i_by1_i = _mm_shuffle_epi32(bx0_bx1_by0_by1, _MM_SHUFFLE(3, 3, 1, 1)); auto ax0bx0_ay0by0 = _mm_mul_epi32(ax0_ax1_ay0_ay1, bx0_bx1_by0_by1); auto ax0bx1_ay0by1 = _mm_mul_epi32(ax0_ax1_ay0_ay1, bx1_i_by1_i); auto ax1bx0_ay1by0 = _mm_mul_epi32(ax1_i_ay1_i, bx0_bx1_by0_by1); auto ax0bx1_ay0by1_32 = _mm_slli_epi64(ax0bx1_ay0by1, 32); auto ax1bx0_ay1by0_32 = _mm_slli_epi64(ax1bx0_ay1by0, 32); return _mm_add_epi64(ax0bx0_ay0by0, _mm_add_epi64(ax0bx1_ay0by1_32, ax1bx0_ay1by0_32)); } 

Godbolt on SSE Multiply64Bit .

+4
source

You will need to implement your own 64-bit multiplication procedure using 32-bit multiplication operations. This will probably not be more efficient than just doing it with scalar code, especially since there will be a lot of vector shuffling to get all the necessary operations.

+3
source

Source: https://habr.com/ru/post/950284/


All Articles