Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/bn/asm/rsaz-2k-avxifma.pl

raw
1 # Copyright 2024-2026 The OpenSSL Project Authors. All Rights Reserved.
2 # Copyright (c) 2024, Intel Corporation. All Rights Reserved.
3 #
4 # Licensed under the Apache License 2.0 (the "License"). You may not use
5 # this file except in compliance with the License. You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
8 #
9 # Written by Zhiguo Zhou <[email protected]> and Wangyang Guo <[email protected]>.
10 # Special thanks to Tomasz Kantecki <[email protected]> for his valuable suggestions.
11 #
12 # October 2024
13 #
14 # Initial release.
15 #
16
17 # $output is the last argument if it looks like a file (it has an extension)
18 # $flavour is the first argument if it doesn't look like a file
19 $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
20 $flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
21
22 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
23 $avxifma=0;
24
25 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
26 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
27 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
28 die "can't locate x86_64-xlate.pl";
29
30 if (`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
31 =~ /GNU assembler version ([0-9]+)\.([0-9]+)/) {
32 my $ver = $1 + $2/100.0; # 3.1->3.01, 3.10->3.10
33 $avxifma = ($ver >= 2.40);
34 }
35
36 if (!$avxifma && `$ENV{CC} -v 2>&1`
37 =~ /\s*((?:clang|LLVM) version|.*based on LLVM) ([0-9]+)\.([0-9]+)\.([0-9]+)?/) {
38 my $ver = $2 + $3/100.0 + $4/10000.0; # 3.1.0->3.01, 3.10.1->3.1001
39 $avxifma = ($ver>=16.0);
40 }
41
42 if ($win64 && ($flavour =~ /nasm/ || $ENV{ASM} =~ /nasm/) &&
43 `nasm -v 2>&1` =~ /NASM version ([0-9]+)\.([0-9]+)(?:\.([0-9]+))?(rc[0-9]+)?/) {
44 my $ver = $1 + $2/100.0 + $3/10000.0; # 3.1.0->3.01, 3.10.1->3.1001
45 $avxifma = ($ver > 2.16) + ($ver == 2.16 && !defined($4));
46 }
47
48 open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\""
49 or die "can't call $xlate: $!";
50 *STDOUT=*OUT;
51
52 if ($avxifma>0) {{{
53 @_6_args_universal_ABI = ("%rdi","%rsi","%rdx","%rcx","%r8","%r9");
54
55 $code.=<<___;
56 .text
57 .extern OPENSSL_ia32cap_P
58 .globl ossl_rsaz_avxifma_eligible
59 .type ossl_rsaz_avxifma_eligible,\@abi-omnipotent
60 .align 32
61 ossl_rsaz_avxifma_eligible:
62 mov OPENSSL_ia32cap_P+20(%rip), %ecx
63 xor %eax,%eax
64 and \$`1<<23`, %ecx # avxifma
65 cmp \$`1<<23`, %ecx
66 cmove %ecx,%eax
67 ret
68 .size ossl_rsaz_avxifma_eligible, .-ossl_rsaz_avxifma_eligible
69 ___
70
71 ###############################################################################
72 # Almost Montgomery Multiplication (AMM) for 20-digit number in radix 2^52.
73 #
74 # AMM is defined as presented in the paper [1].
75 #
76 # The input and output are presented in 2^52 radix domain, i.e.
77 # |res|, |a|, |b|, |m| are arrays of 20 64-bit qwords with 12 high bits zeroed.
78 # |k0| is a Montgomery coefficient, which is here k0 = -1/m mod 2^64
79 #
80 # NB: the AMM implementation does not perform "conditional" subtraction step
81 # specified in the original algorithm as according to the Lemma 1 from the paper
82 # [2], the result will be always < 2*m and can be used as a direct input to
83 # the next AMM iteration. This post-condition is true, provided the correct
84 # parameter |s| (notion of the Lemma 1 from [2]) is chosen, i.e. s >= n + 2 * k,
85 # which matches our case: 1040 > 1024 + 2 * 1.
86 #
87 # [1] Gueron, S. Efficient software implementations of modular exponentiation.
88 # DOI: 10.1007/s13389-012-0031-5
89 # [2] Gueron, S. Enhanced Montgomery Multiplication.
90 # DOI: 10.1007/3-540-36400-5_5
91 #
92 # void ossl_rsaz_amm52x20_x1_avxifma256(BN_ULONG *res,
93 # const BN_ULONG *a,
94 # const BN_ULONG *b,
95 # const BN_ULONG *m,
96 # BN_ULONG k0);
97 ###############################################################################
98 {
99 # input parameters ("%rdi","%rsi","%rdx","%rcx","%r8")
100 my ($res,$a,$b,$m,$k0) = @_6_args_universal_ABI;
101
102 my $mask52 = "%rax";
103 my $acc0_0 = "%r9";
104 my $acc0_0_low = "%r9d";
105 my $acc0_1 = "%r15";
106 my $acc0_1_low = "%r15d";
107 my $b_ptr = "%r11";
108
109 my $iter = "%ebx";
110
111 my $zero = "%ymm0";
112 my $Bi = "%ymm1";
113 my $Yi = "%ymm2";
114 my $Yi_xmm = "%xmm2";
115 my ($R0_0,$R0_0h,$R1_0,$R1_0h,$R2_0) = ("%ymm3",map("%ymm$_",(5..8)));
116 my ($R0_1,$R0_1h,$R1_1,$R1_1h,$R2_1) = ("%ymm4",map("%ymm$_",(9..12)));
117
118 # Registers mapping for normalization.
119 my ($T0,$T0h,$T1,$T1h,$T2,$tmp) = ("$zero", "$Bi", "$Yi", map("%ymm$_", (13..15)));
120 my $T0_xmm = "%xmm0";
121
122 sub amm52x20_x1() {
123 # _data_offset - offset in the |a| or |m| arrays pointing to the beginning
124 # of data for corresponding AMM operation;
125 # _b_offset - offset in the |b| array pointing to the next qword digit;
126 my ($_data_offset,$_b_offset,$_acc,$_R0,$_R0h,$_R1,$_R1h,$_R2,$_k0) = @_;
127 $code.=<<___;
128 movq $_b_offset($b_ptr), %r13 # b[i]
129
130 vpbroadcastq $_b_offset($b_ptr), $Bi # broadcast b[i]
131 movq $_data_offset($a), %rdx
132 mulx %r13, %r13, %r12 # a[0]*b[i] = (t0,t2)
133 addq %r13, $_acc # acc += t0
134 movq %r12, %r10
135 adcq \$0, %r10 # t2 += CF
136
137 movq $_k0, %r13
138 imulq $_acc, %r13 # acc * k0
139 andq $mask52, %r13 # yi = (acc * k0) & mask52
140
141 vmovq %r13, $Yi_xmm
142 vpbroadcastq $Yi_xmm, $Yi # broadcast y[i]
143 movq $_data_offset($m), %rdx
144 mulx %r13, %r13, %r12 # yi * m[0] = (t0,t1)
145 addq %r13, $_acc # acc += t0
146 adcq %r12, %r10 # t2 += (t1 + CF)
147
148 shrq \$52, $_acc
149 salq \$12, %r10
150 or %r10, $_acc # acc = ((acc >> 52) | (t2 << 12))
151
152 lea -168(%rsp), %rsp
153 {vex} vpmadd52luq `$_data_offset`($a), $Bi, $_R0
154 {vex} vpmadd52luq `$_data_offset+32`($a), $Bi, $_R0h
155 {vex} vpmadd52luq `$_data_offset+64*1`($a), $Bi, $_R1
156 {vex} vpmadd52luq `$_data_offset+64*1+32`($a), $Bi, $_R1h
157 {vex} vpmadd52luq `$_data_offset+64*2`($a), $Bi, $_R2
158
159 {vex} vpmadd52luq `$_data_offset`($m), $Yi, $_R0
160 {vex} vpmadd52luq `$_data_offset+32`($m), $Yi, $_R0h
161 {vex} vpmadd52luq `$_data_offset+64*1`($m), $Yi, $_R1
162 {vex} vpmadd52luq `$_data_offset+64*1+32`($m), $Yi, $_R1h
163 {vex} vpmadd52luq `$_data_offset+64*2`($m), $Yi, $_R2
164
165 # Shift accumulators right by 1 qword, zero extending the highest one
166 vmovdqu $_R0, `32*0`(%rsp)
167 vmovdqu $_R0h, `32*1`(%rsp)
168 vmovdqu $_R1, `32*2`(%rsp)
169 vmovdqu $_R1h, `32*3`(%rsp)
170 vmovdqu $_R2, `32*4`(%rsp)
171 movq \$0, `32*5`(%rsp)
172
173 vmovdqu `32*0 + 8`(%rsp), $_R0
174 vmovdqu `32*1 + 8`(%rsp), $_R0h
175 vmovdqu `32*2 + 8`(%rsp), $_R1
176 vmovdqu `32*3 + 8`(%rsp), $_R1h
177 vmovdqu `32*4 + 8`(%rsp), $_R2
178
179 addq 8(%rsp), $_acc # acc += R0[0]
180
181 {vex} vpmadd52huq `$_data_offset`($a), $Bi, $_R0
182 {vex} vpmadd52huq `$_data_offset+32`($a), $Bi, $_R0h
183 {vex} vpmadd52huq `$_data_offset+64*1`($a), $Bi, $_R1
184 {vex} vpmadd52huq `$_data_offset+64*1+32`($a), $Bi, $_R1h
185 {vex} vpmadd52huq `$_data_offset+64*2`($a), $Bi, $_R2
186
187 {vex} vpmadd52huq `$_data_offset`($m), $Yi, $_R0
188 {vex} vpmadd52huq `$_data_offset+32`($m), $Yi, $_R0h
189 {vex} vpmadd52huq `$_data_offset+64*1`($m), $Yi, $_R1
190 {vex} vpmadd52huq `$_data_offset+64*1+32`($m), $Yi, $_R1h
191 {vex} vpmadd52huq `$_data_offset+64*2`($m), $Yi, $_R2
192 lea 168(%rsp),%rsp
193 ___
194 }
195
196 # Normalization routine: handles carry bits and gets bignum qwords to normalized
197 # 2^52 representation.
198 #
199 # Uses %r8-14,%e[bcd]x
200 sub amm52x20_x1_norm {
201 my ($_acc,$_R0,$_R0h,$_R1,$_R1h,$_R2) = @_;
202 $code.=<<___;
203 # Put accumulator to low qword in R0
204 vmovq $_acc, $T0_xmm
205 vpbroadcastq $T0_xmm, $T0
206 vpblendd \$3, $T0, $_R0, $_R0
207
208 # Extract "carries" (12 high bits) from each QW of R0..R2
209 # Save them to LSB of QWs in T0..T2
210 vpsrlq \$52, $_R0, $T0
211 vpsrlq \$52, $_R0h, $T0h
212 vpsrlq \$52, $_R1, $T1
213 vpsrlq \$52, $_R1h, $T1h
214 vpsrlq \$52, $_R2, $T2
215
216 # "Shift left" T0..T2 by 1 QW
217 vpermq \$144, $T2, $T2
218 vpermq \$3, $T1h, $tmp
219 vblendpd \$1, $tmp, $T2, $T2
220
221 vpermq \$144, $T1h, $T1h
222 vpermq \$3, $T1, $tmp
223 vblendpd \$1, $tmp, $T1h, $T1h
224
225 vpermq \$144, $T1, $T1
226 vpermq \$3, $T0h, $tmp
227 vblendpd \$1, $tmp, $T1, $T1
228
229 vpermq \$144, $T0h, $T0h
230 vpermq \$3, $T0, $tmp
231 vblendpd \$1, $tmp, $T0h, $T0h
232
233 vpermq \$144, $T0, $T0
234 vpand .Lhigh64x3(%rip), $T0, $T0
235
236 # Drop "carries" from R0..R2 QWs
237 vpand .Lmask52x4(%rip), $_R0, $_R0
238 vpand .Lmask52x4(%rip), $_R0h, $_R0h
239 vpand .Lmask52x4(%rip), $_R1, $_R1
240 vpand .Lmask52x4(%rip), $_R1h, $_R1h
241 vpand .Lmask52x4(%rip), $_R2, $_R2
242
243 # Sum R0..R2 with corresponding adjusted carries
244 vpaddq $T0, $_R0, $_R0
245 vpaddq $T0h, $_R0h, $_R0h
246 vpaddq $T1, $_R1, $_R1
247 vpaddq $T1h, $_R1h, $_R1h
248 vpaddq $T2, $_R2, $_R2
249
250 # Now handle carry bits from this addition
251 # Get mask of QWs which 52-bit parts overflow...
252 vpcmpgtq .Lmask52x4(%rip), $_R0, $T0
253 vpcmpgtq .Lmask52x4(%rip), $_R0h, $T0h
254 vpcmpgtq .Lmask52x4(%rip), $_R1, $T1
255 vpcmpgtq .Lmask52x4(%rip), $_R1h, $T1h
256 vpcmpgtq .Lmask52x4(%rip), $_R2, $T2
257 vmovmskpd $T0, %r14d
258 vmovmskpd $T0h, %r13d
259 vmovmskpd $T1, %r12d
260 vmovmskpd $T1h, %r11d
261 vmovmskpd $T2, %r10d
262
263 # ...or saturated
264 vpcmpeqq .Lmask52x4(%rip), $_R0, $T0
265 vpcmpeqq .Lmask52x4(%rip), $_R0h, $T0h
266 vpcmpeqq .Lmask52x4(%rip), $_R1, $T1
267 vpcmpeqq .Lmask52x4(%rip), $_R1h, $T1h
268 vpcmpeqq .Lmask52x4(%rip), $_R2, $T2
269 vmovmskpd $T0, %r9d
270 vmovmskpd $T0h, %r8d
271 vmovmskpd $T1, %ebx
272 vmovmskpd $T1h, %ecx
273 vmovmskpd $T2, %edx
274
275 # Get mask of QWs where carries shall be propagated to.
276 # Merge 4-bit masks to 8-bit values to use add with carry.
277 shl \$4, %r13b
278 or %r13b, %r14b
279 shl \$4, %r11b
280 or %r11b, %r12b
281
282 add %r14b, %r14b
283 adc %r12b, %r12b
284 adc %r10b, %r10b
285
286 shl \$4, %r8b
287 or %r8b,%r9b
288 shl \$4, %cl
289 or %cl, %bl
290
291 add %r9b, %r14b
292 adc %bl, %r12b
293 adc %dl, %r10b
294
295 xor %r9b, %r14b
296 xor %bl, %r12b
297 xor %dl, %r10b
298
299 lea .Lkmasklut(%rip), %rdx
300
301 mov %r14b, %r13b
302 and \$0xf, %r14
303 vpsubq .Lmask52x4(%rip), $_R0, $T0
304 shl \$5, %r14
305 vmovapd (%rdx,%r14), $T1
306 vblendvpd $T1, $T0, $_R0, $_R0
307
308 shr \$4, %r13b
309 and \$0xf, %r13
310 vpsubq .Lmask52x4(%rip), $_R0h, $T0
311 shl \$5, %r13
312 vmovapd (%rdx,%r13), $T1
313 vblendvpd $T1, $T0, $_R0h, $_R0h
314
315 mov %r12b, %r11b
316 and \$0xf, %r12
317 vpsubq .Lmask52x4(%rip), $_R1, $T0
318 shl \$5, %r12
319 vmovapd (%rdx,%r12), $T1
320 vblendvpd $T1, $T0, $_R1, $_R1
321
322 shr \$4, %r11b
323 and \$0xf, %r11
324 vpsubq .Lmask52x4(%rip), $_R1h, $T0
325 shl \$5, %r11
326 vmovapd (%rdx,%r11), $T1
327 vblendvpd $T1, $T0, $_R1h, $_R1h
328
329 and \$0xf, %r10
330 vpsubq .Lmask52x4(%rip), $_R2, $T0
331 shl \$5, %r10
332 vmovapd (%rdx,%r10), $T1
333 vblendvpd $T1, $T0, $_R2, $_R2
334
335 # Add carries according to the obtained mask
336 vpand .Lmask52x4(%rip), $_R0, $_R0
337 vpand .Lmask52x4(%rip), $_R0h, $_R0h
338 vpand .Lmask52x4(%rip), $_R1, $_R1
339 vpand .Lmask52x4(%rip), $_R1h, $_R1h
340 vpand .Lmask52x4(%rip), $_R2, $_R2
341 ___
342 }
343
344 $code.=<<___;
345 .text
346
347 .globl ossl_rsaz_amm52x20_x1_avxifma256
348 .type ossl_rsaz_amm52x20_x1_avxifma256,\@function,5
349 .align 32
350 ossl_rsaz_amm52x20_x1_avxifma256:
351 .cfi_startproc
352 endbranch
353 push %rbx
354 .cfi_push %rbx
355 push %rbp
356 .cfi_push %rbp
357 push %r12
358 .cfi_push %r12
359 push %r13
360 .cfi_push %r13
361 push %r14
362 .cfi_push %r14
363 push %r15
364 .cfi_push %r15
365 ___
366 $code.=<<___ if ($win64);
367 push %rsi # save non-volatile registers
368 push %rdi
369 lea -168(%rsp), %rsp # 16*10 + (8 bytes to get correct 16-byte SIMD alignment)
370 vmovapd %xmm6, `16*0`(%rsp)
371 vmovapd %xmm7, `16*1`(%rsp)
372 vmovapd %xmm8, `16*2`(%rsp)
373 vmovapd %xmm9, `16*3`(%rsp)
374 vmovapd %xmm10, `16*4`(%rsp)
375 vmovapd %xmm11, `16*5`(%rsp)
376 vmovapd %xmm12, `16*6`(%rsp)
377 vmovapd %xmm13, `16*7`(%rsp)
378 vmovapd %xmm14, `16*8`(%rsp)
379 vmovapd %xmm15, `16*9`(%rsp)
380 ___
381 $code.=<<___;
382 .Lossl_rsaz_amm52x20_x1_avxifma256_body:
383
384 # Zeroing accumulators
385 vpxor $zero, $zero, $zero
386 vmovapd $zero, $R0_0
387 vmovapd $zero, $R0_0h
388 vmovapd $zero, $R1_0
389 vmovapd $zero, $R1_0h
390 vmovapd $zero, $R2_0
391
392 xorl $acc0_0_low, $acc0_0_low
393
394 movq $b, $b_ptr # backup address of b
395 movq \$0xfffffffffffff, $mask52 # 52-bit mask
396
397 # Loop over 20 digits unrolled by 4
398 mov \$5, $iter
399
400 .align 32
401 .Lloop5:
402 ___
403 foreach my $idx (0..3) {
404 &amm52x20_x1(0,8*$idx,$acc0_0,$R0_0,$R0_0h,$R1_0,$R1_0h,$R2_0,$k0);
405 }
406 $code.=<<___;
407 lea `4*8`($b_ptr), $b_ptr
408 dec $iter
409 jne .Lloop5
410 ___
411 &amm52x20_x1_norm($acc0_0,$R0_0,$R0_0h,$R1_0,$R1_0h,$R2_0);
412 $code.=<<___;
413
414 vmovdqu $R0_0, `0*32`($res)
415 vmovdqu $R0_0h, `1*32`($res)
416 vmovdqu $R1_0, `2*32`($res)
417 vmovdqu $R1_0h, `3*32`($res)
418 vmovdqu $R2_0, `4*32`($res)
419
420 vzeroupper
421 ___
422 $code.=<<___ if ($win64);
423 vmovapd `16*0`(%rsp), %xmm6
424 vmovapd `16*1`(%rsp), %xmm7
425 vmovapd `16*2`(%rsp), %xmm8
426 vmovapd `16*3`(%rsp), %xmm9
427 vmovapd `16*4`(%rsp), %xmm10
428 vmovapd `16*5`(%rsp), %xmm11
429 vmovapd `16*6`(%rsp), %xmm12
430 vmovapd `16*7`(%rsp), %xmm13
431 vmovapd `16*8`(%rsp), %xmm14
432 vmovapd `16*9`(%rsp), %xmm15
433 lea 168(%rsp), %rsp
434 pop %rdi
435 pop %rsi
436 ___
437 $code.=<<___;
438 mov 0(%rsp),%r15
439 .cfi_restore %r15
440 mov 8(%rsp),%r14
441 .cfi_restore %r14
442 mov 16(%rsp),%r13
443 .cfi_restore %r13
444 mov 24(%rsp),%r12
445 .cfi_restore %r12
446 mov 32(%rsp),%rbp
447 .cfi_restore %rbp
448 mov 40(%rsp),%rbx
449 .cfi_restore %rbx
450 lea 48(%rsp),%rsp
451 .cfi_adjust_cfa_offset -48
452 .Lossl_rsaz_amm52x20_x1_avxifma256_epilogue:
453 ret
454 .cfi_endproc
455 .size ossl_rsaz_amm52x20_x1_avxifma256, .-ossl_rsaz_amm52x20_x1_avxifma256
456 ___
457
458 $code.=<<___;
459 .section .rodata align=32
460 .align 32
461 .Lmask52x4:
462 .quad 0xfffffffffffff
463 .quad 0xfffffffffffff
464 .quad 0xfffffffffffff
465 .quad 0xfffffffffffff
466 .Lhigh64x3:
467 .quad 0x0
468 .quad 0xffffffffffffffff
469 .quad 0xffffffffffffffff
470 .quad 0xffffffffffffffff
471 .Lkmasklut:
472 #0000
473 .quad 0x0
474 .quad 0x0
475 .quad 0x0
476 .quad 0x0
477 #0001
478 .quad 0xffffffffffffffff
479 .quad 0x0
480 .quad 0x0
481 .quad 0x0
482 #0010
483 .quad 0x0
484 .quad 0xffffffffffffffff
485 .quad 0x0
486 .quad 0x0
487 #0011
488 .quad 0xffffffffffffffff
489 .quad 0xffffffffffffffff
490 .quad 0x0
491 .quad 0x0
492 #0100
493 .quad 0x0
494 .quad 0x0
495 .quad 0xffffffffffffffff
496 .quad 0x0
497 #0101
498 .quad 0xffffffffffffffff
499 .quad 0x0
500 .quad 0xffffffffffffffff
501 .quad 0x0
502 #0110
503 .quad 0x0
504 .quad 0xffffffffffffffff
505 .quad 0xffffffffffffffff
506 .quad 0x0
507 #0111
508 .quad 0xffffffffffffffff
509 .quad 0xffffffffffffffff
510 .quad 0xffffffffffffffff
511 .quad 0x0
512 #1000
513 .quad 0x0
514 .quad 0x0
515 .quad 0x0
516 .quad 0xffffffffffffffff
517 #1001
518 .quad 0xffffffffffffffff
519 .quad 0x0
520 .quad 0x0
521 .quad 0xffffffffffffffff
522 #1010
523 .quad 0x0
524 .quad 0xffffffffffffffff
525 .quad 0x0
526 .quad 0xffffffffffffffff
527 #1011
528 .quad 0xffffffffffffffff
529 .quad 0xffffffffffffffff
530 .quad 0x0
531 .quad 0xffffffffffffffff
532 #1100
533 .quad 0x0
534 .quad 0x0
535 .quad 0xffffffffffffffff
536 .quad 0xffffffffffffffff
537 #1101
538 .quad 0xffffffffffffffff
539 .quad 0x0
540 .quad 0xffffffffffffffff
541 .quad 0xffffffffffffffff
542 #1110
543 .quad 0x0
544 .quad 0xffffffffffffffff
545 .quad 0xffffffffffffffff
546 .quad 0xffffffffffffffff
547 #1111
548 .quad 0xffffffffffffffff
549 .quad 0xffffffffffffffff
550 .quad 0xffffffffffffffff
551 .quad 0xffffffffffffffff
552 ___
553
554 ###############################################################################
555 # Dual Almost Montgomery Multiplication for 20-digit number in radix 2^52
556 #
557 # See description of ossl_rsaz_amm52x20_x1_ifma256() above for details about Almost
558 # Montgomery Multiplication algorithm and function input parameters description.
559 #
560 # This function does two AMMs for two independent inputs, hence dual.
561 #
562 # void ossl_rsaz_amm52x20_x2_avxifma256(BN_ULONG out[2][20],
563 # const BN_ULONG a[2][20],
564 # const BN_ULONG b[2][20],
565 # const BN_ULONG m[2][20],
566 # const BN_ULONG k0[2]);
567 ###############################################################################
568
569 $code.=<<___;
570 .text
571
572 .globl ossl_rsaz_amm52x20_x2_avxifma256
573 .type ossl_rsaz_amm52x20_x2_avxifma256,\@function,5
574 .align 32
575 ossl_rsaz_amm52x20_x2_avxifma256:
576 .cfi_startproc
577 endbranch
578 push %rbx
579 .cfi_push %rbx
580 push %rbp
581 .cfi_push %rbp
582 push %r12
583 .cfi_push %r12
584 push %r13
585 .cfi_push %r13
586 push %r14
587 .cfi_push %r14
588 push %r15
589 .cfi_push %r15
590 ___
591 $code.=<<___ if ($win64);
592 push %rsi # save non-volatile registers
593 push %rdi
594 lea -168(%rsp), %rsp # 16*10 + (8 bytes to get correct 16-byte SIMD alignment)
595 vmovapd %xmm6, `16*0`(%rsp)
596 vmovapd %xmm7, `16*1`(%rsp)
597 vmovapd %xmm8, `16*2`(%rsp)
598 vmovapd %xmm9, `16*3`(%rsp)
599 vmovapd %xmm10, `16*4`(%rsp)
600 vmovapd %xmm11, `16*5`(%rsp)
601 vmovapd %xmm12, `16*6`(%rsp)
602 vmovapd %xmm13, `16*7`(%rsp)
603 vmovapd %xmm14, `16*8`(%rsp)
604 vmovapd %xmm15, `16*9`(%rsp)
605 ___
606 $code.=<<___;
607 .Lossl_rsaz_amm52x20_x2_avxifma256_body:
608
609 # Zeroing accumulators
610 vpxor $zero, $zero, $zero
611 vmovapd $zero, $R0_0
612 vmovapd $zero, $R0_0h
613 vmovapd $zero, $R1_0
614 vmovapd $zero, $R1_0h
615 vmovapd $zero, $R2_0
616 vmovapd $zero, $R0_1
617 vmovapd $zero, $R0_1h
618 vmovapd $zero, $R1_1
619 vmovapd $zero, $R1_1h
620 vmovapd $zero, $R2_1
621
622 xorl $acc0_0_low, $acc0_0_low
623 xorl $acc0_1_low, $acc0_1_low
624
625 movq $b, $b_ptr # backup address of b
626 movq \$0xfffffffffffff, $mask52 # 52-bit mask
627
628 mov \$20, $iter
629
630 .align 32
631 .Lloop20:
632 ___
633 &amm52x20_x1( 0, 0,$acc0_0,$R0_0,$R0_0h,$R1_0,$R1_0h,$R2_0,"($k0)");
634 # 20*8 = offset of the next dimension in two-dimension array
635 &amm52x20_x1(20*8,20*8,$acc0_1,$R0_1,$R0_1h,$R1_1,$R1_1h,$R2_1,"8($k0)");
636 $code.=<<___;
637 lea 8($b_ptr), $b_ptr
638 dec $iter
639 jne .Lloop20
640 ___
641 &amm52x20_x1_norm($acc0_0,$R0_0,$R0_0h,$R1_0,$R1_0h,$R2_0);
642 &amm52x20_x1_norm($acc0_1,$R0_1,$R0_1h,$R1_1,$R1_1h,$R2_1);
643 $code.=<<___;
644
645 vmovdqu $R0_0, `0*32`($res)
646 vmovdqu $R0_0h, `1*32`($res)
647 vmovdqu $R1_0, `2*32`($res)
648 vmovdqu $R1_0h, `3*32`($res)
649 vmovdqu $R2_0, `4*32`($res)
650
651 vmovdqu $R0_1, `5*32`($res)
652 vmovdqu $R0_1h, `6*32`($res)
653 vmovdqu $R1_1, `7*32`($res)
654 vmovdqu $R1_1h, `8*32`($res)
655 vmovdqu $R2_1, `9*32`($res)
656
657 vzeroupper
658 ___
659 $code.=<<___ if ($win64);
660 vmovapd `16*0`(%rsp), %xmm6
661 vmovapd `16*1`(%rsp), %xmm7
662 vmovapd `16*2`(%rsp), %xmm8
663 vmovapd `16*3`(%rsp), %xmm9
664 vmovapd `16*4`(%rsp), %xmm10
665 vmovapd `16*5`(%rsp), %xmm11
666 vmovapd `16*6`(%rsp), %xmm12
667 vmovapd `16*7`(%rsp), %xmm13
668 vmovapd `16*8`(%rsp), %xmm14
669 vmovapd `16*9`(%rsp), %xmm15
670 lea 168(%rsp), %rsp
671 pop %rdi
672 pop %rsi
673 ___
674 $code.=<<___;
675 mov 0(%rsp),%r15
676 .cfi_restore %r15
677 mov 8(%rsp),%r14
678 .cfi_restore %r14
679 mov 16(%rsp),%r13
680 .cfi_restore %r13
681 mov 24(%rsp),%r12
682 .cfi_restore %r12
683 mov 32(%rsp),%rbp
684 .cfi_restore %rbp
685 mov 40(%rsp),%rbx
686 .cfi_restore %rbx
687 lea 48(%rsp),%rsp
688 .cfi_adjust_cfa_offset -48
689 .Lossl_rsaz_amm52x20_x2_avxifma256_epilogue:
690 ret
691 .cfi_endproc
692 .size ossl_rsaz_amm52x20_x2_avxifma256, .-ossl_rsaz_amm52x20_x2_avxifma256
693 ___
694 }
695
696 ###############################################################################
697 # Constant time extraction from the precomputed table of powers base^i, where
698 # i = 0..2^EXP_WIN_SIZE-1
699 #
700 # The input |red_table| contains precomputations for two independent base values.
701 # |red_table_idx1| and |red_table_idx2| are corresponding power indexes.
702 #
703 # Extracted value (output) is 2 20 digit numbers in 2^52 radix.
704 #
705 # void ossl_extract_multiplier_2x20_win5_avx(BN_ULONG *red_Y,
706 # const BN_ULONG red_table[1 << EXP_WIN_SIZE][2][20],
707 # int red_table_idx1, int red_table_idx2);
708 #
709 # EXP_WIN_SIZE = 5
710 ###############################################################################
711 {
712 # input parameters
713 my ($out,$red_tbl,$red_tbl_idx1,$red_tbl_idx2)=$win64 ? ("%rcx","%rdx","%r8", "%r9") : # Win64 order
714 ("%rdi","%rsi","%rdx","%rcx"); # Unix order
715
716 my ($t0,$t1,$t2,$t3,$t4,$t5) = map("%ymm$_", (0..5));
717 my ($t6,$t7,$t8,$t9) = map("%ymm$_", (6..9));
718 my ($tmp,$cur_idx,$idx1,$idx2,$ones,$mask) = map("%ymm$_", (10..15));
719
720 my @t = ($t0,$t1,$t2,$t3,$t4,$t5,$t6,$t7,$t8,$t9);
721 my $t0xmm = $t0;
722 my $tmp_xmm = "%xmm10";
723 $t0xmm =~ s/%y/%x/;
724
725 $code.=<<___;
726 .text
727
728 .align 32
729 .globl ossl_extract_multiplier_2x20_win5_avx
730 .type ossl_extract_multiplier_2x20_win5_avx,\@abi-omnipotent
731 ossl_extract_multiplier_2x20_win5_avx:
732 .cfi_startproc
733 endbranch
734 ___
735 $code.=<<___ if ($win64);
736 push %rsi # save non-volatile registers
737 push %rdi
738 lea -168(%rsp), %rsp # 16*10 + (8 bytes to get correct 16-byte SIMD alignment)
739 vmovapd %xmm6, `16*0`(%rsp)
740 vmovapd %xmm7, `16*1`(%rsp)
741 vmovapd %xmm8, `16*2`(%rsp)
742 vmovapd %xmm9, `16*3`(%rsp)
743 vmovapd %xmm10, `16*4`(%rsp)
744 vmovapd %xmm11, `16*5`(%rsp)
745 vmovapd %xmm12, `16*6`(%rsp)
746 vmovapd %xmm13, `16*7`(%rsp)
747 vmovapd %xmm14, `16*8`(%rsp)
748 vmovapd %xmm15, `16*9`(%rsp)
749 ___
750 $code.=<<___;
751 vmovapd .Lones(%rip), $ones # broadcast ones
752 vmovq $red_tbl_idx1, $tmp_xmm
753 vpbroadcastq $tmp_xmm, $idx1
754 vmovq $red_tbl_idx2, $tmp_xmm
755 vpbroadcastq $tmp_xmm, $idx2
756 leaq `(1<<5)*2*20*8`($red_tbl), %rax # holds end of the tbl
757
758 # zeroing t0..n, cur_idx
759 vpxor $t0xmm, $t0xmm, $t0xmm
760 vmovapd $t0, $cur_idx
761 ___
762 foreach (1..9) {
763 $code.="vmovapd $t0, $t[$_] \n";
764 }
765 $code.=<<___;
766
767 .align 32
768 .Lloop:
769 vpcmpeqq $cur_idx, $idx1, $mask # mask of (idx1 == cur_idx)
770 ___
771 foreach (0..4) {
772 $code.=<<___;
773 vmovdqu `${_}*32`($red_tbl), $tmp # load data from red_tbl
774 vblendvpd $mask, $tmp, $t[$_], ${t[$_]} # extract data when mask is not zero
775 ___
776 }
777 $code.=<<___;
778 vpcmpeqq $cur_idx, $idx2, $mask # mask of (idx2 == cur_idx)
779 ___
780 foreach (5..9) {
781 $code.=<<___;
782 vmovdqu `${_}*32`($red_tbl), $tmp # load data from red_tbl
783 vblendvpd $mask, $tmp, $t[$_], ${t[$_]} # extract data when mask is not zero
784 ___
785 }
786 $code.=<<___;
787 vpaddq $ones, $cur_idx, $cur_idx # increment cur_idx
788 addq \$`2*20*8`, $red_tbl
789 cmpq $red_tbl, %rax
790 jne .Lloop
791 ___
792 # store t0..n
793 foreach (0..9) {
794 $code.="vmovdqu $t[$_], `${_}*32`($out) \n";
795 }
796 $code.=<<___;
797 vzeroupper
798 ___
799 $code.=<<___ if ($win64);
800 vmovapd `16*0`(%rsp), %xmm6
801 vmovapd `16*1`(%rsp), %xmm7
802 vmovapd `16*2`(%rsp), %xmm8
803 vmovapd `16*3`(%rsp), %xmm9
804 vmovapd `16*4`(%rsp), %xmm10
805 vmovapd `16*5`(%rsp), %xmm11
806 vmovapd `16*6`(%rsp), %xmm12
807 vmovapd `16*7`(%rsp), %xmm13
808 vmovapd `16*8`(%rsp), %xmm14
809 vmovapd `16*9`(%rsp), %xmm15
810 lea 168(%rsp), %rsp
811 pop %rdi
812 pop %rsi
813 ___
814 $code.=<<___;
815 ret
816 .cfi_endproc
817 .size ossl_extract_multiplier_2x20_win5_avx, .-ossl_extract_multiplier_2x20_win5_avx
818 ___
819 $code.=<<___;
820 .section .rodata align=32
821 .align 32
822 .Lones:
823 .quad 1,1,1,1
824 .Lzeros:
825 .quad 0,0,0,0
826 ___
827 }
828
829 if ($win64) {
830 $rec="%rcx";
831 $frame="%rdx";
832 $context="%r8";
833 $disp="%r9";
834
835 $code.=<<___;
836 .extern __imp_RtlVirtualUnwind
837 .type rsaz_def_handler,\@abi-omnipotent
838 .align 16
839 rsaz_def_handler:
840 push %rsi
841 push %rdi
842 push %rbx
843 push %rbp
844 push %r12
845 push %r13
846 push %r14
847 push %r15
848 pushfq
849 sub \$64,%rsp
850
851 mov 120($context),%rax # pull context->Rax
852 mov 248($context),%rbx # pull context->Rip
853
854 mov 8($disp),%rsi # disp->ImageBase
855 mov 56($disp),%r11 # disp->HandlerData
856
857 mov 0(%r11),%r10d # HandlerData[0]
858 lea (%rsi,%r10),%r10 # prologue label
859 cmp %r10,%rbx # context->Rip<.Lprologue
860 jb .Lcommon_seh_tail
861
862 mov 152($context),%rax # pull context->Rsp
863
864 mov 4(%r11),%r10d # HandlerData[1]
865 lea (%rsi,%r10),%r10 # epilogue label
866 cmp %r10,%rbx # context->Rip>=.Lepilogue
867 jae .Lcommon_seh_tail
868
869 lea 48(%rax),%rax
870
871 mov -8(%rax),%rbx
872 mov -16(%rax),%rbp
873 mov -24(%rax),%r12
874 mov -32(%rax),%r13
875 mov -40(%rax),%r14
876 mov -48(%rax),%r15
877 mov %rbx,144($context) # restore context->Rbx
878 mov %rbp,160($context) # restore context->Rbp
879 mov %r12,216($context) # restore context->R12
880 mov %r13,224($context) # restore context->R13
881 mov %r14,232($context) # restore context->R14
882 mov %r15,240($context) # restore context->R14
883
884 .Lcommon_seh_tail:
885 mov 8(%rax),%rdi
886 mov 16(%rax),%rsi
887 mov %rax,152($context) # restore context->Rsp
888 mov %rsi,168($context) # restore context->Rsi
889 mov %rdi,176($context) # restore context->Rdi
890
891 mov 40($disp),%rdi # disp->ContextRecord
892 mov $context,%rsi # context
893 mov \$154,%ecx # sizeof(CONTEXT)
894 .long 0xa548f3fc # cld; rep movsq
895
896 mov $disp,%rsi
897 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
898 mov 8(%rsi),%rdx # arg2, disp->ImageBase
899 mov 0(%rsi),%r8 # arg3, disp->ControlPc
900 mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
901 mov 40(%rsi),%r10 # disp->ContextRecord
902 lea 56(%rsi),%r11 # &disp->HandlerData
903 lea 24(%rsi),%r12 # &disp->EstablisherFrame
904 mov %r10,32(%rsp) # arg5
905 mov %r11,40(%rsp) # arg6
906 mov %r12,48(%rsp) # arg7
907 mov %rcx,56(%rsp) # arg8, (NULL)
908 call *__imp_RtlVirtualUnwind(%rip)
909
910 mov \$1,%eax # ExceptionContinueSearch
911 add \$64,%rsp
912 popfq
913 pop %r15
914 pop %r14
915 pop %r13
916 pop %r12
917 pop %rbp
918 pop %rbx
919 pop %rdi
920 pop %rsi
921 ret
922 .size rsaz_def_handler,.-rsaz_def_handler
923
924 .section .pdata
925 .align 4
926 .rva .LSEH_begin_ossl_rsaz_amm52x20_x1_avxifma256
927 .rva .LSEH_end_ossl_rsaz_amm52x20_x1_avxifma256
928 .rva .LSEH_info_ossl_rsaz_amm52x20_x1_avxifma256
929
930 .rva .LSEH_begin_ossl_rsaz_amm52x20_x2_avxifma256
931 .rva .LSEH_end_ossl_rsaz_amm52x20_x2_avxifma256
932 .rva .LSEH_info_ossl_rsaz_amm52x20_x2_avxifma256
933
934 .section .xdata
935 .align 8
936 .LSEH_info_ossl_rsaz_amm52x20_x1_avxifma256:
937 .byte 9,0,0,0
938 .rva rsaz_def_handler
939 .rva .Lossl_rsaz_amm52x20_x1_avxifma256_body,.Lossl_rsaz_amm52x20_x1_avxifma256_epilogue
940 .LSEH_info_ossl_rsaz_amm52x20_x2_avxifma256:
941 .byte 9,0,0,0
942 .rva rsaz_def_handler
943 .rva .Lossl_rsaz_amm52x20_x2_avxifma256_body,.Lossl_rsaz_amm52x20_x2_avxifma256_epilogue
944 ___
945 }
946 }}} else {{{ # fallback for old assembler
947 $code.=<<___;
948 .text
949
950 .globl ossl_rsaz_avxifma_eligible
951 .type ossl_rsaz_avxifma_eligible,\@abi-omnipotent
952 ossl_rsaz_avxifma_eligible:
953 xor %eax,%eax
954 ret
955 .size ossl_rsaz_avxifma_eligible, .-ossl_rsaz_avxifma_eligible
956
957 .globl ossl_rsaz_amm52x20_x1_avxifma256
958 .globl ossl_rsaz_amm52x20_x2_avxifma256
959 .globl ossl_extract_multiplier_2x20_win5_avx
960 .type ossl_rsaz_amm52x20_x1_avxifma256,\@abi-omnipotent
961 ossl_rsaz_amm52x20_x1_avxifma256:
962 ossl_rsaz_amm52x20_x2_avxifma256:
963 ossl_extract_multiplier_2x20_win5_avx:
964 .byte 0x0f,0x0b # ud2
965 ret
966 .size ossl_rsaz_amm52x20_x1_avxifma256, .-ossl_rsaz_amm52x20_x1_avxifma256
967 ___
968 }}}
969
970 $code =~ s/\`([^\`]*)\`/eval $1/gem;
971 print $code;
972 close STDOUT or die "error closing STDOUT: $!";