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