SSE multiplication of 2 64-bit integers

后端 未结 3 1479
长情又很酷
长情又很酷 2020-12-20 18:45

How to multiply two 64-bit integers by another 2 64-bit integers? I didn\'t find any instruction which can do it.

3条回答
  •  無奈伤痛
    2020-12-20 19:29

    I know this is an old question but I was actually looking for exactly this. As there's still no instruction for it I implemented the 64 bit multiply myself with the pmuldq as Paul R mentioned. This is what I came up with:

    // requires g++ -msse4.1 ...
    
    #include 
    #include 
    
    __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 at SSE Multiply64Bit.

提交回复
热议问题