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