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