Lukas Hohl
Lukas Hohl

Reputation: 69

How can I multiply 2 x 2 matrices of 64 bit integers efficiently using SSE/AVX instructions?

Is there a way to multiply 2 x 2 matrices of unsigned 64 bit integers using SSE or AVX,

that is more efficient than just using none SSE/AVX instructions?

Upvotes: 0

Views: 430

Answers (2)

user371416
user371416

Reputation: 56

If you want the full 128-bit results, things are slightly more complicated (again, ymm0 = matrix A and ymm1 = matrix B on input):

vpcmpeqq      ymm12,ymm12,ymm12
vpermq        ymm2,ymm0,0x8D
vpermq        ymm3,ymm1,0x4E
vpermq        ymm0,ymm0,0xD8
vpsllq        ymm12,ymm12,63
vpclmullqlqdq xmm4,xmm0,xmm1
vpclmulhqlqdq xmm5,xmm0,xmm1
vpclmullqhqdq xmm6,xmm0,xmm1
vpclmulhqhqdq xmm7,xmm0,xmm1
vpclmullqlqdq xmm8,xmm2,xmm3
vpclmulhqlqdq xmm9,xmm2,xmm3
vpclmullqhqdq xmm10,xmm2,xmm3
vpclmulhqhqdq xmm11,xmm2,xmm3
vpunpcklqdq   xmm0,xmm4,xmm5
vpunpckhqdq   xmm1,xmm4,xmm5
vpunpcklqdq   xmm2,xmm6,xmm7
vpunpckhqdq   xmm3,xmm6,xmm7
vpunpcklqdq   xmm4,xmm8,xmm9
vpunpckhqdq   xmm5,xmm8,xmm9
vpunpcklqdq   xmm6,xmm10,xmm11
vpunpckhqdq   xmm7,xmm10,xmm11
vinserti128   ymm0,ymm0,xmm2,1
vinserti128   ymm1,ymm1,xmm3,1
vinserti128   ymm2,ymm4,xmm6,1
vinserti128   ymm3,ymm5,xmm7,1
vpaddq        ymm2,ymm2,ymm0
vpaddq        ymm3,ymm3,ymm1
vpxor         ymm4,ymm12,ymm0
vpxor         ymm5,ymm12,ymm2
vpcmpgtq      ymm6,ymm4,ymm5
vpsubq        ymm3,ymm3,ymm6
vpunpcklqdq   ymm0,ymm2,ymm3
vpunpckhqdq   ymm1,ymm2,ymm3

then you get the four 128-bit matrix product coefficients in ymm0 and ymm1.

Upvotes: 0

user371416
user371416

Reputation: 56

If you only want the bottom 64 bit of the result, things are easier. Assume ymm0 to contain the four values of matrix A and ymm1 the four values of matrix B, you could compute the product as follows:

vpermq        ymm2,ymm0,0x8D
vpermq        ymm3,ymm1,0x4E
vpermq        ymm0,ymm0,0xD8
vpclmullqlqdq xmm4,xmm0,xmm1
vpclmulhqlqdq xmm5,xmm0,xmm1
vpclmullqhqdq xmm6,xmm0,xmm1
vpclmulhqhqdq xmm7,xmm0,xmm1
vpclmullqlqdq xmm8,xmm2,xmm3
vpclmulhqlqdq xmm9,xmm2,xmm3
vpclmullqhqdq xmm10,xmm2,xmm3
vpclmulhqhqdq xmm11,xmm2,xmm3
vpunpcklqdq   xmm0,xmm4,xmm6
vpunpcklqdq   xmm1,xmm5,xmm7
vpunpcklqdq   xmm2,xmm8,xmm10
vpunpcklqdq   xmm3,xmm9,xmm11
vinserti128   ymm0,ymm0,xmm1,1
vinserti128   ymm2,ymm2,xmm3,1
vpaddq        ymm0,ymm0,ymm2

As a result, ymm0 contains the four bottom 64 bit integers of the matrix product.

Upvotes: 1

Related Questions