Source file src/simd/archsimd/clmul_amd64.go
1 // Copyright 2026 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 //go:build goexperiment.simdstable 6 7 package archsimd 8 9 import ( 10 "internal/cpu" 11 "math/bits" 12 ) 13 14 // The carryless multiply instruction on Uint64x2 (VPCLMULQDQ with 128-bit XMM 15 // registers) requires the PCLMULQDQ CPU feature, and on Uint64x4 and Uint64x8 16 // (256- and 512-bit registers) it requires the VPCLMULQDQ CPU feature. Many 17 // CPUs have vectors wider than their carryless multiply: for example, CPUs 18 // with AVX2 before Ice Lake and Zen 3 lack VPCLMULQDQ, as do the first few 19 // generations of CPUs with AVX-512. 20 // 21 // To make carryless multiply available whenever its vector type is, each 22 // CarrylessMul method uses the instruction for its own vector type if the 23 // CPU supports it, and otherwise applies the Uint64x2 instruction to each 24 // 128-bit part of its operands. On the rare CPU that has AVX but lacks 25 // PCLMULQDQ, it falls back to software. 26 // 27 // Each method tests a single CPU feature flag for each of these levels, since 28 // internal/cpu ensures that each wider form of the instruction implies the 29 // narrower ones. 30 // 31 // The xyHiLo immediate of the carrylessMultiply intrinsic must be a constant 32 // for it to compile to a single instruction, so each method spells out its 33 // immediate rather than sharing these steps through helper methods. 34 35 // CarrylessMulEven computes the elementwise carryless multiplication of 36 // even-indexed elements of x and y. The result of each carryless multiply is 37 // twice the width of the input elements. The high bits are stored in elements 38 // z[2*i+1] and the low bits are stored in elements z[2*i]. 39 // 40 // concat(z[2*i+1], z[2*i]) = clmul(x[2*i], y[2*i]) 41 // 42 // A carryless multiplication uses bitwise XOR instead of add-with-carry. 43 // For example, to compute the carryless multiply of 0b1110 and 0b1011: 44 // 45 // 1110 46 // ⊗ 1011 47 // ─────── 48 // 1110 49 // 1110 50 // 0000 51 // ⊕ 1110 52 // ─────── 53 // 1100010 54 // 55 // Carryless multiply can also be viewed as multiplying polynomials with 56 // coefficients from GF(2). For example, the above example can be represented as 57 // 58 // (x^3 + x^2 + x^1) * (x^3 + x^1 + x^0) 59 // = (x^6 + x^5 + (1^1)x^4 + (1^1)x^3 + (1^1)x^2 + x^1) 60 // = (x^6 + x^5 + x^1) 61 // 62 // Asm: VPCLMULQDQ if CPU feature AVXCLMUL, Emulated otherwise, CPU Feature: AVX 63 func (x Uint64x2) CarrylessMulEven(y Uint64x2) (z Uint64x2) { 64 if cpu.X86.HasPCLMULQDQ { 65 return x.carrylessMultiply(0x00, y) 66 } 67 return x.carrylessMultiplySoft(0x00, y) 68 } 69 70 // CarrylessMulOdd computes the elementwise carryless multiplication of 71 // odd-indexed elements of x and y. The result of each carryless multiply is 72 // twice the width of the input elements. The high bits are stored in elements 73 // z[2*i+1] and the low bits are stored in elements z[2*i]. 74 // 75 // concat(z[2*i+1], z[2*i]) = clmul(x[2*i+1], y[2*i+1]) 76 // 77 // See [CarrylessMulEven] for details about carryless multiply. 78 // 79 // Asm: VPCLMULQDQ if CPU feature AVXCLMUL, Emulated otherwise, CPU Feature: AVX 80 func (x Uint64x2) CarrylessMulOdd(y Uint64x2) (z Uint64x2) { 81 if cpu.X86.HasPCLMULQDQ { 82 return x.carrylessMultiply(0x11, y) 83 } 84 return x.carrylessMultiplySoft(0x11, y) 85 } 86 87 // CarrylessMulOddEven computes the elementwise carryless multiplication of 88 // odd-indexed elements of x and even-indexed elements of y. The result of each 89 // carryless multiply is twice the width of the input elements. The high bits 90 // are stored in elements z[2*i+1] and the low bits are stored in elements 91 // z[2*i]. 92 // 93 // concat(z[2*i+1], z[2*i]) = clmul(x[2*i+1], y[2*i]) 94 // 95 // See [CarrylessMulEven] for details about carryless multiply. 96 // 97 // Asm: VPCLMULQDQ if CPU feature AVXCLMUL, Emulated otherwise, CPU Feature: AVX 98 func (x Uint64x2) CarrylessMulOddEven(y Uint64x2) (z Uint64x2) { 99 if cpu.X86.HasPCLMULQDQ { 100 return x.carrylessMultiply(0x01, y) 101 } 102 return x.carrylessMultiplySoft(0x01, y) 103 } 104 105 // CarrylessMulEvenOdd computes the elementwise carryless multiplication of 106 // even-indexed elements of x and odd-indexed elements of y. The result of each 107 // carryless multiply is twice the width of the input elements. The high bits 108 // are stored in elements z[2*i+1] and the low bits are stored in elements 109 // z[2*i]. 110 // 111 // concat(z[2*i+1], z[2*i]) = clmul(x[2*i], y[2*i+1]) 112 // 113 // See [CarrylessMulEven] for details about carryless multiply. 114 // 115 // Asm: VPCLMULQDQ if CPU feature AVXCLMUL, Emulated otherwise, CPU Feature: AVX 116 func (x Uint64x2) CarrylessMulEvenOdd(y Uint64x2) (z Uint64x2) { 117 if cpu.X86.HasPCLMULQDQ { 118 return x.carrylessMultiply(0x10, y) 119 } 120 return x.carrylessMultiplySoft(0x10, y) 121 } 122 123 // CarrylessMulEven computes the elementwise carryless multiplication of 124 // even-indexed elements of x and y. The result of each carryless multiply is 125 // twice the width of the input elements. The high bits are stored in elements 126 // z[2*i+1] and the low bits are stored in elements z[2*i]. 127 // 128 // concat(z[2*i+1], z[2*i]) = clmul(x[2*i], y[2*i]) 129 // 130 // A carryless multiplication uses bitwise XOR instead of add-with-carry. 131 // For example, to compute the carryless multiply of 0b1110 and 0b1011: 132 // 133 // 1110 134 // ⊗ 1011 135 // ─────── 136 // 1110 137 // 1110 138 // 0000 139 // ⊕ 1110 140 // ─────── 141 // 1100010 142 // 143 // Carryless multiply can also be viewed as multiplying polynomials with 144 // coefficients from GF(2). For example, the above example can be represented as 145 // 146 // (x^3 + x^2 + x^1) * (x^3 + x^1 + x^0) 147 // = (x^6 + x^5 + (1^1)x^4 + (1^1)x^3 + (1^1)x^2 + x^1) 148 // = (x^6 + x^5 + x^1) 149 // 150 // Asm: VPCLMULQDQ if CPU feature AVX2CLMUL, Emulated otherwise, CPU Feature: AVX2 151 func (x Uint64x4) CarrylessMulEven(y Uint64x4) (z Uint64x4) { 152 if cpu.X86.HasVPCLMULQDQ { 153 return x.carrylessMultiply(0x00, y) 154 } 155 if cpu.X86.HasPCLMULQDQ { 156 lo := x.Lo().carrylessMultiply(0x00, y.Lo()) 157 hi := x.Hi().carrylessMultiply(0x00, y.Hi()) 158 return lo.Enlarge().SetHi(hi) 159 } 160 return x.carrylessMultiplySoft(0x00, y) 161 } 162 163 // CarrylessMulOdd computes the elementwise carryless multiplication of 164 // odd-indexed elements of x and y. The result of each carryless multiply is 165 // twice the width of the input elements. The high bits are stored in elements 166 // z[2*i+1] and the low bits are stored in elements z[2*i]. 167 // 168 // concat(z[2*i+1], z[2*i]) = clmul(x[2*i+1], y[2*i+1]) 169 // 170 // See [CarrylessMulEven] for details about carryless multiply. 171 // 172 // Asm: VPCLMULQDQ if CPU feature AVX2CLMUL, Emulated otherwise, CPU Feature: AVX2 173 func (x Uint64x4) CarrylessMulOdd(y Uint64x4) (z Uint64x4) { 174 if cpu.X86.HasVPCLMULQDQ { 175 return x.carrylessMultiply(0x11, y) 176 } 177 if cpu.X86.HasPCLMULQDQ { 178 lo := x.Lo().carrylessMultiply(0x11, y.Lo()) 179 hi := x.Hi().carrylessMultiply(0x11, y.Hi()) 180 return lo.Enlarge().SetHi(hi) 181 } 182 return x.carrylessMultiplySoft(0x11, y) 183 } 184 185 // CarrylessMulOddEven computes the elementwise carryless multiplication of 186 // odd-indexed elements of x and even-indexed elements of y. The result of each 187 // carryless multiply is twice the width of the input elements. The high bits 188 // are stored in elements z[2*i+1] and the low bits are stored in elements 189 // z[2*i]. 190 // 191 // concat(z[2*i+1], z[2*i]) = clmul(x[2*i+1], y[2*i]) 192 // 193 // See [CarrylessMulEven] for details about carryless multiply. 194 // 195 // Asm: VPCLMULQDQ if CPU feature AVX2CLMUL, Emulated otherwise, CPU Feature: AVX2 196 func (x Uint64x4) CarrylessMulOddEven(y Uint64x4) (z Uint64x4) { 197 if cpu.X86.HasVPCLMULQDQ { 198 return x.carrylessMultiply(0x01, y) 199 } 200 if cpu.X86.HasPCLMULQDQ { 201 lo := x.Lo().carrylessMultiply(0x01, y.Lo()) 202 hi := x.Hi().carrylessMultiply(0x01, y.Hi()) 203 return lo.Enlarge().SetHi(hi) 204 } 205 return x.carrylessMultiplySoft(0x01, y) 206 } 207 208 // CarrylessMulEvenOdd computes the elementwise carryless multiplication of 209 // even-indexed elements of x and odd-indexed elements of y. The result of each 210 // carryless multiply is twice the width of the input elements. The high bits 211 // are stored in elements z[2*i+1] and the low bits are stored in elements 212 // z[2*i]. 213 // 214 // concat(z[2*i+1], z[2*i]) = clmul(x[2*i], y[2*i+1]) 215 // 216 // See [CarrylessMulEven] for details about carryless multiply. 217 // 218 // Asm: VPCLMULQDQ if CPU feature AVX2CLMUL, Emulated otherwise, CPU Feature: AVX2 219 func (x Uint64x4) CarrylessMulEvenOdd(y Uint64x4) (z Uint64x4) { 220 if cpu.X86.HasVPCLMULQDQ { 221 return x.carrylessMultiply(0x10, y) 222 } 223 if cpu.X86.HasPCLMULQDQ { 224 lo := x.Lo().carrylessMultiply(0x10, y.Lo()) 225 hi := x.Hi().carrylessMultiply(0x10, y.Hi()) 226 return lo.Enlarge().SetHi(hi) 227 } 228 return x.carrylessMultiplySoft(0x10, y) 229 } 230 231 // CarrylessMulEven computes the elementwise carryless multiplication of 232 // even-indexed elements of x and y. The result of each carryless multiply is 233 // twice the width of the input elements. The high bits are stored in elements 234 // z[2*i+1] and the low bits are stored in elements z[2*i]. 235 // 236 // concat(z[2*i+1], z[2*i]) = clmul(x[2*i], y[2*i]) 237 // 238 // A carryless multiplication uses bitwise XOR instead of add-with-carry. 239 // For example, to compute the carryless multiply of 0b1110 and 0b1011: 240 // 241 // 1110 242 // ⊗ 1011 243 // ─────── 244 // 1110 245 // 1110 246 // 0000 247 // ⊕ 1110 248 // ─────── 249 // 1100010 250 // 251 // Carryless multiply can also be viewed as multiplying polynomials with 252 // coefficients from GF(2). For example, the above example can be represented as 253 // 254 // (x^3 + x^2 + x^1) * (x^3 + x^1 + x^0) 255 // = (x^6 + x^5 + (1^1)x^4 + (1^1)x^3 + (1^1)x^2 + x^1) 256 // = (x^6 + x^5 + x^1) 257 // 258 // Asm: VPCLMULQDQ if CPU feature AVX512CLMUL, Emulated otherwise, CPU Feature: AVX512 259 func (x Uint64x8) CarrylessMulEven(y Uint64x8) (z Uint64x8) { 260 if cpu.X86.HasAVX512VPCLMULQDQ { 261 return x.carrylessMultiply(0x00, y) 262 } 263 // There is no Uint64x4 (256-bit) CLMUL feature separate from Uint64x8 264 // CLMUL, so try falling back to Uint64x2 (128-bit) CLMUL. 265 if cpu.X86.HasPCLMULQDQ { 266 xlo, xhi, ylo, yhi := x.Lo(), x.Hi(), y.Lo(), y.Hi() 267 z0 := xlo.Lo().carrylessMultiply(0x00, ylo.Lo()) 268 z1 := xlo.Hi().carrylessMultiply(0x00, ylo.Hi()) 269 z2 := xhi.Lo().carrylessMultiply(0x00, yhi.Lo()) 270 z3 := xhi.Hi().carrylessMultiply(0x00, yhi.Hi()) 271 return z0.Enlarge().SetHi(z1).Enlarge().SetHi(z2.Enlarge().SetHi(z3)) 272 } 273 return x.carrylessMultiplySoft(0x00, y) 274 } 275 276 // CarrylessMulOdd computes the elementwise carryless multiplication of 277 // odd-indexed elements of x and y. The result of each carryless multiply is 278 // twice the width of the input elements. The high bits are stored in elements 279 // z[2*i+1] and the low bits are stored in elements z[2*i]. 280 // 281 // concat(z[2*i+1], z[2*i]) = clmul(x[2*i+1], y[2*i+1]) 282 // 283 // See [CarrylessMulEven] for details about carryless multiply. 284 // 285 // Asm: VPCLMULQDQ if CPU feature AVX512CLMUL, Emulated otherwise, CPU Feature: AVX512 286 func (x Uint64x8) CarrylessMulOdd(y Uint64x8) (z Uint64x8) { 287 if cpu.X86.HasAVX512VPCLMULQDQ { 288 return x.carrylessMultiply(0x11, y) 289 } 290 if cpu.X86.HasPCLMULQDQ { 291 xlo, xhi, ylo, yhi := x.Lo(), x.Hi(), y.Lo(), y.Hi() 292 z0 := xlo.Lo().carrylessMultiply(0x11, ylo.Lo()) 293 z1 := xlo.Hi().carrylessMultiply(0x11, ylo.Hi()) 294 z2 := xhi.Lo().carrylessMultiply(0x11, yhi.Lo()) 295 z3 := xhi.Hi().carrylessMultiply(0x11, yhi.Hi()) 296 return z0.Enlarge().SetHi(z1).Enlarge().SetHi(z2.Enlarge().SetHi(z3)) 297 } 298 return x.carrylessMultiplySoft(0x11, y) 299 } 300 301 // CarrylessMulOddEven computes the elementwise carryless multiplication of 302 // odd-indexed elements of x and even-indexed elements of y. The result of each 303 // carryless multiply is twice the width of the input elements. The high bits 304 // are stored in elements z[2*i+1] and the low bits are stored in elements 305 // z[2*i]. 306 // 307 // concat(z[2*i+1], z[2*i]) = clmul(x[2*i+1], y[2*i]) 308 // 309 // See [CarrylessMulEven] for details about carryless multiply. 310 // 311 // Asm: VPCLMULQDQ if CPU feature AVX512CLMUL, Emulated otherwise, CPU Feature: AVX512 312 func (x Uint64x8) CarrylessMulOddEven(y Uint64x8) (z Uint64x8) { 313 if cpu.X86.HasAVX512VPCLMULQDQ { 314 return x.carrylessMultiply(0x01, y) 315 } 316 if cpu.X86.HasPCLMULQDQ { 317 xlo, xhi, ylo, yhi := x.Lo(), x.Hi(), y.Lo(), y.Hi() 318 z0 := xlo.Lo().carrylessMultiply(0x01, ylo.Lo()) 319 z1 := xlo.Hi().carrylessMultiply(0x01, ylo.Hi()) 320 z2 := xhi.Lo().carrylessMultiply(0x01, yhi.Lo()) 321 z3 := xhi.Hi().carrylessMultiply(0x01, yhi.Hi()) 322 return z0.Enlarge().SetHi(z1).Enlarge().SetHi(z2.Enlarge().SetHi(z3)) 323 } 324 return x.carrylessMultiplySoft(0x01, y) 325 } 326 327 // CarrylessMulEvenOdd computes the elementwise carryless multiplication of 328 // even-indexed elements of x and odd-indexed elements of y. The result of each 329 // carryless multiply is twice the width of the input elements. The high bits 330 // are stored in elements z[2*i+1] and the low bits are stored in elements 331 // z[2*i]. 332 // 333 // concat(z[2*i+1], z[2*i]) = clmul(x[2*i], y[2*i+1]) 334 // 335 // See [CarrylessMulEven] for details about carryless multiply. 336 // 337 // Asm: VPCLMULQDQ if CPU feature AVX512CLMUL, Emulated otherwise, CPU Feature: AVX512 338 func (x Uint64x8) CarrylessMulEvenOdd(y Uint64x8) (z Uint64x8) { 339 if cpu.X86.HasAVX512VPCLMULQDQ { 340 return x.carrylessMultiply(0x10, y) 341 } 342 if cpu.X86.HasPCLMULQDQ { 343 xlo, xhi, ylo, yhi := x.Lo(), x.Hi(), y.Lo(), y.Hi() 344 z0 := xlo.Lo().carrylessMultiply(0x10, ylo.Lo()) 345 z1 := xlo.Hi().carrylessMultiply(0x10, ylo.Hi()) 346 z2 := xhi.Lo().carrylessMultiply(0x10, yhi.Lo()) 347 z3 := xhi.Hi().carrylessMultiply(0x10, yhi.Hi()) 348 return z0.Enlarge().SetHi(z1).Enlarge().SetHi(z2.Enlarge().SetHi(z3)) 349 } 350 return x.carrylessMultiplySoft(0x10, y) 351 } 352 353 // The carrylessMultiplySoft methods are all marked "//go:noinline" to ensure 354 // that the hardware fast paths of the CarrylessMul* methods are small 355 // enough to inline. 356 357 // carrylessMultiplySoft is a software implementation of x.carrylessMultiply(xyHiLo, y). 358 // 359 //go:noinline 360 func (x Uint64x2) carrylessMultiplySoft(xyHiLo uint8, y Uint64x2) Uint64x2 { 361 var xs, ys, zs [2]uint64 362 x.StoreArray(&xs) 363 y.StoreArray(&ys) 364 carrylessMultiplySoft(zs[:], xs[:], ys[:], xyHiLo) 365 return LoadUint64x2Array(&zs) 366 } 367 368 // carrylessMultiplySoft is a software implementation of x.carrylessMultiply(xyHiLo, y). 369 // 370 //go:noinline 371 func (x Uint64x4) carrylessMultiplySoft(xyHiLo uint8, y Uint64x4) Uint64x4 { 372 var xs, ys, zs [4]uint64 373 x.StoreArray(&xs) 374 y.StoreArray(&ys) 375 carrylessMultiplySoft(zs[:], xs[:], ys[:], xyHiLo) 376 return LoadUint64x4Array(&zs) 377 } 378 379 // carrylessMultiplySoft is a software implementation of x.carrylessMultiply(xyHiLo, y). 380 // 381 //go:noinline 382 func (x Uint64x8) carrylessMultiplySoft(xyHiLo uint8, y Uint64x8) Uint64x8 { 383 var xs, ys, zs [8]uint64 384 x.StoreArray(&xs) 385 y.StoreArray(&ys) 386 carrylessMultiplySoft(zs[:], xs[:], ys[:], xyHiLo) 387 return LoadUint64x8Array(&zs) 388 } 389 390 // carrylessMultiplySoft sets each pair of elements of z to the 128-bit 391 // carryless product of one element from the corresponding pair of x and one 392 // from the corresponding pair of y. As for the carrylessMultiply intrinsic, 393 // bit 0 of xyHiLo selects the low (0) or high (1) element of each pair of x, 394 // and bit 4 does the same for y. 395 func carrylessMultiplySoft(z, x, y []uint64, xyHiLo uint8) { 396 xi, yi := int(xyHiLo&1), int(xyHiLo>>4&1) 397 for i := 0; i < len(z); i += 2 { 398 z[i], z[i+1] = carrylessMultiply64(x[i+xi], y[i+yi]) 399 } 400 } 401 402 // carrylessMultiply64 returns the 128-bit carryless product of x and y. It is 403 // constant-time, assuming that integer multiplication is. 404 // 405 // It uses integer multiplication with "holes": it splits each operand into 406 // five parts, where part i holds the bits whose position is i mod 5, so that 407 // the set bits of each part are separated by at least four zeros. In the 408 // integer product of two parts, the bits whose position is (i+j) mod 5 are 409 // then sums of at most 13 one-bit products, which fit in the four bits of the 410 // hole and so never carry into each other. The low bit of each sum is the 411 // carryless product bit. See 412 // https://timtaubert.de/blog/2017/06/verified-binary-multiplication-for-ghash/. 413 func carrylessMultiply64(x, y uint64) (lo, hi uint64) { 414 const ( 415 // mi selects the bits whose position is i mod 5. Since 64 is 4 416 // mod 5, in the high half of a 128-bit value, m(i+1) selects 417 // those bits instead. 418 m0 = 0x1084210842108421 419 m1 = 0x2108421084210842 420 m2 = 0x4210842108421084 421 m3 = 0x8421084210842108 422 m4 = 0x0842108421084210 423 ) 424 x0, x1, x2, x3, x4 := x&m0, x&m1, x&m2, x&m3, x&m4 425 y0, y1, y2, y3, y4 := y&m0, y&m1, y&m2, y&m3, y&m4 426 427 // mul5 returns the XOR of the five integer products ai*bi. In the 428 // products for result part k, the indexes of each ai and bi sum to 429 // k mod 5. 430 mul5 := func(a0, b0, a1, b1, a2, b2, a3, b3, a4, b4 uint64) (lo, hi uint64) { 431 h0, l0 := bits.Mul64(a0, b0) 432 h1, l1 := bits.Mul64(a1, b1) 433 h2, l2 := bits.Mul64(a2, b2) 434 h3, l3 := bits.Mul64(a3, b3) 435 h4, l4 := bits.Mul64(a4, b4) 436 return l0 ^ l1 ^ l2 ^ l3 ^ l4, h0 ^ h1 ^ h2 ^ h3 ^ h4 437 } 438 l0, h0 := mul5(x0, y0, x1, y4, x2, y3, x3, y2, x4, y1) 439 l1, h1 := mul5(x0, y1, x1, y0, x2, y4, x3, y3, x4, y2) 440 l2, h2 := mul5(x0, y2, x1, y1, x2, y0, x3, y4, x4, y3) 441 l3, h3 := mul5(x0, y3, x1, y2, x2, y1, x3, y0, x4, y4) 442 l4, h4 := mul5(x0, y4, x1, y3, x2, y2, x3, y1, x4, y0) 443 444 lo = l0&m0 | l1&m1 | l2&m2 | l3&m3 | l4&m4 445 hi = h0&m1 | h1&m2 | h2&m3 | h3&m4 | h4&m0 446 return lo, hi 447 } 448