| 1 |
//go:generate go run src.go -out ../amd64.s -stubs ../stubs_amd64.go -pkg dilithium |
| 2 |
|
| 3 |
// AVX2 optimized version of Poly.[Inv]NTT(). See the comments on the generic |
| 4 |
// implementation for details on the maths involved. |
| 5 |
package main |
| 6 |
|
| 7 |
import ( |
| 8 |
. "github.com/mmcloughlin/avo/build" // nolint:golint,stylecheck |
| 9 |
. "github.com/mmcloughlin/avo/operand" // nolint:golint,stylecheck |
| 10 |
. "github.com/mmcloughlin/avo/reg" // nolint:golint,stylecheck |
| 11 |
|
| 12 |
"github.com/cloudflare/circl/sign/internal/dilithium/params" |
| 13 |
) |
| 14 |
|
| 15 |
// XXX align Poly on 16 bytes such that we can use aligned moves |
| 16 |
// XXX ensure Zetas and InvZetas are 16 byte aligned. |
| 17 |
|
| 18 |
func broadcastImm32(c uint32, out Op) { |
| 19 |
tmp1 := GP32() |
| 20 |
tmp2 := XMM() |
| 21 |
MOVL(U32(c), tmp1) |
| 22 |
VMOVD(tmp1, tmp2) |
| 23 |
VPBROADCASTD(tmp2, out) |
| 24 |
} |
| 25 |
|
| 26 |
// Performs AND with an 64b immediate. |
| 27 |
func andImm64(c uint64, inout Op) { |
| 28 |
tmp := GP64() |
| 29 |
MOVQ(U64(c), tmp) |
| 30 |
ANDQ(tmp, inout) |
| 31 |
} |
| 32 |
|
| 33 |
// Executes the permutation (a[2] b[0]) (a[3] b[1]) when considering only the |
| 34 |
// even positions of a and b seen as [8]uint32. |
| 35 |
func swapInner(a, b Op) { |
| 36 |
tmp := YMM() |
| 37 |
VPERM2I128(U8(32), b, a, tmp) // 0 + 2*16 |
| 38 |
VPERM2I128(U8(49), b, a, b) // 1 + 3*16 |
| 39 |
VMOVDQA(tmp, a) |
| 40 |
} |
| 41 |
|
| 42 |
// Executes the permutation (a[1] b[0]) (a[3] b[2]) when considering only the |
| 43 |
// even positions of a and b seen as [8]uint32. |
| 44 |
func oddCrossing(a, b Op) { |
| 45 |
tmp := YMM() |
| 46 |
VPUNPCKLQDQ(b, a, tmp) |
| 47 |
VPUNPCKHQDQ(b, a, b) |
| 48 |
VMOVDQA(tmp, a) |
| 49 |
} |
| 50 |
|
| 51 |
// nolint:funlen |
| 52 |
func nttAVX2() { |
| 53 |
// We perform the same operations as the generic implementation of NTT, |
| 54 |
// but use AVX2 to perform 16 butterflies at the same time. For the |
| 55 |
// first few levels this is straight forward. For the final levels we |
| 56 |
// need to move some coefficients around to be able to use the AVX2 |
| 57 |
// instructions. |
| 58 |
|
| 59 |
TEXT("nttAVX2", 0, "func(p *[256]uint32)") |
| 60 |
Pragma("noescape") |
| 61 |
pPtr := Load(Param("p"), GP64()) |
| 62 |
zetasPtr := GP64() |
| 63 |
LEAQ(NewDataAddr(Symbol{Name: "·Zetas"}, 0), zetasPtr) |
| 64 |
|
| 65 |
// We allocate a [256]uint64 on the stack aligned to 32 bytes to hold |
| 66 |
// "buf" which contains intermediate coefficients like "p" in the generic |
| 67 |
// algorithm, but then in uint64s instead of uint32s. |
| 68 |
bufPtr := GP64() |
| 69 |
LEAQ(AllocLocal(256*8+32), bufPtr) // +32 to be able to align |
| 70 |
|
| 71 |
andImm64(0xffffffffffffffe0, bufPtr) |
| 72 |
|
| 73 |
q := YMM() |
| 74 |
broadcastImm32(params.Q, q) |
| 75 |
doubleQ := YMM() |
| 76 |
broadcastImm32(2*params.Q, doubleQ) |
| 77 |
qinv := YMM() |
| 78 |
broadcastImm32(params.Qinv, qinv) // 4236238847 = -(q^-1) mod 2³² |
| 79 |
|
| 80 |
// Computes 4x4 Cooley--Tukey butterflies (a,b) ↦ (a + ζb, a - ζb). |
| 81 |
ctButterfly := func(a1, b1, zeta1, a2, b2, zeta2, a3, b3, zeta3, |
| 82 |
a4, b4, zeta4 Op) { |
| 83 |
t := [4]Op{YMM(), YMM(), YMM(), YMM()} |
| 84 |
a := [4]Op{a1, a2, a3, a4} |
| 85 |
b := [4]Op{b1, b2, b3, b4} |
| 86 |
zeta := [4]Op{zeta1, zeta2, zeta3, zeta4} |
| 87 |
|
| 88 |
// Set b = bζ. |
| 89 |
for i := 0; i < 4; i++ { |
| 90 |
VPMULUDQ(b[i], zeta[i], b[i]) |
| 91 |
} |
| 92 |
|
| 93 |
// Now we reduce b below 2Q with the method of reduceLe2Q(): |
| 94 |
// |
| 95 |
// t := ((b * 4236238847) & 0xffffffff) * uint64(Q) |
| 96 |
// return uint32((b + t) >> 32) |
| 97 |
for i := 0; i < 4; i++ { |
| 98 |
// t = b * 4236238847. |
| 99 |
VPMULUDQ(qinv, b[i], t[i]) |
| 100 |
} |
| 101 |
|
| 102 |
// t = (t & 0xffffffff) * Q. The and is implicit as VPMULUDQ |
| 103 |
// is a parallel 32b x 32b -> 64b multiplication. |
| 104 |
for i := 0; i < 4; i++ { |
| 105 |
VPMULUDQ(q, t[i], t[i]) |
| 106 |
} |
| 107 |
|
| 108 |
// t = b + t |
| 109 |
for i := 0; i < 4; i++ { |
| 110 |
VPADDQ(t[i], b[i], t[i]) |
| 111 |
} |
| 112 |
|
| 113 |
// t = t >> 32 |
| 114 |
for i := 0; i < 4; i++ { |
| 115 |
VPSRLQ(U8(32), t[i], t[i]) |
| 116 |
} |
| 117 |
|
| 118 |
// b = a + 2Q |
| 119 |
for i := 0; i < 4; i++ { |
| 120 |
VPADDD(a[i], doubleQ, b[i]) |
| 121 |
} |
| 122 |
|
| 123 |
// a += t |
| 124 |
for i := 0; i < 4; i++ { |
| 125 |
VPADDD(t[i], a[i], a[i]) |
| 126 |
} |
| 127 |
|
| 128 |
// b = b - t |
| 129 |
for i := 0; i < 4; i++ { |
| 130 |
VPSUBD(t[i], b[i], b[i]) |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
zs := [4]Op{YMM(), YMM(), YMM(), YMM()} |
| 135 |
var xs [8]VecVirtual |
| 136 |
for i := 0; i < 8; i++ { |
| 137 |
xs[i] = YMM() |
| 138 |
} |
| 139 |
|
| 140 |
// With AVX2 we can compute 4*4 Cooley--Tukey butterflies at the same time. |
| 141 |
// As loading and storing from memory is expensive, we try to compute |
| 142 |
// as much at the same time. |
| 143 |
|
| 144 |
// First, second and third level. |
| 145 |
// The first butterfly at the third level is (0, 32). To compute it, we |
| 146 |
// need to compute some butterflies on the second level and in turn |
| 147 |
// the butterflies (0, 128), (32, 160), (64, 192) and (96, 224) on the |
| 148 |
// first level. As we need to compute them anyway, we compute the |
| 149 |
// butterflies (0, 32), (64, 96), (128, 160) and (192, 224) on the |
| 150 |
// third level at the same time. Using the uint64x4 AVX2 registers, |
| 151 |
// we compute (0, 32), (1, 33), ..., (4, 36), (64, 96), (64, 97), ... |
| 152 |
// in one go. This is one eighth of the third level. We repeat another |
| 153 |
// seven times with a shifted offset to compute the third level. |
| 154 |
|
| 155 |
// XXX should we really unroll this loop? |
| 156 |
for offset := 0; offset < 8; offset++ { |
| 157 |
// First level. |
| 158 |
// Load the coefficients. First uint32s of xs[0], xs[1], ... |
| 159 |
// contains p[0], p[32], p[64], ..., p[224]. |
| 160 |
for i := 0; i < 8; i++ { |
| 161 |
// Loads 4 32b coefficients at the same time; zeropads them to 64b |
| 162 |
// and puts them in xs[i]. |
| 163 |
VPMOVZXDQ(Mem{Base: pPtr, Disp: 4 * (32*i + 4*offset)}, xs[i]) |
| 164 |
} |
| 165 |
|
| 166 |
// XXX At the moment we've completely unrolled, so we could, if we want, |
| 167 |
// hardcode the Zetas here instead of looking them up from memory. |
| 168 |
// Is that worth it? |
| 169 |
|
| 170 |
VPBROADCASTD(Mem{Base: zetasPtr, Disp: 1 * 4}, zs[0]) // Zetas[1] |
| 171 |
|
| 172 |
ctButterfly( |
| 173 |
xs[0], xs[4], zs[0], |
| 174 |
xs[1], xs[5], zs[0], |
| 175 |
xs[2], xs[6], zs[0], |
| 176 |
xs[3], xs[7], zs[0], |
| 177 |
) |
| 178 |
|
| 179 |
// Second level |
| 180 |
VPBROADCASTD(Mem{Base: zetasPtr, Disp: 2 * 4}, zs[0]) // Zetas[2] |
| 181 |
VPBROADCASTD(Mem{Base: zetasPtr, Disp: 3 * 4}, zs[1]) // Zetas[3] |
| 182 |
|
| 183 |
ctButterfly( |
| 184 |
xs[0], xs[2], zs[0], |
| 185 |
xs[1], xs[3], zs[0], |
| 186 |
xs[4], xs[6], zs[1], |
| 187 |
xs[5], xs[7], zs[1], |
| 188 |
) |
| 189 |
|
| 190 |
// Third level |
| 191 |
VPBROADCASTD(Mem{Base: zetasPtr, Disp: 4 * 4}, zs[0]) // Zetas[4] |
| 192 |
VPBROADCASTD(Mem{Base: zetasPtr, Disp: 5 * 4}, zs[1]) // Zetas[5] |
| 193 |
VPBROADCASTD(Mem{Base: zetasPtr, Disp: 6 * 4}, zs[2]) // Zetas[6] |
| 194 |
VPBROADCASTD(Mem{Base: zetasPtr, Disp: 7 * 4}, zs[3]) // Zetas[7] |
| 195 |
|
| 196 |
ctButterfly( |
| 197 |
xs[0], xs[1], zs[0], |
| 198 |
xs[2], xs[3], zs[1], |
| 199 |
xs[4], xs[5], zs[2], |
| 200 |
xs[6], xs[7], zs[3], |
| 201 |
) |
| 202 |
|
| 203 |
for i := 0; i < 8; i++ { |
| 204 |
VMOVDQA(xs[i], Mem{Base: bufPtr, Disp: 8 * (32*i + 4*offset)}) |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
// Fourth, fifth, sixth, seventh and eighth level. |
| 209 |
// If we want to compute the butterfly (0, 1) in the eighth level, we need |
| 210 |
// to compute the first 2 butterflies in the seventh level; the first 4 |
| 211 |
// of the sixth, ... and the first 16 in the fourth level which needs the |
| 212 |
// first 32 coefficients already computed in the third level. |
| 213 |
// Going forward again, we see that we can use these to compute the first |
| 214 |
// 32 coefficients. As each level requires 16 butterflies, we can |
| 215 |
// conveniently perform these all in our YMM registers. |
| 216 |
// After that we repeat the same method for the next 32 coefficients and |
| 217 |
// continue for a total of eight times to finish the computation of |
| 218 |
// the NTT. |
| 219 |
|
| 220 |
// XXX should we really unroll this loop? |
| 221 |
for offset := 0; offset < 8; offset++ { |
| 222 |
// Load the first 32 coefficients from level 3. Recall that bufPtr |
| 223 |
// has 64 bits of space for each coefficient. |
| 224 |
for i := 0; i < 8; i++ { |
| 225 |
VMOVDQA(Mem{Base: bufPtr, Disp: 8 * 4 * (8*offset + i)}, xs[i]) |
| 226 |
} |
| 227 |
|
| 228 |
// Fourth level |
| 229 |
VPBROADCASTD(Mem{Base: zetasPtr, Disp: (8 + offset) * 4}, zs[0]) |
| 230 |
ctButterfly( |
| 231 |
xs[0], xs[4], zs[0], |
| 232 |
xs[1], xs[5], zs[0], |
| 233 |
xs[2], xs[6], zs[0], |
| 234 |
xs[3], xs[7], zs[0], |
| 235 |
) |
| 236 |
|
| 237 |
// Fifth level |
| 238 |
VPBROADCASTD(Mem{Base: zetasPtr, Disp: (16 + offset*2) * 4}, zs[0]) |
| 239 |
VPBROADCASTD(Mem{Base: zetasPtr, Disp: (16 + offset*2 + 1) * 4}, zs[1]) |
| 240 |
ctButterfly( |
| 241 |
xs[0], xs[2], zs[0], |
| 242 |
xs[1], xs[3], zs[0], |
| 243 |
xs[4], xs[6], zs[1], |
| 244 |
xs[5], xs[7], zs[1], |
| 245 |
) |
| 246 |
|
| 247 |
// Sixth level |
| 248 |
for i := 0; i < 4; i++ { |
| 249 |
VPBROADCASTD(Mem{Base: zetasPtr, Disp: (32 + offset*4 + i) * 4}, zs[i]) |
| 250 |
} |
| 251 |
ctButterfly( |
| 252 |
xs[0], xs[1], zs[0], |
| 253 |
xs[2], xs[3], zs[1], |
| 254 |
xs[4], xs[5], zs[2], |
| 255 |
xs[6], xs[7], zs[3], |
| 256 |
) |
| 257 |
|
| 258 |
// Seventh level |
| 259 |
// Now things get a bit trickier. We have to compute the butterflies |
| 260 |
// (0, 2), (1, 3), (4, 6), (5, 7), etc which don't fit our ctButterfly() |
| 261 |
// routine, which likes to have four consecutive butterflies. |
| 262 |
// To work around this, we swap 2 with 4 and 3 with 5, etc., which |
| 263 |
// allows us to use our old routine. |
| 264 |
|
| 265 |
tmp := YMM() |
| 266 |
// XXX optimize? We might want to add a small extra table for just |
| 267 |
// these zetas so that we don't have to blend them. |
| 268 |
for i := 0; i < 4; i++ { |
| 269 |
VPBROADCASTD(Mem{Base: zetasPtr, Disp: (64 + offset*8 + i*2) * 4}, tmp) |
| 270 |
VPBROADCASTD(Mem{Base: zetasPtr, Disp: (64 + offset*8 + i*2 + 1) * 4}, zs[i]) |
| 271 |
VPBLENDD(U8(240), zs[i], tmp, zs[i]) |
| 272 |
} |
| 273 |
|
| 274 |
swapInner(xs[0], xs[1]) |
| 275 |
swapInner(xs[2], xs[3]) |
| 276 |
swapInner(xs[4], xs[5]) |
| 277 |
swapInner(xs[6], xs[7]) |
| 278 |
|
| 279 |
ctButterfly( |
| 280 |
xs[0], xs[1], zs[0], |
| 281 |
xs[2], xs[3], zs[1], |
| 282 |
xs[4], xs[5], zs[2], |
| 283 |
xs[6], xs[7], zs[3], |
| 284 |
) |
| 285 |
|
| 286 |
// Eighth level |
| 287 |
// Finally, we have to perform the butterflies (0, 1), (2, 3), etc. |
| 288 |
// Swapping 1 with 4 and 3 with 6 (etc.) will ensure that a |
| 289 |
// straight-forward call to our ctButterfly() routine will do the right |
| 290 |
// thing. |
| 291 |
oddCrossing(xs[0], xs[1]) |
| 292 |
oddCrossing(xs[2], xs[3]) |
| 293 |
oddCrossing(xs[4], xs[5]) |
| 294 |
oddCrossing(xs[6], xs[7]) |
| 295 |
|
| 296 |
for i := 0; i < 4; i++ { |
| 297 |
VPMOVZXDQ(Mem{Base: zetasPtr, Disp: (128 + 4*i + offset*16) * 4}, zs[i]) |
| 298 |
} |
| 299 |
|
| 300 |
ctButterfly( |
| 301 |
xs[0], xs[1], zs[0], |
| 302 |
xs[2], xs[3], zs[1], |
| 303 |
xs[4], xs[5], zs[2], |
| 304 |
xs[6], xs[7], zs[3], |
| 305 |
) |
| 306 |
|
| 307 |
// Packing. |
| 308 |
// Due to swapInner() and oddCrossing() our coefficients are laid out |
| 309 |
// as 0, 2, 4, 6, 1, 3, 5, 7, 8, 10, ... in xs[0], xs[1], ... |
| 310 |
// with junk 32b in between. By shifting the odd xss 32b to the |
| 311 |
// left and merging them with the even xss, we get the desired |
| 312 |
// order 0, 1, 2, 3, ... without any padding, which can then be |
| 313 |
// moved out into memory. |
| 314 |
|
| 315 |
VPSLLQ(U8(32), xs[1], xs[1]) |
| 316 |
VPSLLQ(U8(32), xs[3], xs[3]) |
| 317 |
VPSLLQ(U8(32), xs[5], xs[5]) |
| 318 |
VPSLLQ(U8(32), xs[7], xs[7]) |
| 319 |
|
| 320 |
VPBLENDD(U8(170), xs[1], xs[0], xs[0]) |
| 321 |
VPBLENDD(U8(170), xs[3], xs[2], xs[2]) |
| 322 |
VPBLENDD(U8(170), xs[5], xs[4], xs[4]) |
| 323 |
VPBLENDD(U8(170), xs[7], xs[6], xs[6]) |
| 324 |
|
| 325 |
for i := 0; i < 4; i++ { |
| 326 |
VMOVDQU(xs[2*i], Mem{Base: pPtr, Disp: 8 * 4 * (4*offset + i)}) |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
RET() |
| 331 |
} |
| 332 |
|
| 333 |
// nolint:funlen |
| 334 |
func invNttAVX2() { |
| 335 |
// Just like with the generic implementation, we do the operations of |
| 336 |
// NTT in reverse, except for two things: we hoist out all divisions by |
| 337 |
// two from the Gentleman-Sande butterflies and accumulate them to one |
| 338 |
// big division by 2⁸ at the end. |
| 339 |
TEXT("invNttAVX2", 0, "func(p *[256]uint32)") |
| 340 |
Pragma("noescape") |
| 341 |
pPtr := Load(Param("p"), GP64()) |
| 342 |
zetasPtr := GP64() |
| 343 |
LEAQ(NewDataAddr(Symbol{Name: "·InvZetas"}, 0), zetasPtr) |
| 344 |
|
| 345 |
// We allocate a [256]uint64 on the stack aligned to 32 bytes to hold |
| 346 |
// "buf" which contains intermediate coefficients like "p" in the generic |
| 347 |
// algorithm, but then in uint64s instead of uint32s. |
| 348 |
bufPtr := GP64() |
| 349 |
LEAQ(AllocLocal(256*8+32), bufPtr) // +32 to be able to align |
| 350 |
andImm64(0xffffffffffffffe0, bufPtr) |
| 351 |
|
| 352 |
q := YMM() |
| 353 |
broadcastImm32(params.Q, q) |
| 354 |
q256 := YMM() |
| 355 |
broadcastImm32(256*params.Q, q256) |
| 356 |
qinv := YMM() |
| 357 |
broadcastImm32(params.Qinv, qinv) |
| 358 |
|
| 359 |
// Computes 4x4 doubled Gentleman--Sande butterflies (a,b) ↦ (a+b, ζ(a-b)). |
| 360 |
gsButterfly := func(a1, b1, zeta1, a2, b2, zeta2, a3, b3, zeta3, |
| 361 |
a4, b4, zeta4 Op) { |
| 362 |
t := [4]Op{YMM(), YMM(), YMM(), YMM()} |
| 363 |
a := [4]Op{a1, a2, a3, a4} |
| 364 |
b := [4]Op{b1, b2, b3, b4} |
| 365 |
zeta := [4]Op{zeta1, zeta2, zeta3, zeta4} |
| 366 |
|
| 367 |
// XXX be more parallel when we have more registers available, when |
| 368 |
// we don't use the full four registers for zetas. |
| 369 |
for i := 0; i < 4; i++ { |
| 370 |
// Set t = 256Q + a in preparation of subtracting b |
| 371 |
VPADDD(a[i], q256, t[i]) |
| 372 |
|
| 373 |
// Set t = t - b |
| 374 |
VPSUBD(b[i], t[i], t[i]) |
| 375 |
|
| 376 |
// Set a = a + b |
| 377 |
VPADDD(a[i], b[i], a[i]) |
| 378 |
|
| 379 |
// Set b = tζ |
| 380 |
VPMULUDQ(t[i], zeta[i], b[i]) |
| 381 |
} |
| 382 |
|
| 383 |
// Now we reduce b below 2Q with the method of reduceLe2Q(): |
| 384 |
// |
| 385 |
// t := ((b * 4236238847) & 0xffffffff) * uint64(Q) |
| 386 |
// return uint32((b + t) >> 32) |
| 387 |
for i := 0; i < 4; i++ { |
| 388 |
// t = b * 4236238847. |
| 389 |
VPMULUDQ(qinv, b[i], t[i]) |
| 390 |
} |
| 391 |
|
| 392 |
// t = (t & 0xffffffff) * Q. The and is implicit as VPMULUDQ |
| 393 |
// is a parallel 32b x 32b -> 64b multiplication. |
| 394 |
for i := 0; i < 4; i++ { |
| 395 |
VPMULUDQ(q, t[i], t[i]) |
| 396 |
} |
| 397 |
|
| 398 |
// t = b + t |
| 399 |
for i := 0; i < 4; i++ { |
| 400 |
VPADDQ(t[i], b[i], t[i]) |
| 401 |
} |
| 402 |
|
| 403 |
// b = t >> 32 |
| 404 |
for i := 0; i < 4; i++ { |
| 405 |
VPSRLQ(U8(32), t[i], b[i]) |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
zs := [4]Op{YMM(), YMM(), YMM(), YMM()} |
| 410 |
var xs [8]VecVirtual |
| 411 |
for i := 0; i < 8; i++ { |
| 412 |
xs[i] = YMM() |
| 413 |
} |
| 414 |
|
| 415 |
// XXX should we really unroll this loop? |
| 416 |
for offset := 0; offset < 8; offset++ { |
| 417 |
// Load coeffs 0 1 2 3 4 5 6 7 into xs[0], 8 ... 16 into xs[1], etc. |
| 418 |
for i := 0; i < 4; i++ { |
| 419 |
VMOVDQU(Mem{Base: pPtr, Disp: 8 * 4 * (4*offset + i)}, xs[2*i]) |
| 420 |
} |
| 421 |
|
| 422 |
// Move odd coeffs of xs[2*i] into xs[2*i+1] and shift down. Ignoring |
| 423 |
// the odd coefficients, we have 0 2 4 6 in xs[0] and 1 3 4 5 in xs[1]. |
| 424 |
for i := 0; i < 4; i++ { |
| 425 |
VPSRLQ(U8(32), xs[2*i], xs[2*i+1]) |
| 426 |
} |
| 427 |
|
| 428 |
// Eighth level |
| 429 |
for i := 0; i < 4; i++ { |
| 430 |
VPMOVZXDQ(Mem{Base: zetasPtr, Disp: 4 * 4 * (4*offset + i)}, zs[i]) |
| 431 |
} |
| 432 |
|
| 433 |
gsButterfly( |
| 434 |
xs[0], xs[1], zs[0], |
| 435 |
xs[2], xs[3], zs[1], |
| 436 |
xs[4], xs[5], zs[2], |
| 437 |
xs[6], xs[7], zs[3], |
| 438 |
) |
| 439 |
|
| 440 |
// See comments in nttAVX2() above about oddCrossing() and swapInner(). |
| 441 |
oddCrossing(xs[0], xs[1]) |
| 442 |
oddCrossing(xs[2], xs[3]) |
| 443 |
oddCrossing(xs[4], xs[5]) |
| 444 |
oddCrossing(xs[6], xs[7]) |
| 445 |
|
| 446 |
// Seventh level |
| 447 |
tmp := YMM() |
| 448 |
// XXX optimize? We might want to add a small extra table for just |
| 449 |
// these zetas so that we don't have to blend them. |
| 450 |
for i := 0; i < 4; i++ { |
| 451 |
VPBROADCASTD(Mem{Base: zetasPtr, Disp: (128 + offset*8 + i*2) * 4}, tmp) |
| 452 |
VPBROADCASTD(Mem{Base: zetasPtr, Disp: (128 + offset*8 + i*2 + 1) * 4}, zs[i]) |
| 453 |
VPBLENDD(U8(240), zs[i], tmp, zs[i]) |
| 454 |
} |
| 455 |
|
| 456 |
gsButterfly( |
| 457 |
xs[0], xs[1], zs[0], |
| 458 |
xs[2], xs[3], zs[1], |
| 459 |
xs[4], xs[5], zs[2], |
| 460 |
xs[6], xs[7], zs[3], |
| 461 |
) |
| 462 |
|
| 463 |
// See comments in nttAVX2() above about oddCrossing() and swapInner() |
| 464 |
swapInner(xs[0], xs[1]) |
| 465 |
swapInner(xs[2], xs[3]) |
| 466 |
swapInner(xs[4], xs[5]) |
| 467 |
swapInner(xs[6], xs[7]) |
| 468 |
|
| 469 |
// Sixth level |
| 470 |
for i := 0; i < 4; i++ { |
| 471 |
VPBROADCASTD(Mem{Base: zetasPtr, Disp: (192 + offset*4 + i) * 4}, zs[i]) |
| 472 |
} |
| 473 |
|
| 474 |
gsButterfly( |
| 475 |
xs[0], xs[1], zs[0], |
| 476 |
xs[2], xs[3], zs[1], |
| 477 |
xs[4], xs[5], zs[2], |
| 478 |
xs[6], xs[7], zs[3], |
| 479 |
) |
| 480 |
|
| 481 |
// Fifth level |
| 482 |
VPBROADCASTD(Mem{Base: zetasPtr, Disp: (224 + offset*2) * 4}, zs[0]) |
| 483 |
VPBROADCASTD(Mem{Base: zetasPtr, Disp: (224 + offset*2 + 1) * 4}, zs[1]) |
| 484 |
|
| 485 |
gsButterfly( |
| 486 |
xs[0], xs[2], zs[0], |
| 487 |
xs[1], xs[3], zs[0], |
| 488 |
xs[4], xs[6], zs[1], |
| 489 |
xs[5], xs[7], zs[1], |
| 490 |
) |
| 491 |
|
| 492 |
// Fourth level |
| 493 |
VPBROADCASTD(Mem{Base: zetasPtr, Disp: (240 + offset) * 4}, zs[0]) |
| 494 |
|
| 495 |
gsButterfly( |
| 496 |
xs[0], xs[4], zs[0], |
| 497 |
xs[1], xs[5], zs[0], |
| 498 |
xs[2], xs[6], zs[0], |
| 499 |
xs[3], xs[7], zs[0], |
| 500 |
) |
| 501 |
|
| 502 |
for i := 0; i < 8; i++ { |
| 503 |
VMOVDQA(xs[i], Mem{Base: bufPtr, Disp: 8 * 4 * (8*offset + i)}) |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
// XXX should we really unroll this loop? |
| 508 |
for offset := 0; offset < 8; offset++ { |
| 509 |
for i := 0; i < 8; i++ { |
| 510 |
VMOVDQA(Mem{Base: bufPtr, Disp: 8 * (32*i + 4*offset)}, xs[i]) |
| 511 |
} |
| 512 |
|
| 513 |
// Third level |
| 514 |
VPBROADCASTD(Mem{Base: zetasPtr, Disp: (248 * 4)}, zs[0]) |
| 515 |
VPBROADCASTD(Mem{Base: zetasPtr, Disp: (249 * 4)}, zs[1]) |
| 516 |
VPBROADCASTD(Mem{Base: zetasPtr, Disp: (250 * 4)}, zs[2]) |
| 517 |
VPBROADCASTD(Mem{Base: zetasPtr, Disp: (251 * 4)}, zs[3]) |
| 518 |
|
| 519 |
gsButterfly( |
| 520 |
xs[0], xs[1], zs[0], |
| 521 |
xs[2], xs[3], zs[1], |
| 522 |
xs[4], xs[5], zs[2], |
| 523 |
xs[6], xs[7], zs[3], |
| 524 |
) |
| 525 |
|
| 526 |
// Second level |
| 527 |
VPBROADCASTD(Mem{Base: zetasPtr, Disp: (252 * 4)}, zs[0]) |
| 528 |
VPBROADCASTD(Mem{Base: zetasPtr, Disp: (253 * 4)}, zs[1]) |
| 529 |
|
| 530 |
gsButterfly( |
| 531 |
xs[0], xs[2], zs[0], |
| 532 |
xs[1], xs[3], zs[0], |
| 533 |
xs[4], xs[6], zs[1], |
| 534 |
xs[5], xs[7], zs[1], |
| 535 |
) |
| 536 |
|
| 537 |
// First level |
| 538 |
VPBROADCASTD(Mem{Base: zetasPtr, Disp: (254 * 4)}, zs[0]) |
| 539 |
|
| 540 |
gsButterfly( |
| 541 |
xs[0], xs[4], zs[0], |
| 542 |
xs[1], xs[5], zs[0], |
| 543 |
xs[2], xs[6], zs[0], |
| 544 |
xs[3], xs[7], zs[0], |
| 545 |
) |
| 546 |
|
| 547 |
// Finally, we multiply by 41978 = (256)^-1 R² ... |
| 548 |
rOver256 := YMM() |
| 549 |
broadcastImm32(params.ROver256, rOver256) |
| 550 |
|
| 551 |
for i := 0; i < 8; i++ { |
| 552 |
VPMULUDQ(xs[i], rOver256, xs[i]) |
| 553 |
} |
| 554 |
|
| 555 |
var t [8]VecVirtual |
| 556 |
// (we need this loop, otherwise we run out of YMM registers.) |
| 557 |
for j := 0; j <= 4; j += 4 { |
| 558 |
// ... and reduce below 2Q with the method of reduceLe2Q(): |
| 559 |
// |
| 560 |
// t := ((x * 4236238847) & 0xffffffff) * uint64(Q) |
| 561 |
// return uint32((x + t) >> 32) |
| 562 |
for i := j; i < 4+j; i++ { |
| 563 |
t[i] = YMM() |
| 564 |
// t = x * 4236238847. |
| 565 |
VPMULUDQ(qinv, xs[i], t[i]) |
| 566 |
} |
| 567 |
|
| 568 |
// t = (t & 0xffffffff) * Q. The and is implicit as VPMULUDQ |
| 569 |
// is a parallel 32b x 32b -> 64b multiplication. |
| 570 |
for i := j; i < 4+j; i++ { |
| 571 |
VPMULUDQ(q, t[i], t[i]) |
| 572 |
} |
| 573 |
|
| 574 |
// t = x + t |
| 575 |
for i := j; i < 4+j; i++ { |
| 576 |
VPADDQ(t[i], xs[i], t[i]) |
| 577 |
} |
| 578 |
|
| 579 |
// x = t >> 32 |
| 580 |
for i := j; i < 4+j; i++ { |
| 581 |
VPSRLQ(U8(32), t[i], xs[i]) |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
for i := 0; i < 8; i++ { |
| 586 |
VMOVDQA(xs[i], Mem{Base: bufPtr, Disp: 8 * (32*i + 4*offset)}) |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
// Finally, we copy the 32b results from the [256]uint64 buf to |
| 591 |
// the [256]uint32 p. |
| 592 |
// XXX is this the most efficient way? |
| 593 |
for j := 0; j < 8; j++ { |
| 594 |
for i := 0; i < 8; i++ { |
| 595 |
VMOVDQA(Mem{Base: bufPtr, Disp: 32 * (8*j + i)}, xs[i]) |
| 596 |
} |
| 597 |
// Recall that oddCrossing after swapInner will permute the |
| 598 |
// even coefficients from 0 1 2 3 4 5 6 7 to 0 2 4 6 1 3 5 7 and so |
| 599 |
// then we can simply shift and blend the last four into the first four |
| 600 |
// as we did at the end of nttAVX2(). |
| 601 |
for i := 0; i < 4; i++ { |
| 602 |
swapInner(xs[2*i], xs[2*i+1]) |
| 603 |
} |
| 604 |
for i := 0; i < 4; i++ { |
| 605 |
oddCrossing(xs[2*i], xs[2*i+1]) |
| 606 |
} |
| 607 |
for i := 0; i < 4; i++ { |
| 608 |
VPSLLQ(U8(32), xs[2*i+1], xs[2*i+1]) |
| 609 |
} |
| 610 |
for i := 0; i < 4; i++ { |
| 611 |
VPBLENDD(U8(170), xs[2*i+1], xs[2*i], xs[2*i]) |
| 612 |
} |
| 613 |
for i := 0; i < 4; i++ { |
| 614 |
VMOVDQU(xs[2*i], Mem{Base: pPtr, Disp: 32 * (4*j + i)}) |
| 615 |
} |
| 616 |
} |
| 617 |
|
| 618 |
RET() |
| 619 |
} |
| 620 |
|
| 621 |
// XXX Split out into separate file. To do this we need to figure out how |
| 622 |
// to share code properly between avo modules. |
| 623 |
func mulHatAVX2() { |
| 624 |
TEXT("mulHatAVX2", NOSPLIT, "func(p, a, b *[256]uint32)") |
| 625 |
Pragma("noescape") |
| 626 |
pPtr := Load(Param("p"), GP64()) |
| 627 |
aPtr := Load(Param("a"), GP64()) |
| 628 |
bPtr := Load(Param("b"), GP64()) |
| 629 |
|
| 630 |
q := YMM() |
| 631 |
broadcastImm32(params.Q, q) |
| 632 |
qinv := YMM() |
| 633 |
broadcastImm32(params.Qinv, qinv) |
| 634 |
|
| 635 |
var a [4]VecVirtual |
| 636 |
var b [4]VecVirtual |
| 637 |
for i := 0; i < 4; i++ { |
| 638 |
a[i] = YMM() |
| 639 |
b[i] = YMM() |
| 640 |
} |
| 641 |
|
| 642 |
// XXX Is this loop unrolling worthwhile? |
| 643 |
for j := 0; j < 16; j++ { |
| 644 |
// XXX We could use 6 registers each (instead of 4). Does that make |
| 645 |
// it faster? |
| 646 |
for i := 0; i < 4; i++ { |
| 647 |
VPMOVZXDQ(Mem{Base: aPtr, Disp: 16 * (4*j + i)}, a[i]) |
| 648 |
} |
| 649 |
for i := 0; i < 4; i++ { |
| 650 |
VPMOVZXDQ(Mem{Base: bPtr, Disp: 16 * (4*j + i)}, b[i]) |
| 651 |
} |
| 652 |
for i := 0; i < 4; i++ { |
| 653 |
VPMULUDQ(a[i], b[i], b[i]) |
| 654 |
} |
| 655 |
|
| 656 |
// Now we reduce b below 2Q with the method of reduceLe2Q(): |
| 657 |
// |
| 658 |
// a := ((b * 4236238847) & 0xffffffff) * uint64(Q) |
| 659 |
// return uint32((b + a) >> 32) |
| 660 |
for i := 0; i < 4; i++ { |
| 661 |
// a = b * 4236238847. |
| 662 |
VPMULUDQ(qinv, b[i], a[i]) |
| 663 |
} |
| 664 |
|
| 665 |
// t = (t & 0xffffffff) * Q. The and is implicit as VPMULUDQ |
| 666 |
// is a parallel 32b x 32b -> 64b multiplication. |
| 667 |
for i := 0; i < 4; i++ { |
| 668 |
VPMULUDQ(q, a[i], a[i]) |
| 669 |
} |
| 670 |
|
| 671 |
// t = b + a |
| 672 |
for i := 0; i < 4; i++ { |
| 673 |
VPADDQ(a[i], b[i], a[i]) |
| 674 |
} |
| 675 |
|
| 676 |
// b = a >> 32 |
| 677 |
for i := 0; i < 4; i++ { |
| 678 |
VPSRLQ(U8(32), a[i], b[i]) |
| 679 |
} |
| 680 |
|
| 681 |
// Pack into p. See end of invNttAvx2() for a description of the method. |
| 682 |
// XXX is there a better way to do this that avoids the PERM |
| 683 |
// in oddCrossing? |
| 684 |
for i := 0; i < 2; i++ { |
| 685 |
swapInner(b[2*i], b[2*i+1]) |
| 686 |
} |
| 687 |
for i := 0; i < 2; i++ { |
| 688 |
oddCrossing(b[2*i], b[2*i+1]) |
| 689 |
} |
| 690 |
for i := 0; i < 2; i++ { |
| 691 |
VPSLLQ(U8(32), b[2*i+1], b[2*i+1]) |
| 692 |
} |
| 693 |
for i := 0; i < 2; i++ { |
| 694 |
VPBLENDD(U8(170), b[2*i+1], b[2*i], b[2*i]) |
| 695 |
} |
| 696 |
for i := 0; i < 2; i++ { |
| 697 |
VMOVDQU(b[2*i], Mem{Base: pPtr, Disp: 32 * (2*j + i)}) |
| 698 |
} |
| 699 |
} |
| 700 |
|
| 701 |
RET() |
| 702 |
} |
| 703 |
|
| 704 |
func addAVX2() { |
| 705 |
TEXT("addAVX2", NOSPLIT, "func(p, a, b *[256]uint32)") |
| 706 |
Pragma("noescape") |
| 707 |
pPtr := Load(Param("p"), GP64()) |
| 708 |
aPtr := Load(Param("a"), GP64()) |
| 709 |
bPtr := Load(Param("b"), GP64()) |
| 710 |
|
| 711 |
var a [8]VecVirtual |
| 712 |
var b [8]VecVirtual |
| 713 |
for i := 0; i < 8; i++ { |
| 714 |
a[i] = YMM() |
| 715 |
b[i] = YMM() |
| 716 |
} |
| 717 |
|
| 718 |
// XXX is unrolling worth it? |
| 719 |
for j := 0; j < 4; j++ { |
| 720 |
for i := 0; i < 8; i++ { |
| 721 |
VMOVDQU(Mem{Base: aPtr, Disp: 32 * (8*j + i)}, a[i]) |
| 722 |
} |
| 723 |
for i := 0; i < 8; i++ { |
| 724 |
VMOVDQU(Mem{Base: bPtr, Disp: 32 * (8*j + i)}, b[i]) |
| 725 |
} |
| 726 |
for i := 0; i < 8; i++ { |
| 727 |
VPADDD(a[i], b[i], b[i]) |
| 728 |
} |
| 729 |
for i := 0; i < 8; i++ { |
| 730 |
VMOVDQU(b[i], Mem{Base: pPtr, Disp: 32 * (8*j + i)}) |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
RET() |
| 735 |
} |
| 736 |
|
| 737 |
func subAVX2() { |
| 738 |
TEXT("subAVX2", NOSPLIT, "func(p, a, b *[256]uint32)") |
| 739 |
Pragma("noescape") |
| 740 |
pPtr := Load(Param("p"), GP64()) |
| 741 |
aPtr := Load(Param("a"), GP64()) |
| 742 |
bPtr := Load(Param("b"), GP64()) |
| 743 |
|
| 744 |
var a [4]VecVirtual |
| 745 |
var b [4]VecVirtual |
| 746 |
for i := 0; i < 4; i++ { |
| 747 |
a[i] = YMM() |
| 748 |
b[i] = YMM() |
| 749 |
} |
| 750 |
|
| 751 |
doubleQ := YMM() |
| 752 |
broadcastImm32(2*params.Q, doubleQ) |
| 753 |
|
| 754 |
// XXX is unrolling worth it? |
| 755 |
for j := 0; j < 8; j++ { |
| 756 |
for i := 0; i < 4; i++ { |
| 757 |
VMOVDQU(Mem{Base: aPtr, Disp: 32 * (4*j + i)}, a[i]) |
| 758 |
} |
| 759 |
for i := 0; i < 4; i++ { |
| 760 |
VMOVDQU(Mem{Base: bPtr, Disp: 32 * (4*j + i)}, b[i]) |
| 761 |
} |
| 762 |
for i := 0; i < 4; i++ { |
| 763 |
VPSUBD(b[i], doubleQ, b[i]) |
| 764 |
} |
| 765 |
for i := 0; i < 4; i++ { |
| 766 |
VPADDD(a[i], b[i], b[i]) |
| 767 |
} |
| 768 |
for i := 0; i < 4; i++ { |
| 769 |
VMOVDQU(b[i], Mem{Base: pPtr, Disp: 32 * (4*j + i)}) |
| 770 |
} |
| 771 |
} |
| 772 |
|
| 773 |
RET() |
| 774 |
} |
| 775 |
|
| 776 |
func packLe16AVX2() { |
| 777 |
TEXT("packLe16AVX2", NOSPLIT, "func(p *[256]uint32, buf *byte)") |
| 778 |
Pragma("noescape") |
| 779 |
pPtr := Load(Param("p"), GP64()) |
| 780 |
bufPtr := Load(Param("buf"), GP64()) |
| 781 |
|
| 782 |
var a [8]VecVirtual |
| 783 |
var b [8]VecVirtual |
| 784 |
for i := 0; i < 8; i++ { |
| 785 |
a[i] = YMM() |
| 786 |
b[i] = YMM() |
| 787 |
} |
| 788 |
|
| 789 |
for j := 0; j < 4; j++ { |
| 790 |
// We load p[0], ..., p[7] into a[0], p[8], ..., p[15] into a[1], etc., |
| 791 |
// so we may consider a as a matrix. We transpose a in the usual way. |
| 792 |
for i := 0; i < 4; i++ { |
| 793 |
VMOVDQU(Mem{Base: pPtr, Disp: 32 * (8*j + 2*i)}, a[2*i]) |
| 794 |
VPUNPCKLDQ(Mem{Base: pPtr, Disp: 32 * (8*j + 2*i + 1)}, |
| 795 |
a[2*i], a[2*i]) |
| 796 |
VMOVDQU(Mem{Base: pPtr, Disp: 32 * (8*j + 2*i)}, a[2*i+1]) |
| 797 |
VPUNPCKHDQ(Mem{Base: pPtr, Disp: 32 * (8*j + 2*i + 1)}, |
| 798 |
a[2*i+1], a[2*i+1]) |
| 799 |
} |
| 800 |
|
| 801 |
VPUNPCKLQDQ(a[2], a[0], b[0]) |
| 802 |
VPUNPCKHQDQ(a[2], a[0], b[1]) |
| 803 |
VPUNPCKLQDQ(a[3], a[1], b[2]) |
| 804 |
VPUNPCKHQDQ(a[3], a[1], b[3]) |
| 805 |
VPUNPCKLQDQ(a[6], a[4], b[4]) |
| 806 |
VPUNPCKHQDQ(a[6], a[4], b[5]) |
| 807 |
VPUNPCKLQDQ(a[7], a[5], b[6]) |
| 808 |
VPUNPCKHQDQ(a[7], a[5], b[7]) |
| 809 |
|
| 810 |
VPERM2I128(U8(32), b[4], b[0], a[0]) |
| 811 |
VPERM2I128(U8(32), b[5], b[1], a[1]) |
| 812 |
VPERM2I128(U8(32), b[6], b[2], a[2]) |
| 813 |
VPERM2I128(U8(32), b[7], b[3], a[3]) |
| 814 |
VPERM2I128(U8(49), b[4], b[0], a[4]) |
| 815 |
VPERM2I128(U8(49), b[5], b[1], a[5]) |
| 816 |
VPERM2I128(U8(49), b[6], b[2], a[6]) |
| 817 |
VPERM2I128(U8(49), b[7], b[3], a[7]) |
| 818 |
|
| 819 |
// a has been transposed, so a[0] contains p[0], p[8], ... and |
| 820 |
// a[1] contains p[1], p[9], ..., etc. We shift a[i] by 4*i to the left |
| 821 |
// and or them together. |
| 822 |
for i := 1; i < 8; i++ { |
| 823 |
VPSLLD(U8(4*i), a[i], a[i]) |
| 824 |
} |
| 825 |
|
| 826 |
VPOR(a[0], a[1], a[1]) |
| 827 |
VPOR(a[2], a[3], a[3]) |
| 828 |
VPOR(a[4], a[5], a[5]) |
| 829 |
VPOR(a[6], a[7], a[7]) |
| 830 |
VPOR(a[1], a[3], a[3]) |
| 831 |
VPOR(a[5], a[7], a[7]) |
| 832 |
VPOR(a[3], a[7], a[7]) |
| 833 |
|
| 834 |
VMOVDQU(a[7], Mem{Base: bufPtr, Disp: 32 * j}) |
| 835 |
} |
| 836 |
|
| 837 |
RET() |
| 838 |
} |
| 839 |
|
| 840 |
func reduceLe2QAVX2() { |
| 841 |
TEXT("reduceLe2QAVX2", NOSPLIT, "func(p *[256]uint32)") |
| 842 |
Pragma("noescape") |
| 843 |
pPtr := Load(Param("p"), GP64()) |
| 844 |
|
| 845 |
var a, b, c [4]VecVirtual |
| 846 |
for i := 0; i < 4; i++ { |
| 847 |
a[i] = YMM() |
| 848 |
b[i] = YMM() |
| 849 |
c[i] = YMM() |
| 850 |
} |
| 851 |
twoToThe23MinusOne := YMM() |
| 852 |
broadcastImm32((1<<23)-1, twoToThe23MinusOne) |
| 853 |
|
| 854 |
// We use the same computation as used in reduceLe2Q() for the separate |
| 855 |
// coefficients. |
| 856 |
for j := 0; j < 8; j++ { |
| 857 |
for i := 0; i < 4; i++ { |
| 858 |
VMOVDQU(Mem{Base: pPtr, Disp: 32 * (4*j + i)}, a[i]) |
| 859 |
} |
| 860 |
|
| 861 |
// b = a >> 23 |
| 862 |
for i := 0; i < 4; i++ { |
| 863 |
VPSRLD(U8(23), a[i], b[i]) |
| 864 |
} |
| 865 |
|
| 866 |
// a = a & 2²³-1 |
| 867 |
for i := 0; i < 4; i++ { |
| 868 |
VPAND(a[i], twoToThe23MinusOne, a[i]) |
| 869 |
} |
| 870 |
|
| 871 |
// c = (b << 13) - b |
| 872 |
for i := 0; i < 4; i++ { |
| 873 |
VPSLLD(U8(13), b[i], c[i]) |
| 874 |
} |
| 875 |
for i := 0; i < 4; i++ { |
| 876 |
VPSUBD(b[i], c[i], c[i]) |
| 877 |
} |
| 878 |
|
| 879 |
// a = a + c |
| 880 |
for i := 0; i < 4; i++ { |
| 881 |
VPADDD(a[i], c[i], a[i]) |
| 882 |
} |
| 883 |
|
| 884 |
// Write back |
| 885 |
for i := 0; i < 4; i++ { |
| 886 |
VMOVDQU(a[i], Mem{Base: pPtr, Disp: 32 * (4*j + i)}) |
| 887 |
} |
| 888 |
} |
| 889 |
|
| 890 |
RET() |
| 891 |
} |
| 892 |
|
| 893 |
func le2qModQAVX2() { |
| 894 |
TEXT("le2qModQAVX2", NOSPLIT, "func(p *[256]uint32)") |
| 895 |
Pragma("noescape") |
| 896 |
pPtr := Load(Param("p"), GP64()) |
| 897 |
|
| 898 |
// We use the same method as le2qModQ(). |
| 899 |
var a, m [4]VecVirtual |
| 900 |
for i := 0; i < 4; i++ { |
| 901 |
a[i] = YMM() |
| 902 |
m[i] = YMM() |
| 903 |
} |
| 904 |
|
| 905 |
q := YMM() |
| 906 |
broadcastImm32(params.Q, q) |
| 907 |
|
| 908 |
for j := 0; j < 8; j++ { |
| 909 |
for i := 0; i < 4; i++ { |
| 910 |
VMOVDQU(Mem{Base: pPtr, Disp: 32 * (4*j + i)}, a[i]) |
| 911 |
} |
| 912 |
|
| 913 |
// a -= Q |
| 914 |
for i := 0; i < 4; i++ { |
| 915 |
VPSUBD(q, a[i], a[i]) |
| 916 |
} |
| 917 |
|
| 918 |
// m = uint32(int32(a) >> 31) |
| 919 |
for i := 0; i < 4; i++ { |
| 920 |
VPSRAD(U8(31), a[i], m[i]) |
| 921 |
} |
| 922 |
|
| 923 |
// m &= q |
| 924 |
for i := 0; i < 4; i++ { |
| 925 |
VPAND(m[i], q, m[i]) |
| 926 |
} |
| 927 |
|
| 928 |
// a += m |
| 929 |
for i := 0; i < 4; i++ { |
| 930 |
VPADDD(a[i], m[i], a[i]) |
| 931 |
} |
| 932 |
|
| 933 |
for i := 0; i < 4; i++ { |
| 934 |
VMOVDQU(a[i], Mem{Base: pPtr, Disp: 32 * (4*j + i)}) |
| 935 |
} |
| 936 |
} |
| 937 |
|
| 938 |
RET() |
| 939 |
} |
| 940 |
|
| 941 |
func exceedsAVX2() { |
| 942 |
TEXT("exceedsAVX2", NOSPLIT, "func(p *[256]uint32, bound uint32) uint8") |
| 943 |
Pragma("noescape") |
| 944 |
pPtr := Load(Param("p"), GP64()) |
| 945 |
bound := Load(Param("bound"), GP32()) |
| 946 |
|
| 947 |
var a, b [4]VecVirtual |
| 948 |
for i := 0; i < 4; i++ { |
| 949 |
a[i] = YMM() |
| 950 |
b[i] = YMM() |
| 951 |
} |
| 952 |
|
| 953 |
boundX4 := XMM() |
| 954 |
boundX8 := YMM() |
| 955 |
VMOVD(bound, boundX4) |
| 956 |
VPBROADCASTD(boundX4, boundX8) |
| 957 |
|
| 958 |
qMinusOneDiv2 := YMM() |
| 959 |
broadcastImm32((params.Q-1)/2, qMinusOneDiv2) |
| 960 |
|
| 961 |
signMaskX8 := YMM() |
| 962 |
broadcastImm32(0x80000000, signMaskX8) |
| 963 |
|
| 964 |
signsMask := GP32() |
| 965 |
MOVL(U32(0x88888888), signsMask) |
| 966 |
|
| 967 |
for j := 0; j < 8; j++ { |
| 968 |
for i := 0; i < 4; i++ { |
| 969 |
VMOVDQU(Mem{Base: pPtr, Disp: 32 * (4*j + i)}, a[i]) |
| 970 |
} |
| 971 |
|
| 972 |
// We use the same method as Poly.exceedsGeneric(). |
| 973 |
|
| 974 |
// a = (Q-1)/2 - a |
| 975 |
for i := 0; i < 4; i++ { |
| 976 |
VPSUBD(a[i], qMinusOneDiv2, a[i]) |
| 977 |
} |
| 978 |
|
| 979 |
// b = a >> 31 |
| 980 |
for i := 0; i < 4; i++ { |
| 981 |
VPSRAD(U8(31), a[i], b[i]) |
| 982 |
} |
| 983 |
|
| 984 |
// a = a ^ b |
| 985 |
for i := 0; i < 4; i++ { |
| 986 |
VPXOR(a[i], b[i], a[i]) |
| 987 |
} |
| 988 |
|
| 989 |
// a = (Q-1)/2 - a |
| 990 |
for i := 0; i < 4; i++ { |
| 991 |
VPSUBD(a[i], qMinusOneDiv2, a[i]) |
| 992 |
} |
| 993 |
|
| 994 |
// Here exceedsGeneric() checks if a ⩾ bound. We'll be more clever. |
| 995 |
// a ⩾ bound iff a - bound ⩾ 0, so set a = a - bound first. |
| 996 |
for i := 0; i < 4; i++ { |
| 997 |
VPSUBD(boundX8, a[i], a[i]) |
| 998 |
} |
| 999 |
|
| 1000 |
// a &= 0x80000000. Leaves the sign. Should be zero. |
| 1001 |
for i := 0; i < 4; i++ { |
| 1002 |
VPAND(a[i], signMaskX8, a[i]) |
| 1003 |
} |
| 1004 |
|
| 1005 |
for i := 0; i < 4; i++ { |
| 1006 |
// Move the high bits, which are all zero except possibly for |
| 1007 |
// the sign bits, into tmp. |
| 1008 |
tmp := GP32() |
| 1009 |
VPMOVMSKB(a[i], tmp) |
| 1010 |
|
| 1011 |
// If one of the sign bits is zero, then one of the as is |
| 1012 |
// positive hence the bound is exceeded. |
| 1013 |
XORL(signsMask, tmp) // 0b10001000100010001000100010001000 |
| 1014 |
TESTL(tmp, tmp) |
| 1015 |
JNZ(LabelRef("exceeded")) |
| 1016 |
} |
| 1017 |
} |
| 1018 |
|
| 1019 |
ret := GP8() |
| 1020 |
XORB(ret, ret) |
| 1021 |
Store(ret, ReturnIndex(0)) |
| 1022 |
RET() |
| 1023 |
|
| 1024 |
Label("exceeded") |
| 1025 |
MOVB(U8(1), ret) |
| 1026 |
Store(ret, ReturnIndex(0)) |
| 1027 |
RET() |
| 1028 |
} |
| 1029 |
|
| 1030 |
func mulBy2toDAVX2() { |
| 1031 |
TEXT("mulBy2toDAVX2", NOSPLIT, "func(p, q *[256]uint32)") |
| 1032 |
Pragma("noescape") |
| 1033 |
pPtr := Load(Param("p"), GP64()) |
| 1034 |
qPtr := Load(Param("q"), GP64()) |
| 1035 |
|
| 1036 |
var x [8]VecVirtual |
| 1037 |
for i := 0; i < 8; i++ { |
| 1038 |
x[i] = YMM() |
| 1039 |
} |
| 1040 |
|
| 1041 |
for j := 0; j < 4; j++ { |
| 1042 |
for i := 0; i < 8; i++ { |
| 1043 |
VMOVDQU(Mem{Base: qPtr, Disp: 32 * (8*j + i)}, x[i]) |
| 1044 |
} |
| 1045 |
for i := 0; i < 8; i++ { |
| 1046 |
VPSLLD(U8(params.D), x[i], x[i]) |
| 1047 |
} |
| 1048 |
for i := 0; i < 8; i++ { |
| 1049 |
VMOVDQU(x[i], Mem{Base: pPtr, Disp: 32 * (8*j + i)}) |
| 1050 |
} |
| 1051 |
} |
| 1052 |
|
| 1053 |
RET() |
| 1054 |
} |
| 1055 |
|
| 1056 |
func main() { |
| 1057 |
ConstraintExpr("amd64,!purego") |
| 1058 |
|
| 1059 |
nttAVX2() |
| 1060 |
invNttAVX2() |
| 1061 |
mulHatAVX2() |
| 1062 |
addAVX2() |
| 1063 |
subAVX2() |
| 1064 |
packLe16AVX2() |
| 1065 |
reduceLe2QAVX2() |
| 1066 |
le2qModQAVX2() |
| 1067 |
exceedsAVX2() |
| 1068 |
mulBy2toDAVX2() |
| 1069 |
|
| 1070 |
Generate() |
| 1071 |
} |