ec/asm/ecp_nistz256-x86_64.pl: fix typo-bug in Win64 SE handler.
[openssl.git] / crypto / ec / asm / ecp_nistz256-x86_64.pl
1 #! /usr/bin/env perl
2 # Copyright 2014-2016 The OpenSSL Project Authors. All Rights Reserved.
3 #
4 # Licensed under the OpenSSL license (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
10 ##############################################################################
11 #                                                                            #
12 # Copyright 2014 Intel Corporation                                           #
13 #                                                                            #
14 # Licensed under the Apache License, Version 2.0 (the "License");            #
15 # you may not use this file except in compliance with the License.           #
16 # You may obtain a copy of the License at                                    #
17 #                                                                            #
18 #    http://www.apache.org/licenses/LICENSE-2.0                              #
19 #                                                                            #
20 # Unless required by applicable law or agreed to in writing, software        #
21 # distributed under the License is distributed on an "AS IS" BASIS,          #
22 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
23 # See the License for the specific language governing permissions and        #
24 # limitations under the License.                                             #
25 #                                                                            #
26 ##############################################################################
27 #                                                                            #
28 #  Developers and authors:                                                   #
29 #  Shay Gueron (1, 2), and Vlad Krasnov (1)                                  #
30 #  (1) Intel Corporation, Israel Development Center                          #
31 #  (2) University of Haifa                                                   #
32 #  Reference:                                                                #
33 #  S.Gueron and V.Krasnov, "Fast Prime Field Elliptic Curve Cryptography with#
34 #                           256 Bit Primes"                                  #
35 #                                                                            #
36 ##############################################################################
37
38 # Further optimization by <appro@openssl.org>:
39 #
40 #               this/original   with/without -DECP_NISTZ256_ASM(*)
41 # Opteron       +12-49%         +110-150%
42 # Bulldozer     +14-45%         +175-210%
43 # P4            +18-46%         n/a :-(
44 # Westmere      +12-34%         +80-87%
45 # Sandy Bridge  +9-35%          +110-120%
46 # Ivy Bridge    +9-35%          +110-125%
47 # Haswell       +8-37%          +140-160%
48 # Broadwell     +18-58%         +145-210%
49 # Atom          +15-50%         +130-180%
50 # VIA Nano      +43-160%        +300-480%
51 #
52 # (*)   "without -DECP_NISTZ256_ASM" refers to build with
53 #       "enable-ec_nistp_64_gcc_128";
54 #
55 # Ranges denote minimum and maximum improvement coefficients depending
56 # on benchmark. Lower coefficients are for ECDSA sign, relatively fastest
57 # server-side operation. Keep in mind that +100% means 2x improvement.
58
59 $flavour = shift;
60 $output  = shift;
61 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
62
63 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
64
65 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
66 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
67 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
68 die "can't locate x86_64-xlate.pl";
69
70 open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\"";
71 *STDOUT=*OUT;
72
73 if (`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
74                 =~ /GNU assembler version ([2-9]\.[0-9]+)/) {
75         $avx = ($1>=2.19) + ($1>=2.22);
76         $addx = ($1>=2.23);
77 }
78
79 if (!$addx && $win64 && ($flavour =~ /nasm/ || $ENV{ASM} =~ /nasm/) &&
80             `nasm -v 2>&1` =~ /NASM version ([2-9]\.[0-9]+)/) {
81         $avx = ($1>=2.09) + ($1>=2.10);
82         $addx = ($1>=2.10);
83 }
84
85 if (!$addx && $win64 && ($flavour =~ /masm/ || $ENV{ASM} =~ /ml64/) &&
86             `ml64 2>&1` =~ /Version ([0-9]+)\./) {
87         $avx = ($1>=10) + ($1>=11);
88         $addx = ($1>=12);
89 }
90
91 if (!$addx && `$ENV{CC} -v 2>&1` =~ /((?:^clang|LLVM) version|.*based on LLVM) ([3-9])\.([0-9]+)/) {
92         my $ver = $2 + $3/100.0;        # 3.1->3.01, 3.10->3.10
93         $avx = ($ver>=3.0) + ($ver>=3.01);
94         $addx = ($ver>=3.03);
95 }
96
97 $code.=<<___;
98 .text
99 .extern OPENSSL_ia32cap_P
100
101 # The polynomial
102 .align 64
103 .Lpoly:
104 .quad 0xffffffffffffffff, 0x00000000ffffffff, 0x0000000000000000, 0xffffffff00000001
105
106 # 2^512 mod P precomputed for NIST P256 polynomial
107 .LRR:
108 .quad 0x0000000000000003, 0xfffffffbffffffff, 0xfffffffffffffffe, 0x00000004fffffffd
109
110 .LOne:
111 .long 1,1,1,1,1,1,1,1
112 .LTwo:
113 .long 2,2,2,2,2,2,2,2
114 .LThree:
115 .long 3,3,3,3,3,3,3,3
116 .LONE_mont:
117 .quad 0x0000000000000001, 0xffffffff00000000, 0xffffffffffffffff, 0x00000000fffffffe
118 ___
119
120 {
121 ################################################################################
122 # void ecp_nistz256_mul_by_2(uint64_t res[4], uint64_t a[4]);
123
124 my ($a0,$a1,$a2,$a3)=map("%r$_",(8..11));
125 my ($t0,$t1,$t2,$t3,$t4)=("%rax","%rdx","%rcx","%r12","%r13");
126 my ($r_ptr,$a_ptr,$b_ptr)=("%rdi","%rsi","%rdx");
127
128 $code.=<<___;
129
130 .globl  ecp_nistz256_mul_by_2
131 .type   ecp_nistz256_mul_by_2,\@function,2
132 .align  64
133 ecp_nistz256_mul_by_2:
134         push    %r12
135         push    %r13
136 .Lmul_by_2_body:
137
138         mov     8*0($a_ptr), $a0
139         xor     $t4,$t4
140         mov     8*1($a_ptr), $a1
141         add     $a0, $a0                # a0:a3+a0:a3
142         mov     8*2($a_ptr), $a2
143         adc     $a1, $a1
144         mov     8*3($a_ptr), $a3
145         lea     .Lpoly(%rip), $a_ptr
146          mov    $a0, $t0
147         adc     $a2, $a2
148         adc     $a3, $a3
149          mov    $a1, $t1
150         adc     \$0, $t4
151
152         sub     8*0($a_ptr), $a0
153          mov    $a2, $t2
154         sbb     8*1($a_ptr), $a1
155         sbb     8*2($a_ptr), $a2
156          mov    $a3, $t3
157         sbb     8*3($a_ptr), $a3
158         sbb     \$0, $t4
159
160         cmovc   $t0, $a0
161         cmovc   $t1, $a1
162         mov     $a0, 8*0($r_ptr)
163         cmovc   $t2, $a2
164         mov     $a1, 8*1($r_ptr)
165         cmovc   $t3, $a3
166         mov     $a2, 8*2($r_ptr)
167         mov     $a3, 8*3($r_ptr)
168
169         mov     0(%rsp),%r13
170         mov     8(%rsp),%r12
171         lea     16(%rsp),%rsp
172 .Lmul_by_2_epilogue:
173         ret
174 .size   ecp_nistz256_mul_by_2,.-ecp_nistz256_mul_by_2
175
176 ################################################################################
177 # void ecp_nistz256_div_by_2(uint64_t res[4], uint64_t a[4]);
178 .globl  ecp_nistz256_div_by_2
179 .type   ecp_nistz256_div_by_2,\@function,2
180 .align  32
181 ecp_nistz256_div_by_2:
182         push    %r12
183         push    %r13
184 .Ldiv_by_2_body:
185
186         mov     8*0($a_ptr), $a0
187         mov     8*1($a_ptr), $a1
188         mov     8*2($a_ptr), $a2
189          mov    $a0, $t0
190         mov     8*3($a_ptr), $a3
191         lea     .Lpoly(%rip), $a_ptr
192
193          mov    $a1, $t1
194         xor     $t4, $t4
195         add     8*0($a_ptr), $a0
196          mov    $a2, $t2
197         adc     8*1($a_ptr), $a1
198         adc     8*2($a_ptr), $a2
199          mov    $a3, $t3
200         adc     8*3($a_ptr), $a3
201         adc     \$0, $t4
202         xor     $a_ptr, $a_ptr          # borrow $a_ptr
203         test    \$1, $t0
204
205         cmovz   $t0, $a0
206         cmovz   $t1, $a1
207         cmovz   $t2, $a2
208         cmovz   $t3, $a3
209         cmovz   $a_ptr, $t4
210
211         mov     $a1, $t0                # a0:a3>>1
212         shr     \$1, $a0
213         shl     \$63, $t0
214         mov     $a2, $t1
215         shr     \$1, $a1
216         or      $t0, $a0
217         shl     \$63, $t1
218         mov     $a3, $t2
219         shr     \$1, $a2
220         or      $t1, $a1
221         shl     \$63, $t2
222         shr     \$1, $a3
223         shl     \$63, $t4
224         or      $t2, $a2
225         or      $t4, $a3
226
227         mov     $a0, 8*0($r_ptr)
228         mov     $a1, 8*1($r_ptr)
229         mov     $a2, 8*2($r_ptr)
230         mov     $a3, 8*3($r_ptr)
231
232         mov     0(%rsp),%r13
233         mov     8(%rsp),%r12
234         lea     16(%rsp),%rsp
235 .Ldiv_by_2_epilogue:
236         ret
237 .size   ecp_nistz256_div_by_2,.-ecp_nistz256_div_by_2
238
239 ################################################################################
240 # void ecp_nistz256_mul_by_3(uint64_t res[4], uint64_t a[4]);
241 .globl  ecp_nistz256_mul_by_3
242 .type   ecp_nistz256_mul_by_3,\@function,2
243 .align  32
244 ecp_nistz256_mul_by_3:
245         push    %r12
246         push    %r13
247 .Lmul_by_3_body:
248
249         mov     8*0($a_ptr), $a0
250         xor     $t4, $t4
251         mov     8*1($a_ptr), $a1
252         add     $a0, $a0                # a0:a3+a0:a3
253         mov     8*2($a_ptr), $a2
254         adc     $a1, $a1
255         mov     8*3($a_ptr), $a3
256          mov    $a0, $t0
257         adc     $a2, $a2
258         adc     $a3, $a3
259          mov    $a1, $t1
260         adc     \$0, $t4
261
262         sub     \$-1, $a0
263          mov    $a2, $t2
264         sbb     .Lpoly+8*1(%rip), $a1
265         sbb     \$0, $a2
266          mov    $a3, $t3
267         sbb     .Lpoly+8*3(%rip), $a3
268         sbb     \$0, $t4
269
270         cmovc   $t0, $a0
271         cmovc   $t1, $a1
272         cmovc   $t2, $a2
273         cmovc   $t3, $a3
274
275         xor     $t4, $t4
276         add     8*0($a_ptr), $a0        # a0:a3+=a_ptr[0:3]
277         adc     8*1($a_ptr), $a1
278          mov    $a0, $t0
279         adc     8*2($a_ptr), $a2
280         adc     8*3($a_ptr), $a3
281          mov    $a1, $t1
282         adc     \$0, $t4
283
284         sub     \$-1, $a0
285          mov    $a2, $t2
286         sbb     .Lpoly+8*1(%rip), $a1
287         sbb     \$0, $a2
288          mov    $a3, $t3
289         sbb     .Lpoly+8*3(%rip), $a3
290         sbb     \$0, $t4
291
292         cmovc   $t0, $a0
293         cmovc   $t1, $a1
294         mov     $a0, 8*0($r_ptr)
295         cmovc   $t2, $a2
296         mov     $a1, 8*1($r_ptr)
297         cmovc   $t3, $a3
298         mov     $a2, 8*2($r_ptr)
299         mov     $a3, 8*3($r_ptr)
300
301         mov     0(%rsp),%r13
302         mov     8(%rsp),%r12
303         lea     16(%rsp),%rsp
304 .Lmul_by_3_epilogue:
305         ret
306 .size   ecp_nistz256_mul_by_3,.-ecp_nistz256_mul_by_3
307
308 ################################################################################
309 # void ecp_nistz256_add(uint64_t res[4], uint64_t a[4], uint64_t b[4]);
310 .globl  ecp_nistz256_add
311 .type   ecp_nistz256_add,\@function,3
312 .align  32
313 ecp_nistz256_add:
314         push    %r12
315         push    %r13
316 .Ladd_body:
317
318         mov     8*0($a_ptr), $a0
319         xor     $t4, $t4
320         mov     8*1($a_ptr), $a1
321         mov     8*2($a_ptr), $a2
322         mov     8*3($a_ptr), $a3
323         lea     .Lpoly(%rip), $a_ptr
324
325         add     8*0($b_ptr), $a0
326         adc     8*1($b_ptr), $a1
327          mov    $a0, $t0
328         adc     8*2($b_ptr), $a2
329         adc     8*3($b_ptr), $a3
330          mov    $a1, $t1
331         adc     \$0, $t4
332
333         sub     8*0($a_ptr), $a0
334          mov    $a2, $t2
335         sbb     8*1($a_ptr), $a1
336         sbb     8*2($a_ptr), $a2
337          mov    $a3, $t3
338         sbb     8*3($a_ptr), $a3
339         sbb     \$0, $t4
340
341         cmovc   $t0, $a0
342         cmovc   $t1, $a1
343         mov     $a0, 8*0($r_ptr)
344         cmovc   $t2, $a2
345         mov     $a1, 8*1($r_ptr)
346         cmovc   $t3, $a3
347         mov     $a2, 8*2($r_ptr)
348         mov     $a3, 8*3($r_ptr)
349
350         mov     0(%rsp),%r13
351         mov     8(%rsp),%r12
352         lea     16(%rsp),%rsp
353 .Ladd_epilogue:
354         ret
355 .size   ecp_nistz256_add,.-ecp_nistz256_add
356
357 ################################################################################
358 # void ecp_nistz256_sub(uint64_t res[4], uint64_t a[4], uint64_t b[4]);
359 .globl  ecp_nistz256_sub
360 .type   ecp_nistz256_sub,\@function,3
361 .align  32
362 ecp_nistz256_sub:
363         push    %r12
364         push    %r13
365 .Lsub_body:
366
367         mov     8*0($a_ptr), $a0
368         xor     $t4, $t4
369         mov     8*1($a_ptr), $a1
370         mov     8*2($a_ptr), $a2
371         mov     8*3($a_ptr), $a3
372         lea     .Lpoly(%rip), $a_ptr
373
374         sub     8*0($b_ptr), $a0
375         sbb     8*1($b_ptr), $a1
376          mov    $a0, $t0
377         sbb     8*2($b_ptr), $a2
378         sbb     8*3($b_ptr), $a3
379          mov    $a1, $t1
380         sbb     \$0, $t4
381
382         add     8*0($a_ptr), $a0
383          mov    $a2, $t2
384         adc     8*1($a_ptr), $a1
385         adc     8*2($a_ptr), $a2
386          mov    $a3, $t3
387         adc     8*3($a_ptr), $a3
388         test    $t4, $t4
389
390         cmovz   $t0, $a0
391         cmovz   $t1, $a1
392         mov     $a0, 8*0($r_ptr)
393         cmovz   $t2, $a2
394         mov     $a1, 8*1($r_ptr)
395         cmovz   $t3, $a3
396         mov     $a2, 8*2($r_ptr)
397         mov     $a3, 8*3($r_ptr)
398
399         mov     0(%rsp),%r13
400         mov     8(%rsp),%r12
401         lea     16(%rsp),%rsp
402 .Lsub_epilogue:
403         ret
404 .size   ecp_nistz256_sub,.-ecp_nistz256_sub
405
406 ################################################################################
407 # void ecp_nistz256_neg(uint64_t res[4], uint64_t a[4]);
408 .globl  ecp_nistz256_neg
409 .type   ecp_nistz256_neg,\@function,2
410 .align  32
411 ecp_nistz256_neg:
412         push    %r12
413         push    %r13
414 .Lneg_body:
415
416         xor     $a0, $a0
417         xor     $a1, $a1
418         xor     $a2, $a2
419         xor     $a3, $a3
420         xor     $t4, $t4
421
422         sub     8*0($a_ptr), $a0
423         sbb     8*1($a_ptr), $a1
424         sbb     8*2($a_ptr), $a2
425          mov    $a0, $t0
426         sbb     8*3($a_ptr), $a3
427         lea     .Lpoly(%rip), $a_ptr
428          mov    $a1, $t1
429         sbb     \$0, $t4
430
431         add     8*0($a_ptr), $a0
432          mov    $a2, $t2
433         adc     8*1($a_ptr), $a1
434         adc     8*2($a_ptr), $a2
435          mov    $a3, $t3
436         adc     8*3($a_ptr), $a3
437         test    $t4, $t4
438
439         cmovz   $t0, $a0
440         cmovz   $t1, $a1
441         mov     $a0, 8*0($r_ptr)
442         cmovz   $t2, $a2
443         mov     $a1, 8*1($r_ptr)
444         cmovz   $t3, $a3
445         mov     $a2, 8*2($r_ptr)
446         mov     $a3, 8*3($r_ptr)
447
448         mov     0(%rsp),%r13
449         mov     8(%rsp),%r12
450         lea     16(%rsp),%rsp
451 .Lneg_epilogue:
452         ret
453 .size   ecp_nistz256_neg,.-ecp_nistz256_neg
454 ___
455 }
456 {
457 my ($r_ptr,$a_ptr,$b_org,$b_ptr)=("%rdi","%rsi","%rdx","%rbx");
458 my ($acc0,$acc1,$acc2,$acc3,$acc4,$acc5,$acc6,$acc7)=map("%r$_",(8..15));
459 my ($t0,$t1,$t2,$t3,$t4)=("%rcx","%rbp","%rbx","%rdx","%rax");
460 my ($poly1,$poly3)=($acc6,$acc7);
461
462 $code.=<<___;
463 ################################################################################
464 # void ecp_nistz256_to_mont(
465 #   uint64_t res[4],
466 #   uint64_t in[4]);
467 .globl  ecp_nistz256_to_mont
468 .type   ecp_nistz256_to_mont,\@function,2
469 .align  32
470 ecp_nistz256_to_mont:
471 ___
472 $code.=<<___    if ($addx);
473         mov     \$0x80100, %ecx
474         and     OPENSSL_ia32cap_P+8(%rip), %ecx
475 ___
476 $code.=<<___;
477         lea     .LRR(%rip), $b_org
478         jmp     .Lmul_mont
479 .size   ecp_nistz256_to_mont,.-ecp_nistz256_to_mont
480
481 ################################################################################
482 # void ecp_nistz256_mul_mont(
483 #   uint64_t res[4],
484 #   uint64_t a[4],
485 #   uint64_t b[4]);
486
487 .globl  ecp_nistz256_mul_mont
488 .type   ecp_nistz256_mul_mont,\@function,3
489 .align  32
490 ecp_nistz256_mul_mont:
491 ___
492 $code.=<<___    if ($addx);
493         mov     \$0x80100, %ecx
494         and     OPENSSL_ia32cap_P+8(%rip), %ecx
495 ___
496 $code.=<<___;
497 .Lmul_mont:
498         push    %rbp
499         push    %rbx
500         push    %r12
501         push    %r13
502         push    %r14
503         push    %r15
504 .Lmul_body:
505 ___
506 $code.=<<___    if ($addx);
507         cmp     \$0x80100, %ecx
508         je      .Lmul_montx
509 ___
510 $code.=<<___;
511         mov     $b_org, $b_ptr
512         mov     8*0($b_org), %rax
513         mov     8*0($a_ptr), $acc1
514         mov     8*1($a_ptr), $acc2
515         mov     8*2($a_ptr), $acc3
516         mov     8*3($a_ptr), $acc4
517
518         call    __ecp_nistz256_mul_montq
519 ___
520 $code.=<<___    if ($addx);
521         jmp     .Lmul_mont_done
522
523 .align  32
524 .Lmul_montx:
525         mov     $b_org, $b_ptr
526         mov     8*0($b_org), %rdx
527         mov     8*0($a_ptr), $acc1
528         mov     8*1($a_ptr), $acc2
529         mov     8*2($a_ptr), $acc3
530         mov     8*3($a_ptr), $acc4
531         lea     -128($a_ptr), $a_ptr    # control u-op density
532
533         call    __ecp_nistz256_mul_montx
534 ___
535 $code.=<<___;
536 .Lmul_mont_done:
537         mov     0(%rsp),%r15
538         mov     8(%rsp),%r14
539         mov     16(%rsp),%r13
540         mov     24(%rsp),%r12
541         mov     32(%rsp),%rbx
542         mov     40(%rsp),%rbp
543         lea     48(%rsp),%rsp
544 .Lmul_epilogue:
545         ret
546 .size   ecp_nistz256_mul_mont,.-ecp_nistz256_mul_mont
547
548 .type   __ecp_nistz256_mul_montq,\@abi-omnipotent
549 .align  32
550 __ecp_nistz256_mul_montq:
551         ########################################################################
552         # Multiply a by b[0]
553         mov     %rax, $t1
554         mulq    $acc1
555         mov     .Lpoly+8*1(%rip),$poly1
556         mov     %rax, $acc0
557         mov     $t1, %rax
558         mov     %rdx, $acc1
559
560         mulq    $acc2
561         mov     .Lpoly+8*3(%rip),$poly3
562         add     %rax, $acc1
563         mov     $t1, %rax
564         adc     \$0, %rdx
565         mov     %rdx, $acc2
566
567         mulq    $acc3
568         add     %rax, $acc2
569         mov     $t1, %rax
570         adc     \$0, %rdx
571         mov     %rdx, $acc3
572
573         mulq    $acc4
574         add     %rax, $acc3
575          mov    $acc0, %rax
576         adc     \$0, %rdx
577         xor     $acc5, $acc5
578         mov     %rdx, $acc4
579
580         ########################################################################
581         # First reduction step
582         # Basically now we want to multiply acc[0] by p256,
583         # and add the result to the acc.
584         # Due to the special form of p256 we do some optimizations
585         #
586         # acc[0] x p256[0..1] = acc[0] x 2^96 - acc[0]
587         # then we add acc[0] and get acc[0] x 2^96
588
589         mov     $acc0, $t1
590         shl     \$32, $acc0
591         mulq    $poly3
592         shr     \$32, $t1
593         add     $acc0, $acc1            # +=acc[0]<<96
594         adc     $t1, $acc2
595         adc     %rax, $acc3
596          mov    8*1($b_ptr), %rax
597         adc     %rdx, $acc4
598         adc     \$0, $acc5
599         xor     $acc0, $acc0
600
601         ########################################################################
602         # Multiply by b[1]
603         mov     %rax, $t1
604         mulq    8*0($a_ptr)
605         add     %rax, $acc1
606         mov     $t1, %rax
607         adc     \$0, %rdx
608         mov     %rdx, $t0
609
610         mulq    8*1($a_ptr)
611         add     $t0, $acc2
612         adc     \$0, %rdx
613         add     %rax, $acc2
614         mov     $t1, %rax
615         adc     \$0, %rdx
616         mov     %rdx, $t0
617
618         mulq    8*2($a_ptr)
619         add     $t0, $acc3
620         adc     \$0, %rdx
621         add     %rax, $acc3
622         mov     $t1, %rax
623         adc     \$0, %rdx
624         mov     %rdx, $t0
625
626         mulq    8*3($a_ptr)
627         add     $t0, $acc4
628         adc     \$0, %rdx
629         add     %rax, $acc4
630          mov    $acc1, %rax
631         adc     %rdx, $acc5
632         adc     \$0, $acc0
633
634         ########################################################################
635         # Second reduction step
636         mov     $acc1, $t1
637         shl     \$32, $acc1
638         mulq    $poly3
639         shr     \$32, $t1
640         add     $acc1, $acc2
641         adc     $t1, $acc3
642         adc     %rax, $acc4
643          mov    8*2($b_ptr), %rax
644         adc     %rdx, $acc5
645         adc     \$0, $acc0
646         xor     $acc1, $acc1
647
648         ########################################################################
649         # Multiply by b[2]
650         mov     %rax, $t1
651         mulq    8*0($a_ptr)
652         add     %rax, $acc2
653         mov     $t1, %rax
654         adc     \$0, %rdx
655         mov     %rdx, $t0
656
657         mulq    8*1($a_ptr)
658         add     $t0, $acc3
659         adc     \$0, %rdx
660         add     %rax, $acc3
661         mov     $t1, %rax
662         adc     \$0, %rdx
663         mov     %rdx, $t0
664
665         mulq    8*2($a_ptr)
666         add     $t0, $acc4
667         adc     \$0, %rdx
668         add     %rax, $acc4
669         mov     $t1, %rax
670         adc     \$0, %rdx
671         mov     %rdx, $t0
672
673         mulq    8*3($a_ptr)
674         add     $t0, $acc5
675         adc     \$0, %rdx
676         add     %rax, $acc5
677          mov    $acc2, %rax
678         adc     %rdx, $acc0
679         adc     \$0, $acc1
680
681         ########################################################################
682         # Third reduction step
683         mov     $acc2, $t1
684         shl     \$32, $acc2
685         mulq    $poly3
686         shr     \$32, $t1
687         add     $acc2, $acc3
688         adc     $t1, $acc4
689         adc     %rax, $acc5
690          mov    8*3($b_ptr), %rax
691         adc     %rdx, $acc0
692         adc     \$0, $acc1
693         xor     $acc2, $acc2
694
695         ########################################################################
696         # Multiply by b[3]
697         mov     %rax, $t1
698         mulq    8*0($a_ptr)
699         add     %rax, $acc3
700         mov     $t1, %rax
701         adc     \$0, %rdx
702         mov     %rdx, $t0
703
704         mulq    8*1($a_ptr)
705         add     $t0, $acc4
706         adc     \$0, %rdx
707         add     %rax, $acc4
708         mov     $t1, %rax
709         adc     \$0, %rdx
710         mov     %rdx, $t0
711
712         mulq    8*2($a_ptr)
713         add     $t0, $acc5
714         adc     \$0, %rdx
715         add     %rax, $acc5
716         mov     $t1, %rax
717         adc     \$0, %rdx
718         mov     %rdx, $t0
719
720         mulq    8*3($a_ptr)
721         add     $t0, $acc0
722         adc     \$0, %rdx
723         add     %rax, $acc0
724          mov    $acc3, %rax
725         adc     %rdx, $acc1
726         adc     \$0, $acc2
727
728         ########################################################################
729         # Final reduction step
730         mov     $acc3, $t1
731         shl     \$32, $acc3
732         mulq    $poly3
733         shr     \$32, $t1
734         add     $acc3, $acc4
735         adc     $t1, $acc5
736          mov    $acc4, $t0
737         adc     %rax, $acc0
738         adc     %rdx, $acc1
739          mov    $acc5, $t1
740         adc     \$0, $acc2
741
742         ########################################################################
743         # Branch-less conditional subtraction of P
744         sub     \$-1, $acc4             # .Lpoly[0]
745          mov    $acc0, $t2
746         sbb     $poly1, $acc5           # .Lpoly[1]
747         sbb     \$0, $acc0              # .Lpoly[2]
748          mov    $acc1, $t3
749         sbb     $poly3, $acc1           # .Lpoly[3]
750         sbb     \$0, $acc2
751
752         cmovc   $t0, $acc4
753         cmovc   $t1, $acc5
754         mov     $acc4, 8*0($r_ptr)
755         cmovc   $t2, $acc0
756         mov     $acc5, 8*1($r_ptr)
757         cmovc   $t3, $acc1
758         mov     $acc0, 8*2($r_ptr)
759         mov     $acc1, 8*3($r_ptr)
760
761         ret
762 .size   __ecp_nistz256_mul_montq,.-__ecp_nistz256_mul_montq
763
764 ################################################################################
765 # void ecp_nistz256_sqr_mont(
766 #   uint64_t res[4],
767 #   uint64_t a[4]);
768
769 # we optimize the square according to S.Gueron and V.Krasnov,
770 # "Speeding up Big-Number Squaring"
771 .globl  ecp_nistz256_sqr_mont
772 .type   ecp_nistz256_sqr_mont,\@function,2
773 .align  32
774 ecp_nistz256_sqr_mont:
775 ___
776 $code.=<<___    if ($addx);
777         mov     \$0x80100, %ecx
778         and     OPENSSL_ia32cap_P+8(%rip), %ecx
779 ___
780 $code.=<<___;
781         push    %rbp
782         push    %rbx
783         push    %r12
784         push    %r13
785         push    %r14
786         push    %r15
787 .Lsqr_body:
788 ___
789 $code.=<<___    if ($addx);
790         cmp     \$0x80100, %ecx
791         je      .Lsqr_montx
792 ___
793 $code.=<<___;
794         mov     8*0($a_ptr), %rax
795         mov     8*1($a_ptr), $acc6
796         mov     8*2($a_ptr), $acc7
797         mov     8*3($a_ptr), $acc0
798
799         call    __ecp_nistz256_sqr_montq
800 ___
801 $code.=<<___    if ($addx);
802         jmp     .Lsqr_mont_done
803
804 .align  32
805 .Lsqr_montx:
806         mov     8*0($a_ptr), %rdx
807         mov     8*1($a_ptr), $acc6
808         mov     8*2($a_ptr), $acc7
809         mov     8*3($a_ptr), $acc0
810         lea     -128($a_ptr), $a_ptr    # control u-op density
811
812         call    __ecp_nistz256_sqr_montx
813 ___
814 $code.=<<___;
815 .Lsqr_mont_done:
816         mov     0(%rsp),%r15
817         mov     8(%rsp),%r14
818         mov     16(%rsp),%r13
819         mov     24(%rsp),%r12
820         mov     32(%rsp),%rbx
821         mov     40(%rsp),%rbp
822         lea     48(%rsp),%rsp
823 .Lsqr_epilogue:
824         ret
825 .size   ecp_nistz256_sqr_mont,.-ecp_nistz256_sqr_mont
826
827 .type   __ecp_nistz256_sqr_montq,\@abi-omnipotent
828 .align  32
829 __ecp_nistz256_sqr_montq:
830         mov     %rax, $acc5
831         mulq    $acc6                   # a[1]*a[0]
832         mov     %rax, $acc1
833         mov     $acc7, %rax
834         mov     %rdx, $acc2
835
836         mulq    $acc5                   # a[0]*a[2]
837         add     %rax, $acc2
838         mov     $acc0, %rax
839         adc     \$0, %rdx
840         mov     %rdx, $acc3
841
842         mulq    $acc5                   # a[0]*a[3]
843         add     %rax, $acc3
844          mov    $acc7, %rax
845         adc     \$0, %rdx
846         mov     %rdx, $acc4
847
848         #################################
849         mulq    $acc6                   # a[1]*a[2]
850         add     %rax, $acc3
851         mov     $acc0, %rax
852         adc     \$0, %rdx
853         mov     %rdx, $t1
854
855         mulq    $acc6                   # a[1]*a[3]
856         add     %rax, $acc4
857          mov    $acc0, %rax
858         adc     \$0, %rdx
859         add     $t1, $acc4
860         mov     %rdx, $acc5
861         adc     \$0, $acc5
862
863         #################################
864         mulq    $acc7                   # a[2]*a[3]
865         xor     $acc7, $acc7
866         add     %rax, $acc5
867          mov    8*0($a_ptr), %rax
868         mov     %rdx, $acc6
869         adc     \$0, $acc6
870
871         add     $acc1, $acc1            # acc1:6<<1
872         adc     $acc2, $acc2
873         adc     $acc3, $acc3
874         adc     $acc4, $acc4
875         adc     $acc5, $acc5
876         adc     $acc6, $acc6
877         adc     \$0, $acc7
878
879         mulq    %rax
880         mov     %rax, $acc0
881         mov     8*1($a_ptr), %rax
882         mov     %rdx, $t0
883
884         mulq    %rax
885         add     $t0, $acc1
886         adc     %rax, $acc2
887         mov     8*2($a_ptr), %rax
888         adc     \$0, %rdx
889         mov     %rdx, $t0
890
891         mulq    %rax
892         add     $t0, $acc3
893         adc     %rax, $acc4
894         mov     8*3($a_ptr), %rax
895         adc     \$0, %rdx
896         mov     %rdx, $t0
897
898         mulq    %rax
899         add     $t0, $acc5
900         adc     %rax, $acc6
901          mov    $acc0, %rax
902         adc     %rdx, $acc7
903
904         mov     .Lpoly+8*1(%rip), $a_ptr
905         mov     .Lpoly+8*3(%rip), $t1
906
907         ##########################################
908         # Now the reduction
909         # First iteration
910         mov     $acc0, $t0
911         shl     \$32, $acc0
912         mulq    $t1
913         shr     \$32, $t0
914         add     $acc0, $acc1            # +=acc[0]<<96
915         adc     $t0, $acc2
916         adc     %rax, $acc3
917          mov    $acc1, %rax
918         adc     \$0, %rdx
919
920         ##########################################
921         # Second iteration
922         mov     $acc1, $t0
923         shl     \$32, $acc1
924         mov     %rdx, $acc0
925         mulq    $t1
926         shr     \$32, $t0
927         add     $acc1, $acc2
928         adc     $t0, $acc3
929         adc     %rax, $acc0
930          mov    $acc2, %rax
931         adc     \$0, %rdx
932
933         ##########################################
934         # Third iteration
935         mov     $acc2, $t0
936         shl     \$32, $acc2
937         mov     %rdx, $acc1
938         mulq    $t1
939         shr     \$32, $t0
940         add     $acc2, $acc3
941         adc     $t0, $acc0
942         adc     %rax, $acc1
943          mov    $acc3, %rax
944         adc     \$0, %rdx
945
946         ###########################################
947         # Last iteration
948         mov     $acc3, $t0
949         shl     \$32, $acc3
950         mov     %rdx, $acc2
951         mulq    $t1
952         shr     \$32, $t0
953         add     $acc3, $acc0
954         adc     $t0, $acc1
955         adc     %rax, $acc2
956         adc     \$0, %rdx
957         xor     $acc3, $acc3
958
959         ############################################
960         # Add the rest of the acc
961         add     $acc0, $acc4
962         adc     $acc1, $acc5
963          mov    $acc4, $acc0
964         adc     $acc2, $acc6
965         adc     %rdx, $acc7
966          mov    $acc5, $acc1
967         adc     \$0, $acc3
968
969         sub     \$-1, $acc4             # .Lpoly[0]
970          mov    $acc6, $acc2
971         sbb     $a_ptr, $acc5           # .Lpoly[1]
972         sbb     \$0, $acc6              # .Lpoly[2]
973          mov    $acc7, $t0
974         sbb     $t1, $acc7              # .Lpoly[3]
975         sbb     \$0, $acc3
976
977         cmovc   $acc0, $acc4
978         cmovc   $acc1, $acc5
979         mov     $acc4, 8*0($r_ptr)
980         cmovc   $acc2, $acc6
981         mov     $acc5, 8*1($r_ptr)
982         cmovc   $t0, $acc7
983         mov     $acc6, 8*2($r_ptr)
984         mov     $acc7, 8*3($r_ptr)
985
986         ret
987 .size   __ecp_nistz256_sqr_montq,.-__ecp_nistz256_sqr_montq
988 ___
989
990 if ($addx) {
991 $code.=<<___;
992 .type   __ecp_nistz256_mul_montx,\@abi-omnipotent
993 .align  32
994 __ecp_nistz256_mul_montx:
995         ########################################################################
996         # Multiply by b[0]
997         mulx    $acc1, $acc0, $acc1
998         mulx    $acc2, $t0, $acc2
999         mov     \$32, $poly1
1000         xor     $acc5, $acc5            # cf=0
1001         mulx    $acc3, $t1, $acc3
1002         mov     .Lpoly+8*3(%rip), $poly3
1003         adc     $t0, $acc1
1004         mulx    $acc4, $t0, $acc4
1005          mov    $acc0, %rdx
1006         adc     $t1, $acc2
1007          shlx   $poly1,$acc0,$t1
1008         adc     $t0, $acc3
1009          shrx   $poly1,$acc0,$t0
1010         adc     \$0, $acc4
1011
1012         ########################################################################
1013         # First reduction step
1014         add     $t1, $acc1
1015         adc     $t0, $acc2
1016
1017         mulx    $poly3, $t0, $t1
1018          mov    8*1($b_ptr), %rdx
1019         adc     $t0, $acc3
1020         adc     $t1, $acc4
1021         adc     \$0, $acc5
1022         xor     $acc0, $acc0            # $acc0=0,cf=0,of=0
1023
1024         ########################################################################
1025         # Multiply by b[1]
1026         mulx    8*0+128($a_ptr), $t0, $t1
1027         adcx    $t0, $acc1
1028         adox    $t1, $acc2
1029
1030         mulx    8*1+128($a_ptr), $t0, $t1
1031         adcx    $t0, $acc2
1032         adox    $t1, $acc3
1033
1034         mulx    8*2+128($a_ptr), $t0, $t1
1035         adcx    $t0, $acc3
1036         adox    $t1, $acc4
1037
1038         mulx    8*3+128($a_ptr), $t0, $t1
1039          mov    $acc1, %rdx
1040         adcx    $t0, $acc4
1041          shlx   $poly1, $acc1, $t0
1042         adox    $t1, $acc5
1043          shrx   $poly1, $acc1, $t1
1044
1045         adcx    $acc0, $acc5
1046         adox    $acc0, $acc0
1047         adc     \$0, $acc0
1048
1049         ########################################################################
1050         # Second reduction step
1051         add     $t0, $acc2
1052         adc     $t1, $acc3
1053
1054         mulx    $poly3, $t0, $t1
1055          mov    8*2($b_ptr), %rdx
1056         adc     $t0, $acc4
1057         adc     $t1, $acc5
1058         adc     \$0, $acc0
1059         xor     $acc1 ,$acc1            # $acc1=0,cf=0,of=0
1060
1061         ########################################################################
1062         # Multiply by b[2]
1063         mulx    8*0+128($a_ptr), $t0, $t1
1064         adcx    $t0, $acc2
1065         adox    $t1, $acc3
1066
1067         mulx    8*1+128($a_ptr), $t0, $t1
1068         adcx    $t0, $acc3
1069         adox    $t1, $acc4
1070
1071         mulx    8*2+128($a_ptr), $t0, $t1
1072         adcx    $t0, $acc4
1073         adox    $t1, $acc5
1074
1075         mulx    8*3+128($a_ptr), $t0, $t1
1076          mov    $acc2, %rdx
1077         adcx    $t0, $acc5
1078          shlx   $poly1, $acc2, $t0
1079         adox    $t1, $acc0
1080          shrx   $poly1, $acc2, $t1
1081
1082         adcx    $acc1, $acc0
1083         adox    $acc1, $acc1
1084         adc     \$0, $acc1
1085
1086         ########################################################################
1087         # Third reduction step
1088         add     $t0, $acc3
1089         adc     $t1, $acc4
1090
1091         mulx    $poly3, $t0, $t1
1092          mov    8*3($b_ptr), %rdx
1093         adc     $t0, $acc5
1094         adc     $t1, $acc0
1095         adc     \$0, $acc1
1096         xor     $acc2, $acc2            # $acc2=0,cf=0,of=0
1097
1098         ########################################################################
1099         # Multiply by b[3]
1100         mulx    8*0+128($a_ptr), $t0, $t1
1101         adcx    $t0, $acc3
1102         adox    $t1, $acc4
1103
1104         mulx    8*1+128($a_ptr), $t0, $t1
1105         adcx    $t0, $acc4
1106         adox    $t1, $acc5
1107
1108         mulx    8*2+128($a_ptr), $t0, $t1
1109         adcx    $t0, $acc5
1110         adox    $t1, $acc0
1111
1112         mulx    8*3+128($a_ptr), $t0, $t1
1113          mov    $acc3, %rdx
1114         adcx    $t0, $acc0
1115          shlx   $poly1, $acc3, $t0
1116         adox    $t1, $acc1
1117          shrx   $poly1, $acc3, $t1
1118
1119         adcx    $acc2, $acc1
1120         adox    $acc2, $acc2
1121         adc     \$0, $acc2
1122
1123         ########################################################################
1124         # Fourth reduction step
1125         add     $t0, $acc4
1126         adc     $t1, $acc5
1127
1128         mulx    $poly3, $t0, $t1
1129          mov    $acc4, $t2
1130         mov     .Lpoly+8*1(%rip), $poly1
1131         adc     $t0, $acc0
1132          mov    $acc5, $t3
1133         adc     $t1, $acc1
1134         adc     \$0, $acc2
1135
1136         ########################################################################
1137         # Branch-less conditional subtraction of P
1138         xor     %eax, %eax
1139          mov    $acc0, $t0
1140         sbb     \$-1, $acc4             # .Lpoly[0]
1141         sbb     $poly1, $acc5           # .Lpoly[1]
1142         sbb     \$0, $acc0              # .Lpoly[2]
1143          mov    $acc1, $t1
1144         sbb     $poly3, $acc1           # .Lpoly[3]
1145         sbb     \$0, $acc2
1146
1147         cmovc   $t2, $acc4
1148         cmovc   $t3, $acc5
1149         mov     $acc4, 8*0($r_ptr)
1150         cmovc   $t0, $acc0
1151         mov     $acc5, 8*1($r_ptr)
1152         cmovc   $t1, $acc1
1153         mov     $acc0, 8*2($r_ptr)
1154         mov     $acc1, 8*3($r_ptr)
1155
1156         ret
1157 .size   __ecp_nistz256_mul_montx,.-__ecp_nistz256_mul_montx
1158
1159 .type   __ecp_nistz256_sqr_montx,\@abi-omnipotent
1160 .align  32
1161 __ecp_nistz256_sqr_montx:
1162         mulx    $acc6, $acc1, $acc2     # a[0]*a[1]
1163         mulx    $acc7, $t0, $acc3       # a[0]*a[2]
1164         xor     %eax, %eax
1165         adc     $t0, $acc2
1166         mulx    $acc0, $t1, $acc4       # a[0]*a[3]
1167          mov    $acc6, %rdx
1168         adc     $t1, $acc3
1169         adc     \$0, $acc4
1170         xor     $acc5, $acc5            # $acc5=0,cf=0,of=0
1171
1172         #################################
1173         mulx    $acc7, $t0, $t1         # a[1]*a[2]
1174         adcx    $t0, $acc3
1175         adox    $t1, $acc4
1176
1177         mulx    $acc0, $t0, $t1         # a[1]*a[3]
1178          mov    $acc7, %rdx
1179         adcx    $t0, $acc4
1180         adox    $t1, $acc5
1181         adc     \$0, $acc5
1182
1183         #################################
1184         mulx    $acc0, $t0, $acc6       # a[2]*a[3]
1185          mov    8*0+128($a_ptr), %rdx
1186         xor     $acc7, $acc7            # $acc7=0,cf=0,of=0
1187          adcx   $acc1, $acc1            # acc1:6<<1
1188         adox    $t0, $acc5
1189          adcx   $acc2, $acc2
1190         adox    $acc7, $acc6            # of=0
1191
1192         mulx    %rdx, $acc0, $t1
1193         mov     8*1+128($a_ptr), %rdx
1194          adcx   $acc3, $acc3
1195         adox    $t1, $acc1
1196          adcx   $acc4, $acc4
1197         mulx    %rdx, $t0, $t4
1198         mov     8*2+128($a_ptr), %rdx
1199          adcx   $acc5, $acc5
1200         adox    $t0, $acc2
1201          adcx   $acc6, $acc6
1202         .byte   0x67
1203         mulx    %rdx, $t0, $t1
1204         mov     8*3+128($a_ptr), %rdx
1205         adox    $t4, $acc3
1206          adcx   $acc7, $acc7
1207         adox    $t0, $acc4
1208          mov    \$32, $a_ptr
1209         adox    $t1, $acc5
1210         .byte   0x67,0x67
1211         mulx    %rdx, $t0, $t4
1212          mov    $acc0, %rdx
1213         adox    $t0, $acc6
1214          shlx   $a_ptr, $acc0, $t0
1215         adox    $t4, $acc7
1216          shrx   $a_ptr, $acc0, $t4
1217          mov    .Lpoly+8*3(%rip), $t1
1218
1219         # reduction step 1
1220         add     $t0, $acc1
1221         adc     $t4, $acc2
1222
1223         mulx    $t1, $t0, $acc0
1224          mov    $acc1, %rdx
1225         adc     $t0, $acc3
1226          shlx   $a_ptr, $acc1, $t0
1227         adc     \$0, $acc0
1228          shrx   $a_ptr, $acc1, $t4
1229
1230         # reduction step 2
1231         add     $t0, $acc2
1232         adc     $t4, $acc3
1233
1234         mulx    $t1, $t0, $acc1
1235          mov    $acc2, %rdx
1236         adc     $t0, $acc0
1237          shlx   $a_ptr, $acc2, $t0
1238         adc     \$0, $acc1
1239          shrx   $a_ptr, $acc2, $t4
1240
1241         # reduction step 3
1242         add     $t0, $acc3
1243         adc     $t4, $acc0
1244
1245         mulx    $t1, $t0, $acc2
1246          mov    $acc3, %rdx
1247         adc     $t0, $acc1
1248          shlx   $a_ptr, $acc3, $t0
1249         adc     \$0, $acc2
1250          shrx   $a_ptr, $acc3, $t4
1251
1252         # reduction step 4
1253         add     $t0, $acc0
1254         adc     $t4, $acc1
1255
1256         mulx    $t1, $t0, $acc3
1257         adc     $t0, $acc2
1258         adc     \$0, $acc3
1259
1260         xor     $t3, $t3                # cf=0
1261         adc     $acc0, $acc4            # accumulate upper half
1262          mov    .Lpoly+8*1(%rip), $a_ptr
1263         adc     $acc1, $acc5
1264          mov    $acc4, $acc0
1265         adc     $acc2, $acc6
1266         adc     $acc3, $acc7
1267          mov    $acc5, $acc1
1268         adc     \$0, $t3
1269
1270         xor     %eax, %eax              # cf=0
1271         sbb     \$-1, $acc4             # .Lpoly[0]
1272          mov    $acc6, $acc2
1273         sbb     $a_ptr, $acc5           # .Lpoly[1]
1274         sbb     \$0, $acc6              # .Lpoly[2]
1275          mov    $acc7, $acc3
1276         sbb     $t1, $acc7              # .Lpoly[3]
1277         sbb     \$0, $t3
1278
1279         cmovc   $acc0, $acc4
1280         cmovc   $acc1, $acc5
1281         mov     $acc4, 8*0($r_ptr)
1282         cmovc   $acc2, $acc6
1283         mov     $acc5, 8*1($r_ptr)
1284         cmovc   $acc3, $acc7
1285         mov     $acc6, 8*2($r_ptr)
1286         mov     $acc7, 8*3($r_ptr)
1287
1288         ret
1289 .size   __ecp_nistz256_sqr_montx,.-__ecp_nistz256_sqr_montx
1290 ___
1291 }
1292 }
1293 {
1294 my ($r_ptr,$in_ptr)=("%rdi","%rsi");
1295 my ($acc0,$acc1,$acc2,$acc3)=map("%r$_",(8..11));
1296 my ($t0,$t1,$t2)=("%rcx","%r12","%r13");
1297
1298 $code.=<<___;
1299 ################################################################################
1300 # void ecp_nistz256_from_mont(
1301 #   uint64_t res[4],
1302 #   uint64_t in[4]);
1303 # This one performs Montgomery multiplication by 1, so we only need the reduction
1304
1305 .globl  ecp_nistz256_from_mont
1306 .type   ecp_nistz256_from_mont,\@function,2
1307 .align  32
1308 ecp_nistz256_from_mont:
1309         push    %r12
1310         push    %r13
1311 .Lfrom_body:
1312
1313         mov     8*0($in_ptr), %rax
1314         mov     .Lpoly+8*3(%rip), $t2
1315         mov     8*1($in_ptr), $acc1
1316         mov     8*2($in_ptr), $acc2
1317         mov     8*3($in_ptr), $acc3
1318         mov     %rax, $acc0
1319         mov     .Lpoly+8*1(%rip), $t1
1320
1321         #########################################
1322         # First iteration
1323         mov     %rax, $t0
1324         shl     \$32, $acc0
1325         mulq    $t2
1326         shr     \$32, $t0
1327         add     $acc0, $acc1
1328         adc     $t0, $acc2
1329         adc     %rax, $acc3
1330          mov    $acc1, %rax
1331         adc     \$0, %rdx
1332
1333         #########################################
1334         # Second iteration
1335         mov     $acc1, $t0
1336         shl     \$32, $acc1
1337         mov     %rdx, $acc0
1338         mulq    $t2
1339         shr     \$32, $t0
1340         add     $acc1, $acc2
1341         adc     $t0, $acc3
1342         adc     %rax, $acc0
1343          mov    $acc2, %rax
1344         adc     \$0, %rdx
1345
1346         ##########################################
1347         # Third iteration
1348         mov     $acc2, $t0
1349         shl     \$32, $acc2
1350         mov     %rdx, $acc1
1351         mulq    $t2
1352         shr     \$32, $t0
1353         add     $acc2, $acc3
1354         adc     $t0, $acc0
1355         adc     %rax, $acc1
1356          mov    $acc3, %rax
1357         adc     \$0, %rdx
1358
1359         ###########################################
1360         # Last iteration
1361         mov     $acc3, $t0
1362         shl     \$32, $acc3
1363         mov     %rdx, $acc2
1364         mulq    $t2
1365         shr     \$32, $t0
1366         add     $acc3, $acc0
1367         adc     $t0, $acc1
1368          mov    $acc0, $t0
1369         adc     %rax, $acc2
1370          mov    $acc1, $in_ptr
1371         adc     \$0, %rdx
1372
1373         ###########################################
1374         # Branch-less conditional subtraction
1375         sub     \$-1, $acc0
1376          mov    $acc2, %rax
1377         sbb     $t1, $acc1
1378         sbb     \$0, $acc2
1379          mov    %rdx, $acc3
1380         sbb     $t2, %rdx
1381         sbb     $t2, $t2
1382
1383         cmovnz  $t0, $acc0
1384         cmovnz  $in_ptr, $acc1
1385         mov     $acc0, 8*0($r_ptr)
1386         cmovnz  %rax, $acc2
1387         mov     $acc1, 8*1($r_ptr)
1388         cmovz   %rdx, $acc3
1389         mov     $acc2, 8*2($r_ptr)
1390         mov     $acc3, 8*3($r_ptr)
1391
1392         mov     0(%rsp),%r13
1393         mov     8(%rsp),%r12
1394         lea     16(%rsp),%rsp
1395 .Lfrom_epilogue:
1396         ret
1397 .size   ecp_nistz256_from_mont,.-ecp_nistz256_from_mont
1398 ___
1399 }
1400 {
1401 my ($val,$in_t,$index)=$win64?("%rcx","%rdx","%r8d"):("%rdi","%rsi","%edx");
1402 my ($ONE,$INDEX,$Ra,$Rb,$Rc,$Rd,$Re,$Rf)=map("%xmm$_",(0..7));
1403 my ($M0,$T0a,$T0b,$T0c,$T0d,$T0e,$T0f,$TMP0)=map("%xmm$_",(8..15));
1404 my ($M1,$T2a,$T2b,$TMP2,$M2,$T2a,$T2b,$TMP2)=map("%xmm$_",(8..15));
1405
1406 $code.=<<___;
1407 ################################################################################
1408 # void ecp_nistz256_scatter_w5(uint64_t *val, uint64_t *in_t, int index);
1409 .globl  ecp_nistz256_scatter_w5
1410 .type   ecp_nistz256_scatter_w5,\@abi-omnipotent
1411 .align  32
1412 ecp_nistz256_scatter_w5:
1413         lea     -3($index,$index,2), $index
1414         movdqa  0x00($in_t), %xmm0
1415         shl     \$5, $index
1416         movdqa  0x10($in_t), %xmm1
1417         movdqa  0x20($in_t), %xmm2
1418         movdqa  0x30($in_t), %xmm3
1419         movdqa  0x40($in_t), %xmm4
1420         movdqa  0x50($in_t), %xmm5
1421         movdqa  %xmm0, 0x00($val,$index)
1422         movdqa  %xmm1, 0x10($val,$index)
1423         movdqa  %xmm2, 0x20($val,$index)
1424         movdqa  %xmm3, 0x30($val,$index)
1425         movdqa  %xmm4, 0x40($val,$index)
1426         movdqa  %xmm5, 0x50($val,$index)
1427
1428         ret
1429 .size   ecp_nistz256_scatter_w5,.-ecp_nistz256_scatter_w5
1430
1431 ################################################################################
1432 # void ecp_nistz256_gather_w5(uint64_t *val, uint64_t *in_t, int index);
1433 .globl  ecp_nistz256_gather_w5
1434 .type   ecp_nistz256_gather_w5,\@abi-omnipotent
1435 .align  32
1436 ecp_nistz256_gather_w5:
1437 ___
1438 $code.=<<___    if ($avx>1);
1439         mov     OPENSSL_ia32cap_P+8(%rip), %eax
1440         test    \$`1<<5`, %eax
1441         jnz     .Lavx2_gather_w5
1442 ___
1443 $code.=<<___    if ($win64);
1444         lea     -0x88(%rsp), %rax
1445 .LSEH_begin_ecp_nistz256_gather_w5:
1446         .byte   0x48,0x8d,0x60,0xe0             #lea    -0x20(%rax), %rsp
1447         .byte   0x0f,0x29,0x70,0xe0             #movaps %xmm6, -0x20(%rax)
1448         .byte   0x0f,0x29,0x78,0xf0             #movaps %xmm7, -0x10(%rax)
1449         .byte   0x44,0x0f,0x29,0x00             #movaps %xmm8, 0(%rax)
1450         .byte   0x44,0x0f,0x29,0x48,0x10        #movaps %xmm9, 0x10(%rax)
1451         .byte   0x44,0x0f,0x29,0x50,0x20        #movaps %xmm10, 0x20(%rax)
1452         .byte   0x44,0x0f,0x29,0x58,0x30        #movaps %xmm11, 0x30(%rax)
1453         .byte   0x44,0x0f,0x29,0x60,0x40        #movaps %xmm12, 0x40(%rax)
1454         .byte   0x44,0x0f,0x29,0x68,0x50        #movaps %xmm13, 0x50(%rax)
1455         .byte   0x44,0x0f,0x29,0x70,0x60        #movaps %xmm14, 0x60(%rax)
1456         .byte   0x44,0x0f,0x29,0x78,0x70        #movaps %xmm15, 0x70(%rax)
1457 ___
1458 $code.=<<___;
1459         movdqa  .LOne(%rip), $ONE
1460         movd    $index, $INDEX
1461
1462         pxor    $Ra, $Ra
1463         pxor    $Rb, $Rb
1464         pxor    $Rc, $Rc
1465         pxor    $Rd, $Rd
1466         pxor    $Re, $Re
1467         pxor    $Rf, $Rf
1468
1469         movdqa  $ONE, $M0
1470         pshufd  \$0, $INDEX, $INDEX
1471
1472         mov     \$16, %rax
1473 .Lselect_loop_sse_w5:
1474
1475         movdqa  $M0, $TMP0
1476         paddd   $ONE, $M0
1477         pcmpeqd $INDEX, $TMP0
1478
1479         movdqa  16*0($in_t), $T0a
1480         movdqa  16*1($in_t), $T0b
1481         movdqa  16*2($in_t), $T0c
1482         movdqa  16*3($in_t), $T0d
1483         movdqa  16*4($in_t), $T0e
1484         movdqa  16*5($in_t), $T0f
1485         lea 16*6($in_t), $in_t
1486
1487         pand    $TMP0, $T0a
1488         pand    $TMP0, $T0b
1489         por     $T0a, $Ra
1490         pand    $TMP0, $T0c
1491         por     $T0b, $Rb
1492         pand    $TMP0, $T0d
1493         por     $T0c, $Rc
1494         pand    $TMP0, $T0e
1495         por     $T0d, $Rd
1496         pand    $TMP0, $T0f
1497         por     $T0e, $Re
1498         por     $T0f, $Rf
1499
1500         dec     %rax
1501         jnz     .Lselect_loop_sse_w5
1502
1503         movdqu  $Ra, 16*0($val)
1504         movdqu  $Rb, 16*1($val)
1505         movdqu  $Rc, 16*2($val)
1506         movdqu  $Rd, 16*3($val)
1507         movdqu  $Re, 16*4($val)
1508         movdqu  $Rf, 16*5($val)
1509 ___
1510 $code.=<<___    if ($win64);
1511         movaps  (%rsp), %xmm6
1512         movaps  0x10(%rsp), %xmm7
1513         movaps  0x20(%rsp), %xmm8
1514         movaps  0x30(%rsp), %xmm9
1515         movaps  0x40(%rsp), %xmm10
1516         movaps  0x50(%rsp), %xmm11
1517         movaps  0x60(%rsp), %xmm12
1518         movaps  0x70(%rsp), %xmm13
1519         movaps  0x80(%rsp), %xmm14
1520         movaps  0x90(%rsp), %xmm15
1521         lea     0xa8(%rsp), %rsp
1522 ___
1523 $code.=<<___;
1524         ret
1525 .LSEH_end_ecp_nistz256_gather_w5:
1526 .size   ecp_nistz256_gather_w5,.-ecp_nistz256_gather_w5
1527
1528 ################################################################################
1529 # void ecp_nistz256_scatter_w7(uint64_t *val, uint64_t *in_t, int index);
1530 .globl  ecp_nistz256_scatter_w7
1531 .type   ecp_nistz256_scatter_w7,\@abi-omnipotent
1532 .align  32
1533 ecp_nistz256_scatter_w7:
1534         movdqu  0x00($in_t), %xmm0
1535         shl     \$6, $index
1536         movdqu  0x10($in_t), %xmm1
1537         movdqu  0x20($in_t), %xmm2
1538         movdqu  0x30($in_t), %xmm3
1539         movdqa  %xmm0, 0x00($val,$index)
1540         movdqa  %xmm1, 0x10($val,$index)
1541         movdqa  %xmm2, 0x20($val,$index)
1542         movdqa  %xmm3, 0x30($val,$index)
1543
1544         ret
1545 .size   ecp_nistz256_scatter_w7,.-ecp_nistz256_scatter_w7
1546
1547 ################################################################################
1548 # void ecp_nistz256_gather_w7(uint64_t *val, uint64_t *in_t, int index);
1549 .globl  ecp_nistz256_gather_w7
1550 .type   ecp_nistz256_gather_w7,\@abi-omnipotent
1551 .align  32
1552 ecp_nistz256_gather_w7:
1553 ___
1554 $code.=<<___    if ($avx>1);
1555         mov     OPENSSL_ia32cap_P+8(%rip), %eax
1556         test    \$`1<<5`, %eax
1557         jnz     .Lavx2_gather_w7
1558 ___
1559 $code.=<<___    if ($win64);
1560         lea     -0x88(%rsp), %rax
1561 .LSEH_begin_ecp_nistz256_gather_w7:
1562         .byte   0x48,0x8d,0x60,0xe0             #lea    -0x20(%rax), %rsp
1563         .byte   0x0f,0x29,0x70,0xe0             #movaps %xmm6, -0x20(%rax)
1564         .byte   0x0f,0x29,0x78,0xf0             #movaps %xmm7, -0x10(%rax)
1565         .byte   0x44,0x0f,0x29,0x00             #movaps %xmm8, 0(%rax)
1566         .byte   0x44,0x0f,0x29,0x48,0x10        #movaps %xmm9, 0x10(%rax)
1567         .byte   0x44,0x0f,0x29,0x50,0x20        #movaps %xmm10, 0x20(%rax)
1568         .byte   0x44,0x0f,0x29,0x58,0x30        #movaps %xmm11, 0x30(%rax)
1569         .byte   0x44,0x0f,0x29,0x60,0x40        #movaps %xmm12, 0x40(%rax)
1570         .byte   0x44,0x0f,0x29,0x68,0x50        #movaps %xmm13, 0x50(%rax)
1571         .byte   0x44,0x0f,0x29,0x70,0x60        #movaps %xmm14, 0x60(%rax)
1572         .byte   0x44,0x0f,0x29,0x78,0x70        #movaps %xmm15, 0x70(%rax)
1573 ___
1574 $code.=<<___;
1575         movdqa  .LOne(%rip), $M0
1576         movd    $index, $INDEX
1577
1578         pxor    $Ra, $Ra
1579         pxor    $Rb, $Rb
1580         pxor    $Rc, $Rc
1581         pxor    $Rd, $Rd
1582
1583         movdqa  $M0, $ONE
1584         pshufd  \$0, $INDEX, $INDEX
1585         mov     \$64, %rax
1586
1587 .Lselect_loop_sse_w7:
1588         movdqa  $M0, $TMP0
1589         paddd   $ONE, $M0
1590         movdqa  16*0($in_t), $T0a
1591         movdqa  16*1($in_t), $T0b
1592         pcmpeqd $INDEX, $TMP0
1593         movdqa  16*2($in_t), $T0c
1594         movdqa  16*3($in_t), $T0d
1595         lea     16*4($in_t), $in_t
1596
1597         pand    $TMP0, $T0a
1598         pand    $TMP0, $T0b
1599         por     $T0a, $Ra
1600         pand    $TMP0, $T0c
1601         por     $T0b, $Rb
1602         pand    $TMP0, $T0d
1603         por     $T0c, $Rc
1604         prefetcht0      255($in_t)
1605         por     $T0d, $Rd
1606
1607         dec     %rax
1608         jnz     .Lselect_loop_sse_w7
1609
1610         movdqu  $Ra, 16*0($val)
1611         movdqu  $Rb, 16*1($val)
1612         movdqu  $Rc, 16*2($val)
1613         movdqu  $Rd, 16*3($val)
1614 ___
1615 $code.=<<___    if ($win64);
1616         movaps  (%rsp), %xmm6
1617         movaps  0x10(%rsp), %xmm7
1618         movaps  0x20(%rsp), %xmm8
1619         movaps  0x30(%rsp), %xmm9
1620         movaps  0x40(%rsp), %xmm10
1621         movaps  0x50(%rsp), %xmm11
1622         movaps  0x60(%rsp), %xmm12
1623         movaps  0x70(%rsp), %xmm13
1624         movaps  0x80(%rsp), %xmm14
1625         movaps  0x90(%rsp), %xmm15
1626         lea     0xa8(%rsp), %rsp
1627 ___
1628 $code.=<<___;
1629         ret
1630 .LSEH_end_ecp_nistz256_gather_w7:
1631 .size   ecp_nistz256_gather_w7,.-ecp_nistz256_gather_w7
1632 ___
1633 }
1634 if ($avx>1) {
1635 my ($val,$in_t,$index)=$win64?("%rcx","%rdx","%r8d"):("%rdi","%rsi","%edx");
1636 my ($TWO,$INDEX,$Ra,$Rb,$Rc)=map("%ymm$_",(0..4));
1637 my ($M0,$T0a,$T0b,$T0c,$TMP0)=map("%ymm$_",(5..9));
1638 my ($M1,$T1a,$T1b,$T1c,$TMP1)=map("%ymm$_",(10..14));
1639
1640 $code.=<<___;
1641 ################################################################################
1642 # void ecp_nistz256_avx2_gather_w5(uint64_t *val, uint64_t *in_t, int index);
1643 .type   ecp_nistz256_avx2_gather_w5,\@abi-omnipotent
1644 .align  32
1645 ecp_nistz256_avx2_gather_w5:
1646 .Lavx2_gather_w5:
1647         vzeroupper
1648 ___
1649 $code.=<<___    if ($win64);
1650         lea     -0x88(%rsp), %rax
1651         mov     %rsp,%r11
1652 .LSEH_begin_ecp_nistz256_avx2_gather_w5:
1653         .byte   0x48,0x8d,0x60,0xe0             # lea   -0x20(%rax), %rsp
1654         .byte   0xc5,0xf8,0x29,0x70,0xe0        # vmovaps %xmm6, -0x20(%rax)
1655         .byte   0xc5,0xf8,0x29,0x78,0xf0        # vmovaps %xmm7, -0x10(%rax)
1656         .byte   0xc5,0x78,0x29,0x40,0x00        # vmovaps %xmm8, 8(%rax)
1657         .byte   0xc5,0x78,0x29,0x48,0x10        # vmovaps %xmm9, 0x10(%rax)
1658         .byte   0xc5,0x78,0x29,0x50,0x20        # vmovaps %xmm10, 0x20(%rax)
1659         .byte   0xc5,0x78,0x29,0x58,0x30        # vmovaps %xmm11, 0x30(%rax)
1660         .byte   0xc5,0x78,0x29,0x60,0x40        # vmovaps %xmm12, 0x40(%rax)
1661         .byte   0xc5,0x78,0x29,0x68,0x50        # vmovaps %xmm13, 0x50(%rax)
1662         .byte   0xc5,0x78,0x29,0x70,0x60        # vmovaps %xmm14, 0x60(%rax)
1663         .byte   0xc5,0x78,0x29,0x78,0x70        # vmovaps %xmm15, 0x70(%rax)
1664 ___
1665 $code.=<<___;
1666         vmovdqa .LTwo(%rip), $TWO
1667
1668         vpxor   $Ra, $Ra, $Ra
1669         vpxor   $Rb, $Rb, $Rb
1670         vpxor   $Rc, $Rc, $Rc
1671
1672         vmovdqa .LOne(%rip), $M0
1673         vmovdqa .LTwo(%rip), $M1
1674
1675         vmovd   $index, %xmm1
1676         vpermd  $INDEX, $Ra, $INDEX
1677
1678         mov     \$8, %rax
1679 .Lselect_loop_avx2_w5:
1680
1681         vmovdqa 32*0($in_t), $T0a
1682         vmovdqa 32*1($in_t), $T0b
1683         vmovdqa 32*2($in_t), $T0c
1684
1685         vmovdqa 32*3($in_t), $T1a
1686         vmovdqa 32*4($in_t), $T1b
1687         vmovdqa 32*5($in_t), $T1c
1688
1689         vpcmpeqd        $INDEX, $M0, $TMP0
1690         vpcmpeqd        $INDEX, $M1, $TMP1
1691
1692         vpaddd  $TWO, $M0, $M0
1693         vpaddd  $TWO, $M1, $M1
1694         lea     32*6($in_t), $in_t
1695
1696         vpand   $TMP0, $T0a, $T0a
1697         vpand   $TMP0, $T0b, $T0b
1698         vpand   $TMP0, $T0c, $T0c
1699         vpand   $TMP1, $T1a, $T1a
1700         vpand   $TMP1, $T1b, $T1b
1701         vpand   $TMP1, $T1c, $T1c
1702
1703         vpxor   $T0a, $Ra, $Ra
1704         vpxor   $T0b, $Rb, $Rb
1705         vpxor   $T0c, $Rc, $Rc
1706         vpxor   $T1a, $Ra, $Ra
1707         vpxor   $T1b, $Rb, $Rb
1708         vpxor   $T1c, $Rc, $Rc
1709
1710         dec %rax
1711         jnz .Lselect_loop_avx2_w5
1712
1713         vmovdqu $Ra, 32*0($val)
1714         vmovdqu $Rb, 32*1($val)
1715         vmovdqu $Rc, 32*2($val)
1716         vzeroupper
1717 ___
1718 $code.=<<___    if ($win64);
1719         movaps  (%rsp), %xmm6
1720         movaps  0x10(%rsp), %xmm7
1721         movaps  0x20(%rsp), %xmm8
1722         movaps  0x30(%rsp), %xmm9
1723         movaps  0x40(%rsp), %xmm10
1724         movaps  0x50(%rsp), %xmm11
1725         movaps  0x60(%rsp), %xmm12
1726         movaps  0x70(%rsp), %xmm13
1727         movaps  0x80(%rsp), %xmm14
1728         movaps  0x90(%rsp), %xmm15
1729         lea     (%r11), %rsp
1730 ___
1731 $code.=<<___;
1732         ret
1733 .LSEH_end_ecp_nistz256_avx2_gather_w5:
1734 .size   ecp_nistz256_avx2_gather_w5,.-ecp_nistz256_avx2_gather_w5
1735 ___
1736 }
1737 if ($avx>1) {
1738 my ($val,$in_t,$index)=$win64?("%rcx","%rdx","%r8d"):("%rdi","%rsi","%edx");
1739 my ($THREE,$INDEX,$Ra,$Rb)=map("%ymm$_",(0..3));
1740 my ($M0,$T0a,$T0b,$TMP0)=map("%ymm$_",(4..7));
1741 my ($M1,$T1a,$T1b,$TMP1)=map("%ymm$_",(8..11));
1742 my ($M2,$T2a,$T2b,$TMP2)=map("%ymm$_",(12..15));
1743
1744 $code.=<<___;
1745
1746 ################################################################################
1747 # void ecp_nistz256_avx2_gather_w7(uint64_t *val, uint64_t *in_t, int index);
1748 .globl  ecp_nistz256_avx2_gather_w7
1749 .type   ecp_nistz256_avx2_gather_w7,\@abi-omnipotent
1750 .align  32
1751 ecp_nistz256_avx2_gather_w7:
1752 .Lavx2_gather_w7:
1753         vzeroupper
1754 ___
1755 $code.=<<___    if ($win64);
1756         mov     %rsp,%r11
1757         lea     -0x88(%rsp), %rax
1758 .LSEH_begin_ecp_nistz256_avx2_gather_w7:
1759         .byte   0x48,0x8d,0x60,0xe0             # lea   -0x20(%rax), %rsp
1760         .byte   0xc5,0xf8,0x29,0x70,0xe0        # vmovaps %xmm6, -0x20(%rax)
1761         .byte   0xc5,0xf8,0x29,0x78,0xf0        # vmovaps %xmm7, -0x10(%rax)
1762         .byte   0xc5,0x78,0x29,0x40,0x00        # vmovaps %xmm8, 8(%rax)
1763         .byte   0xc5,0x78,0x29,0x48,0x10        # vmovaps %xmm9, 0x10(%rax)
1764         .byte   0xc5,0x78,0x29,0x50,0x20        # vmovaps %xmm10, 0x20(%rax)
1765         .byte   0xc5,0x78,0x29,0x58,0x30        # vmovaps %xmm11, 0x30(%rax)
1766         .byte   0xc5,0x78,0x29,0x60,0x40        # vmovaps %xmm12, 0x40(%rax)
1767         .byte   0xc5,0x78,0x29,0x68,0x50        # vmovaps %xmm13, 0x50(%rax)
1768         .byte   0xc5,0x78,0x29,0x70,0x60        # vmovaps %xmm14, 0x60(%rax)
1769         .byte   0xc5,0x78,0x29,0x78,0x70        # vmovaps %xmm15, 0x70(%rax)
1770 ___
1771 $code.=<<___;
1772         vmovdqa .LThree(%rip), $THREE
1773
1774         vpxor   $Ra, $Ra, $Ra
1775         vpxor   $Rb, $Rb, $Rb
1776
1777         vmovdqa .LOne(%rip), $M0
1778         vmovdqa .LTwo(%rip), $M1
1779         vmovdqa .LThree(%rip), $M2
1780
1781         vmovd   $index, %xmm1
1782         vpermd  $INDEX, $Ra, $INDEX
1783         # Skip index = 0, because it is implicitly the point at infinity
1784
1785         mov     \$21, %rax
1786 .Lselect_loop_avx2_w7:
1787
1788         vmovdqa 32*0($in_t), $T0a
1789         vmovdqa 32*1($in_t), $T0b
1790
1791         vmovdqa 32*2($in_t), $T1a
1792         vmovdqa 32*3($in_t), $T1b
1793
1794         vmovdqa 32*4($in_t), $T2a
1795         vmovdqa 32*5($in_t), $T2b
1796
1797         vpcmpeqd        $INDEX, $M0, $TMP0
1798         vpcmpeqd        $INDEX, $M1, $TMP1
1799         vpcmpeqd        $INDEX, $M2, $TMP2
1800
1801         vpaddd  $THREE, $M0, $M0
1802         vpaddd  $THREE, $M1, $M1
1803         vpaddd  $THREE, $M2, $M2
1804         lea     32*6($in_t), $in_t
1805
1806         vpand   $TMP0, $T0a, $T0a
1807         vpand   $TMP0, $T0b, $T0b
1808         vpand   $TMP1, $T1a, $T1a
1809         vpand   $TMP1, $T1b, $T1b
1810         vpand   $TMP2, $T2a, $T2a
1811         vpand   $TMP2, $T2b, $T2b
1812
1813         vpxor   $T0a, $Ra, $Ra
1814         vpxor   $T0b, $Rb, $Rb
1815         vpxor   $T1a, $Ra, $Ra
1816         vpxor   $T1b, $Rb, $Rb
1817         vpxor   $T2a, $Ra, $Ra
1818         vpxor   $T2b, $Rb, $Rb
1819
1820         dec %rax
1821         jnz .Lselect_loop_avx2_w7
1822
1823
1824         vmovdqa 32*0($in_t), $T0a
1825         vmovdqa 32*1($in_t), $T0b
1826
1827         vpcmpeqd        $INDEX, $M0, $TMP0
1828
1829         vpand   $TMP0, $T0a, $T0a
1830         vpand   $TMP0, $T0b, $T0b
1831
1832         vpxor   $T0a, $Ra, $Ra
1833         vpxor   $T0b, $Rb, $Rb
1834
1835         vmovdqu $Ra, 32*0($val)
1836         vmovdqu $Rb, 32*1($val)
1837         vzeroupper
1838 ___
1839 $code.=<<___    if ($win64);
1840         movaps  (%rsp), %xmm6
1841         movaps  0x10(%rsp), %xmm7
1842         movaps  0x20(%rsp), %xmm8
1843         movaps  0x30(%rsp), %xmm9
1844         movaps  0x40(%rsp), %xmm10
1845         movaps  0x50(%rsp), %xmm11
1846         movaps  0x60(%rsp), %xmm12
1847         movaps  0x70(%rsp), %xmm13
1848         movaps  0x80(%rsp), %xmm14
1849         movaps  0x90(%rsp), %xmm15
1850         lea     (%r11), %rsp
1851 ___
1852 $code.=<<___;
1853         ret
1854 .LSEH_end_ecp_nistz256_avx2_gather_w7:
1855 .size   ecp_nistz256_avx2_gather_w7,.-ecp_nistz256_avx2_gather_w7
1856 ___
1857 } else {
1858 $code.=<<___;
1859 .globl  ecp_nistz256_avx2_gather_w7
1860 .type   ecp_nistz256_avx2_gather_w7,\@function,3
1861 .align  32
1862 ecp_nistz256_avx2_gather_w7:
1863         .byte   0x0f,0x0b       # ud2
1864         ret
1865 .size   ecp_nistz256_avx2_gather_w7,.-ecp_nistz256_avx2_gather_w7
1866 ___
1867 }
1868 {{{
1869 ########################################################################
1870 # This block implements higher level point_double, point_add and
1871 # point_add_affine. The key to performance in this case is to allow
1872 # out-of-order execution logic to overlap computations from next step
1873 # with tail processing from current step. By using tailored calling
1874 # sequence we minimize inter-step overhead to give processor better
1875 # shot at overlapping operations...
1876 #
1877 # You will notice that input data is copied to stack. Trouble is that
1878 # there are no registers to spare for holding original pointers and
1879 # reloading them, pointers, would create undesired dependencies on
1880 # effective addresses calculation paths. In other words it's too done
1881 # to favour out-of-order execution logic.
1882 #                                               <appro@openssl.org>
1883
1884 my ($r_ptr,$a_ptr,$b_org,$b_ptr)=("%rdi","%rsi","%rdx","%rbx");
1885 my ($acc0,$acc1,$acc2,$acc3,$acc4,$acc5,$acc6,$acc7)=map("%r$_",(8..15));
1886 my ($t0,$t1,$t2,$t3,$t4)=("%rax","%rbp","%rcx",$acc4,$acc4);
1887 my ($poly1,$poly3)=($acc6,$acc7);
1888
1889 sub load_for_mul () {
1890 my ($a,$b,$src0) = @_;
1891 my $bias = $src0 eq "%rax" ? 0 : -128;
1892
1893 "       mov     $b, $src0
1894         lea     $b, $b_ptr
1895         mov     8*0+$a, $acc1
1896         mov     8*1+$a, $acc2
1897         lea     $bias+$a, $a_ptr
1898         mov     8*2+$a, $acc3
1899         mov     8*3+$a, $acc4"
1900 }
1901
1902 sub load_for_sqr () {
1903 my ($a,$src0) = @_;
1904 my $bias = $src0 eq "%rax" ? 0 : -128;
1905
1906 "       mov     8*0+$a, $src0
1907         mov     8*1+$a, $acc6
1908         lea     $bias+$a, $a_ptr
1909         mov     8*2+$a, $acc7
1910         mov     8*3+$a, $acc0"
1911 }
1912
1913                                                                         {
1914 ########################################################################
1915 # operate in 4-5-0-1 "name space" that matches multiplication output
1916 #
1917 my ($a0,$a1,$a2,$a3,$t3,$t4)=($acc4,$acc5,$acc0,$acc1,$acc2,$acc3);
1918
1919 $code.=<<___;
1920 .type   __ecp_nistz256_add_toq,\@abi-omnipotent
1921 .align  32
1922 __ecp_nistz256_add_toq:
1923         xor     $t4,$t4
1924         add     8*0($b_ptr), $a0
1925         adc     8*1($b_ptr), $a1
1926          mov    $a0, $t0
1927         adc     8*2($b_ptr), $a2
1928         adc     8*3($b_ptr), $a3
1929          mov    $a1, $t1
1930         adc     \$0, $t4
1931
1932         sub     \$-1, $a0
1933          mov    $a2, $t2
1934         sbb     $poly1, $a1
1935         sbb     \$0, $a2
1936          mov    $a3, $t3
1937         sbb     $poly3, $a3
1938         sbb     \$0, $t4
1939
1940         cmovc   $t0, $a0
1941         cmovc   $t1, $a1
1942         mov     $a0, 8*0($r_ptr)
1943         cmovc   $t2, $a2
1944         mov     $a1, 8*1($r_ptr)
1945         cmovc   $t3, $a3
1946         mov     $a2, 8*2($r_ptr)
1947         mov     $a3, 8*3($r_ptr)
1948
1949         ret
1950 .size   __ecp_nistz256_add_toq,.-__ecp_nistz256_add_toq
1951
1952 .type   __ecp_nistz256_sub_fromq,\@abi-omnipotent
1953 .align  32
1954 __ecp_nistz256_sub_fromq:
1955         sub     8*0($b_ptr), $a0
1956         sbb     8*1($b_ptr), $a1
1957          mov    $a0, $t0
1958         sbb     8*2($b_ptr), $a2
1959         sbb     8*3($b_ptr), $a3
1960          mov    $a1, $t1
1961         sbb     $t4, $t4
1962
1963         add     \$-1, $a0
1964          mov    $a2, $t2
1965         adc     $poly1, $a1
1966         adc     \$0, $a2
1967          mov    $a3, $t3
1968         adc     $poly3, $a3
1969         test    $t4, $t4
1970
1971         cmovz   $t0, $a0
1972         cmovz   $t1, $a1
1973         mov     $a0, 8*0($r_ptr)
1974         cmovz   $t2, $a2
1975         mov     $a1, 8*1($r_ptr)
1976         cmovz   $t3, $a3
1977         mov     $a2, 8*2($r_ptr)
1978         mov     $a3, 8*3($r_ptr)
1979
1980         ret
1981 .size   __ecp_nistz256_sub_fromq,.-__ecp_nistz256_sub_fromq
1982
1983 .type   __ecp_nistz256_subq,\@abi-omnipotent
1984 .align  32
1985 __ecp_nistz256_subq:
1986         sub     $a0, $t0
1987         sbb     $a1, $t1
1988          mov    $t0, $a0
1989         sbb     $a2, $t2
1990         sbb     $a3, $t3
1991          mov    $t1, $a1
1992         sbb     $t4, $t4
1993
1994         add     \$-1, $t0
1995          mov    $t2, $a2
1996         adc     $poly1, $t1
1997         adc     \$0, $t2
1998          mov    $t3, $a3
1999         adc     $poly3, $t3
2000         test    $t4, $t4
2001
2002         cmovnz  $t0, $a0
2003         cmovnz  $t1, $a1
2004         cmovnz  $t2, $a2
2005         cmovnz  $t3, $a3
2006
2007         ret
2008 .size   __ecp_nistz256_subq,.-__ecp_nistz256_subq
2009
2010 .type   __ecp_nistz256_mul_by_2q,\@abi-omnipotent
2011 .align  32
2012 __ecp_nistz256_mul_by_2q:
2013         xor     $t4, $t4
2014         add     $a0, $a0                # a0:a3+a0:a3
2015         adc     $a1, $a1
2016          mov    $a0, $t0
2017         adc     $a2, $a2
2018         adc     $a3, $a3
2019          mov    $a1, $t1
2020         adc     \$0, $t4
2021
2022         sub     \$-1, $a0
2023          mov    $a2, $t2
2024         sbb     $poly1, $a1
2025         sbb     \$0, $a2
2026          mov    $a3, $t3
2027         sbb     $poly3, $a3
2028         sbb     \$0, $t4
2029
2030         cmovc   $t0, $a0
2031         cmovc   $t1, $a1
2032         mov     $a0, 8*0($r_ptr)
2033         cmovc   $t2, $a2
2034         mov     $a1, 8*1($r_ptr)
2035         cmovc   $t3, $a3
2036         mov     $a2, 8*2($r_ptr)
2037         mov     $a3, 8*3($r_ptr)
2038
2039         ret
2040 .size   __ecp_nistz256_mul_by_2q,.-__ecp_nistz256_mul_by_2q
2041 ___
2042                                                                         }
2043 sub gen_double () {
2044     my $x = shift;
2045     my ($src0,$sfx,$bias);
2046     my ($S,$M,$Zsqr,$in_x,$tmp0)=map(32*$_,(0..4));
2047
2048     if ($x ne "x") {
2049         $src0 = "%rax";
2050         $sfx  = "";
2051         $bias = 0;
2052
2053 $code.=<<___;
2054 .globl  ecp_nistz256_point_double
2055 .type   ecp_nistz256_point_double,\@function,2
2056 .align  32
2057 ecp_nistz256_point_double:
2058 ___
2059 $code.=<<___    if ($addx);
2060         mov     \$0x80100, %ecx
2061         and     OPENSSL_ia32cap_P+8(%rip), %ecx
2062         cmp     \$0x80100, %ecx
2063         je      .Lpoint_doublex
2064 ___
2065     } else {
2066         $src0 = "%rdx";
2067         $sfx  = "x";
2068         $bias = 128;
2069
2070 $code.=<<___;
2071 .type   ecp_nistz256_point_doublex,\@function,2
2072 .align  32
2073 ecp_nistz256_point_doublex:
2074 .Lpoint_doublex:
2075 ___
2076     }
2077 $code.=<<___;
2078         push    %rbp
2079         push    %rbx
2080         push    %r12
2081         push    %r13
2082         push    %r14
2083         push    %r15
2084         sub     \$32*5+8, %rsp
2085 .Lpoint_double${x}_body:
2086
2087 .Lpoint_double_shortcut$x:
2088         movdqu  0x00($a_ptr), %xmm0             # copy  *(P256_POINT *)$a_ptr.x
2089         mov     $a_ptr, $b_ptr                  # backup copy
2090         movdqu  0x10($a_ptr), %xmm1
2091          mov    0x20+8*0($a_ptr), $acc4         # load in_y in "5-4-0-1" order
2092          mov    0x20+8*1($a_ptr), $acc5
2093          mov    0x20+8*2($a_ptr), $acc0
2094          mov    0x20+8*3($a_ptr), $acc1
2095          mov    .Lpoly+8*1(%rip), $poly1
2096          mov    .Lpoly+8*3(%rip), $poly3
2097         movdqa  %xmm0, $in_x(%rsp)
2098         movdqa  %xmm1, $in_x+0x10(%rsp)
2099         lea     0x20($r_ptr), $acc2
2100         lea     0x40($r_ptr), $acc3
2101         movq    $r_ptr, %xmm0
2102         movq    $acc2, %xmm1
2103         movq    $acc3, %xmm2
2104
2105         lea     $S(%rsp), $r_ptr
2106         call    __ecp_nistz256_mul_by_2$x       # p256_mul_by_2(S, in_y);
2107
2108         mov     0x40+8*0($a_ptr), $src0
2109         mov     0x40+8*1($a_ptr), $acc6
2110         mov     0x40+8*2($a_ptr), $acc7
2111         mov     0x40+8*3($a_ptr), $acc0
2112         lea     0x40-$bias($a_ptr), $a_ptr
2113         lea     $Zsqr(%rsp), $r_ptr
2114         call    __ecp_nistz256_sqr_mont$x       # p256_sqr_mont(Zsqr, in_z);
2115
2116         `&load_for_sqr("$S(%rsp)", "$src0")`
2117         lea     $S(%rsp), $r_ptr
2118         call    __ecp_nistz256_sqr_mont$x       # p256_sqr_mont(S, S);
2119
2120         mov     0x20($b_ptr), $src0             # $b_ptr is still valid
2121         mov     0x40+8*0($b_ptr), $acc1
2122         mov     0x40+8*1($b_ptr), $acc2
2123         mov     0x40+8*2($b_ptr), $acc3
2124         mov     0x40+8*3($b_ptr), $acc4
2125         lea     0x40-$bias($b_ptr), $a_ptr
2126         lea     0x20($b_ptr), $b_ptr
2127         movq    %xmm2, $r_ptr
2128         call    __ecp_nistz256_mul_mont$x       # p256_mul_mont(res_z, in_z, in_y);
2129         call    __ecp_nistz256_mul_by_2$x       # p256_mul_by_2(res_z, res_z);
2130
2131         mov     $in_x+8*0(%rsp), $acc4          # "5-4-0-1" order
2132         mov     $in_x+8*1(%rsp), $acc5
2133         lea     $Zsqr(%rsp), $b_ptr
2134         mov     $in_x+8*2(%rsp), $acc0
2135         mov     $in_x+8*3(%rsp), $acc1
2136         lea     $M(%rsp), $r_ptr
2137         call    __ecp_nistz256_add_to$x         # p256_add(M, in_x, Zsqr);
2138
2139         mov     $in_x+8*0(%rsp), $acc4          # "5-4-0-1" order
2140         mov     $in_x+8*1(%rsp), $acc5
2141         lea     $Zsqr(%rsp), $b_ptr
2142         mov     $in_x+8*2(%rsp), $acc0
2143         mov     $in_x+8*3(%rsp), $acc1
2144         lea     $Zsqr(%rsp), $r_ptr
2145         call    __ecp_nistz256_sub_from$x       # p256_sub(Zsqr, in_x, Zsqr);
2146
2147         `&load_for_sqr("$S(%rsp)", "$src0")`
2148         movq    %xmm1, $r_ptr
2149         call    __ecp_nistz256_sqr_mont$x       # p256_sqr_mont(res_y, S);
2150 ___
2151 {
2152 ######## ecp_nistz256_div_by_2(res_y, res_y); ##########################
2153 # operate in 4-5-6-7 "name space" that matches squaring output
2154 #
2155 my ($poly1,$poly3)=($a_ptr,$t1);
2156 my ($a0,$a1,$a2,$a3,$t3,$t4,$t1)=($acc4,$acc5,$acc6,$acc7,$acc0,$acc1,$acc2);
2157
2158 $code.=<<___;
2159         xor     $t4, $t4
2160         mov     $a0, $t0
2161         add     \$-1, $a0
2162         mov     $a1, $t1
2163         adc     $poly1, $a1
2164         mov     $a2, $t2
2165         adc     \$0, $a2
2166         mov     $a3, $t3
2167         adc     $poly3, $a3
2168         adc     \$0, $t4
2169         xor     $a_ptr, $a_ptr          # borrow $a_ptr
2170         test    \$1, $t0
2171
2172         cmovz   $t0, $a0
2173         cmovz   $t1, $a1
2174         cmovz   $t2, $a2
2175         cmovz   $t3, $a3
2176         cmovz   $a_ptr, $t4
2177
2178         mov     $a1, $t0                # a0:a3>>1
2179         shr     \$1, $a0
2180         shl     \$63, $t0
2181         mov     $a2, $t1
2182         shr     \$1, $a1
2183         or      $t0, $a0
2184         shl     \$63, $t1
2185         mov     $a3, $t2
2186         shr     \$1, $a2
2187         or      $t1, $a1
2188         shl     \$63, $t2
2189         mov     $a0, 8*0($r_ptr)
2190         shr     \$1, $a3
2191         mov     $a1, 8*1($r_ptr)
2192         shl     \$63, $t4
2193         or      $t2, $a2
2194         or      $t4, $a3
2195         mov     $a2, 8*2($r_ptr)
2196         mov     $a3, 8*3($r_ptr)
2197 ___
2198 }
2199 $code.=<<___;
2200         `&load_for_mul("$M(%rsp)", "$Zsqr(%rsp)", "$src0")`
2201         lea     $M(%rsp), $r_ptr
2202         call    __ecp_nistz256_mul_mont$x       # p256_mul_mont(M, M, Zsqr);
2203
2204         lea     $tmp0(%rsp), $r_ptr
2205         call    __ecp_nistz256_mul_by_2$x
2206
2207         lea     $M(%rsp), $b_ptr
2208         lea     $M(%rsp), $r_ptr
2209         call    __ecp_nistz256_add_to$x         # p256_mul_by_3(M, M);
2210
2211         `&load_for_mul("$S(%rsp)", "$in_x(%rsp)", "$src0")`
2212         lea     $S(%rsp), $r_ptr
2213         call    __ecp_nistz256_mul_mont$x       # p256_mul_mont(S, S, in_x);
2214
2215         lea     $tmp0(%rsp), $r_ptr
2216         call    __ecp_nistz256_mul_by_2$x       # p256_mul_by_2(tmp0, S);
2217
2218         `&load_for_sqr("$M(%rsp)", "$src0")`
2219         movq    %xmm0, $r_ptr
2220         call    __ecp_nistz256_sqr_mont$x       # p256_sqr_mont(res_x, M);
2221
2222         lea     $tmp0(%rsp), $b_ptr
2223         mov     $acc6, $acc0                    # harmonize sqr output and sub input
2224         mov     $acc7, $acc1
2225         mov     $a_ptr, $poly1
2226         mov     $t1, $poly3
2227         call    __ecp_nistz256_sub_from$x       # p256_sub(res_x, res_x, tmp0);
2228
2229         mov     $S+8*0(%rsp), $t0
2230         mov     $S+8*1(%rsp), $t1
2231         mov     $S+8*2(%rsp), $t2
2232         mov     $S+8*3(%rsp), $acc2             # "4-5-0-1" order
2233         lea     $S(%rsp), $r_ptr
2234         call    __ecp_nistz256_sub$x            # p256_sub(S, S, res_x);
2235
2236         mov     $M(%rsp), $src0
2237         lea     $M(%rsp), $b_ptr
2238         mov     $acc4, $acc6                    # harmonize sub output and mul input
2239         xor     %ecx, %ecx
2240         mov     $acc4, $S+8*0(%rsp)             # have to save:-(
2241         mov     $acc5, $acc2
2242         mov     $acc5, $S+8*1(%rsp)
2243         cmovz   $acc0, $acc3
2244         mov     $acc0, $S+8*2(%rsp)
2245         lea     $S-$bias(%rsp), $a_ptr
2246         cmovz   $acc1, $acc4
2247         mov     $acc1, $S+8*3(%rsp)
2248         mov     $acc6, $acc1
2249         lea     $S(%rsp), $r_ptr
2250         call    __ecp_nistz256_mul_mont$x       # p256_mul_mont(S, S, M);
2251
2252         movq    %xmm1, $b_ptr
2253         movq    %xmm1, $r_ptr
2254         call    __ecp_nistz256_sub_from$x       # p256_sub(res_y, S, res_y);
2255
2256         lea     32*5+56(%rsp), %rsi
2257         mov     -48(%rsi),%r15
2258         mov     -40(%rsi),%r14
2259         mov     -32(%rsi),%r13
2260         mov     -24(%rsi),%r12
2261         mov     -16(%rsi),%rbx
2262         mov     -8(%rsi),%rbp
2263         lea     (%rsi),%rsp
2264 .Lpoint_double${x}_epilogue:
2265         ret
2266 .size   ecp_nistz256_point_double$sfx,.-ecp_nistz256_point_double$sfx
2267 ___
2268 }
2269 &gen_double("q");
2270
2271 sub gen_add () {
2272     my $x = shift;
2273     my ($src0,$sfx,$bias);
2274     my ($H,$Hsqr,$R,$Rsqr,$Hcub,
2275         $U1,$U2,$S1,$S2,
2276         $res_x,$res_y,$res_z,
2277         $in1_x,$in1_y,$in1_z,
2278         $in2_x,$in2_y,$in2_z)=map(32*$_,(0..17));
2279     my ($Z1sqr, $Z2sqr) = ($Hsqr, $Rsqr);
2280
2281     if ($x ne "x") {
2282         $src0 = "%rax";
2283         $sfx  = "";
2284         $bias = 0;
2285
2286 $code.=<<___;
2287 .globl  ecp_nistz256_point_add
2288 .type   ecp_nistz256_point_add,\@function,3
2289 .align  32
2290 ecp_nistz256_point_add:
2291 ___
2292 $code.=<<___    if ($addx);
2293         mov     \$0x80100, %ecx
2294         and     OPENSSL_ia32cap_P+8(%rip), %ecx
2295         cmp     \$0x80100, %ecx
2296         je      .Lpoint_addx
2297 ___
2298     } else {
2299         $src0 = "%rdx";
2300         $sfx  = "x";
2301         $bias = 128;
2302
2303 $code.=<<___;
2304 .type   ecp_nistz256_point_addx,\@function,3
2305 .align  32
2306 ecp_nistz256_point_addx:
2307 .Lpoint_addx:
2308 ___
2309     }
2310 $code.=<<___;
2311         push    %rbp
2312         push    %rbx
2313         push    %r12
2314         push    %r13
2315         push    %r14
2316         push    %r15
2317         sub     \$32*18+8, %rsp
2318 .Lpoint_add${x}_body:
2319
2320         movdqu  0x00($a_ptr), %xmm0             # copy  *(P256_POINT *)$a_ptr
2321         movdqu  0x10($a_ptr), %xmm1
2322         movdqu  0x20($a_ptr), %xmm2
2323         movdqu  0x30($a_ptr), %xmm3
2324         movdqu  0x40($a_ptr), %xmm4
2325         movdqu  0x50($a_ptr), %xmm5
2326         mov     $a_ptr, $b_ptr                  # reassign
2327         mov     $b_org, $a_ptr                  # reassign
2328         movdqa  %xmm0, $in1_x(%rsp)
2329         movdqa  %xmm1, $in1_x+0x10(%rsp)
2330         movdqa  %xmm2, $in1_y(%rsp)
2331         movdqa  %xmm3, $in1_y+0x10(%rsp)
2332         movdqa  %xmm4, $in1_z(%rsp)
2333         movdqa  %xmm5, $in1_z+0x10(%rsp)
2334         por     %xmm4, %xmm5
2335
2336         movdqu  0x00($a_ptr), %xmm0             # copy  *(P256_POINT *)$b_ptr
2337          pshufd \$0xb1, %xmm5, %xmm3
2338         movdqu  0x10($a_ptr), %xmm1
2339         movdqu  0x20($a_ptr), %xmm2
2340          por    %xmm3, %xmm5
2341         movdqu  0x30($a_ptr), %xmm3
2342          mov    0x40+8*0($a_ptr), $src0         # load original in2_z
2343          mov    0x40+8*1($a_ptr), $acc6
2344          mov    0x40+8*2($a_ptr), $acc7
2345          mov    0x40+8*3($a_ptr), $acc0
2346         movdqa  %xmm0, $in2_x(%rsp)
2347          pshufd \$0x1e, %xmm5, %xmm4
2348         movdqa  %xmm1, $in2_x+0x10(%rsp)
2349         movdqu  0x40($a_ptr),%xmm0              # in2_z again
2350         movdqu  0x50($a_ptr),%xmm1
2351         movdqa  %xmm2, $in2_y(%rsp)
2352         movdqa  %xmm3, $in2_y+0x10(%rsp)
2353          por    %xmm4, %xmm5
2354          pxor   %xmm4, %xmm4
2355         por     %xmm0, %xmm1
2356          movq   $r_ptr, %xmm0                   # save $r_ptr
2357
2358         lea     0x40-$bias($a_ptr), $a_ptr      # $a_ptr is still valid
2359          mov    $src0, $in2_z+8*0(%rsp)         # make in2_z copy
2360          mov    $acc6, $in2_z+8*1(%rsp)
2361          mov    $acc7, $in2_z+8*2(%rsp)
2362          mov    $acc0, $in2_z+8*3(%rsp)
2363         lea     $Z2sqr(%rsp), $r_ptr            # Z2^2
2364         call    __ecp_nistz256_sqr_mont$x       # p256_sqr_mont(Z2sqr, in2_z);
2365
2366         pcmpeqd %xmm4, %xmm5
2367         pshufd  \$0xb1, %xmm1, %xmm4
2368         por     %xmm1, %xmm4
2369         pshufd  \$0, %xmm5, %xmm5               # in1infty
2370         pshufd  \$0x1e, %xmm4, %xmm3
2371         por     %xmm3, %xmm4
2372         pxor    %xmm3, %xmm3
2373         pcmpeqd %xmm3, %xmm4
2374         pshufd  \$0, %xmm4, %xmm4               # in2infty
2375          mov    0x40+8*0($b_ptr), $src0         # load original in1_z
2376          mov    0x40+8*1($b_ptr), $acc6
2377          mov    0x40+8*2($b_ptr), $acc7
2378          mov    0x40+8*3($b_ptr), $acc0
2379         movq    $b_ptr, %xmm1
2380
2381         lea     0x40-$bias($b_ptr), $a_ptr
2382         lea     $Z1sqr(%rsp), $r_ptr            # Z1^2
2383         call    __ecp_nistz256_sqr_mont$x       # p256_sqr_mont(Z1sqr, in1_z);
2384
2385         `&load_for_mul("$Z2sqr(%rsp)", "$in2_z(%rsp)", "$src0")`
2386         lea     $S1(%rsp), $r_ptr               # S1 = Z2^3
2387         call    __ecp_nistz256_mul_mont$x       # p256_mul_mont(S1, Z2sqr, in2_z);
2388
2389         `&load_for_mul("$Z1sqr(%rsp)", "$in1_z(%rsp)", "$src0")`
2390         lea     $S2(%rsp), $r_ptr               # S2 = Z1^3
2391         call    __ecp_nistz256_mul_mont$x       # p256_mul_mont(S2, Z1sqr, in1_z);
2392
2393         `&load_for_mul("$S1(%rsp)", "$in1_y(%rsp)", "$src0")`
2394         lea     $S1(%rsp), $r_ptr               # S1 = Y1*Z2^3
2395         call    __ecp_nistz256_mul_mont$x       # p256_mul_mont(S1, S1, in1_y);
2396
2397         `&load_for_mul("$S2(%rsp)", "$in2_y(%rsp)", "$src0")`
2398         lea     $S2(%rsp), $r_ptr               # S2 = Y2*Z1^3
2399         call    __ecp_nistz256_mul_mont$x       # p256_mul_mont(S2, S2, in2_y);
2400
2401         lea     $S1(%rsp), $b_ptr
2402         lea     $R(%rsp), $r_ptr                # R = S2 - S1
2403         call    __ecp_nistz256_sub_from$x       # p256_sub(R, S2, S1);
2404
2405         or      $acc5, $acc4                    # see if result is zero
2406         movdqa  %xmm4, %xmm2
2407         or      $acc0, $acc4
2408         or      $acc1, $acc4
2409         por     %xmm5, %xmm2                    # in1infty || in2infty
2410         movq    $acc4, %xmm3
2411
2412         `&load_for_mul("$Z2sqr(%rsp)", "$in1_x(%rsp)", "$src0")`
2413         lea     $U1(%rsp), $r_ptr               # U1 = X1*Z2^2
2414         call    __ecp_nistz256_mul_mont$x       # p256_mul_mont(U1, in1_x, Z2sqr);
2415
2416         `&load_for_mul("$Z1sqr(%rsp)", "$in2_x(%rsp)", "$src0")`
2417         lea     $U2(%rsp), $r_ptr               # U2 = X2*Z1^2
2418         call    __ecp_nistz256_mul_mont$x       # p256_mul_mont(U2, in2_x, Z1sqr);
2419
2420         lea     $U1(%rsp), $b_ptr
2421         lea     $H(%rsp), $r_ptr                # H = U2 - U1
2422         call    __ecp_nistz256_sub_from$x       # p256_sub(H, U2, U1);
2423
2424         or      $acc5, $acc4                    # see if result is zero
2425         or      $acc0, $acc4
2426         or      $acc1, $acc4
2427
2428         .byte   0x3e                            # predict taken
2429         jnz     .Ladd_proceed$x                 # is_equal(U1,U2)?
2430         movq    %xmm2, $acc0
2431         movq    %xmm3, $acc1
2432         test    $acc0, $acc0
2433         jnz     .Ladd_proceed$x                 # (in1infty || in2infty)?
2434         test    $acc1, $acc1
2435         jz      .Ladd_double$x                  # is_equal(S1,S2)?
2436
2437         movq    %xmm0, $r_ptr                   # restore $r_ptr
2438         pxor    %xmm0, %xmm0
2439         movdqu  %xmm0, 0x00($r_ptr)
2440         movdqu  %xmm0, 0x10($r_ptr)
2441         movdqu  %xmm0, 0x20($r_ptr)
2442         movdqu  %xmm0, 0x30($r_ptr)
2443         movdqu  %xmm0, 0x40($r_ptr)
2444         movdqu  %xmm0, 0x50($r_ptr)
2445         jmp     .Ladd_done$x
2446
2447 .align  32
2448 .Ladd_double$x:
2449         movq    %xmm1, $a_ptr                   # restore $a_ptr
2450         movq    %xmm0, $r_ptr                   # restore $r_ptr
2451         add     \$`32*(18-5)`, %rsp             # difference in frame sizes
2452         jmp     .Lpoint_double_shortcut$x
2453
2454 .align  32
2455 .Ladd_proceed$x:
2456         `&load_for_sqr("$R(%rsp)", "$src0")`
2457         lea     $Rsqr(%rsp), $r_ptr             # R^2
2458         call    __ecp_nistz256_sqr_mont$x       # p256_sqr_mont(Rsqr, R);
2459
2460         `&load_for_mul("$H(%rsp)", "$in1_z(%rsp)", "$src0")`
2461         lea     $res_z(%rsp), $r_ptr            # Z3 = H*Z1*Z2
2462         call    __ecp_nistz256_mul_mont$x       # p256_mul_mont(res_z, H, in1_z);
2463
2464         `&load_for_sqr("$H(%rsp)", "$src0")`
2465         lea     $Hsqr(%rsp), $r_ptr             # H^2
2466         call    __ecp_nistz256_sqr_mont$x       # p256_sqr_mont(Hsqr, H);
2467
2468         `&load_for_mul("$res_z(%rsp)", "$in2_z(%rsp)", "$src0")`
2469         lea     $res_z(%rsp), $r_ptr            # Z3 = H*Z1*Z2
2470         call    __ecp_nistz256_mul_mont$x       # p256_mul_mont(res_z, res_z, in2_z);
2471
2472         `&load_for_mul("$Hsqr(%rsp)", "$H(%rsp)", "$src0")`
2473         lea     $Hcub(%rsp), $r_ptr             # H^3
2474         call    __ecp_nistz256_mul_mont$x       # p256_mul_mont(Hcub, Hsqr, H);
2475
2476         `&load_for_mul("$Hsqr(%rsp)", "$U1(%rsp)", "$src0")`
2477         lea     $U2(%rsp), $r_ptr               # U1*H^2
2478         call    __ecp_nistz256_mul_mont$x       # p256_mul_mont(U2, U1, Hsqr);
2479 ___
2480 {
2481 #######################################################################
2482 # operate in 4-5-0-1 "name space" that matches multiplication output
2483 #
2484 my ($acc0,$acc1,$acc2,$acc3,$t3,$t4)=($acc4,$acc5,$acc0,$acc1,$acc2,$acc3);
2485 my ($poly1, $poly3)=($acc6,$acc7);
2486
2487 $code.=<<___;
2488         #lea    $U2(%rsp), $a_ptr
2489         #lea    $Hsqr(%rsp), $r_ptr     # 2*U1*H^2
2490         #call   __ecp_nistz256_mul_by_2 # ecp_nistz256_mul_by_2(Hsqr, U2);
2491
2492         xor     $t4, $t4
2493         add     $acc0, $acc0            # a0:a3+a0:a3
2494         lea     $Rsqr(%rsp), $a_ptr
2495         adc     $acc1, $acc1
2496          mov    $acc0, $t0
2497         adc     $acc2, $acc2
2498         adc     $acc3, $acc3
2499          mov    $acc1, $t1
2500         adc     \$0, $t4
2501
2502         sub     \$-1, $acc0
2503          mov    $acc2, $t2
2504         sbb     $poly1, $acc1
2505         sbb     \$0, $acc2
2506          mov    $acc3, $t3
2507         sbb     $poly3, $acc3
2508         sbb     \$0, $t4
2509
2510         cmovc   $t0, $acc0
2511         mov     8*0($a_ptr), $t0
2512         cmovc   $t1, $acc1
2513         mov     8*1($a_ptr), $t1
2514         cmovc   $t2, $acc2
2515         mov     8*2($a_ptr), $t2
2516         cmovc   $t3, $acc3
2517         mov     8*3($a_ptr), $t3
2518
2519         call    __ecp_nistz256_sub$x            # p256_sub(res_x, Rsqr, Hsqr);
2520
2521         lea     $Hcub(%rsp), $b_ptr
2522         lea     $res_x(%rsp), $r_ptr
2523         call    __ecp_nistz256_sub_from$x       # p256_sub(res_x, res_x, Hcub);
2524
2525         mov     $U2+8*0(%rsp), $t0
2526         mov     $U2+8*1(%rsp), $t1
2527         mov     $U2+8*2(%rsp), $t2
2528         mov     $U2+8*3(%rsp), $t3
2529         lea     $res_y(%rsp), $r_ptr
2530
2531         call    __ecp_nistz256_sub$x            # p256_sub(res_y, U2, res_x);
2532
2533         mov     $acc0, 8*0($r_ptr)              # save the result, as
2534         mov     $acc1, 8*1($r_ptr)              # __ecp_nistz256_sub doesn't
2535         mov     $acc2, 8*2($r_ptr)
2536         mov     $acc3, 8*3($r_ptr)
2537 ___
2538 }
2539 $code.=<<___;
2540         `&load_for_mul("$S1(%rsp)", "$Hcub(%rsp)", "$src0")`
2541         lea     $S2(%rsp), $r_ptr
2542         call    __ecp_nistz256_mul_mont$x       # p256_mul_mont(S2, S1, Hcub);
2543
2544         `&load_for_mul("$R(%rsp)", "$res_y(%rsp)", "$src0")`
2545         lea     $res_y(%rsp), $r_ptr
2546         call    __ecp_nistz256_mul_mont$x       # p256_mul_mont(res_y, R, res_y);
2547
2548         lea     $S2(%rsp), $b_ptr
2549         lea     $res_y(%rsp), $r_ptr
2550         call    __ecp_nistz256_sub_from$x       # p256_sub(res_y, res_y, S2);
2551
2552         movq    %xmm0, $r_ptr           # restore $r_ptr
2553
2554         movdqa  %xmm5, %xmm0            # copy_conditional(res_z, in2_z, in1infty);
2555         movdqa  %xmm5, %xmm1
2556         pandn   $res_z(%rsp), %xmm0
2557         movdqa  %xmm5, %xmm2
2558         pandn   $res_z+0x10(%rsp), %xmm1
2559         movdqa  %xmm5, %xmm3
2560         pand    $in2_z(%rsp), %xmm2
2561         pand    $in2_z+0x10(%rsp), %xmm3
2562         por     %xmm0, %xmm2
2563         por     %xmm1, %xmm3
2564
2565         movdqa  %xmm4, %xmm0            # copy_conditional(res_z, in1_z, in2infty);
2566         movdqa  %xmm4, %xmm1
2567         pandn   %xmm2, %xmm0
2568         movdqa  %xmm4, %xmm2
2569         pandn   %xmm3, %xmm1
2570         movdqa  %xmm4, %xmm3
2571         pand    $in1_z(%rsp), %xmm2
2572         pand    $in1_z+0x10(%rsp), %xmm3
2573         por     %xmm0, %xmm2
2574         por     %xmm1, %xmm3
2575         movdqu  %xmm2, 0x40($r_ptr)
2576         movdqu  %xmm3, 0x50($r_ptr)
2577
2578         movdqa  %xmm5, %xmm0            # copy_conditional(res_x, in2_x, in1infty);
2579         movdqa  %xmm5, %xmm1
2580         pandn   $res_x(%rsp), %xmm0
2581         movdqa  %xmm5, %xmm2
2582         pandn   $res_x+0x10(%rsp), %xmm1
2583         movdqa  %xmm5, %xmm3
2584         pand    $in2_x(%rsp), %xmm2
2585         pand    $in2_x+0x10(%rsp), %xmm3
2586         por     %xmm0, %xmm2
2587         por     %xmm1, %xmm3
2588
2589         movdqa  %xmm4, %xmm0            # copy_conditional(res_x, in1_x, in2infty);
2590         movdqa  %xmm4, %xmm1
2591         pandn   %xmm2, %xmm0
2592         movdqa  %xmm4, %xmm2
2593         pandn   %xmm3, %xmm1
2594         movdqa  %xmm4, %xmm3
2595         pand    $in1_x(%rsp), %xmm2
2596         pand    $in1_x+0x10(%rsp), %xmm3
2597         por     %xmm0, %xmm2
2598         por     %xmm1, %xmm3
2599         movdqu  %xmm2, 0x00($r_ptr)
2600         movdqu  %xmm3, 0x10($r_ptr)
2601
2602         movdqa  %xmm5, %xmm0            # copy_conditional(res_y, in2_y, in1infty);
2603         movdqa  %xmm5, %xmm1
2604         pandn   $res_y(%rsp), %xmm0
2605         movdqa  %xmm5, %xmm2
2606         pandn   $res_y+0x10(%rsp), %xmm1
2607         movdqa  %xmm5, %xmm3
2608         pand    $in2_y(%rsp), %xmm2
2609         pand    $in2_y+0x10(%rsp), %xmm3
2610         por     %xmm0, %xmm2
2611         por     %xmm1, %xmm3
2612
2613         movdqa  %xmm4, %xmm0            # copy_conditional(res_y, in1_y, in2infty);
2614         movdqa  %xmm4, %xmm1
2615         pandn   %xmm2, %xmm0
2616         movdqa  %xmm4, %xmm2
2617         pandn   %xmm3, %xmm1
2618         movdqa  %xmm4, %xmm3
2619         pand    $in1_y(%rsp), %xmm2
2620         pand    $in1_y+0x10(%rsp), %xmm3
2621         por     %xmm0, %xmm2
2622         por     %xmm1, %xmm3
2623         movdqu  %xmm2, 0x20($r_ptr)
2624         movdqu  %xmm3, 0x30($r_ptr)
2625
2626 .Ladd_done$x:
2627         lea     32*18+56(%rsp), %rsi
2628         mov     -48(%rsi),%r15
2629         mov     -40(%rsi),%r14
2630         mov     -32(%rsi),%r13
2631         mov     -24(%rsi),%r12
2632         mov     -16(%rsi),%rbx
2633         mov     -8(%rsi),%rbp
2634         lea     (%rsi),%rsp
2635 .Lpoint_add${x}_epilogue:
2636         ret
2637 .size   ecp_nistz256_point_add$sfx,.-ecp_nistz256_point_add$sfx
2638 ___
2639 }
2640 &gen_add("q");
2641
2642 sub gen_add_affine () {
2643     my $x = shift;
2644     my ($src0,$sfx,$bias);
2645     my ($U2,$S2,$H,$R,$Hsqr,$Hcub,$Rsqr,
2646         $res_x,$res_y,$res_z,
2647         $in1_x,$in1_y,$in1_z,
2648         $in2_x,$in2_y)=map(32*$_,(0..14));
2649     my $Z1sqr = $S2;
2650
2651     if ($x ne "x") {
2652         $src0 = "%rax";
2653         $sfx  = "";
2654         $bias = 0;
2655
2656 $code.=<<___;
2657 .globl  ecp_nistz256_point_add_affine
2658 .type   ecp_nistz256_point_add_affine,\@function,3
2659 .align  32
2660 ecp_nistz256_point_add_affine:
2661 ___
2662 $code.=<<___    if ($addx);
2663         mov     \$0x80100, %ecx
2664         and     OPENSSL_ia32cap_P+8(%rip), %ecx
2665         cmp     \$0x80100, %ecx
2666         je      .Lpoint_add_affinex
2667 ___
2668     } else {
2669         $src0 = "%rdx";
2670         $sfx  = "x";
2671         $bias = 128;
2672
2673 $code.=<<___;
2674 .type   ecp_nistz256_point_add_affinex,\@function,3
2675 .align  32
2676 ecp_nistz256_point_add_affinex:
2677 .Lpoint_add_affinex:
2678 ___
2679     }
2680 $code.=<<___;
2681         push    %rbp
2682         push    %rbx
2683         push    %r12
2684         push    %r13
2685         push    %r14
2686         push    %r15
2687         sub     \$32*15+8, %rsp
2688 .Ladd_affine${x}_body:
2689
2690         movdqu  0x00($a_ptr), %xmm0     # copy  *(P256_POINT *)$a_ptr
2691         mov     $b_org, $b_ptr          # reassign
2692         movdqu  0x10($a_ptr), %xmm1
2693         movdqu  0x20($a_ptr), %xmm2
2694         movdqu  0x30($a_ptr), %xmm3
2695         movdqu  0x40($a_ptr), %xmm4
2696         movdqu  0x50($a_ptr), %xmm5
2697          mov    0x40+8*0($a_ptr), $src0 # load original in1_z
2698          mov    0x40+8*1($a_ptr), $acc6
2699          mov    0x40+8*2($a_ptr), $acc7
2700          mov    0x40+8*3($a_ptr), $acc0
2701         movdqa  %xmm0, $in1_x(%rsp)
2702         movdqa  %xmm1, $in1_x+0x10(%rsp)
2703         movdqa  %xmm2, $in1_y(%rsp)
2704         movdqa  %xmm3, $in1_y+0x10(%rsp)
2705         movdqa  %xmm4, $in1_z(%rsp)
2706         movdqa  %xmm5, $in1_z+0x10(%rsp)
2707         por     %xmm4, %xmm5
2708
2709         movdqu  0x00($b_ptr), %xmm0     # copy  *(P256_POINT_AFFINE *)$b_ptr
2710          pshufd \$0xb1, %xmm5, %xmm3
2711         movdqu  0x10($b_ptr), %xmm1
2712         movdqu  0x20($b_ptr), %xmm2
2713          por    %xmm3, %xmm5
2714         movdqu  0x30($b_ptr), %xmm3
2715         movdqa  %xmm0, $in2_x(%rsp)
2716          pshufd \$0x1e, %xmm5, %xmm4
2717         movdqa  %xmm1, $in2_x+0x10(%rsp)
2718         por     %xmm0, %xmm1
2719          movq   $r_ptr, %xmm0           # save $r_ptr
2720         movdqa  %xmm2, $in2_y(%rsp)
2721         movdqa  %xmm3, $in2_y+0x10(%rsp)
2722         por     %xmm2, %xmm3
2723          por    %xmm4, %xmm5
2724          pxor   %xmm4, %xmm4
2725         por     %xmm1, %xmm3
2726
2727         lea     0x40-$bias($a_ptr), $a_ptr      # $a_ptr is still valid
2728         lea     $Z1sqr(%rsp), $r_ptr            # Z1^2
2729         call    __ecp_nistz256_sqr_mont$x       # p256_sqr_mont(Z1sqr, in1_z);
2730
2731         pcmpeqd %xmm4, %xmm5
2732         pshufd  \$0xb1, %xmm3, %xmm4
2733          mov    0x00($b_ptr), $src0             # $b_ptr is still valid
2734          #lea   0x00($b_ptr), $b_ptr
2735          mov    $acc4, $acc1                    # harmonize sqr output and mul input
2736         por     %xmm3, %xmm4
2737         pshufd  \$0, %xmm5, %xmm5               # in1infty
2738         pshufd  \$0x1e, %xmm4, %xmm3
2739          mov    $acc5, $acc2
2740         por     %xmm3, %xmm4
2741         pxor    %xmm3, %xmm3
2742          mov    $acc6, $acc3
2743         pcmpeqd %xmm3, %xmm4
2744         pshufd  \$0, %xmm4, %xmm4               # in2infty
2745
2746         lea     $Z1sqr-$bias(%rsp), $a_ptr
2747         mov     $acc7, $acc4
2748         lea     $U2(%rsp), $r_ptr               # U2 = X2*Z1^2
2749         call    __ecp_nistz256_mul_mont$x       # p256_mul_mont(U2, Z1sqr, in2_x);
2750
2751         lea     $in1_x(%rsp), $b_ptr
2752         lea     $H(%rsp), $r_ptr                # H = U2 - U1
2753         call    __ecp_nistz256_sub_from$x       # p256_sub(H, U2, in1_x);
2754
2755         `&load_for_mul("$Z1sqr(%rsp)", "$in1_z(%rsp)", "$src0")`
2756         lea     $S2(%rsp), $r_ptr               # S2 = Z1^3
2757         call    __ecp_nistz256_mul_mont$x       # p256_mul_mont(S2, Z1sqr, in1_z);
2758
2759         `&load_for_mul("$H(%rsp)", "$in1_z(%rsp)", "$src0")`
2760         lea     $res_z(%rsp), $r_ptr            # Z3 = H*Z1*Z2
2761         call    __ecp_nistz256_mul_mont$x       # p256_mul_mont(res_z, H, in1_z);
2762
2763         `&load_for_mul("$S2(%rsp)", "$in2_y(%rsp)", "$src0")`
2764         lea     $S2(%rsp), $r_ptr               # S2 = Y2*Z1^3
2765         call    __ecp_nistz256_mul_mont$x       # p256_mul_mont(S2, S2, in2_y);
2766
2767         lea     $in1_y(%rsp), $b_ptr
2768         lea     $R(%rsp), $r_ptr                # R = S2 - S1
2769         call    __ecp_nistz256_sub_from$x       # p256_sub(R, S2, in1_y);
2770
2771         `&load_for_sqr("$H(%rsp)", "$src0")`
2772         lea     $Hsqr(%rsp), $r_ptr             # H^2
2773         call    __ecp_nistz256_sqr_mont$x       # p256_sqr_mont(Hsqr, H);
2774
2775         `&load_for_sqr("$R(%rsp)", "$src0")`
2776         lea     $Rsqr(%rsp), $r_ptr             # R^2
2777         call    __ecp_nistz256_sqr_mont$x       # p256_sqr_mont(Rsqr, R);
2778
2779         `&load_for_mul("$H(%rsp)", "$Hsqr(%rsp)", "$src0")`
2780         lea     $Hcub(%rsp), $r_ptr             # H^3
2781         call    __ecp_nistz256_mul_mont$x       # p256_mul_mont(Hcub, Hsqr, H);
2782
2783         `&load_for_mul("$Hsqr(%rsp)", "$in1_x(%rsp)", "$src0")`
2784         lea     $U2(%rsp), $r_ptr               # U1*H^2
2785         call    __ecp_nistz256_mul_mont$x       # p256_mul_mont(U2, in1_x, Hsqr);
2786 ___
2787 {
2788 #######################################################################
2789 # operate in 4-5-0-1 "name space" that matches multiplication output
2790 #
2791 my ($acc0,$acc1,$acc2,$acc3,$t3,$t4)=($acc4,$acc5,$acc0,$acc1,$acc2,$acc3);
2792 my ($poly1, $poly3)=($acc6,$acc7);
2793
2794 $code.=<<___;
2795         #lea    $U2(%rsp), $a_ptr
2796         #lea    $Hsqr(%rsp), $r_ptr     # 2*U1*H^2
2797         #call   __ecp_nistz256_mul_by_2 # ecp_nistz256_mul_by_2(Hsqr, U2);
2798
2799         xor     $t4, $t4
2800         add     $acc0, $acc0            # a0:a3+a0:a3
2801         lea     $Rsqr(%rsp), $a_ptr
2802         adc     $acc1, $acc1
2803          mov    $acc0, $t0
2804         adc     $acc2, $acc2
2805         adc     $acc3, $acc3
2806          mov    $acc1, $t1
2807         adc     \$0, $t4
2808
2809         sub     \$-1, $acc0
2810          mov    $acc2, $t2
2811         sbb     $poly1, $acc1
2812         sbb     \$0, $acc2
2813          mov    $acc3, $t3
2814         sbb     $poly3, $acc3
2815         sbb     \$0, $t4
2816
2817         cmovc   $t0, $acc0
2818         mov     8*0($a_ptr), $t0
2819         cmovc   $t1, $acc1
2820         mov     8*1($a_ptr), $t1
2821         cmovc   $t2, $acc2
2822         mov     8*2($a_ptr), $t2
2823         cmovc   $t3, $acc3
2824         mov     8*3($a_ptr), $t3
2825
2826         call    __ecp_nistz256_sub$x            # p256_sub(res_x, Rsqr, Hsqr);
2827
2828         lea     $Hcub(%rsp), $b_ptr
2829         lea     $res_x(%rsp), $r_ptr
2830         call    __ecp_nistz256_sub_from$x       # p256_sub(res_x, res_x, Hcub);
2831
2832         mov     $U2+8*0(%rsp), $t0
2833         mov     $U2+8*1(%rsp), $t1
2834         mov     $U2+8*2(%rsp), $t2
2835         mov     $U2+8*3(%rsp), $t3
2836         lea     $H(%rsp), $r_ptr
2837
2838         call    __ecp_nistz256_sub$x            # p256_sub(H, U2, res_x);
2839
2840         mov     $acc0, 8*0($r_ptr)              # save the result, as
2841         mov     $acc1, 8*1($r_ptr)              # __ecp_nistz256_sub doesn't
2842         mov     $acc2, 8*2($r_ptr)
2843         mov     $acc3, 8*3($r_ptr)
2844 ___
2845 }
2846 $code.=<<___;
2847         `&load_for_mul("$Hcub(%rsp)", "$in1_y(%rsp)", "$src0")`
2848         lea     $S2(%rsp), $r_ptr
2849         call    __ecp_nistz256_mul_mont$x       # p256_mul_mont(S2, Hcub, in1_y);
2850
2851         `&load_for_mul("$H(%rsp)", "$R(%rsp)", "$src0")`
2852         lea     $H(%rsp), $r_ptr
2853         call    __ecp_nistz256_mul_mont$x       # p256_mul_mont(H, H, R);
2854
2855         lea     $S2(%rsp), $b_ptr
2856         lea     $res_y(%rsp), $r_ptr
2857         call    __ecp_nistz256_sub_from$x       # p256_sub(res_y, H, S2);
2858
2859         movq    %xmm0, $r_ptr           # restore $r_ptr
2860
2861         movdqa  %xmm5, %xmm0            # copy_conditional(res_z, ONE, in1infty);
2862         movdqa  %xmm5, %xmm1
2863         pandn   $res_z(%rsp), %xmm0
2864         movdqa  %xmm5, %xmm2
2865         pandn   $res_z+0x10(%rsp), %xmm1
2866         movdqa  %xmm5, %xmm3
2867         pand    .LONE_mont(%rip), %xmm2
2868         pand    .LONE_mont+0x10(%rip), %xmm3
2869         por     %xmm0, %xmm2
2870         por     %xmm1, %xmm3
2871
2872         movdqa  %xmm4, %xmm0            # copy_conditional(res_z, in1_z, in2infty);
2873         movdqa  %xmm4, %xmm1
2874         pandn   %xmm2, %xmm0
2875         movdqa  %xmm4, %xmm2
2876         pandn   %xmm3, %xmm1
2877         movdqa  %xmm4, %xmm3
2878         pand    $in1_z(%rsp), %xmm2
2879         pand    $in1_z+0x10(%rsp), %xmm3
2880         por     %xmm0, %xmm2
2881         por     %xmm1, %xmm3
2882         movdqu  %xmm2, 0x40($r_ptr)
2883         movdqu  %xmm3, 0x50($r_ptr)
2884
2885         movdqa  %xmm5, %xmm0            # copy_conditional(res_x, in2_x, in1infty);
2886         movdqa  %xmm5, %xmm1
2887         pandn   $res_x(%rsp), %xmm0
2888         movdqa  %xmm5, %xmm2
2889         pandn   $res_x+0x10(%rsp), %xmm1
2890         movdqa  %xmm5, %xmm3
2891         pand    $in2_x(%rsp), %xmm2
2892         pand    $in2_x+0x10(%rsp), %xmm3
2893         por     %xmm0, %xmm2
2894         por     %xmm1, %xmm3
2895
2896         movdqa  %xmm4, %xmm0            # copy_conditional(res_x, in1_x, in2infty);
2897         movdqa  %xmm4, %xmm1
2898         pandn   %xmm2, %xmm0
2899         movdqa  %xmm4, %xmm2
2900         pandn   %xmm3, %xmm1
2901         movdqa  %xmm4, %xmm3
2902         pand    $in1_x(%rsp), %xmm2
2903         pand    $in1_x+0x10(%rsp), %xmm3
2904         por     %xmm0, %xmm2
2905         por     %xmm1, %xmm3
2906         movdqu  %xmm2, 0x00($r_ptr)
2907         movdqu  %xmm3, 0x10($r_ptr)
2908
2909         movdqa  %xmm5, %xmm0            # copy_conditional(res_y, in2_y, in1infty);
2910         movdqa  %xmm5, %xmm1
2911         pandn   $res_y(%rsp), %xmm0
2912         movdqa  %xmm5, %xmm2
2913         pandn   $res_y+0x10(%rsp), %xmm1
2914         movdqa  %xmm5, %xmm3
2915         pand    $in2_y(%rsp), %xmm2
2916         pand    $in2_y+0x10(%rsp), %xmm3
2917         por     %xmm0, %xmm2
2918         por     %xmm1, %xmm3
2919
2920         movdqa  %xmm4, %xmm0            # copy_conditional(res_y, in1_y, in2infty);
2921         movdqa  %xmm4, %xmm1
2922         pandn   %xmm2, %xmm0
2923         movdqa  %xmm4, %xmm2
2924         pandn   %xmm3, %xmm1
2925         movdqa  %xmm4, %xmm3
2926         pand    $in1_y(%rsp), %xmm2
2927         pand    $in1_y+0x10(%rsp), %xmm3
2928         por     %xmm0, %xmm2
2929         por     %xmm1, %xmm3
2930         movdqu  %xmm2, 0x20($r_ptr)
2931         movdqu  %xmm3, 0x30($r_ptr)
2932
2933         lea     32*15+56(%rsp), %rsi
2934         mov     -48(%rsi),%r15
2935         mov     -40(%rsi),%r14
2936         mov     -32(%rsi),%r13
2937         mov     -24(%rsi),%r12
2938         mov     -16(%rsi),%rbx
2939         mov     -8(%rsi),%rbp
2940         lea     (%rsi),%rsp
2941 .Ladd_affine${x}_epilogue:
2942         ret
2943 .size   ecp_nistz256_point_add_affine$sfx,.-ecp_nistz256_point_add_affine$sfx
2944 ___
2945 }
2946 &gen_add_affine("q");
2947
2948 ########################################################################
2949 # AD*X magic
2950 #
2951 if ($addx) {                                                            {
2952 ########################################################################
2953 # operate in 4-5-0-1 "name space" that matches multiplication output
2954 #
2955 my ($a0,$a1,$a2,$a3,$t3,$t4)=($acc4,$acc5,$acc0,$acc1,$acc2,$acc3);
2956
2957 $code.=<<___;
2958 .type   __ecp_nistz256_add_tox,\@abi-omnipotent
2959 .align  32
2960 __ecp_nistz256_add_tox:
2961         xor     $t4, $t4
2962         adc     8*0($b_ptr), $a0
2963         adc     8*1($b_ptr), $a1
2964          mov    $a0, $t0
2965         adc     8*2($b_ptr), $a2
2966         adc     8*3($b_ptr), $a3
2967          mov    $a1, $t1
2968         adc     \$0, $t4
2969
2970         xor     $t3, $t3
2971         sbb     \$-1, $a0
2972          mov    $a2, $t2
2973         sbb     $poly1, $a1
2974         sbb     \$0, $a2
2975          mov    $a3, $t3
2976         sbb     $poly3, $a3
2977         sbb     \$0, $t4
2978
2979         cmovc   $t0, $a0
2980         cmovc   $t1, $a1
2981         mov     $a0, 8*0($r_ptr)
2982         cmovc   $t2, $a2
2983         mov     $a1, 8*1($r_ptr)
2984         cmovc   $t3, $a3
2985         mov     $a2, 8*2($r_ptr)
2986         mov     $a3, 8*3($r_ptr)
2987
2988         ret
2989 .size   __ecp_nistz256_add_tox,.-__ecp_nistz256_add_tox
2990
2991 .type   __ecp_nistz256_sub_fromx,\@abi-omnipotent
2992 .align  32
2993 __ecp_nistz256_sub_fromx:
2994         xor     $t4, $t4
2995         sbb     8*0($b_ptr), $a0
2996         sbb     8*1($b_ptr), $a1
2997          mov    $a0, $t0
2998         sbb     8*2($b_ptr), $a2
2999         sbb     8*3($b_ptr), $a3
3000          mov    $a1, $t1
3001         sbb     \$0, $t4
3002
3003         xor     $t3, $t3
3004         adc     \$-1, $a0
3005          mov    $a2, $t2
3006         adc     $poly1, $a1
3007         adc     \$0, $a2
3008          mov    $a3, $t3
3009         adc     $poly3, $a3
3010
3011         bt      \$0, $t4
3012         cmovnc  $t0, $a0
3013         cmovnc  $t1, $a1
3014         mov     $a0, 8*0($r_ptr)
3015         cmovnc  $t2, $a2
3016         mov     $a1, 8*1($r_ptr)
3017         cmovnc  $t3, $a3
3018         mov     $a2, 8*2($r_ptr)
3019         mov     $a3, 8*3($r_ptr)
3020
3021         ret
3022 .size   __ecp_nistz256_sub_fromx,.-__ecp_nistz256_sub_fromx
3023
3024 .type   __ecp_nistz256_subx,\@abi-omnipotent
3025 .align  32
3026 __ecp_nistz256_subx:
3027         xor     $t4, $t4
3028         sbb     $a0, $t0
3029         sbb     $a1, $t1
3030          mov    $t0, $a0
3031         sbb     $a2, $t2
3032         sbb     $a3, $t3
3033          mov    $t1, $a1
3034         sbb     \$0, $t4
3035
3036         xor     $a3 ,$a3
3037         adc     \$-1, $t0
3038          mov    $t2, $a2
3039         adc     $poly1, $t1
3040         adc     \$0, $t2
3041          mov    $t3, $a3
3042         adc     $poly3, $t3
3043
3044         bt      \$0, $t4
3045         cmovc   $t0, $a0
3046         cmovc   $t1, $a1
3047         cmovc   $t2, $a2
3048         cmovc   $t3, $a3
3049
3050         ret
3051 .size   __ecp_nistz256_subx,.-__ecp_nistz256_subx
3052
3053 .type   __ecp_nistz256_mul_by_2x,\@abi-omnipotent
3054 .align  32
3055 __ecp_nistz256_mul_by_2x:
3056         xor     $t4, $t4
3057         adc     $a0, $a0                # a0:a3+a0:a3
3058         adc     $a1, $a1
3059          mov    $a0, $t0
3060         adc     $a2, $a2
3061         adc     $a3, $a3
3062          mov    $a1, $t1
3063         adc     \$0, $t4
3064
3065         xor     $t3, $t3
3066         sbb     \$-1, $a0
3067          mov    $a2, $t2
3068         sbb     $poly1, $a1
3069         sbb     \$0, $a2
3070          mov    $a3, $t3
3071         sbb     $poly3, $a3
3072         sbb     \$0, $t4
3073
3074         cmovc   $t0, $a0
3075         cmovc   $t1, $a1
3076         mov     $a0, 8*0($r_ptr)
3077         cmovc   $t2, $a2
3078         mov     $a1, 8*1($r_ptr)
3079         cmovc   $t3, $a3
3080         mov     $a2, 8*2($r_ptr)
3081         mov     $a3, 8*3($r_ptr)
3082
3083         ret
3084 .size   __ecp_nistz256_mul_by_2x,.-__ecp_nistz256_mul_by_2x
3085 ___
3086                                                                         }
3087 &gen_double("x");
3088 &gen_add("x");
3089 &gen_add_affine("x");
3090 }
3091 }}}
3092
3093 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
3094 #               CONTEXT *context,DISPATCHER_CONTEXT *disp)
3095 if ($win64) {
3096 $rec="%rcx";
3097 $frame="%rdx";
3098 $context="%r8";
3099 $disp="%r9";
3100
3101 $code.=<<___;
3102 .extern __imp_RtlVirtualUnwind
3103
3104 .type   short_handler,\@abi-omnipotent
3105 .align  16
3106 short_handler:
3107         push    %rsi
3108         push    %rdi
3109         push    %rbx
3110         push    %rbp
3111         push    %r12
3112         push    %r13
3113         push    %r14
3114         push    %r15
3115         pushfq
3116         sub     \$64,%rsp
3117
3118         mov     120($context),%rax      # pull context->Rax
3119         mov     248($context),%rbx      # pull context->Rip
3120
3121         mov     8($disp),%rsi           # disp->ImageBase
3122         mov     56($disp),%r11          # disp->HandlerData
3123
3124         mov     0(%r11),%r10d           # HandlerData[0]
3125         lea     (%rsi,%r10),%r10        # end of prologue label
3126         cmp     %r10,%rbx               # context->Rip<end of prologue label
3127         jb      .Lcommon_seh_tail
3128
3129         mov     152($context),%rax      # pull context->Rsp
3130
3131         mov     4(%r11),%r10d           # HandlerData[1]
3132         lea     (%rsi,%r10),%r10        # epilogue label
3133         cmp     %r10,%rbx               # context->Rip>=epilogue label
3134         jae     .Lcommon_seh_tail
3135
3136         lea     16(%rax),%rax
3137
3138         mov     -8(%rax),%r12
3139         mov     -16(%rax),%r13
3140         mov     %r12,216($context)      # restore context->R12
3141         mov     %r13,224($context)      # restore context->R13
3142
3143         jmp     .Lcommon_seh_tail
3144 .size   short_handler,.-short_handler
3145
3146 .type   full_handler,\@abi-omnipotent
3147 .align  16
3148 full_handler:
3149         push    %rsi
3150         push    %rdi
3151         push    %rbx
3152         push    %rbp
3153         push    %r12
3154         push    %r13
3155         push    %r14
3156         push    %r15
3157         pushfq
3158         sub     \$64,%rsp
3159
3160         mov     120($context),%rax      # pull context->Rax
3161         mov     248($context),%rbx      # pull context->Rip
3162
3163         mov     8($disp),%rsi           # disp->ImageBase
3164         mov     56($disp),%r11          # disp->HandlerData
3165
3166         mov     0(%r11),%r10d           # HandlerData[0]
3167         lea     (%rsi,%r10),%r10        # end of prologue label
3168         cmp     %r10,%rbx               # context->Rip<end of prologue label
3169         jb      .Lcommon_seh_tail
3170
3171         mov     152($context),%rax      # pull context->Rsp
3172
3173         mov     4(%r11),%r10d           # HandlerData[1]
3174         lea     (%rsi,%r10),%r10        # epilogue label
3175         cmp     %r10,%rbx               # context->Rip>=epilogue label
3176         jae     .Lcommon_seh_tail
3177
3178         mov     8(%r11),%r10d           # HandlerData[2]
3179         lea     (%rax,%r10),%rax
3180
3181         mov     -8(%rax),%rbp
3182         mov     -16(%rax),%rbx
3183         mov     -24(%rax),%r12
3184         mov     -32(%rax),%r13
3185         mov     -40(%rax),%r14
3186         mov     -48(%rax),%r15
3187         mov     %rbx,144($context)      # restore context->Rbx
3188         mov     %rbp,160($context)      # restore context->Rbp
3189         mov     %r12,216($context)      # restore context->R12
3190         mov     %r13,224($context)      # restore context->R13
3191         mov     %r14,232($context)      # restore context->R14
3192         mov     %r15,240($context)      # restore context->R15
3193
3194 .Lcommon_seh_tail:
3195         mov     8(%rax),%rdi
3196         mov     16(%rax),%rsi
3197         mov     %rax,152($context)      # restore context->Rsp
3198         mov     %rsi,168($context)      # restore context->Rsi
3199         mov     %rdi,176($context)      # restore context->Rdi
3200
3201         mov     40($disp),%rdi          # disp->ContextRecord
3202         mov     $context,%rsi           # context
3203         mov     \$154,%ecx              # sizeof(CONTEXT)
3204         .long   0xa548f3fc              # cld; rep movsq
3205
3206         mov     $disp,%rsi
3207         xor     %rcx,%rcx               # arg1, UNW_FLAG_NHANDLER
3208         mov     8(%rsi),%rdx            # arg2, disp->ImageBase
3209         mov     0(%rsi),%r8             # arg3, disp->ControlPc
3210         mov     16(%rsi),%r9            # arg4, disp->FunctionEntry
3211         mov     40(%rsi),%r10           # disp->ContextRecord
3212         lea     56(%rsi),%r11           # &disp->HandlerData
3213         lea     24(%rsi),%r12           # &disp->EstablisherFrame
3214         mov     %r10,32(%rsp)           # arg5
3215         mov     %r11,40(%rsp)           # arg6
3216         mov     %r12,48(%rsp)           # arg7
3217         mov     %rcx,56(%rsp)           # arg8, (NULL)
3218         call    *__imp_RtlVirtualUnwind(%rip)
3219
3220         mov     \$1,%eax                # ExceptionContinueSearch
3221         add     \$64,%rsp
3222         popfq
3223         pop     %r15
3224         pop     %r14
3225         pop     %r13
3226         pop     %r12
3227         pop     %rbp
3228         pop     %rbx
3229         pop     %rdi
3230         pop     %rsi
3231         ret
3232 .size   full_handler,.-full_handler
3233
3234 .section        .pdata
3235 .align  4
3236         .rva    .LSEH_begin_ecp_nistz256_mul_by_2
3237         .rva    .LSEH_end_ecp_nistz256_mul_by_2
3238         .rva    .LSEH_info_ecp_nistz256_mul_by_2
3239
3240         .rva    .LSEH_begin_ecp_nistz256_div_by_2
3241         .rva    .LSEH_end_ecp_nistz256_div_by_2
3242         .rva    .LSEH_info_ecp_nistz256_div_by_2
3243
3244         .rva    .LSEH_begin_ecp_nistz256_mul_by_3
3245         .rva    .LSEH_end_ecp_nistz256_mul_by_3
3246         .rva    .LSEH_info_ecp_nistz256_mul_by_3
3247
3248         .rva    .LSEH_begin_ecp_nistz256_add
3249         .rva    .LSEH_end_ecp_nistz256_add
3250         .rva    .LSEH_info_ecp_nistz256_add
3251
3252         .rva    .LSEH_begin_ecp_nistz256_sub
3253         .rva    .LSEH_end_ecp_nistz256_sub
3254         .rva    .LSEH_info_ecp_nistz256_sub
3255
3256         .rva    .LSEH_begin_ecp_nistz256_neg
3257         .rva    .LSEH_end_ecp_nistz256_neg
3258         .rva    .LSEH_info_ecp_nistz256_neg
3259
3260         .rva    .LSEH_begin_ecp_nistz256_to_mont
3261         .rva    .LSEH_end_ecp_nistz256_to_mont
3262         .rva    .LSEH_info_ecp_nistz256_to_mont
3263
3264         .rva    .LSEH_begin_ecp_nistz256_mul_mont
3265         .rva    .LSEH_end_ecp_nistz256_mul_mont
3266         .rva    .LSEH_info_ecp_nistz256_mul_mont
3267
3268         .rva    .LSEH_begin_ecp_nistz256_sqr_mont
3269         .rva    .LSEH_end_ecp_nistz256_sqr_mont
3270         .rva    .LSEH_info_ecp_nistz256_sqr_mont
3271
3272         .rva    .LSEH_begin_ecp_nistz256_from_mont
3273         .rva    .LSEH_end_ecp_nistz256_from_mont
3274         .rva    .LSEH_info_ecp_nistz256_from_mont
3275
3276         .rva    .LSEH_begin_ecp_nistz256_gather_w5
3277         .rva    .LSEH_end_ecp_nistz256_gather_w5
3278         .rva    .LSEH_info_ecp_nistz256_gather_wX
3279
3280         .rva    .LSEH_begin_ecp_nistz256_gather_w7
3281         .rva    .LSEH_end_ecp_nistz256_gather_w7
3282         .rva    .LSEH_info_ecp_nistz256_gather_wX
3283 ___
3284 $code.=<<___    if ($avx>1);
3285         .rva    .LSEH_begin_ecp_nistz256_avx2_gather_w5
3286         .rva    .LSEH_end_ecp_nistz256_avx2_gather_w5
3287         .rva    .LSEH_info_ecp_nistz256_avx2_gather_wX
3288
3289         .rva    .LSEH_begin_ecp_nistz256_avx2_gather_w7
3290         .rva    .LSEH_end_ecp_nistz256_avx2_gather_w7
3291         .rva    .LSEH_info_ecp_nistz256_avx2_gather_wX
3292 ___
3293 $code.=<<___;
3294         .rva    .LSEH_begin_ecp_nistz256_point_double
3295         .rva    .LSEH_end_ecp_nistz256_point_double
3296         .rva    .LSEH_info_ecp_nistz256_point_double
3297
3298         .rva    .LSEH_begin_ecp_nistz256_point_add
3299         .rva    .LSEH_end_ecp_nistz256_point_add
3300         .rva    .LSEH_info_ecp_nistz256_point_add
3301
3302         .rva    .LSEH_begin_ecp_nistz256_point_add_affine
3303         .rva    .LSEH_end_ecp_nistz256_point_add_affine
3304         .rva    .LSEH_info_ecp_nistz256_point_add_affine
3305 ___
3306 $code.=<<___ if ($addx);
3307         .rva    .LSEH_begin_ecp_nistz256_point_doublex
3308         .rva    .LSEH_end_ecp_nistz256_point_doublex
3309         .rva    .LSEH_info_ecp_nistz256_point_doublex
3310
3311         .rva    .LSEH_begin_ecp_nistz256_point_addx
3312         .rva    .LSEH_end_ecp_nistz256_point_addx
3313         .rva    .LSEH_info_ecp_nistz256_point_addx
3314
3315         .rva    .LSEH_begin_ecp_nistz256_point_add_affinex
3316         .rva    .LSEH_end_ecp_nistz256_point_add_affinex
3317         .rva    .LSEH_info_ecp_nistz256_point_add_affinex
3318 ___
3319 $code.=<<___;
3320
3321 .section        .xdata
3322 .align  8
3323 .LSEH_info_ecp_nistz256_mul_by_2:
3324         .byte   9,0,0,0
3325         .rva    short_handler
3326         .rva    .Lmul_by_2_body,.Lmul_by_2_epilogue     # HandlerData[]
3327 .LSEH_info_ecp_nistz256_div_by_2:
3328         .byte   9,0,0,0
3329         .rva    short_handler
3330         .rva    .Ldiv_by_2_body,.Ldiv_by_2_epilogue     # HandlerData[]
3331 .LSEH_info_ecp_nistz256_mul_by_3:
3332         .byte   9,0,0,0
3333         .rva    short_handler
3334         .rva    .Lmul_by_3_body,.Lmul_by_3_epilogue     # HandlerData[]
3335 .LSEH_info_ecp_nistz256_add:
3336         .byte   9,0,0,0
3337         .rva    short_handler
3338         .rva    .Ladd_body,.Ladd_epilogue               # HandlerData[]
3339 .LSEH_info_ecp_nistz256_sub:
3340         .byte   9,0,0,0
3341         .rva    short_handler
3342         .rva    .Lsub_body,.Lsub_epilogue               # HandlerData[]
3343 .LSEH_info_ecp_nistz256_neg:
3344         .byte   9,0,0,0
3345         .rva    short_handler
3346         .rva    .Lneg_body,.Lneg_epilogue               # HandlerData[]
3347 .LSEH_info_ecp_nistz256_to_mont:
3348         .byte   9,0,0,0
3349         .rva    full_handler
3350         .rva    .Lmul_body,.Lmul_epilogue               # HandlerData[]
3351         .long   48,0
3352 .LSEH_info_ecp_nistz256_mul_mont:
3353         .byte   9,0,0,0
3354         .rva    full_handler
3355         .rva    .Lmul_body,.Lmul_epilogue               # HandlerData[]
3356         .long   48,0
3357 .LSEH_info_ecp_nistz256_sqr_mont:
3358         .byte   9,0,0,0
3359         .rva    full_handler
3360         .rva    .Lsqr_body,.Lsqr_epilogue               # HandlerData[]
3361         .long   48,0
3362 .LSEH_info_ecp_nistz256_from_mont:
3363         .byte   9,0,0,0
3364         .rva    short_handler
3365         .rva    .Lfrom_body,.Lfrom_epilogue             # HandlerData[]
3366 .LSEH_info_ecp_nistz256_gather_wX:
3367         .byte   0x01,0x33,0x16,0x00
3368         .byte   0x33,0xf8,0x09,0x00     #movaps 0x90(rsp),xmm15
3369         .byte   0x2e,0xe8,0x08,0x00     #movaps 0x80(rsp),xmm14
3370         .byte   0x29,0xd8,0x07,0x00     #movaps 0x70(rsp),xmm13
3371         .byte   0x24,0xc8,0x06,0x00     #movaps 0x60(rsp),xmm12
3372         .byte   0x1f,0xb8,0x05,0x00     #movaps 0x50(rsp),xmm11
3373         .byte   0x1a,0xa8,0x04,0x00     #movaps 0x40(rsp),xmm10
3374         .byte   0x15,0x98,0x03,0x00     #movaps 0x30(rsp),xmm9
3375         .byte   0x10,0x88,0x02,0x00     #movaps 0x20(rsp),xmm8
3376         .byte   0x0c,0x78,0x01,0x00     #movaps 0x10(rsp),xmm7
3377         .byte   0x08,0x68,0x00,0x00     #movaps 0x00(rsp),xmm6
3378         .byte   0x04,0x01,0x15,0x00     #sub    rsp,0xa8
3379         .align  8
3380 ___
3381 $code.=<<___    if ($avx>1);
3382 .LSEH_info_ecp_nistz256_avx2_gather_wX:
3383         .byte   0x01,0x36,0x17,0x0b
3384         .byte   0x36,0xf8,0x09,0x00     # vmovaps 0x90(rsp),xmm15
3385         .byte   0x31,0xe8,0x08,0x00     # vmovaps 0x80(rsp),xmm14
3386         .byte   0x2c,0xd8,0x07,0x00     # vmovaps 0x70(rsp),xmm13
3387         .byte   0x27,0xc8,0x06,0x00     # vmovaps 0x60(rsp),xmm12
3388         .byte   0x22,0xb8,0x05,0x00     # vmovaps 0x50(rsp),xmm11
3389         .byte   0x1d,0xa8,0x04,0x00     # vmovaps 0x40(rsp),xmm10
3390         .byte   0x18,0x98,0x03,0x00     # vmovaps 0x30(rsp),xmm9
3391         .byte   0x13,0x88,0x02,0x00     # vmovaps 0x20(rsp),xmm8
3392         .byte   0x0e,0x78,0x01,0x00     # vmovaps 0x10(rsp),xmm7
3393         .byte   0x09,0x68,0x00,0x00     # vmovaps 0x00(rsp),xmm6
3394         .byte   0x04,0x01,0x15,0x00     # sub     rsp,0xa8
3395         .byte   0x00,0xb3,0x00,0x00     # set_frame r11
3396         .align  8
3397 ___
3398 $code.=<<___;
3399 .LSEH_info_ecp_nistz256_point_double:
3400         .byte   9,0,0,0
3401         .rva    full_handler
3402         .rva    .Lpoint_doubleq_body,.Lpoint_doubleq_epilogue   # HandlerData[]
3403         .long   32*5+56,0
3404 .LSEH_info_ecp_nistz256_point_add:
3405         .byte   9,0,0,0
3406         .rva    full_handler
3407         .rva    .Lpoint_addq_body,.Lpoint_addq_epilogue         # HandlerData[]
3408         .long   32*18+56,0
3409 .LSEH_info_ecp_nistz256_point_add_affine:
3410         .byte   9,0,0,0
3411         .rva    full_handler
3412         .rva    .Ladd_affineq_body,.Ladd_affineq_epilogue       # HandlerData[]
3413         .long   32*15+56,0
3414 ___
3415 $code.=<<___ if ($addx);
3416 .align  8
3417 .LSEH_info_ecp_nistz256_point_doublex:
3418         .byte   9,0,0,0
3419         .rva    full_handler
3420         .rva    .Lpoint_doublex_body,.Lpoint_doublex_epilogue   # HandlerData[]
3421         .long   32*5+56,0
3422 .LSEH_info_ecp_nistz256_point_addx:
3423         .byte   9,0,0,0
3424         .rva    full_handler
3425         .rva    .Lpoint_addx_body,.Lpoint_addx_epilogue         # HandlerData[]
3426         .long   32*18+56,0
3427 .LSEH_info_ecp_nistz256_point_add_affinex:
3428         .byte   9,0,0,0
3429         .rva    full_handler
3430         .rva    .Ladd_affinex_body,.Ladd_affinex_epilogue       # HandlerData[]
3431         .long   32*15+56,0
3432 ___
3433 }
3434
3435 ########################################################################
3436 # Convert ecp_nistz256_table.c to layout expected by ecp_nistz_gather_w7
3437 #
3438 open TABLE,"<ecp_nistz256_table.c"              or
3439 open TABLE,"<${dir}../ecp_nistz256_table.c"     or
3440 die "failed to open ecp_nistz256_table.c:",$!;
3441
3442 use integer;
3443
3444 foreach(<TABLE>) {
3445         s/TOBN\(\s*(0x[0-9a-f]+),\s*(0x[0-9a-f]+)\s*\)/push @arr,hex($2),hex($1)/geo;
3446 }
3447 close TABLE;
3448
3449 die "insane number of elements" if ($#arr != 64*16*37-1);
3450
3451 print <<___;
3452 .text
3453 .globl  ecp_nistz256_precomputed
3454 .type   ecp_nistz256_precomputed,\@object
3455 .align  4096
3456 ecp_nistz256_precomputed:
3457 ___
3458 while (@line=splice(@arr,0,16)) {
3459         print ".long\t",join(',',map { sprintf "0x%08x",$_} @line),"\n";
3460 }
3461 print <<___;
3462 .size   ecp_nistz256_precomputed,.-ecp_nistz256_precomputed
3463 ___
3464
3465 $code =~ s/\`([^\`]*)\`/eval $1/gem;
3466 print $code;
3467 close STDOUT;