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