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