xref: /freebsd/crypto/openssl/crypto/bn/asm/x86_64-mont5.pl (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1#! /usr/bin/env perl
2# Copyright 2011-2022 The OpenSSL Project Authors. All Rights Reserved.
3#
4# Licensed under the Apache License 2.0 (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# Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
12# project. The module is, however, dual licensed under OpenSSL and
13# CRYPTOGAMS licenses depending on where you obtain it. For further
14# details see http://www.openssl.org/~appro/cryptogams/.
15# ====================================================================
16
17# August 2011.
18#
19# Companion to x86_64-mont.pl that optimizes cache-timing attack
20# countermeasures. The subroutines are produced by replacing bp[i]
21# references in their x86_64-mont.pl counterparts with cache-neutral
22# references to powers table computed in BN_mod_exp_mont_consttime.
23# In addition subroutine that scatters elements of the powers table
24# is implemented, so that scatter-/gathering can be tuned without
25# bn_exp.c modifications.
26
27# August 2013.
28#
29# Add MULX/AD*X code paths and additional interfaces to optimize for
30# branch prediction unit. For input lengths that are multiples of 8
31# the np argument is not just modulus value, but one interleaved
32# with 0. This is to optimize post-condition...
33
34# $output is the last argument if it looks like a file (it has an extension)
35# $flavour is the first argument if it doesn't look like a file
36$output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
37$flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
38
39$win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
40
41$0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
42( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
43( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
44die "can't locate x86_64-xlate.pl";
45
46open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\""
47    or die "can't call $xlate: $!";
48*STDOUT=*OUT;
49
50if (`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
51		=~ /GNU assembler version ([2-9]\.[0-9]+)/) {
52	$addx = ($1>=2.23);
53}
54
55if (!$addx && $win64 && ($flavour =~ /nasm/ || $ENV{ASM} =~ /nasm/) &&
56	    `nasm -v 2>&1` =~ /NASM version ([2-9]\.[0-9]+)/) {
57	$addx = ($1>=2.10);
58}
59
60if (!$addx && $win64 && ($flavour =~ /masm/ || $ENV{ASM} =~ /ml64/) &&
61	    `ml64 2>&1` =~ /Version ([0-9]+)\./) {
62	$addx = ($1>=12);
63}
64
65if (!$addx && `$ENV{CC} -v 2>&1` =~ /((?:clang|LLVM) version|.*based on LLVM) ([0-9]+)\.([0-9]+)/) {
66	my $ver = $2 + $3/100.0;	# 3.1->3.01, 3.10->3.10
67	$addx = ($ver>=3.03);
68}
69
70# int bn_mul_mont_gather5(
71$rp="%rdi";	# BN_ULONG *rp,
72$ap="%rsi";	# const BN_ULONG *ap,
73$bp="%rdx";	# const BN_ULONG *bp,
74$np="%rcx";	# const BN_ULONG *np,
75$n0="%r8";	# const BN_ULONG *n0,
76$num="%r9";	# int num,
77		# int idx);	# 0 to 2^5-1, "index" in $bp holding
78				# pre-computed powers of a', interlaced
79				# in such manner that b[0] is $bp[idx],
80				# b[1] is [2^5+idx], etc.
81$lo0="%r10";
82$hi0="%r11";
83$hi1="%r13";
84$i="%r14";
85$j="%r15";
86$m0="%rbx";
87$m1="%rbp";
88
89$code=<<___;
90.text
91
92.extern	OPENSSL_ia32cap_P
93
94.globl	bn_mul_mont_gather5
95.type	bn_mul_mont_gather5,\@function,6
96.align	64
97bn_mul_mont_gather5:
98.cfi_startproc
99	mov	${num}d,${num}d
100	mov	%rsp,%rax
101.cfi_def_cfa_register	%rax
102	test	\$7,${num}d
103	jnz	.Lmul_enter
104___
105$code.=<<___ if ($addx);
106	mov	OPENSSL_ia32cap_P+8(%rip),%r11d
107___
108$code.=<<___;
109	jmp	.Lmul4x_enter
110
111.align	16
112.Lmul_enter:
113	movd	`($win64?56:8)`(%rsp),%xmm5	# load 7th argument
114	push	%rbx
115.cfi_push	%rbx
116	push	%rbp
117.cfi_push	%rbp
118	push	%r12
119.cfi_push	%r12
120	push	%r13
121.cfi_push	%r13
122	push	%r14
123.cfi_push	%r14
124	push	%r15
125.cfi_push	%r15
126
127	neg	$num
128	mov	%rsp,%r11
129	lea	-280(%rsp,$num,8),%r10	# future alloca(8*(num+2)+256+8)
130	neg	$num			# restore $num
131	and	\$-1024,%r10		# minimize TLB usage
132
133	# An OS-agnostic version of __chkstk.
134	#
135	# Some OSes (Windows) insist on stack being "wired" to
136	# physical memory in strictly sequential manner, i.e. if stack
137	# allocation spans two pages, then reference to farmost one can
138	# be punishable by SEGV. But page walking can do good even on
139	# other OSes, because it guarantees that villain thread hits
140	# the guard page before it can make damage to innocent one...
141	sub	%r10,%r11
142	and	\$-4096,%r11
143	lea	(%r10,%r11),%rsp
144	mov	(%rsp),%r11
145	cmp	%r10,%rsp
146	ja	.Lmul_page_walk
147	jmp	.Lmul_page_walk_done
148
149.Lmul_page_walk:
150	lea	-4096(%rsp),%rsp
151	mov	(%rsp),%r11
152	cmp	%r10,%rsp
153	ja	.Lmul_page_walk
154.Lmul_page_walk_done:
155
156	lea	.Linc(%rip),%r10
157	mov	%rax,8(%rsp,$num,8)	# tp[num+1]=%rsp
158.cfi_cfa_expression	%rsp+8,$num,8,mul,plus,deref,+8
159.Lmul_body:
160
161	lea	128($bp),%r12		# reassign $bp (+size optimization)
162___
163		$bp="%r12";
164		$STRIDE=2**5*8;		# 5 is "window size"
165		$N=$STRIDE/4;		# should match cache line size
166$code.=<<___;
167	movdqa	0(%r10),%xmm0		# 00000001000000010000000000000000
168	movdqa	16(%r10),%xmm1		# 00000002000000020000000200000002
169	lea	24-112(%rsp,$num,8),%r10# place the mask after tp[num+3] (+ICache optimization)
170	and	\$-16,%r10
171
172	pshufd	\$0,%xmm5,%xmm5		# broadcast index
173	movdqa	%xmm1,%xmm4
174	movdqa	%xmm1,%xmm2
175___
176########################################################################
177# calculate mask by comparing 0..31 to index and save result to stack
178#
179$code.=<<___;
180	paddd	%xmm0,%xmm1
181	pcmpeqd	%xmm5,%xmm0		# compare to 1,0
182	.byte	0x67
183	movdqa	%xmm4,%xmm3
184___
185for($k=0;$k<$STRIDE/16-4;$k+=4) {
186$code.=<<___;
187	paddd	%xmm1,%xmm2
188	pcmpeqd	%xmm5,%xmm1		# compare to 3,2
189	movdqa	%xmm0,`16*($k+0)+112`(%r10)
190	movdqa	%xmm4,%xmm0
191
192	paddd	%xmm2,%xmm3
193	pcmpeqd	%xmm5,%xmm2		# compare to 5,4
194	movdqa	%xmm1,`16*($k+1)+112`(%r10)
195	movdqa	%xmm4,%xmm1
196
197	paddd	%xmm3,%xmm0
198	pcmpeqd	%xmm5,%xmm3		# compare to 7,6
199	movdqa	%xmm2,`16*($k+2)+112`(%r10)
200	movdqa	%xmm4,%xmm2
201
202	paddd	%xmm0,%xmm1
203	pcmpeqd	%xmm5,%xmm0
204	movdqa	%xmm3,`16*($k+3)+112`(%r10)
205	movdqa	%xmm4,%xmm3
206___
207}
208$code.=<<___;				# last iteration can be optimized
209	paddd	%xmm1,%xmm2
210	pcmpeqd	%xmm5,%xmm1
211	movdqa	%xmm0,`16*($k+0)+112`(%r10)
212
213	paddd	%xmm2,%xmm3
214	.byte	0x67
215	pcmpeqd	%xmm5,%xmm2
216	movdqa	%xmm1,`16*($k+1)+112`(%r10)
217
218	pcmpeqd	%xmm5,%xmm3
219	movdqa	%xmm2,`16*($k+2)+112`(%r10)
220	pand	`16*($k+0)-128`($bp),%xmm0	# while it's still in register
221
222	pand	`16*($k+1)-128`($bp),%xmm1
223	pand	`16*($k+2)-128`($bp),%xmm2
224	movdqa	%xmm3,`16*($k+3)+112`(%r10)
225	pand	`16*($k+3)-128`($bp),%xmm3
226	por	%xmm2,%xmm0
227	por	%xmm3,%xmm1
228___
229for($k=0;$k<$STRIDE/16-4;$k+=4) {
230$code.=<<___;
231	movdqa	`16*($k+0)-128`($bp),%xmm4
232	movdqa	`16*($k+1)-128`($bp),%xmm5
233	movdqa	`16*($k+2)-128`($bp),%xmm2
234	pand	`16*($k+0)+112`(%r10),%xmm4
235	movdqa	`16*($k+3)-128`($bp),%xmm3
236	pand	`16*($k+1)+112`(%r10),%xmm5
237	por	%xmm4,%xmm0
238	pand	`16*($k+2)+112`(%r10),%xmm2
239	por	%xmm5,%xmm1
240	pand	`16*($k+3)+112`(%r10),%xmm3
241	por	%xmm2,%xmm0
242	por	%xmm3,%xmm1
243___
244}
245$code.=<<___;
246	por	%xmm1,%xmm0
247	pshufd	\$0x4e,%xmm0,%xmm1
248	por	%xmm1,%xmm0
249	lea	$STRIDE($bp),$bp
250	movq	%xmm0,$m0		# m0=bp[0]
251
252	mov	($n0),$n0		# pull n0[0] value
253	mov	($ap),%rax
254
255	xor	$i,$i			# i=0
256	xor	$j,$j			# j=0
257
258	mov	$n0,$m1
259	mulq	$m0			# ap[0]*bp[0]
260	mov	%rax,$lo0
261	mov	($np),%rax
262
263	imulq	$lo0,$m1		# "tp[0]"*n0
264	mov	%rdx,$hi0
265
266	mulq	$m1			# np[0]*m1
267	add	%rax,$lo0		# discarded
268	mov	8($ap),%rax
269	adc	\$0,%rdx
270	mov	%rdx,$hi1
271
272	lea	1($j),$j		# j++
273	jmp	.L1st_enter
274
275.align	16
276.L1st:
277	add	%rax,$hi1
278	mov	($ap,$j,8),%rax
279	adc	\$0,%rdx
280	add	$hi0,$hi1		# np[j]*m1+ap[j]*bp[0]
281	mov	$lo0,$hi0
282	adc	\$0,%rdx
283	mov	$hi1,-16(%rsp,$j,8)	# tp[j-1]
284	mov	%rdx,$hi1
285
286.L1st_enter:
287	mulq	$m0			# ap[j]*bp[0]
288	add	%rax,$hi0
289	mov	($np,$j,8),%rax
290	adc	\$0,%rdx
291	lea	1($j),$j		# j++
292	mov	%rdx,$lo0
293
294	mulq	$m1			# np[j]*m1
295	cmp	$num,$j
296	jne	.L1st			# note that upon exit $j==$num, so
297					# they can be used interchangeably
298
299	add	%rax,$hi1
300	adc	\$0,%rdx
301	add	$hi0,$hi1		# np[j]*m1+ap[j]*bp[0]
302	adc	\$0,%rdx
303	mov	$hi1,-16(%rsp,$num,8)	# tp[num-1]
304	mov	%rdx,$hi1
305	mov	$lo0,$hi0
306
307	xor	%rdx,%rdx
308	add	$hi0,$hi1
309	adc	\$0,%rdx
310	mov	$hi1,-8(%rsp,$num,8)
311	mov	%rdx,(%rsp,$num,8)	# store upmost overflow bit
312
313	lea	1($i),$i		# i++
314	jmp	.Louter
315.align	16
316.Louter:
317	lea	24+128(%rsp,$num,8),%rdx	# where 256-byte mask is (+size optimization)
318	and	\$-16,%rdx
319	pxor	%xmm4,%xmm4
320	pxor	%xmm5,%xmm5
321___
322for($k=0;$k<$STRIDE/16;$k+=4) {
323$code.=<<___;
324	movdqa	`16*($k+0)-128`($bp),%xmm0
325	movdqa	`16*($k+1)-128`($bp),%xmm1
326	movdqa	`16*($k+2)-128`($bp),%xmm2
327	movdqa	`16*($k+3)-128`($bp),%xmm3
328	pand	`16*($k+0)-128`(%rdx),%xmm0
329	pand	`16*($k+1)-128`(%rdx),%xmm1
330	por	%xmm0,%xmm4
331	pand	`16*($k+2)-128`(%rdx),%xmm2
332	por	%xmm1,%xmm5
333	pand	`16*($k+3)-128`(%rdx),%xmm3
334	por	%xmm2,%xmm4
335	por	%xmm3,%xmm5
336___
337}
338$code.=<<___;
339	por	%xmm5,%xmm4
340	pshufd	\$0x4e,%xmm4,%xmm0
341	por	%xmm4,%xmm0
342	lea	$STRIDE($bp),$bp
343
344	mov	($ap),%rax		# ap[0]
345	movq	%xmm0,$m0		# m0=bp[i]
346
347	xor	$j,$j			# j=0
348	mov	$n0,$m1
349	mov	(%rsp),$lo0
350
351	mulq	$m0			# ap[0]*bp[i]
352	add	%rax,$lo0		# ap[0]*bp[i]+tp[0]
353	mov	($np),%rax
354	adc	\$0,%rdx
355
356	imulq	$lo0,$m1		# tp[0]*n0
357	mov	%rdx,$hi0
358
359	mulq	$m1			# np[0]*m1
360	add	%rax,$lo0		# discarded
361	mov	8($ap),%rax
362	adc	\$0,%rdx
363	mov	8(%rsp),$lo0		# tp[1]
364	mov	%rdx,$hi1
365
366	lea	1($j),$j		# j++
367	jmp	.Linner_enter
368
369.align	16
370.Linner:
371	add	%rax,$hi1
372	mov	($ap,$j,8),%rax
373	adc	\$0,%rdx
374	add	$lo0,$hi1		# np[j]*m1+ap[j]*bp[i]+tp[j]
375	mov	(%rsp,$j,8),$lo0
376	adc	\$0,%rdx
377	mov	$hi1,-16(%rsp,$j,8)	# tp[j-1]
378	mov	%rdx,$hi1
379
380.Linner_enter:
381	mulq	$m0			# ap[j]*bp[i]
382	add	%rax,$hi0
383	mov	($np,$j,8),%rax
384	adc	\$0,%rdx
385	add	$hi0,$lo0		# ap[j]*bp[i]+tp[j]
386	mov	%rdx,$hi0
387	adc	\$0,$hi0
388	lea	1($j),$j		# j++
389
390	mulq	$m1			# np[j]*m1
391	cmp	$num,$j
392	jne	.Linner			# note that upon exit $j==$num, so
393					# they can be used interchangeably
394	add	%rax,$hi1
395	adc	\$0,%rdx
396	add	$lo0,$hi1		# np[j]*m1+ap[j]*bp[i]+tp[j]
397	mov	(%rsp,$num,8),$lo0
398	adc	\$0,%rdx
399	mov	$hi1,-16(%rsp,$num,8)	# tp[num-1]
400	mov	%rdx,$hi1
401
402	xor	%rdx,%rdx
403	add	$hi0,$hi1
404	adc	\$0,%rdx
405	add	$lo0,$hi1		# pull upmost overflow bit
406	adc	\$0,%rdx
407	mov	$hi1,-8(%rsp,$num,8)
408	mov	%rdx,(%rsp,$num,8)	# store upmost overflow bit
409
410	lea	1($i),$i		# i++
411	cmp	$num,$i
412	jb	.Louter
413
414	xor	$i,$i			# i=0 and clear CF!
415	mov	(%rsp),%rax		# tp[0]
416	lea	(%rsp),$ap		# borrow ap for tp
417	mov	$num,$j			# j=num
418	jmp	.Lsub
419.align	16
420.Lsub:	sbb	($np,$i,8),%rax
421	mov	%rax,($rp,$i,8)		# rp[i]=tp[i]-np[i]
422	mov	8($ap,$i,8),%rax	# tp[i+1]
423	lea	1($i),$i		# i++
424	dec	$j			# doesn't affect CF!
425	jnz	.Lsub
426
427	sbb	\$0,%rax		# handle upmost overflow bit
428	mov	\$-1,%rbx
429	xor	%rax,%rbx
430	xor	$i,$i
431	mov	$num,$j			# j=num
432
433.Lcopy:					# conditional copy
434	mov	($rp,$i,8),%rcx
435	mov	(%rsp,$i,8),%rdx
436	and	%rbx,%rcx
437	and	%rax,%rdx
438	mov	$i,(%rsp,$i,8)		# zap temporary vector
439	or	%rcx,%rdx
440	mov	%rdx,($rp,$i,8)		# rp[i]=tp[i]
441	lea	1($i),$i
442	sub	\$1,$j
443	jnz	.Lcopy
444
445	mov	8(%rsp,$num,8),%rsi	# restore %rsp
446.cfi_def_cfa	%rsi,8
447	mov	\$1,%rax
448
449	mov	-48(%rsi),%r15
450.cfi_restore	%r15
451	mov	-40(%rsi),%r14
452.cfi_restore	%r14
453	mov	-32(%rsi),%r13
454.cfi_restore	%r13
455	mov	-24(%rsi),%r12
456.cfi_restore	%r12
457	mov	-16(%rsi),%rbp
458.cfi_restore	%rbp
459	mov	-8(%rsi),%rbx
460.cfi_restore	%rbx
461	lea	(%rsi),%rsp
462.cfi_def_cfa_register	%rsp
463.Lmul_epilogue:
464	ret
465.cfi_endproc
466.size	bn_mul_mont_gather5,.-bn_mul_mont_gather5
467___
468{{{
469my @A=("%r10","%r11");
470my @N=("%r13","%rdi");
471$code.=<<___;
472.type	bn_mul4x_mont_gather5,\@function,6
473.align	32
474bn_mul4x_mont_gather5:
475.cfi_startproc
476	.byte	0x67
477	mov	%rsp,%rax
478.cfi_def_cfa_register	%rax
479.Lmul4x_enter:
480___
481$code.=<<___ if ($addx);
482	and	\$0x80108,%r11d
483	cmp	\$0x80108,%r11d		# check for AD*X+BMI2+BMI1
484	je	.Lmulx4x_enter
485___
486$code.=<<___;
487	push	%rbx
488.cfi_push	%rbx
489	push	%rbp
490.cfi_push	%rbp
491	push	%r12
492.cfi_push	%r12
493	push	%r13
494.cfi_push	%r13
495	push	%r14
496.cfi_push	%r14
497	push	%r15
498.cfi_push	%r15
499.Lmul4x_prologue:
500
501	.byte	0x67
502	shl	\$3,${num}d		# convert $num to bytes
503	lea	($num,$num,2),%r10	# 3*$num in bytes
504	neg	$num			# -$num
505
506	##############################################################
507	# Ensure that stack frame doesn't alias with $rptr+3*$num
508	# modulo 4096, which covers ret[num], am[num] and n[num]
509	# (see bn_exp.c). This is done to allow memory disambiguation
510	# logic do its magic. [Extra [num] is allocated in order
511	# to align with bn_power5's frame, which is cleansed after
512	# completing exponentiation. Extra 256 bytes is for power mask
513	# calculated from 7th argument, the index.]
514	#
515	lea	-320(%rsp,$num,2),%r11
516	mov	%rsp,%rbp
517	sub	$rp,%r11
518	and	\$4095,%r11
519	cmp	%r11,%r10
520	jb	.Lmul4xsp_alt
521	sub	%r11,%rbp		# align with $rp
522	lea	-320(%rbp,$num,2),%rbp	# future alloca(frame+2*num*8+256)
523	jmp	.Lmul4xsp_done
524
525.align	32
526.Lmul4xsp_alt:
527	lea	4096-320(,$num,2),%r10
528	lea	-320(%rbp,$num,2),%rbp	# future alloca(frame+2*num*8+256)
529	sub	%r10,%r11
530	mov	\$0,%r10
531	cmovc	%r10,%r11
532	sub	%r11,%rbp
533.Lmul4xsp_done:
534	and	\$-64,%rbp
535	mov	%rsp,%r11
536	sub	%rbp,%r11
537	and	\$-4096,%r11
538	lea	(%rbp,%r11),%rsp
539	mov	(%rsp),%r10
540	cmp	%rbp,%rsp
541	ja	.Lmul4x_page_walk
542	jmp	.Lmul4x_page_walk_done
543
544.Lmul4x_page_walk:
545	lea	-4096(%rsp),%rsp
546	mov	(%rsp),%r10
547	cmp	%rbp,%rsp
548	ja	.Lmul4x_page_walk
549.Lmul4x_page_walk_done:
550
551	neg	$num
552
553	mov	%rax,40(%rsp)
554.cfi_cfa_expression	%rsp+40,deref,+8
555.Lmul4x_body:
556
557	call	mul4x_internal
558
559	mov	40(%rsp),%rsi		# restore %rsp
560.cfi_def_cfa	%rsi,8
561	mov	\$1,%rax
562
563	mov	-48(%rsi),%r15
564.cfi_restore	%r15
565	mov	-40(%rsi),%r14
566.cfi_restore	%r14
567	mov	-32(%rsi),%r13
568.cfi_restore	%r13
569	mov	-24(%rsi),%r12
570.cfi_restore	%r12
571	mov	-16(%rsi),%rbp
572.cfi_restore	%rbp
573	mov	-8(%rsi),%rbx
574.cfi_restore	%rbx
575	lea	(%rsi),%rsp
576.cfi_def_cfa_register	%rsp
577.Lmul4x_epilogue:
578	ret
579.cfi_endproc
580.size	bn_mul4x_mont_gather5,.-bn_mul4x_mont_gather5
581
582.type	mul4x_internal,\@abi-omnipotent
583.align	32
584mul4x_internal:
585.cfi_startproc
586	shl	\$5,$num		# $num was in bytes
587	movd	`($win64?56:8)`(%rax),%xmm5	# load 7th argument, index
588	lea	.Linc(%rip),%rax
589	lea	128(%rdx,$num),%r13	# end of powers table (+size optimization)
590	shr	\$5,$num		# restore $num
591___
592		$bp="%r12";
593		$STRIDE=2**5*8;		# 5 is "window size"
594		$N=$STRIDE/4;		# should match cache line size
595		$tp=$i;
596$code.=<<___;
597	movdqa	0(%rax),%xmm0		# 00000001000000010000000000000000
598	movdqa	16(%rax),%xmm1		# 00000002000000020000000200000002
599	lea	88-112(%rsp,$num),%r10	# place the mask after tp[num+1] (+ICache optimization)
600	lea	128(%rdx),$bp		# size optimization
601
602	pshufd	\$0,%xmm5,%xmm5		# broadcast index
603	movdqa	%xmm1,%xmm4
604	.byte	0x67,0x67
605	movdqa	%xmm1,%xmm2
606___
607########################################################################
608# calculate mask by comparing 0..31 to index and save result to stack
609#
610$code.=<<___;
611	paddd	%xmm0,%xmm1
612	pcmpeqd	%xmm5,%xmm0		# compare to 1,0
613	.byte	0x67
614	movdqa	%xmm4,%xmm3
615___
616for($i=0;$i<$STRIDE/16-4;$i+=4) {
617$code.=<<___;
618	paddd	%xmm1,%xmm2
619	pcmpeqd	%xmm5,%xmm1		# compare to 3,2
620	movdqa	%xmm0,`16*($i+0)+112`(%r10)
621	movdqa	%xmm4,%xmm0
622
623	paddd	%xmm2,%xmm3
624	pcmpeqd	%xmm5,%xmm2		# compare to 5,4
625	movdqa	%xmm1,`16*($i+1)+112`(%r10)
626	movdqa	%xmm4,%xmm1
627
628	paddd	%xmm3,%xmm0
629	pcmpeqd	%xmm5,%xmm3		# compare to 7,6
630	movdqa	%xmm2,`16*($i+2)+112`(%r10)
631	movdqa	%xmm4,%xmm2
632
633	paddd	%xmm0,%xmm1
634	pcmpeqd	%xmm5,%xmm0
635	movdqa	%xmm3,`16*($i+3)+112`(%r10)
636	movdqa	%xmm4,%xmm3
637___
638}
639$code.=<<___;				# last iteration can be optimized
640	paddd	%xmm1,%xmm2
641	pcmpeqd	%xmm5,%xmm1
642	movdqa	%xmm0,`16*($i+0)+112`(%r10)
643
644	paddd	%xmm2,%xmm3
645	.byte	0x67
646	pcmpeqd	%xmm5,%xmm2
647	movdqa	%xmm1,`16*($i+1)+112`(%r10)
648
649	pcmpeqd	%xmm5,%xmm3
650	movdqa	%xmm2,`16*($i+2)+112`(%r10)
651	pand	`16*($i+0)-128`($bp),%xmm0	# while it's still in register
652
653	pand	`16*($i+1)-128`($bp),%xmm1
654	pand	`16*($i+2)-128`($bp),%xmm2
655	movdqa	%xmm3,`16*($i+3)+112`(%r10)
656	pand	`16*($i+3)-128`($bp),%xmm3
657	por	%xmm2,%xmm0
658	por	%xmm3,%xmm1
659___
660for($i=0;$i<$STRIDE/16-4;$i+=4) {
661$code.=<<___;
662	movdqa	`16*($i+0)-128`($bp),%xmm4
663	movdqa	`16*($i+1)-128`($bp),%xmm5
664	movdqa	`16*($i+2)-128`($bp),%xmm2
665	pand	`16*($i+0)+112`(%r10),%xmm4
666	movdqa	`16*($i+3)-128`($bp),%xmm3
667	pand	`16*($i+1)+112`(%r10),%xmm5
668	por	%xmm4,%xmm0
669	pand	`16*($i+2)+112`(%r10),%xmm2
670	por	%xmm5,%xmm1
671	pand	`16*($i+3)+112`(%r10),%xmm3
672	por	%xmm2,%xmm0
673	por	%xmm3,%xmm1
674___
675}
676$code.=<<___;
677	por	%xmm1,%xmm0
678	pshufd	\$0x4e,%xmm0,%xmm1
679	por	%xmm1,%xmm0
680	lea	$STRIDE($bp),$bp
681	movq	%xmm0,$m0		# m0=bp[0]
682
683	mov	%r13,16+8(%rsp)		# save end of b[num]
684	mov	$rp, 56+8(%rsp)		# save $rp
685
686	mov	($n0),$n0		# pull n0[0] value
687	mov	($ap),%rax
688	lea	($ap,$num),$ap		# end of a[num]
689	neg	$num
690
691	mov	$n0,$m1
692	mulq	$m0			# ap[0]*bp[0]
693	mov	%rax,$A[0]
694	mov	($np),%rax
695
696	imulq	$A[0],$m1		# "tp[0]"*n0
697	lea	64+8(%rsp),$tp
698	mov	%rdx,$A[1]
699
700	mulq	$m1			# np[0]*m1
701	add	%rax,$A[0]		# discarded
702	mov	8($ap,$num),%rax
703	adc	\$0,%rdx
704	mov	%rdx,$N[1]
705
706	mulq	$m0
707	add	%rax,$A[1]
708	mov	8*1($np),%rax
709	adc	\$0,%rdx
710	mov	%rdx,$A[0]
711
712	mulq	$m1
713	add	%rax,$N[1]
714	mov	16($ap,$num),%rax
715	adc	\$0,%rdx
716	add	$A[1],$N[1]
717	lea	4*8($num),$j		# j=4
718	lea	8*4($np),$np
719	adc	\$0,%rdx
720	mov	$N[1],($tp)
721	mov	%rdx,$N[0]
722	jmp	.L1st4x
723
724.align	32
725.L1st4x:
726	mulq	$m0			# ap[j]*bp[0]
727	add	%rax,$A[0]
728	mov	-8*2($np),%rax
729	lea	32($tp),$tp
730	adc	\$0,%rdx
731	mov	%rdx,$A[1]
732
733	mulq	$m1			# np[j]*m1
734	add	%rax,$N[0]
735	mov	-8($ap,$j),%rax
736	adc	\$0,%rdx
737	add	$A[0],$N[0]		# np[j]*m1+ap[j]*bp[0]
738	adc	\$0,%rdx
739	mov	$N[0],-24($tp)		# tp[j-1]
740	mov	%rdx,$N[1]
741
742	mulq	$m0			# ap[j]*bp[0]
743	add	%rax,$A[1]
744	mov	-8*1($np),%rax
745	adc	\$0,%rdx
746	mov	%rdx,$A[0]
747
748	mulq	$m1			# np[j]*m1
749	add	%rax,$N[1]
750	mov	($ap,$j),%rax
751	adc	\$0,%rdx
752	add	$A[1],$N[1]		# np[j]*m1+ap[j]*bp[0]
753	adc	\$0,%rdx
754	mov	$N[1],-16($tp)		# tp[j-1]
755	mov	%rdx,$N[0]
756
757	mulq	$m0			# ap[j]*bp[0]
758	add	%rax,$A[0]
759	mov	8*0($np),%rax
760	adc	\$0,%rdx
761	mov	%rdx,$A[1]
762
763	mulq	$m1			# np[j]*m1
764	add	%rax,$N[0]
765	mov	8($ap,$j),%rax
766	adc	\$0,%rdx
767	add	$A[0],$N[0]		# np[j]*m1+ap[j]*bp[0]
768	adc	\$0,%rdx
769	mov	$N[0],-8($tp)		# tp[j-1]
770	mov	%rdx,$N[1]
771
772	mulq	$m0			# ap[j]*bp[0]
773	add	%rax,$A[1]
774	mov	8*1($np),%rax
775	adc	\$0,%rdx
776	mov	%rdx,$A[0]
777
778	mulq	$m1			# np[j]*m1
779	add	%rax,$N[1]
780	mov	16($ap,$j),%rax
781	adc	\$0,%rdx
782	add	$A[1],$N[1]		# np[j]*m1+ap[j]*bp[0]
783	lea	8*4($np),$np
784	adc	\$0,%rdx
785	mov	$N[1],($tp)		# tp[j-1]
786	mov	%rdx,$N[0]
787
788	add	\$32,$j			# j+=4
789	jnz	.L1st4x
790
791	mulq	$m0			# ap[j]*bp[0]
792	add	%rax,$A[0]
793	mov	-8*2($np),%rax
794	lea	32($tp),$tp
795	adc	\$0,%rdx
796	mov	%rdx,$A[1]
797
798	mulq	$m1			# np[j]*m1
799	add	%rax,$N[0]
800	mov	-8($ap),%rax
801	adc	\$0,%rdx
802	add	$A[0],$N[0]		# np[j]*m1+ap[j]*bp[0]
803	adc	\$0,%rdx
804	mov	$N[0],-24($tp)		# tp[j-1]
805	mov	%rdx,$N[1]
806
807	mulq	$m0			# ap[j]*bp[0]
808	add	%rax,$A[1]
809	mov	-8*1($np),%rax
810	adc	\$0,%rdx
811	mov	%rdx,$A[0]
812
813	mulq	$m1			# np[j]*m1
814	add	%rax,$N[1]
815	mov	($ap,$num),%rax		# ap[0]
816	adc	\$0,%rdx
817	add	$A[1],$N[1]		# np[j]*m1+ap[j]*bp[0]
818	adc	\$0,%rdx
819	mov	$N[1],-16($tp)		# tp[j-1]
820	mov	%rdx,$N[0]
821
822	lea	($np,$num),$np		# rewind $np
823
824	xor	$N[1],$N[1]
825	add	$A[0],$N[0]
826	adc	\$0,$N[1]
827	mov	$N[0],-8($tp)
828
829	jmp	.Louter4x
830
831.align	32
832.Louter4x:
833	lea	16+128($tp),%rdx	# where 256-byte mask is (+size optimization)
834	pxor	%xmm4,%xmm4
835	pxor	%xmm5,%xmm5
836___
837for($i=0;$i<$STRIDE/16;$i+=4) {
838$code.=<<___;
839	movdqa	`16*($i+0)-128`($bp),%xmm0
840	movdqa	`16*($i+1)-128`($bp),%xmm1
841	movdqa	`16*($i+2)-128`($bp),%xmm2
842	movdqa	`16*($i+3)-128`($bp),%xmm3
843	pand	`16*($i+0)-128`(%rdx),%xmm0
844	pand	`16*($i+1)-128`(%rdx),%xmm1
845	por	%xmm0,%xmm4
846	pand	`16*($i+2)-128`(%rdx),%xmm2
847	por	%xmm1,%xmm5
848	pand	`16*($i+3)-128`(%rdx),%xmm3
849	por	%xmm2,%xmm4
850	por	%xmm3,%xmm5
851___
852}
853$code.=<<___;
854	por	%xmm5,%xmm4
855	pshufd	\$0x4e,%xmm4,%xmm0
856	por	%xmm4,%xmm0
857	lea	$STRIDE($bp),$bp
858	movq	%xmm0,$m0		# m0=bp[i]
859
860	mov	($tp,$num),$A[0]
861	mov	$n0,$m1
862	mulq	$m0			# ap[0]*bp[i]
863	add	%rax,$A[0]		# ap[0]*bp[i]+tp[0]
864	mov	($np),%rax
865	adc	\$0,%rdx
866
867	imulq	$A[0],$m1		# tp[0]*n0
868	mov	%rdx,$A[1]
869	mov	$N[1],($tp)		# store upmost overflow bit
870
871	lea	($tp,$num),$tp		# rewind $tp
872
873	mulq	$m1			# np[0]*m1
874	add	%rax,$A[0]		# "$N[0]", discarded
875	mov	8($ap,$num),%rax
876	adc	\$0,%rdx
877	mov	%rdx,$N[1]
878
879	mulq	$m0			# ap[j]*bp[i]
880	add	%rax,$A[1]
881	mov	8*1($np),%rax
882	adc	\$0,%rdx
883	add	8($tp),$A[1]		# +tp[1]
884	adc	\$0,%rdx
885	mov	%rdx,$A[0]
886
887	mulq	$m1			# np[j]*m1
888	add	%rax,$N[1]
889	mov	16($ap,$num),%rax
890	adc	\$0,%rdx
891	add	$A[1],$N[1]		# np[j]*m1+ap[j]*bp[i]+tp[j]
892	lea	4*8($num),$j		# j=4
893	lea	8*4($np),$np
894	adc	\$0,%rdx
895	mov	%rdx,$N[0]
896	jmp	.Linner4x
897
898.align	32
899.Linner4x:
900	mulq	$m0			# ap[j]*bp[i]
901	add	%rax,$A[0]
902	mov	-8*2($np),%rax
903	adc	\$0,%rdx
904	add	16($tp),$A[0]		# ap[j]*bp[i]+tp[j]
905	lea	32($tp),$tp
906	adc	\$0,%rdx
907	mov	%rdx,$A[1]
908
909	mulq	$m1			# np[j]*m1
910	add	%rax,$N[0]
911	mov	-8($ap,$j),%rax
912	adc	\$0,%rdx
913	add	$A[0],$N[0]
914	adc	\$0,%rdx
915	mov	$N[1],-32($tp)		# tp[j-1]
916	mov	%rdx,$N[1]
917
918	mulq	$m0			# ap[j]*bp[i]
919	add	%rax,$A[1]
920	mov	-8*1($np),%rax
921	adc	\$0,%rdx
922	add	-8($tp),$A[1]
923	adc	\$0,%rdx
924	mov	%rdx,$A[0]
925
926	mulq	$m1			# np[j]*m1
927	add	%rax,$N[1]
928	mov	($ap,$j),%rax
929	adc	\$0,%rdx
930	add	$A[1],$N[1]
931	adc	\$0,%rdx
932	mov	$N[0],-24($tp)		# tp[j-1]
933	mov	%rdx,$N[0]
934
935	mulq	$m0			# ap[j]*bp[i]
936	add	%rax,$A[0]
937	mov	8*0($np),%rax
938	adc	\$0,%rdx
939	add	($tp),$A[0]		# ap[j]*bp[i]+tp[j]
940	adc	\$0,%rdx
941	mov	%rdx,$A[1]
942
943	mulq	$m1			# np[j]*m1
944	add	%rax,$N[0]
945	mov	8($ap,$j),%rax
946	adc	\$0,%rdx
947	add	$A[0],$N[0]
948	adc	\$0,%rdx
949	mov	$N[1],-16($tp)		# tp[j-1]
950	mov	%rdx,$N[1]
951
952	mulq	$m0			# ap[j]*bp[i]
953	add	%rax,$A[1]
954	mov	8*1($np),%rax
955	adc	\$0,%rdx
956	add	8($tp),$A[1]
957	adc	\$0,%rdx
958	mov	%rdx,$A[0]
959
960	mulq	$m1			# np[j]*m1
961	add	%rax,$N[1]
962	mov	16($ap,$j),%rax
963	adc	\$0,%rdx
964	add	$A[1],$N[1]
965	lea	8*4($np),$np
966	adc	\$0,%rdx
967	mov	$N[0],-8($tp)		# tp[j-1]
968	mov	%rdx,$N[0]
969
970	add	\$32,$j			# j+=4
971	jnz	.Linner4x
972
973	mulq	$m0			# ap[j]*bp[i]
974	add	%rax,$A[0]
975	mov	-8*2($np),%rax
976	adc	\$0,%rdx
977	add	16($tp),$A[0]		# ap[j]*bp[i]+tp[j]
978	lea	32($tp),$tp
979	adc	\$0,%rdx
980	mov	%rdx,$A[1]
981
982	mulq	$m1			# np[j]*m1
983	add	%rax,$N[0]
984	mov	-8($ap),%rax
985	adc	\$0,%rdx
986	add	$A[0],$N[0]
987	adc	\$0,%rdx
988	mov	$N[1],-32($tp)		# tp[j-1]
989	mov	%rdx,$N[1]
990
991	mulq	$m0			# ap[j]*bp[i]
992	add	%rax,$A[1]
993	mov	$m1,%rax
994	mov	-8*1($np),$m1
995	adc	\$0,%rdx
996	add	-8($tp),$A[1]
997	adc	\$0,%rdx
998	mov	%rdx,$A[0]
999
1000	mulq	$m1			# np[j]*m1
1001	add	%rax,$N[1]
1002	mov	($ap,$num),%rax		# ap[0]
1003	adc	\$0,%rdx
1004	add	$A[1],$N[1]
1005	adc	\$0,%rdx
1006	mov	$N[0],-24($tp)		# tp[j-1]
1007	mov	%rdx,$N[0]
1008
1009	mov	$N[1],-16($tp)		# tp[j-1]
1010	lea	($np,$num),$np		# rewind $np
1011
1012	xor	$N[1],$N[1]
1013	add	$A[0],$N[0]
1014	adc	\$0,$N[1]
1015	add	($tp),$N[0]		# pull upmost overflow bit
1016	adc	\$0,$N[1]		# upmost overflow bit
1017	mov	$N[0],-8($tp)
1018
1019	cmp	16+8(%rsp),$bp
1020	jb	.Louter4x
1021___
1022if (1) {
1023$code.=<<___;
1024	xor	%rax,%rax
1025	sub	$N[0],$m1		# compare top-most words
1026	adc	$j,$j			# $j is zero
1027	or	$j,$N[1]
1028	sub	$N[1],%rax		# %rax=-$N[1]
1029	lea	($tp,$num),%rbx		# tptr in .sqr4x_sub
1030	mov	($np),%r12
1031	lea	($np),%rbp		# nptr in .sqr4x_sub
1032	mov	%r9,%rcx
1033	sar	\$3+2,%rcx
1034	mov	56+8(%rsp),%rdi		# rptr in .sqr4x_sub
1035	dec	%r12			# so that after 'not' we get -n[0]
1036	xor	%r10,%r10
1037	mov	8*1(%rbp),%r13
1038	mov	8*2(%rbp),%r14
1039	mov	8*3(%rbp),%r15
1040	jmp	.Lsqr4x_sub_entry
1041___
1042} else {
1043my @ri=("%rax",$bp,$m0,$m1);
1044my $rp="%rdx";
1045$code.=<<___
1046	xor	\$1,$N[1]
1047	lea	($tp,$num),$tp		# rewind $tp
1048	sar	\$5,$num		# cf=0
1049	lea	($np,$N[1],8),$np
1050	mov	56+8(%rsp),$rp		# restore $rp
1051	jmp	.Lsub4x
1052
1053.align	32
1054.Lsub4x:
1055	.byte	0x66
1056	mov	8*0($tp),@ri[0]
1057	mov	8*1($tp),@ri[1]
1058	.byte	0x66
1059	sbb	16*0($np),@ri[0]
1060	mov	8*2($tp),@ri[2]
1061	sbb	16*1($np),@ri[1]
1062	mov	3*8($tp),@ri[3]
1063	lea	4*8($tp),$tp
1064	sbb	16*2($np),@ri[2]
1065	mov	@ri[0],8*0($rp)
1066	sbb	16*3($np),@ri[3]
1067	lea	16*4($np),$np
1068	mov	@ri[1],8*1($rp)
1069	mov	@ri[2],8*2($rp)
1070	mov	@ri[3],8*3($rp)
1071	lea	8*4($rp),$rp
1072
1073	inc	$num
1074	jnz	.Lsub4x
1075
1076	ret
1077___
1078}
1079$code.=<<___;
1080.cfi_endproc
1081.size	mul4x_internal,.-mul4x_internal
1082___
1083}}}
1084{{{
1085######################################################################
1086# void bn_power5(
1087my $rptr="%rdi";	# BN_ULONG *rptr,
1088my $aptr="%rsi";	# const BN_ULONG *aptr,
1089my $bptr="%rdx";	# const void *table,
1090my $nptr="%rcx";	# const BN_ULONG *nptr,
1091my $n0  ="%r8";		# const BN_ULONG *n0);
1092my $num ="%r9";		# int num, has to be divisible by 8
1093			# int pwr
1094
1095my ($i,$j,$tptr)=("%rbp","%rcx",$rptr);
1096my @A0=("%r10","%r11");
1097my @A1=("%r12","%r13");
1098my ($a0,$a1,$ai)=("%r14","%r15","%rbx");
1099
1100$code.=<<___;
1101.globl	bn_power5
1102.type	bn_power5,\@function,6
1103.align	32
1104bn_power5:
1105.cfi_startproc
1106	mov	%rsp,%rax
1107.cfi_def_cfa_register	%rax
1108___
1109$code.=<<___ if ($addx);
1110	mov	OPENSSL_ia32cap_P+8(%rip),%r11d
1111	and	\$0x80108,%r11d
1112	cmp	\$0x80108,%r11d		# check for AD*X+BMI2+BMI1
1113	je	.Lpowerx5_enter
1114___
1115$code.=<<___;
1116	push	%rbx
1117.cfi_push	%rbx
1118	push	%rbp
1119.cfi_push	%rbp
1120	push	%r12
1121.cfi_push	%r12
1122	push	%r13
1123.cfi_push	%r13
1124	push	%r14
1125.cfi_push	%r14
1126	push	%r15
1127.cfi_push	%r15
1128.Lpower5_prologue:
1129
1130	shl	\$3,${num}d		# convert $num to bytes
1131	lea	($num,$num,2),%r10d	# 3*$num
1132	neg	$num
1133	mov	($n0),$n0		# *n0
1134
1135	##############################################################
1136	# Ensure that stack frame doesn't alias with $rptr+3*$num
1137	# modulo 4096, which covers ret[num], am[num] and n[num]
1138	# (see bn_exp.c). This is done to allow memory disambiguation
1139	# logic do its magic. [Extra 256 bytes is for power mask
1140	# calculated from 7th argument, the index.]
1141	#
1142	lea	-320(%rsp,$num,2),%r11
1143	mov	%rsp,%rbp
1144	sub	$rptr,%r11
1145	and	\$4095,%r11
1146	cmp	%r11,%r10
1147	jb	.Lpwr_sp_alt
1148	sub	%r11,%rbp		# align with $aptr
1149	lea	-320(%rbp,$num,2),%rbp	# future alloca(frame+2*num*8+256)
1150	jmp	.Lpwr_sp_done
1151
1152.align	32
1153.Lpwr_sp_alt:
1154	lea	4096-320(,$num,2),%r10
1155	lea	-320(%rbp,$num,2),%rbp	# future alloca(frame+2*num*8+256)
1156	sub	%r10,%r11
1157	mov	\$0,%r10
1158	cmovc	%r10,%r11
1159	sub	%r11,%rbp
1160.Lpwr_sp_done:
1161	and	\$-64,%rbp
1162	mov	%rsp,%r11
1163	sub	%rbp,%r11
1164	and	\$-4096,%r11
1165	lea	(%rbp,%r11),%rsp
1166	mov	(%rsp),%r10
1167	cmp	%rbp,%rsp
1168	ja	.Lpwr_page_walk
1169	jmp	.Lpwr_page_walk_done
1170
1171.Lpwr_page_walk:
1172	lea	-4096(%rsp),%rsp
1173	mov	(%rsp),%r10
1174	cmp	%rbp,%rsp
1175	ja	.Lpwr_page_walk
1176.Lpwr_page_walk_done:
1177
1178	mov	$num,%r10
1179	neg	$num
1180
1181	##############################################################
1182	# Stack layout
1183	#
1184	# +0	saved $num, used in reduction section
1185	# +8	&t[2*$num], used in reduction section
1186	# +32	saved *n0
1187	# +40	saved %rsp
1188	# +48	t[2*$num]
1189	#
1190	mov	$n0,  32(%rsp)
1191	mov	%rax, 40(%rsp)		# save original %rsp
1192.cfi_cfa_expression	%rsp+40,deref,+8
1193.Lpower5_body:
1194	movq	$rptr,%xmm1		# save $rptr, used in sqr8x
1195	movq	$nptr,%xmm2		# save $nptr
1196	movq	%r10, %xmm3		# -$num, used in sqr8x
1197	movq	$bptr,%xmm4
1198
1199	call	__bn_sqr8x_internal
1200	call	__bn_post4x_internal
1201	call	__bn_sqr8x_internal
1202	call	__bn_post4x_internal
1203	call	__bn_sqr8x_internal
1204	call	__bn_post4x_internal
1205	call	__bn_sqr8x_internal
1206	call	__bn_post4x_internal
1207	call	__bn_sqr8x_internal
1208	call	__bn_post4x_internal
1209
1210	movq	%xmm2,$nptr
1211	movq	%xmm4,$bptr
1212	mov	$aptr,$rptr
1213	mov	40(%rsp),%rax
1214	lea	32(%rsp),$n0
1215
1216	call	mul4x_internal
1217
1218	mov	40(%rsp),%rsi		# restore %rsp
1219.cfi_def_cfa	%rsi,8
1220	mov	\$1,%rax
1221	mov	-48(%rsi),%r15
1222.cfi_restore	%r15
1223	mov	-40(%rsi),%r14
1224.cfi_restore	%r14
1225	mov	-32(%rsi),%r13
1226.cfi_restore	%r13
1227	mov	-24(%rsi),%r12
1228.cfi_restore	%r12
1229	mov	-16(%rsi),%rbp
1230.cfi_restore	%rbp
1231	mov	-8(%rsi),%rbx
1232.cfi_restore	%rbx
1233	lea	(%rsi),%rsp
1234.cfi_def_cfa_register	%rsp
1235.Lpower5_epilogue:
1236	ret
1237.cfi_endproc
1238.size	bn_power5,.-bn_power5
1239
1240.globl	bn_sqr8x_internal
1241.hidden	bn_sqr8x_internal
1242.type	bn_sqr8x_internal,\@abi-omnipotent
1243.align	32
1244bn_sqr8x_internal:
1245__bn_sqr8x_internal:
1246.cfi_startproc
1247	##############################################################
1248	# Squaring part:
1249	#
1250	# a) multiply-n-add everything but a[i]*a[i];
1251	# b) shift result of a) by 1 to the left and accumulate
1252	#    a[i]*a[i] products;
1253	#
1254	##############################################################
1255	#                                                     a[1]a[0]
1256	#                                                 a[2]a[0]
1257	#                                             a[3]a[0]
1258	#                                             a[2]a[1]
1259	#                                         a[4]a[0]
1260	#                                         a[3]a[1]
1261	#                                     a[5]a[0]
1262	#                                     a[4]a[1]
1263	#                                     a[3]a[2]
1264	#                                 a[6]a[0]
1265	#                                 a[5]a[1]
1266	#                                 a[4]a[2]
1267	#                             a[7]a[0]
1268	#                             a[6]a[1]
1269	#                             a[5]a[2]
1270	#                             a[4]a[3]
1271	#                         a[7]a[1]
1272	#                         a[6]a[2]
1273	#                         a[5]a[3]
1274	#                     a[7]a[2]
1275	#                     a[6]a[3]
1276	#                     a[5]a[4]
1277	#                 a[7]a[3]
1278	#                 a[6]a[4]
1279	#             a[7]a[4]
1280	#             a[6]a[5]
1281	#         a[7]a[5]
1282	#     a[7]a[6]
1283	#                                                     a[1]a[0]
1284	#                                                 a[2]a[0]
1285	#                                             a[3]a[0]
1286	#                                         a[4]a[0]
1287	#                                     a[5]a[0]
1288	#                                 a[6]a[0]
1289	#                             a[7]a[0]
1290	#                                             a[2]a[1]
1291	#                                         a[3]a[1]
1292	#                                     a[4]a[1]
1293	#                                 a[5]a[1]
1294	#                             a[6]a[1]
1295	#                         a[7]a[1]
1296	#                                     a[3]a[2]
1297	#                                 a[4]a[2]
1298	#                             a[5]a[2]
1299	#                         a[6]a[2]
1300	#                     a[7]a[2]
1301	#                             a[4]a[3]
1302	#                         a[5]a[3]
1303	#                     a[6]a[3]
1304	#                 a[7]a[3]
1305	#                     a[5]a[4]
1306	#                 a[6]a[4]
1307	#             a[7]a[4]
1308	#             a[6]a[5]
1309	#         a[7]a[5]
1310	#     a[7]a[6]
1311	#                                                         a[0]a[0]
1312	#                                                 a[1]a[1]
1313	#                                         a[2]a[2]
1314	#                                 a[3]a[3]
1315	#                         a[4]a[4]
1316	#                 a[5]a[5]
1317	#         a[6]a[6]
1318	# a[7]a[7]
1319
1320	lea	32(%r10),$i		# $i=-($num-32)
1321	lea	($aptr,$num),$aptr	# end of a[] buffer, ($aptr,$i)=&ap[2]
1322
1323	mov	$num,$j			# $j=$num
1324
1325					# comments apply to $num==8 case
1326	mov	-32($aptr,$i),$a0	# a[0]
1327	lea	48+8(%rsp,$num,2),$tptr	# end of tp[] buffer, &tp[2*$num]
1328	mov	-24($aptr,$i),%rax	# a[1]
1329	lea	-32($tptr,$i),$tptr	# end of tp[] window, &tp[2*$num-"$i"]
1330	mov	-16($aptr,$i),$ai	# a[2]
1331	mov	%rax,$a1
1332
1333	mul	$a0			# a[1]*a[0]
1334	mov	%rax,$A0[0]		# a[1]*a[0]
1335	 mov	$ai,%rax		# a[2]
1336	mov	%rdx,$A0[1]
1337	mov	$A0[0],-24($tptr,$i)	# t[1]
1338
1339	mul	$a0			# a[2]*a[0]
1340	add	%rax,$A0[1]
1341	 mov	$ai,%rax
1342	adc	\$0,%rdx
1343	mov	$A0[1],-16($tptr,$i)	# t[2]
1344	mov	%rdx,$A0[0]
1345
1346
1347	 mov	-8($aptr,$i),$ai	# a[3]
1348	mul	$a1			# a[2]*a[1]
1349	mov	%rax,$A1[0]		# a[2]*a[1]+t[3]
1350	 mov	$ai,%rax
1351	mov	%rdx,$A1[1]
1352
1353	 lea	($i),$j
1354	mul	$a0			# a[3]*a[0]
1355	add	%rax,$A0[0]		# a[3]*a[0]+a[2]*a[1]+t[3]
1356	 mov	$ai,%rax
1357	mov	%rdx,$A0[1]
1358	adc	\$0,$A0[1]
1359	add	$A1[0],$A0[0]
1360	adc	\$0,$A0[1]
1361	mov	$A0[0],-8($tptr,$j)	# t[3]
1362	jmp	.Lsqr4x_1st
1363
1364.align	32
1365.Lsqr4x_1st:
1366	 mov	($aptr,$j),$ai		# a[4]
1367	mul	$a1			# a[3]*a[1]
1368	add	%rax,$A1[1]		# a[3]*a[1]+t[4]
1369	 mov	$ai,%rax
1370	mov	%rdx,$A1[0]
1371	adc	\$0,$A1[0]
1372
1373	mul	$a0			# a[4]*a[0]
1374	add	%rax,$A0[1]		# a[4]*a[0]+a[3]*a[1]+t[4]
1375	 mov	$ai,%rax		# a[3]
1376	 mov	8($aptr,$j),$ai		# a[5]
1377	mov	%rdx,$A0[0]
1378	adc	\$0,$A0[0]
1379	add	$A1[1],$A0[1]
1380	adc	\$0,$A0[0]
1381
1382
1383	mul	$a1			# a[4]*a[3]
1384	add	%rax,$A1[0]		# a[4]*a[3]+t[5]
1385	 mov	$ai,%rax
1386	 mov	$A0[1],($tptr,$j)	# t[4]
1387	mov	%rdx,$A1[1]
1388	adc	\$0,$A1[1]
1389
1390	mul	$a0			# a[5]*a[2]
1391	add	%rax,$A0[0]		# a[5]*a[2]+a[4]*a[3]+t[5]
1392	 mov	$ai,%rax
1393	 mov	16($aptr,$j),$ai	# a[6]
1394	mov	%rdx,$A0[1]
1395	adc	\$0,$A0[1]
1396	add	$A1[0],$A0[0]
1397	adc	\$0,$A0[1]
1398
1399	mul	$a1			# a[5]*a[3]
1400	add	%rax,$A1[1]		# a[5]*a[3]+t[6]
1401	 mov	$ai,%rax
1402	 mov	$A0[0],8($tptr,$j)	# t[5]
1403	mov	%rdx,$A1[0]
1404	adc	\$0,$A1[0]
1405
1406	mul	$a0			# a[6]*a[2]
1407	add	%rax,$A0[1]		# a[6]*a[2]+a[5]*a[3]+t[6]
1408	 mov	$ai,%rax		# a[3]
1409	 mov	24($aptr,$j),$ai	# a[7]
1410	mov	%rdx,$A0[0]
1411	adc	\$0,$A0[0]
1412	add	$A1[1],$A0[1]
1413	adc	\$0,$A0[0]
1414
1415
1416	mul	$a1			# a[6]*a[5]
1417	add	%rax,$A1[0]		# a[6]*a[5]+t[7]
1418	 mov	$ai,%rax
1419	 mov	$A0[1],16($tptr,$j)	# t[6]
1420	mov	%rdx,$A1[1]
1421	adc	\$0,$A1[1]
1422	 lea	32($j),$j
1423
1424	mul	$a0			# a[7]*a[4]
1425	add	%rax,$A0[0]		# a[7]*a[4]+a[6]*a[5]+t[6]
1426	 mov	$ai,%rax
1427	mov	%rdx,$A0[1]
1428	adc	\$0,$A0[1]
1429	add	$A1[0],$A0[0]
1430	adc	\$0,$A0[1]
1431	mov	$A0[0],-8($tptr,$j)	# t[7]
1432
1433	cmp	\$0,$j
1434	jne	.Lsqr4x_1st
1435
1436	mul	$a1			# a[7]*a[5]
1437	add	%rax,$A1[1]
1438	lea	16($i),$i
1439	adc	\$0,%rdx
1440	add	$A0[1],$A1[1]
1441	adc	\$0,%rdx
1442
1443	mov	$A1[1],($tptr)		# t[8]
1444	mov	%rdx,$A1[0]
1445	mov	%rdx,8($tptr)		# t[9]
1446	jmp	.Lsqr4x_outer
1447
1448.align	32
1449.Lsqr4x_outer:				# comments apply to $num==6 case
1450	mov	-32($aptr,$i),$a0	# a[0]
1451	lea	48+8(%rsp,$num,2),$tptr	# end of tp[] buffer, &tp[2*$num]
1452	mov	-24($aptr,$i),%rax	# a[1]
1453	lea	-32($tptr,$i),$tptr	# end of tp[] window, &tp[2*$num-"$i"]
1454	mov	-16($aptr,$i),$ai	# a[2]
1455	mov	%rax,$a1
1456
1457	mul	$a0			# a[1]*a[0]
1458	mov	-24($tptr,$i),$A0[0]	# t[1]
1459	add	%rax,$A0[0]		# a[1]*a[0]+t[1]
1460	 mov	$ai,%rax		# a[2]
1461	adc	\$0,%rdx
1462	mov	$A0[0],-24($tptr,$i)	# t[1]
1463	mov	%rdx,$A0[1]
1464
1465	mul	$a0			# a[2]*a[0]
1466	add	%rax,$A0[1]
1467	 mov	$ai,%rax
1468	adc	\$0,%rdx
1469	add	-16($tptr,$i),$A0[1]	# a[2]*a[0]+t[2]
1470	mov	%rdx,$A0[0]
1471	adc	\$0,$A0[0]
1472	mov	$A0[1],-16($tptr,$i)	# t[2]
1473
1474	xor	$A1[0],$A1[0]
1475
1476	 mov	-8($aptr,$i),$ai	# a[3]
1477	mul	$a1			# a[2]*a[1]
1478	add	%rax,$A1[0]		# a[2]*a[1]+t[3]
1479	 mov	$ai,%rax
1480	adc	\$0,%rdx
1481	add	-8($tptr,$i),$A1[0]
1482	mov	%rdx,$A1[1]
1483	adc	\$0,$A1[1]
1484
1485	mul	$a0			# a[3]*a[0]
1486	add	%rax,$A0[0]		# a[3]*a[0]+a[2]*a[1]+t[3]
1487	 mov	$ai,%rax
1488	adc	\$0,%rdx
1489	add	$A1[0],$A0[0]
1490	mov	%rdx,$A0[1]
1491	adc	\$0,$A0[1]
1492	mov	$A0[0],-8($tptr,$i)	# t[3]
1493
1494	lea	($i),$j
1495	jmp	.Lsqr4x_inner
1496
1497.align	32
1498.Lsqr4x_inner:
1499	 mov	($aptr,$j),$ai		# a[4]
1500	mul	$a1			# a[3]*a[1]
1501	add	%rax,$A1[1]		# a[3]*a[1]+t[4]
1502	 mov	$ai,%rax
1503	mov	%rdx,$A1[0]
1504	adc	\$0,$A1[0]
1505	add	($tptr,$j),$A1[1]
1506	adc	\$0,$A1[0]
1507
1508	.byte	0x67
1509	mul	$a0			# a[4]*a[0]
1510	add	%rax,$A0[1]		# a[4]*a[0]+a[3]*a[1]+t[4]
1511	 mov	$ai,%rax		# a[3]
1512	 mov	8($aptr,$j),$ai		# a[5]
1513	mov	%rdx,$A0[0]
1514	adc	\$0,$A0[0]
1515	add	$A1[1],$A0[1]
1516	adc	\$0,$A0[0]
1517
1518	mul	$a1			# a[4]*a[3]
1519	add	%rax,$A1[0]		# a[4]*a[3]+t[5]
1520	mov	$A0[1],($tptr,$j)	# t[4]
1521	 mov	$ai,%rax
1522	mov	%rdx,$A1[1]
1523	adc	\$0,$A1[1]
1524	add	8($tptr,$j),$A1[0]
1525	lea	16($j),$j		# j++
1526	adc	\$0,$A1[1]
1527
1528	mul	$a0			# a[5]*a[2]
1529	add	%rax,$A0[0]		# a[5]*a[2]+a[4]*a[3]+t[5]
1530	 mov	$ai,%rax
1531	adc	\$0,%rdx
1532	add	$A1[0],$A0[0]
1533	mov	%rdx,$A0[1]
1534	adc	\$0,$A0[1]
1535	mov	$A0[0],-8($tptr,$j)	# t[5], "preloaded t[1]" below
1536
1537	cmp	\$0,$j
1538	jne	.Lsqr4x_inner
1539
1540	.byte	0x67
1541	mul	$a1			# a[5]*a[3]
1542	add	%rax,$A1[1]
1543	adc	\$0,%rdx
1544	add	$A0[1],$A1[1]
1545	adc	\$0,%rdx
1546
1547	mov	$A1[1],($tptr)		# t[6], "preloaded t[2]" below
1548	mov	%rdx,$A1[0]
1549	mov	%rdx,8($tptr)		# t[7], "preloaded t[3]" below
1550
1551	add	\$16,$i
1552	jnz	.Lsqr4x_outer
1553
1554					# comments apply to $num==4 case
1555	mov	-32($aptr),$a0		# a[0]
1556	lea	48+8(%rsp,$num,2),$tptr	# end of tp[] buffer, &tp[2*$num]
1557	mov	-24($aptr),%rax		# a[1]
1558	lea	-32($tptr,$i),$tptr	# end of tp[] window, &tp[2*$num-"$i"]
1559	mov	-16($aptr),$ai		# a[2]
1560	mov	%rax,$a1
1561
1562	mul	$a0			# a[1]*a[0]
1563	add	%rax,$A0[0]		# a[1]*a[0]+t[1], preloaded t[1]
1564	 mov	$ai,%rax		# a[2]
1565	mov	%rdx,$A0[1]
1566	adc	\$0,$A0[1]
1567
1568	mul	$a0			# a[2]*a[0]
1569	add	%rax,$A0[1]
1570	 mov	$ai,%rax
1571	 mov	$A0[0],-24($tptr)	# t[1]
1572	mov	%rdx,$A0[0]
1573	adc	\$0,$A0[0]
1574	add	$A1[1],$A0[1]		# a[2]*a[0]+t[2], preloaded t[2]
1575	 mov	-8($aptr),$ai		# a[3]
1576	adc	\$0,$A0[0]
1577
1578	mul	$a1			# a[2]*a[1]
1579	add	%rax,$A1[0]		# a[2]*a[1]+t[3], preloaded t[3]
1580	 mov	$ai,%rax
1581	 mov	$A0[1],-16($tptr)	# t[2]
1582	mov	%rdx,$A1[1]
1583	adc	\$0,$A1[1]
1584
1585	mul	$a0			# a[3]*a[0]
1586	add	%rax,$A0[0]		# a[3]*a[0]+a[2]*a[1]+t[3]
1587	 mov	$ai,%rax
1588	mov	%rdx,$A0[1]
1589	adc	\$0,$A0[1]
1590	add	$A1[0],$A0[0]
1591	adc	\$0,$A0[1]
1592	mov	$A0[0],-8($tptr)	# t[3]
1593
1594	mul	$a1			# a[3]*a[1]
1595	add	%rax,$A1[1]
1596	 mov	-16($aptr),%rax		# a[2]
1597	adc	\$0,%rdx
1598	add	$A0[1],$A1[1]
1599	adc	\$0,%rdx
1600
1601	mov	$A1[1],($tptr)		# t[4]
1602	mov	%rdx,$A1[0]
1603	mov	%rdx,8($tptr)		# t[5]
1604
1605	mul	$ai			# a[2]*a[3]
1606___
1607{
1608my ($shift,$carry)=($a0,$a1);
1609my @S=(@A1,$ai,$n0);
1610$code.=<<___;
1611	 add	\$16,$i
1612	 xor	$shift,$shift
1613	 sub	$num,$i			# $i=16-$num
1614	 xor	$carry,$carry
1615
1616	add	$A1[0],%rax		# t[5]
1617	adc	\$0,%rdx
1618	mov	%rax,8($tptr)		# t[5]
1619	mov	%rdx,16($tptr)		# t[6]
1620	mov	$carry,24($tptr)	# t[7]
1621
1622	 mov	-16($aptr,$i),%rax	# a[0]
1623	lea	48+8(%rsp),$tptr
1624	 xor	$A0[0],$A0[0]		# t[0]
1625	 mov	8($tptr),$A0[1]		# t[1]
1626
1627	lea	($shift,$A0[0],2),$S[0]	# t[2*i]<<1 | shift
1628	shr	\$63,$A0[0]
1629	lea	($j,$A0[1],2),$S[1]	# t[2*i+1]<<1 |
1630	shr	\$63,$A0[1]
1631	or	$A0[0],$S[1]		# | t[2*i]>>63
1632	 mov	16($tptr),$A0[0]	# t[2*i+2]	# prefetch
1633	mov	$A0[1],$shift		# shift=t[2*i+1]>>63
1634	mul	%rax			# a[i]*a[i]
1635	neg	$carry			# mov $carry,cf
1636	 mov	24($tptr),$A0[1]	# t[2*i+2+1]	# prefetch
1637	adc	%rax,$S[0]
1638	 mov	-8($aptr,$i),%rax	# a[i+1]	# prefetch
1639	mov	$S[0],($tptr)
1640	adc	%rdx,$S[1]
1641
1642	lea	($shift,$A0[0],2),$S[2]	# t[2*i]<<1 | shift
1643	 mov	$S[1],8($tptr)
1644	 sbb	$carry,$carry		# mov cf,$carry
1645	shr	\$63,$A0[0]
1646	lea	($j,$A0[1],2),$S[3]	# t[2*i+1]<<1 |
1647	shr	\$63,$A0[1]
1648	or	$A0[0],$S[3]		# | t[2*i]>>63
1649	 mov	32($tptr),$A0[0]	# t[2*i+2]	# prefetch
1650	mov	$A0[1],$shift		# shift=t[2*i+1]>>63
1651	mul	%rax			# a[i]*a[i]
1652	neg	$carry			# mov $carry,cf
1653	 mov	40($tptr),$A0[1]	# t[2*i+2+1]	# prefetch
1654	adc	%rax,$S[2]
1655	 mov	0($aptr,$i),%rax	# a[i+1]	# prefetch
1656	mov	$S[2],16($tptr)
1657	adc	%rdx,$S[3]
1658	lea	16($i),$i
1659	mov	$S[3],24($tptr)
1660	sbb	$carry,$carry		# mov cf,$carry
1661	lea	64($tptr),$tptr
1662	jmp	.Lsqr4x_shift_n_add
1663
1664.align	32
1665.Lsqr4x_shift_n_add:
1666	lea	($shift,$A0[0],2),$S[0]	# t[2*i]<<1 | shift
1667	shr	\$63,$A0[0]
1668	lea	($j,$A0[1],2),$S[1]	# t[2*i+1]<<1 |
1669	shr	\$63,$A0[1]
1670	or	$A0[0],$S[1]		# | t[2*i]>>63
1671	 mov	-16($tptr),$A0[0]	# t[2*i+2]	# prefetch
1672	mov	$A0[1],$shift		# shift=t[2*i+1]>>63
1673	mul	%rax			# a[i]*a[i]
1674	neg	$carry			# mov $carry,cf
1675	 mov	-8($tptr),$A0[1]	# t[2*i+2+1]	# prefetch
1676	adc	%rax,$S[0]
1677	 mov	-8($aptr,$i),%rax	# a[i+1]	# prefetch
1678	mov	$S[0],-32($tptr)
1679	adc	%rdx,$S[1]
1680
1681	lea	($shift,$A0[0],2),$S[2]	# t[2*i]<<1 | shift
1682	 mov	$S[1],-24($tptr)
1683	 sbb	$carry,$carry		# mov cf,$carry
1684	shr	\$63,$A0[0]
1685	lea	($j,$A0[1],2),$S[3]	# t[2*i+1]<<1 |
1686	shr	\$63,$A0[1]
1687	or	$A0[0],$S[3]		# | t[2*i]>>63
1688	 mov	0($tptr),$A0[0]		# t[2*i+2]	# prefetch
1689	mov	$A0[1],$shift		# shift=t[2*i+1]>>63
1690	mul	%rax			# a[i]*a[i]
1691	neg	$carry			# mov $carry,cf
1692	 mov	8($tptr),$A0[1]		# t[2*i+2+1]	# prefetch
1693	adc	%rax,$S[2]
1694	 mov	0($aptr,$i),%rax	# a[i+1]	# prefetch
1695	mov	$S[2],-16($tptr)
1696	adc	%rdx,$S[3]
1697
1698	lea	($shift,$A0[0],2),$S[0]	# t[2*i]<<1 | shift
1699	 mov	$S[3],-8($tptr)
1700	 sbb	$carry,$carry		# mov cf,$carry
1701	shr	\$63,$A0[0]
1702	lea	($j,$A0[1],2),$S[1]	# t[2*i+1]<<1 |
1703	shr	\$63,$A0[1]
1704	or	$A0[0],$S[1]		# | t[2*i]>>63
1705	 mov	16($tptr),$A0[0]	# t[2*i+2]	# prefetch
1706	mov	$A0[1],$shift		# shift=t[2*i+1]>>63
1707	mul	%rax			# a[i]*a[i]
1708	neg	$carry			# mov $carry,cf
1709	 mov	24($tptr),$A0[1]	# t[2*i+2+1]	# prefetch
1710	adc	%rax,$S[0]
1711	 mov	8($aptr,$i),%rax	# a[i+1]	# prefetch
1712	mov	$S[0],0($tptr)
1713	adc	%rdx,$S[1]
1714
1715	lea	($shift,$A0[0],2),$S[2]	# t[2*i]<<1 | shift
1716	 mov	$S[1],8($tptr)
1717	 sbb	$carry,$carry		# mov cf,$carry
1718	shr	\$63,$A0[0]
1719	lea	($j,$A0[1],2),$S[3]	# t[2*i+1]<<1 |
1720	shr	\$63,$A0[1]
1721	or	$A0[0],$S[3]		# | t[2*i]>>63
1722	 mov	32($tptr),$A0[0]	# t[2*i+2]	# prefetch
1723	mov	$A0[1],$shift		# shift=t[2*i+1]>>63
1724	mul	%rax			# a[i]*a[i]
1725	neg	$carry			# mov $carry,cf
1726	 mov	40($tptr),$A0[1]	# t[2*i+2+1]	# prefetch
1727	adc	%rax,$S[2]
1728	 mov	16($aptr,$i),%rax	# a[i+1]	# prefetch
1729	mov	$S[2],16($tptr)
1730	adc	%rdx,$S[3]
1731	mov	$S[3],24($tptr)
1732	sbb	$carry,$carry		# mov cf,$carry
1733	lea	64($tptr),$tptr
1734	add	\$32,$i
1735	jnz	.Lsqr4x_shift_n_add
1736
1737	lea	($shift,$A0[0],2),$S[0]	# t[2*i]<<1 | shift
1738	.byte	0x67
1739	shr	\$63,$A0[0]
1740	lea	($j,$A0[1],2),$S[1]	# t[2*i+1]<<1 |
1741	shr	\$63,$A0[1]
1742	or	$A0[0],$S[1]		# | t[2*i]>>63
1743	 mov	-16($tptr),$A0[0]	# t[2*i+2]	# prefetch
1744	mov	$A0[1],$shift		# shift=t[2*i+1]>>63
1745	mul	%rax			# a[i]*a[i]
1746	neg	$carry			# mov $carry,cf
1747	 mov	-8($tptr),$A0[1]	# t[2*i+2+1]	# prefetch
1748	adc	%rax,$S[0]
1749	 mov	-8($aptr),%rax		# a[i+1]	# prefetch
1750	mov	$S[0],-32($tptr)
1751	adc	%rdx,$S[1]
1752
1753	lea	($shift,$A0[0],2),$S[2]	# t[2*i]<<1|shift
1754	 mov	$S[1],-24($tptr)
1755	 sbb	$carry,$carry		# mov cf,$carry
1756	shr	\$63,$A0[0]
1757	lea	($j,$A0[1],2),$S[3]	# t[2*i+1]<<1 |
1758	shr	\$63,$A0[1]
1759	or	$A0[0],$S[3]		# | t[2*i]>>63
1760	mul	%rax			# a[i]*a[i]
1761	neg	$carry			# mov $carry,cf
1762	adc	%rax,$S[2]
1763	adc	%rdx,$S[3]
1764	mov	$S[2],-16($tptr)
1765	mov	$S[3],-8($tptr)
1766___
1767}
1768######################################################################
1769# Montgomery reduction part, "word-by-word" algorithm.
1770#
1771# This new path is inspired by multiple submissions from Intel, by
1772# Shay Gueron, Vlad Krasnov, Erdinc Ozturk, James Guilford,
1773# Vinodh Gopal...
1774{
1775my ($nptr,$tptr,$carry,$m0)=("%rbp","%rdi","%rsi","%rbx");
1776
1777$code.=<<___;
1778	movq	%xmm2,$nptr
1779__bn_sqr8x_reduction:
1780	xor	%rax,%rax
1781	lea	($nptr,$num),%rcx	# end of n[]
1782	lea	48+8(%rsp,$num,2),%rdx	# end of t[] buffer
1783	mov	%rcx,0+8(%rsp)
1784	lea	48+8(%rsp,$num),$tptr	# end of initial t[] window
1785	mov	%rdx,8+8(%rsp)
1786	neg	$num
1787	jmp	.L8x_reduction_loop
1788
1789.align	32
1790.L8x_reduction_loop:
1791	lea	($tptr,$num),$tptr	# start of current t[] window
1792	.byte	0x66
1793	mov	8*0($tptr),$m0
1794	mov	8*1($tptr),%r9
1795	mov	8*2($tptr),%r10
1796	mov	8*3($tptr),%r11
1797	mov	8*4($tptr),%r12
1798	mov	8*5($tptr),%r13
1799	mov	8*6($tptr),%r14
1800	mov	8*7($tptr),%r15
1801	mov	%rax,(%rdx)		# store top-most carry bit
1802	lea	8*8($tptr),$tptr
1803
1804	.byte	0x67
1805	mov	$m0,%r8
1806	imulq	32+8(%rsp),$m0		# n0*a[0]
1807	mov	8*0($nptr),%rax		# n[0]
1808	mov	\$8,%ecx
1809	jmp	.L8x_reduce
1810
1811.align	32
1812.L8x_reduce:
1813	mulq	$m0
1814	 mov	8*1($nptr),%rax		# n[1]
1815	neg	%r8
1816	mov	%rdx,%r8
1817	adc	\$0,%r8
1818
1819	mulq	$m0
1820	add	%rax,%r9
1821	 mov	8*2($nptr),%rax
1822	adc	\$0,%rdx
1823	add	%r9,%r8
1824	 mov	$m0,48-8+8(%rsp,%rcx,8)	# put aside n0*a[i]
1825	mov	%rdx,%r9
1826	adc	\$0,%r9
1827
1828	mulq	$m0
1829	add	%rax,%r10
1830	 mov	8*3($nptr),%rax
1831	adc	\$0,%rdx
1832	add	%r10,%r9
1833	 mov	32+8(%rsp),$carry	# pull n0, borrow $carry
1834	mov	%rdx,%r10
1835	adc	\$0,%r10
1836
1837	mulq	$m0
1838	add	%rax,%r11
1839	 mov	8*4($nptr),%rax
1840	adc	\$0,%rdx
1841	 imulq	%r8,$carry		# modulo-scheduled
1842	add	%r11,%r10
1843	mov	%rdx,%r11
1844	adc	\$0,%r11
1845
1846	mulq	$m0
1847	add	%rax,%r12
1848	 mov	8*5($nptr),%rax
1849	adc	\$0,%rdx
1850	add	%r12,%r11
1851	mov	%rdx,%r12
1852	adc	\$0,%r12
1853
1854	mulq	$m0
1855	add	%rax,%r13
1856	 mov	8*6($nptr),%rax
1857	adc	\$0,%rdx
1858	add	%r13,%r12
1859	mov	%rdx,%r13
1860	adc	\$0,%r13
1861
1862	mulq	$m0
1863	add	%rax,%r14
1864	 mov	8*7($nptr),%rax
1865	adc	\$0,%rdx
1866	add	%r14,%r13
1867	mov	%rdx,%r14
1868	adc	\$0,%r14
1869
1870	mulq	$m0
1871	 mov	$carry,$m0		# n0*a[i]
1872	add	%rax,%r15
1873	 mov	8*0($nptr),%rax		# n[0]
1874	adc	\$0,%rdx
1875	add	%r15,%r14
1876	mov	%rdx,%r15
1877	adc	\$0,%r15
1878
1879	dec	%ecx
1880	jnz	.L8x_reduce
1881
1882	lea	8*8($nptr),$nptr
1883	xor	%rax,%rax
1884	mov	8+8(%rsp),%rdx		# pull end of t[]
1885	cmp	0+8(%rsp),$nptr		# end of n[]?
1886	jae	.L8x_no_tail
1887
1888	.byte	0x66
1889	add	8*0($tptr),%r8
1890	adc	8*1($tptr),%r9
1891	adc	8*2($tptr),%r10
1892	adc	8*3($tptr),%r11
1893	adc	8*4($tptr),%r12
1894	adc	8*5($tptr),%r13
1895	adc	8*6($tptr),%r14
1896	adc	8*7($tptr),%r15
1897	sbb	$carry,$carry		# top carry
1898
1899	mov	48+56+8(%rsp),$m0	# pull n0*a[0]
1900	mov	\$8,%ecx
1901	mov	8*0($nptr),%rax
1902	jmp	.L8x_tail
1903
1904.align	32
1905.L8x_tail:
1906	mulq	$m0
1907	add	%rax,%r8
1908	 mov	8*1($nptr),%rax
1909	 mov	%r8,($tptr)		# save result
1910	mov	%rdx,%r8
1911	adc	\$0,%r8
1912
1913	mulq	$m0
1914	add	%rax,%r9
1915	 mov	8*2($nptr),%rax
1916	adc	\$0,%rdx
1917	add	%r9,%r8
1918	 lea	8($tptr),$tptr		# $tptr++
1919	mov	%rdx,%r9
1920	adc	\$0,%r9
1921
1922	mulq	$m0
1923	add	%rax,%r10
1924	 mov	8*3($nptr),%rax
1925	adc	\$0,%rdx
1926	add	%r10,%r9
1927	mov	%rdx,%r10
1928	adc	\$0,%r10
1929
1930	mulq	$m0
1931	add	%rax,%r11
1932	 mov	8*4($nptr),%rax
1933	adc	\$0,%rdx
1934	add	%r11,%r10
1935	mov	%rdx,%r11
1936	adc	\$0,%r11
1937
1938	mulq	$m0
1939	add	%rax,%r12
1940	 mov	8*5($nptr),%rax
1941	adc	\$0,%rdx
1942	add	%r12,%r11
1943	mov	%rdx,%r12
1944	adc	\$0,%r12
1945
1946	mulq	$m0
1947	add	%rax,%r13
1948	 mov	8*6($nptr),%rax
1949	adc	\$0,%rdx
1950	add	%r13,%r12
1951	mov	%rdx,%r13
1952	adc	\$0,%r13
1953
1954	mulq	$m0
1955	add	%rax,%r14
1956	 mov	8*7($nptr),%rax
1957	adc	\$0,%rdx
1958	add	%r14,%r13
1959	mov	%rdx,%r14
1960	adc	\$0,%r14
1961
1962	mulq	$m0
1963	 mov	48-16+8(%rsp,%rcx,8),$m0# pull n0*a[i]
1964	add	%rax,%r15
1965	adc	\$0,%rdx
1966	add	%r15,%r14
1967	 mov	8*0($nptr),%rax		# pull n[0]
1968	mov	%rdx,%r15
1969	adc	\$0,%r15
1970
1971	dec	%ecx
1972	jnz	.L8x_tail
1973
1974	lea	8*8($nptr),$nptr
1975	mov	8+8(%rsp),%rdx		# pull end of t[]
1976	cmp	0+8(%rsp),$nptr		# end of n[]?
1977	jae	.L8x_tail_done		# break out of loop
1978
1979	 mov	48+56+8(%rsp),$m0	# pull n0*a[0]
1980	neg	$carry
1981	 mov	8*0($nptr),%rax		# pull n[0]
1982	adc	8*0($tptr),%r8
1983	adc	8*1($tptr),%r9
1984	adc	8*2($tptr),%r10
1985	adc	8*3($tptr),%r11
1986	adc	8*4($tptr),%r12
1987	adc	8*5($tptr),%r13
1988	adc	8*6($tptr),%r14
1989	adc	8*7($tptr),%r15
1990	sbb	$carry,$carry		# top carry
1991
1992	mov	\$8,%ecx
1993	jmp	.L8x_tail
1994
1995.align	32
1996.L8x_tail_done:
1997	xor	%rax,%rax
1998	add	(%rdx),%r8		# can this overflow?
1999	adc	\$0,%r9
2000	adc	\$0,%r10
2001	adc	\$0,%r11
2002	adc	\$0,%r12
2003	adc	\$0,%r13
2004	adc	\$0,%r14
2005	adc	\$0,%r15
2006	adc	\$0,%rax
2007
2008	neg	$carry
2009.L8x_no_tail:
2010	adc	8*0($tptr),%r8
2011	adc	8*1($tptr),%r9
2012	adc	8*2($tptr),%r10
2013	adc	8*3($tptr),%r11
2014	adc	8*4($tptr),%r12
2015	adc	8*5($tptr),%r13
2016	adc	8*6($tptr),%r14
2017	adc	8*7($tptr),%r15
2018	adc	\$0,%rax		# top-most carry
2019	 mov	-8($nptr),%rcx		# np[num-1]
2020	 xor	$carry,$carry
2021
2022	movq	%xmm2,$nptr		# restore $nptr
2023
2024	mov	%r8,8*0($tptr)		# store top 512 bits
2025	mov	%r9,8*1($tptr)
2026	 movq	%xmm3,$num		# $num is %r9, can't be moved upwards
2027	mov	%r10,8*2($tptr)
2028	mov	%r11,8*3($tptr)
2029	mov	%r12,8*4($tptr)
2030	mov	%r13,8*5($tptr)
2031	mov	%r14,8*6($tptr)
2032	mov	%r15,8*7($tptr)
2033	lea	8*8($tptr),$tptr
2034
2035	cmp	%rdx,$tptr		# end of t[]?
2036	jb	.L8x_reduction_loop
2037	ret
2038.cfi_endproc
2039.size	bn_sqr8x_internal,.-bn_sqr8x_internal
2040___
2041}
2042##############################################################
2043# Post-condition, 4x unrolled
2044#
2045{
2046my ($tptr,$nptr)=("%rbx","%rbp");
2047$code.=<<___;
2048.type	__bn_post4x_internal,\@abi-omnipotent
2049.align	32
2050__bn_post4x_internal:
2051.cfi_startproc
2052	mov	8*0($nptr),%r12
2053	lea	(%rdi,$num),$tptr	# %rdi was $tptr above
2054	mov	$num,%rcx
2055	movq	%xmm1,$rptr		# restore $rptr
2056	neg	%rax
2057	movq	%xmm1,$aptr		# prepare for back-to-back call
2058	sar	\$3+2,%rcx
2059	dec	%r12			# so that after 'not' we get -n[0]
2060	xor	%r10,%r10
2061	mov	8*1($nptr),%r13
2062	mov	8*2($nptr),%r14
2063	mov	8*3($nptr),%r15
2064	jmp	.Lsqr4x_sub_entry
2065
2066.align	16
2067.Lsqr4x_sub:
2068	mov	8*0($nptr),%r12
2069	mov	8*1($nptr),%r13
2070	mov	8*2($nptr),%r14
2071	mov	8*3($nptr),%r15
2072.Lsqr4x_sub_entry:
2073	lea	8*4($nptr),$nptr
2074	not	%r12
2075	not	%r13
2076	not	%r14
2077	not	%r15
2078	and	%rax,%r12
2079	and	%rax,%r13
2080	and	%rax,%r14
2081	and	%rax,%r15
2082
2083	neg	%r10			# mov %r10,%cf
2084	adc	8*0($tptr),%r12
2085	adc	8*1($tptr),%r13
2086	adc	8*2($tptr),%r14
2087	adc	8*3($tptr),%r15
2088	mov	%r12,8*0($rptr)
2089	lea	8*4($tptr),$tptr
2090	mov	%r13,8*1($rptr)
2091	sbb	%r10,%r10		# mov %cf,%r10
2092	mov	%r14,8*2($rptr)
2093	mov	%r15,8*3($rptr)
2094	lea	8*4($rptr),$rptr
2095
2096	inc	%rcx			# pass %cf
2097	jnz	.Lsqr4x_sub
2098
2099	mov	$num,%r10		# prepare for back-to-back call
2100	neg	$num			# restore $num
2101	ret
2102.cfi_endproc
2103.size	__bn_post4x_internal,.-__bn_post4x_internal
2104___
2105}
2106}}}
2107
2108if ($addx) {{{
2109my $bp="%rdx";	# restore original value
2110
2111$code.=<<___;
2112.type	bn_mulx4x_mont_gather5,\@function,6
2113.align	32
2114bn_mulx4x_mont_gather5:
2115.cfi_startproc
2116	mov	%rsp,%rax
2117.cfi_def_cfa_register	%rax
2118.Lmulx4x_enter:
2119	push	%rbx
2120.cfi_push	%rbx
2121	push	%rbp
2122.cfi_push	%rbp
2123	push	%r12
2124.cfi_push	%r12
2125	push	%r13
2126.cfi_push	%r13
2127	push	%r14
2128.cfi_push	%r14
2129	push	%r15
2130.cfi_push	%r15
2131.Lmulx4x_prologue:
2132
2133	shl	\$3,${num}d		# convert $num to bytes
2134	lea	($num,$num,2),%r10	# 3*$num in bytes
2135	neg	$num			# -$num
2136	mov	($n0),$n0		# *n0
2137
2138	##############################################################
2139	# Ensure that stack frame doesn't alias with $rptr+3*$num
2140	# modulo 4096, which covers ret[num], am[num] and n[num]
2141	# (see bn_exp.c). This is done to allow memory disambiguation
2142	# logic do its magic. [Extra [num] is allocated in order
2143	# to align with bn_power5's frame, which is cleansed after
2144	# completing exponentiation. Extra 256 bytes is for power mask
2145	# calculated from 7th argument, the index.]
2146	#
2147	lea	-320(%rsp,$num,2),%r11
2148	mov	%rsp,%rbp
2149	sub	$rp,%r11
2150	and	\$4095,%r11
2151	cmp	%r11,%r10
2152	jb	.Lmulx4xsp_alt
2153	sub	%r11,%rbp		# align with $aptr
2154	lea	-320(%rbp,$num,2),%rbp	# future alloca(frame+2*$num*8+256)
2155	jmp	.Lmulx4xsp_done
2156
2157.Lmulx4xsp_alt:
2158	lea	4096-320(,$num,2),%r10
2159	lea	-320(%rbp,$num,2),%rbp	# future alloca(frame+2*$num*8+256)
2160	sub	%r10,%r11
2161	mov	\$0,%r10
2162	cmovc	%r10,%r11
2163	sub	%r11,%rbp
2164.Lmulx4xsp_done:
2165	and	\$-64,%rbp		# ensure alignment
2166	mov	%rsp,%r11
2167	sub	%rbp,%r11
2168	and	\$-4096,%r11
2169	lea	(%rbp,%r11),%rsp
2170	mov	(%rsp),%r10
2171	cmp	%rbp,%rsp
2172	ja	.Lmulx4x_page_walk
2173	jmp	.Lmulx4x_page_walk_done
2174
2175.Lmulx4x_page_walk:
2176	lea	-4096(%rsp),%rsp
2177	mov	(%rsp),%r10
2178	cmp	%rbp,%rsp
2179	ja	.Lmulx4x_page_walk
2180.Lmulx4x_page_walk_done:
2181
2182	##############################################################
2183	# Stack layout
2184	# +0	-num
2185	# +8	off-loaded &b[i]
2186	# +16	end of b[num]
2187	# +24	inner counter
2188	# +32	saved n0
2189	# +40	saved %rsp
2190	# +48
2191	# +56	saved rp
2192	# +64	tmp[num+1]
2193	#
2194	mov	$n0, 32(%rsp)		# save *n0
2195	mov	%rax,40(%rsp)		# save original %rsp
2196.cfi_cfa_expression	%rsp+40,deref,+8
2197.Lmulx4x_body:
2198	call	mulx4x_internal
2199
2200	mov	40(%rsp),%rsi		# restore %rsp
2201.cfi_def_cfa	%rsi,8
2202	mov	\$1,%rax
2203
2204	mov	-48(%rsi),%r15
2205.cfi_restore	%r15
2206	mov	-40(%rsi),%r14
2207.cfi_restore	%r14
2208	mov	-32(%rsi),%r13
2209.cfi_restore	%r13
2210	mov	-24(%rsi),%r12
2211.cfi_restore	%r12
2212	mov	-16(%rsi),%rbp
2213.cfi_restore	%rbp
2214	mov	-8(%rsi),%rbx
2215.cfi_restore	%rbx
2216	lea	(%rsi),%rsp
2217.cfi_def_cfa_register	%rsp
2218.Lmulx4x_epilogue:
2219	ret
2220.cfi_endproc
2221.size	bn_mulx4x_mont_gather5,.-bn_mulx4x_mont_gather5
2222
2223.type	mulx4x_internal,\@abi-omnipotent
2224.align	32
2225mulx4x_internal:
2226.cfi_startproc
2227	mov	$num,8(%rsp)		# save -$num (it was in bytes)
2228	mov	$num,%r10
2229	neg	$num			# restore $num
2230	shl	\$5,$num
2231	neg	%r10			# restore $num
2232	lea	128($bp,$num),%r13	# end of powers table (+size optimization)
2233	shr	\$5+5,$num
2234	movd	`($win64?56:8)`(%rax),%xmm5	# load 7th argument
2235	sub	\$1,$num
2236	lea	.Linc(%rip),%rax
2237	mov	%r13,16+8(%rsp)		# end of b[num]
2238	mov	$num,24+8(%rsp)		# inner counter
2239	mov	$rp, 56+8(%rsp)		# save $rp
2240___
2241my ($aptr, $bptr, $nptr, $tptr, $mi,  $bi,  $zero, $num)=
2242   ("%rsi","%rdi","%rcx","%rbx","%r8","%r9","%rbp","%rax");
2243my $rptr=$bptr;
2244my $STRIDE=2**5*8;		# 5 is "window size"
2245my $N=$STRIDE/4;		# should match cache line size
2246$code.=<<___;
2247	movdqa	0(%rax),%xmm0		# 00000001000000010000000000000000
2248	movdqa	16(%rax),%xmm1		# 00000002000000020000000200000002
2249	lea	88-112(%rsp,%r10),%r10	# place the mask after tp[num+1] (+ICache optimization)
2250	lea	128($bp),$bptr		# size optimization
2251
2252	pshufd	\$0,%xmm5,%xmm5		# broadcast index
2253	movdqa	%xmm1,%xmm4
2254	.byte	0x67
2255	movdqa	%xmm1,%xmm2
2256___
2257########################################################################
2258# calculate mask by comparing 0..31 to index and save result to stack
2259#
2260$code.=<<___;
2261	.byte	0x67
2262	paddd	%xmm0,%xmm1
2263	pcmpeqd	%xmm5,%xmm0		# compare to 1,0
2264	movdqa	%xmm4,%xmm3
2265___
2266for($i=0;$i<$STRIDE/16-4;$i+=4) {
2267$code.=<<___;
2268	paddd	%xmm1,%xmm2
2269	pcmpeqd	%xmm5,%xmm1		# compare to 3,2
2270	movdqa	%xmm0,`16*($i+0)+112`(%r10)
2271	movdqa	%xmm4,%xmm0
2272
2273	paddd	%xmm2,%xmm3
2274	pcmpeqd	%xmm5,%xmm2		# compare to 5,4
2275	movdqa	%xmm1,`16*($i+1)+112`(%r10)
2276	movdqa	%xmm4,%xmm1
2277
2278	paddd	%xmm3,%xmm0
2279	pcmpeqd	%xmm5,%xmm3		# compare to 7,6
2280	movdqa	%xmm2,`16*($i+2)+112`(%r10)
2281	movdqa	%xmm4,%xmm2
2282
2283	paddd	%xmm0,%xmm1
2284	pcmpeqd	%xmm5,%xmm0
2285	movdqa	%xmm3,`16*($i+3)+112`(%r10)
2286	movdqa	%xmm4,%xmm3
2287___
2288}
2289$code.=<<___;				# last iteration can be optimized
2290	.byte	0x67
2291	paddd	%xmm1,%xmm2
2292	pcmpeqd	%xmm5,%xmm1
2293	movdqa	%xmm0,`16*($i+0)+112`(%r10)
2294
2295	paddd	%xmm2,%xmm3
2296	pcmpeqd	%xmm5,%xmm2
2297	movdqa	%xmm1,`16*($i+1)+112`(%r10)
2298
2299	pcmpeqd	%xmm5,%xmm3
2300	movdqa	%xmm2,`16*($i+2)+112`(%r10)
2301
2302	pand	`16*($i+0)-128`($bptr),%xmm0	# while it's still in register
2303	pand	`16*($i+1)-128`($bptr),%xmm1
2304	pand	`16*($i+2)-128`($bptr),%xmm2
2305	movdqa	%xmm3,`16*($i+3)+112`(%r10)
2306	pand	`16*($i+3)-128`($bptr),%xmm3
2307	por	%xmm2,%xmm0
2308	por	%xmm3,%xmm1
2309___
2310for($i=0;$i<$STRIDE/16-4;$i+=4) {
2311$code.=<<___;
2312	movdqa	`16*($i+0)-128`($bptr),%xmm4
2313	movdqa	`16*($i+1)-128`($bptr),%xmm5
2314	movdqa	`16*($i+2)-128`($bptr),%xmm2
2315	pand	`16*($i+0)+112`(%r10),%xmm4
2316	movdqa	`16*($i+3)-128`($bptr),%xmm3
2317	pand	`16*($i+1)+112`(%r10),%xmm5
2318	por	%xmm4,%xmm0
2319	pand	`16*($i+2)+112`(%r10),%xmm2
2320	por	%xmm5,%xmm1
2321	pand	`16*($i+3)+112`(%r10),%xmm3
2322	por	%xmm2,%xmm0
2323	por	%xmm3,%xmm1
2324___
2325}
2326$code.=<<___;
2327	pxor	%xmm1,%xmm0
2328	pshufd	\$0x4e,%xmm0,%xmm1
2329	por	%xmm1,%xmm0
2330	lea	$STRIDE($bptr),$bptr
2331	movq	%xmm0,%rdx		# bp[0]
2332	lea	64+8*4+8(%rsp),$tptr
2333
2334	mov	%rdx,$bi
2335	mulx	0*8($aptr),$mi,%rax	# a[0]*b[0]
2336	mulx	1*8($aptr),%r11,%r12	# a[1]*b[0]
2337	add	%rax,%r11
2338	mulx	2*8($aptr),%rax,%r13	# ...
2339	adc	%rax,%r12
2340	adc	\$0,%r13
2341	mulx	3*8($aptr),%rax,%r14
2342
2343	mov	$mi,%r15
2344	imulq	32+8(%rsp),$mi		# "t[0]"*n0
2345	xor	$zero,$zero		# cf=0, of=0
2346	mov	$mi,%rdx
2347
2348	mov	$bptr,8+8(%rsp)		# off-load &b[i]
2349
2350	lea	4*8($aptr),$aptr
2351	adcx	%rax,%r13
2352	adcx	$zero,%r14		# cf=0
2353
2354	mulx	0*8($nptr),%rax,%r10
2355	adcx	%rax,%r15		# discarded
2356	adox	%r11,%r10
2357	mulx	1*8($nptr),%rax,%r11
2358	adcx	%rax,%r10
2359	adox	%r12,%r11
2360	mulx	2*8($nptr),%rax,%r12
2361	mov	24+8(%rsp),$bptr	# counter value
2362	mov	%r10,-8*4($tptr)
2363	adcx	%rax,%r11
2364	adox	%r13,%r12
2365	mulx	3*8($nptr),%rax,%r15
2366	 mov	$bi,%rdx
2367	mov	%r11,-8*3($tptr)
2368	adcx	%rax,%r12
2369	adox	$zero,%r15		# of=0
2370	lea	4*8($nptr),$nptr
2371	mov	%r12,-8*2($tptr)
2372	jmp	.Lmulx4x_1st
2373
2374.align	32
2375.Lmulx4x_1st:
2376	adcx	$zero,%r15		# cf=0, modulo-scheduled
2377	mulx	0*8($aptr),%r10,%rax	# a[4]*b[0]
2378	adcx	%r14,%r10
2379	mulx	1*8($aptr),%r11,%r14	# a[5]*b[0]
2380	adcx	%rax,%r11
2381	mulx	2*8($aptr),%r12,%rax	# ...
2382	adcx	%r14,%r12
2383	mulx	3*8($aptr),%r13,%r14
2384	 .byte	0x67,0x67
2385	 mov	$mi,%rdx
2386	adcx	%rax,%r13
2387	adcx	$zero,%r14		# cf=0
2388	lea	4*8($aptr),$aptr
2389	lea	4*8($tptr),$tptr
2390
2391	adox	%r15,%r10
2392	mulx	0*8($nptr),%rax,%r15
2393	adcx	%rax,%r10
2394	adox	%r15,%r11
2395	mulx	1*8($nptr),%rax,%r15
2396	adcx	%rax,%r11
2397	adox	%r15,%r12
2398	mulx	2*8($nptr),%rax,%r15
2399	mov	%r10,-5*8($tptr)
2400	adcx	%rax,%r12
2401	mov	%r11,-4*8($tptr)
2402	adox	%r15,%r13
2403	mulx	3*8($nptr),%rax,%r15
2404	 mov	$bi,%rdx
2405	mov	%r12,-3*8($tptr)
2406	adcx	%rax,%r13
2407	adox	$zero,%r15
2408	lea	4*8($nptr),$nptr
2409	mov	%r13,-2*8($tptr)
2410
2411	dec	$bptr			# of=0, pass cf
2412	jnz	.Lmulx4x_1st
2413
2414	mov	8(%rsp),$num		# load -num
2415	adc	$zero,%r15		# modulo-scheduled
2416	lea	($aptr,$num),$aptr	# rewind $aptr
2417	add	%r15,%r14
2418	mov	8+8(%rsp),$bptr		# re-load &b[i]
2419	adc	$zero,$zero		# top-most carry
2420	mov	%r14,-1*8($tptr)
2421	jmp	.Lmulx4x_outer
2422
2423.align	32
2424.Lmulx4x_outer:
2425	lea	16-256($tptr),%r10	# where 256-byte mask is (+density control)
2426	pxor	%xmm4,%xmm4
2427	.byte	0x67,0x67
2428	pxor	%xmm5,%xmm5
2429___
2430for($i=0;$i<$STRIDE/16;$i+=4) {
2431$code.=<<___;
2432	movdqa	`16*($i+0)-128`($bptr),%xmm0
2433	movdqa	`16*($i+1)-128`($bptr),%xmm1
2434	movdqa	`16*($i+2)-128`($bptr),%xmm2
2435	pand	`16*($i+0)+256`(%r10),%xmm0
2436	movdqa	`16*($i+3)-128`($bptr),%xmm3
2437	pand	`16*($i+1)+256`(%r10),%xmm1
2438	por	%xmm0,%xmm4
2439	pand	`16*($i+2)+256`(%r10),%xmm2
2440	por	%xmm1,%xmm5
2441	pand	`16*($i+3)+256`(%r10),%xmm3
2442	por	%xmm2,%xmm4
2443	por	%xmm3,%xmm5
2444___
2445}
2446$code.=<<___;
2447	por	%xmm5,%xmm4
2448	pshufd	\$0x4e,%xmm4,%xmm0
2449	por	%xmm4,%xmm0
2450	lea	$STRIDE($bptr),$bptr
2451	movq	%xmm0,%rdx		# m0=bp[i]
2452
2453	mov	$zero,($tptr)		# save top-most carry
2454	lea	4*8($tptr,$num),$tptr	# rewind $tptr
2455	mulx	0*8($aptr),$mi,%r11	# a[0]*b[i]
2456	xor	$zero,$zero		# cf=0, of=0
2457	mov	%rdx,$bi
2458	mulx	1*8($aptr),%r14,%r12	# a[1]*b[i]
2459	adox	-4*8($tptr),$mi		# +t[0]
2460	adcx	%r14,%r11
2461	mulx	2*8($aptr),%r15,%r13	# ...
2462	adox	-3*8($tptr),%r11
2463	adcx	%r15,%r12
2464	mulx	3*8($aptr),%rdx,%r14
2465	adox	-2*8($tptr),%r12
2466	adcx	%rdx,%r13
2467	lea	($nptr,$num),$nptr	# rewind $nptr
2468	lea	4*8($aptr),$aptr
2469	adox	-1*8($tptr),%r13
2470	adcx	$zero,%r14
2471	adox	$zero,%r14
2472
2473	mov	$mi,%r15
2474	imulq	32+8(%rsp),$mi		# "t[0]"*n0
2475
2476	mov	$mi,%rdx
2477	xor	$zero,$zero		# cf=0, of=0
2478	mov	$bptr,8+8(%rsp)		# off-load &b[i]
2479
2480	mulx	0*8($nptr),%rax,%r10
2481	adcx	%rax,%r15		# discarded
2482	adox	%r11,%r10
2483	mulx	1*8($nptr),%rax,%r11
2484	adcx	%rax,%r10
2485	adox	%r12,%r11
2486	mulx	2*8($nptr),%rax,%r12
2487	adcx	%rax,%r11
2488	adox	%r13,%r12
2489	mulx	3*8($nptr),%rax,%r15
2490	 mov	$bi,%rdx
2491	mov	24+8(%rsp),$bptr	# counter value
2492	mov	%r10,-8*4($tptr)
2493	adcx	%rax,%r12
2494	mov	%r11,-8*3($tptr)
2495	adox	$zero,%r15		# of=0
2496	mov	%r12,-8*2($tptr)
2497	lea	4*8($nptr),$nptr
2498	jmp	.Lmulx4x_inner
2499
2500.align	32
2501.Lmulx4x_inner:
2502	mulx	0*8($aptr),%r10,%rax	# a[4]*b[i]
2503	adcx	$zero,%r15		# cf=0, modulo-scheduled
2504	adox	%r14,%r10
2505	mulx	1*8($aptr),%r11,%r14	# a[5]*b[i]
2506	adcx	0*8($tptr),%r10
2507	adox	%rax,%r11
2508	mulx	2*8($aptr),%r12,%rax	# ...
2509	adcx	1*8($tptr),%r11
2510	adox	%r14,%r12
2511	mulx	3*8($aptr),%r13,%r14
2512	 mov	$mi,%rdx
2513	adcx	2*8($tptr),%r12
2514	adox	%rax,%r13
2515	adcx	3*8($tptr),%r13
2516	adox	$zero,%r14		# of=0
2517	lea	4*8($aptr),$aptr
2518	lea	4*8($tptr),$tptr
2519	adcx	$zero,%r14		# cf=0
2520
2521	adox	%r15,%r10
2522	mulx	0*8($nptr),%rax,%r15
2523	adcx	%rax,%r10
2524	adox	%r15,%r11
2525	mulx	1*8($nptr),%rax,%r15
2526	adcx	%rax,%r11
2527	adox	%r15,%r12
2528	mulx	2*8($nptr),%rax,%r15
2529	mov	%r10,-5*8($tptr)
2530	adcx	%rax,%r12
2531	adox	%r15,%r13
2532	mov	%r11,-4*8($tptr)
2533	mulx	3*8($nptr),%rax,%r15
2534	 mov	$bi,%rdx
2535	lea	4*8($nptr),$nptr
2536	mov	%r12,-3*8($tptr)
2537	adcx	%rax,%r13
2538	adox	$zero,%r15
2539	mov	%r13,-2*8($tptr)
2540
2541	dec	$bptr			# of=0, pass cf
2542	jnz	.Lmulx4x_inner
2543
2544	mov	0+8(%rsp),$num		# load -num
2545	adc	$zero,%r15		# modulo-scheduled
2546	sub	0*8($tptr),$bptr	# pull top-most carry to %cf
2547	mov	8+8(%rsp),$bptr		# re-load &b[i]
2548	mov	16+8(%rsp),%r10
2549	adc	%r15,%r14
2550	lea	($aptr,$num),$aptr	# rewind $aptr
2551	adc	$zero,$zero		# top-most carry
2552	mov	%r14,-1*8($tptr)
2553
2554	cmp	%r10,$bptr
2555	jb	.Lmulx4x_outer
2556
2557	mov	-8($nptr),%r10
2558	mov	$zero,%r8
2559	mov	($nptr,$num),%r12
2560	lea	($nptr,$num),%rbp	# rewind $nptr
2561	mov	$num,%rcx
2562	lea	($tptr,$num),%rdi	# rewind $tptr
2563	xor	%eax,%eax
2564	xor	%r15,%r15
2565	sub	%r14,%r10		# compare top-most words
2566	adc	%r15,%r15
2567	or	%r15,%r8
2568	sar	\$3+2,%rcx
2569	sub	%r8,%rax		# %rax=-%r8
2570	mov	56+8(%rsp),%rdx		# restore rp
2571	dec	%r12			# so that after 'not' we get -n[0]
2572	mov	8*1(%rbp),%r13
2573	xor	%r8,%r8
2574	mov	8*2(%rbp),%r14
2575	mov	8*3(%rbp),%r15
2576	jmp	.Lsqrx4x_sub_entry	# common post-condition
2577.cfi_endproc
2578.size	mulx4x_internal,.-mulx4x_internal
2579___
2580}{
2581######################################################################
2582# void bn_power5(
2583my $rptr="%rdi";	# BN_ULONG *rptr,
2584my $aptr="%rsi";	# const BN_ULONG *aptr,
2585my $bptr="%rdx";	# const void *table,
2586my $nptr="%rcx";	# const BN_ULONG *nptr,
2587my $n0  ="%r8";		# const BN_ULONG *n0);
2588my $num ="%r9";		# int num, has to be divisible by 8
2589			# int pwr);
2590
2591my ($i,$j,$tptr)=("%rbp","%rcx",$rptr);
2592my @A0=("%r10","%r11");
2593my @A1=("%r12","%r13");
2594my ($a0,$a1,$ai)=("%r14","%r15","%rbx");
2595
2596$code.=<<___;
2597.type	bn_powerx5,\@function,6
2598.align	32
2599bn_powerx5:
2600.cfi_startproc
2601	mov	%rsp,%rax
2602.cfi_def_cfa_register	%rax
2603.Lpowerx5_enter:
2604	push	%rbx
2605.cfi_push	%rbx
2606	push	%rbp
2607.cfi_push	%rbp
2608	push	%r12
2609.cfi_push	%r12
2610	push	%r13
2611.cfi_push	%r13
2612	push	%r14
2613.cfi_push	%r14
2614	push	%r15
2615.cfi_push	%r15
2616.Lpowerx5_prologue:
2617
2618	shl	\$3,${num}d		# convert $num to bytes
2619	lea	($num,$num,2),%r10	# 3*$num in bytes
2620	neg	$num
2621	mov	($n0),$n0		# *n0
2622
2623	##############################################################
2624	# Ensure that stack frame doesn't alias with $rptr+3*$num
2625	# modulo 4096, which covers ret[num], am[num] and n[num]
2626	# (see bn_exp.c). This is done to allow memory disambiguation
2627	# logic do its magic. [Extra 256 bytes is for power mask
2628	# calculated from 7th argument, the index.]
2629	#
2630	lea	-320(%rsp,$num,2),%r11
2631	mov	%rsp,%rbp
2632	sub	$rptr,%r11
2633	and	\$4095,%r11
2634	cmp	%r11,%r10
2635	jb	.Lpwrx_sp_alt
2636	sub	%r11,%rbp		# align with $aptr
2637	lea	-320(%rbp,$num,2),%rbp	# future alloca(frame+2*$num*8+256)
2638	jmp	.Lpwrx_sp_done
2639
2640.align	32
2641.Lpwrx_sp_alt:
2642	lea	4096-320(,$num,2),%r10
2643	lea	-320(%rbp,$num,2),%rbp	# alloca(frame+2*$num*8+256)
2644	sub	%r10,%r11
2645	mov	\$0,%r10
2646	cmovc	%r10,%r11
2647	sub	%r11,%rbp
2648.Lpwrx_sp_done:
2649	and	\$-64,%rbp
2650	mov	%rsp,%r11
2651	sub	%rbp,%r11
2652	and	\$-4096,%r11
2653	lea	(%rbp,%r11),%rsp
2654	mov	(%rsp),%r10
2655	cmp	%rbp,%rsp
2656	ja	.Lpwrx_page_walk
2657	jmp	.Lpwrx_page_walk_done
2658
2659.Lpwrx_page_walk:
2660	lea	-4096(%rsp),%rsp
2661	mov	(%rsp),%r10
2662	cmp	%rbp,%rsp
2663	ja	.Lpwrx_page_walk
2664.Lpwrx_page_walk_done:
2665
2666	mov	$num,%r10
2667	neg	$num
2668
2669	##############################################################
2670	# Stack layout
2671	#
2672	# +0	saved $num, used in reduction section
2673	# +8	&t[2*$num], used in reduction section
2674	# +16	intermediate carry bit
2675	# +24	top-most carry bit, used in reduction section
2676	# +32	saved *n0
2677	# +40	saved %rsp
2678	# +48	t[2*$num]
2679	#
2680	pxor	%xmm0,%xmm0
2681	movq	$rptr,%xmm1		# save $rptr
2682	movq	$nptr,%xmm2		# save $nptr
2683	movq	%r10, %xmm3		# -$num
2684	movq	$bptr,%xmm4
2685	mov	$n0,  32(%rsp)
2686	mov	%rax, 40(%rsp)		# save original %rsp
2687.cfi_cfa_expression	%rsp+40,deref,+8
2688.Lpowerx5_body:
2689
2690	call	__bn_sqrx8x_internal
2691	call	__bn_postx4x_internal
2692	call	__bn_sqrx8x_internal
2693	call	__bn_postx4x_internal
2694	call	__bn_sqrx8x_internal
2695	call	__bn_postx4x_internal
2696	call	__bn_sqrx8x_internal
2697	call	__bn_postx4x_internal
2698	call	__bn_sqrx8x_internal
2699	call	__bn_postx4x_internal
2700
2701	mov	%r10,$num		# -num
2702	mov	$aptr,$rptr
2703	movq	%xmm2,$nptr
2704	movq	%xmm4,$bptr
2705	mov	40(%rsp),%rax
2706
2707	call	mulx4x_internal
2708
2709	mov	40(%rsp),%rsi		# restore %rsp
2710.cfi_def_cfa	%rsi,8
2711	mov	\$1,%rax
2712
2713	mov	-48(%rsi),%r15
2714.cfi_restore	%r15
2715	mov	-40(%rsi),%r14
2716.cfi_restore	%r14
2717	mov	-32(%rsi),%r13
2718.cfi_restore	%r13
2719	mov	-24(%rsi),%r12
2720.cfi_restore	%r12
2721	mov	-16(%rsi),%rbp
2722.cfi_restore	%rbp
2723	mov	-8(%rsi),%rbx
2724.cfi_restore	%rbx
2725	lea	(%rsi),%rsp
2726.cfi_def_cfa_register	%rsp
2727.Lpowerx5_epilogue:
2728	ret
2729.cfi_endproc
2730.size	bn_powerx5,.-bn_powerx5
2731
2732.globl	bn_sqrx8x_internal
2733.hidden	bn_sqrx8x_internal
2734.type	bn_sqrx8x_internal,\@abi-omnipotent
2735.align	32
2736bn_sqrx8x_internal:
2737__bn_sqrx8x_internal:
2738.cfi_startproc
2739	##################################################################
2740	# Squaring part:
2741	#
2742	# a) multiply-n-add everything but a[i]*a[i];
2743	# b) shift result of a) by 1 to the left and accumulate
2744	#    a[i]*a[i] products;
2745	#
2746	##################################################################
2747	# a[7]a[7]a[6]a[6]a[5]a[5]a[4]a[4]a[3]a[3]a[2]a[2]a[1]a[1]a[0]a[0]
2748	#                                                     a[1]a[0]
2749	#                                                 a[2]a[0]
2750	#                                             a[3]a[0]
2751	#                                             a[2]a[1]
2752	#                                         a[3]a[1]
2753	#                                     a[3]a[2]
2754	#
2755	#                                         a[4]a[0]
2756	#                                     a[5]a[0]
2757	#                                 a[6]a[0]
2758	#                             a[7]a[0]
2759	#                                     a[4]a[1]
2760	#                                 a[5]a[1]
2761	#                             a[6]a[1]
2762	#                         a[7]a[1]
2763	#                                 a[4]a[2]
2764	#                             a[5]a[2]
2765	#                         a[6]a[2]
2766	#                     a[7]a[2]
2767	#                             a[4]a[3]
2768	#                         a[5]a[3]
2769	#                     a[6]a[3]
2770	#                 a[7]a[3]
2771	#
2772	#                     a[5]a[4]
2773	#                 a[6]a[4]
2774	#             a[7]a[4]
2775	#             a[6]a[5]
2776	#         a[7]a[5]
2777	#     a[7]a[6]
2778	# a[7]a[7]a[6]a[6]a[5]a[5]a[4]a[4]a[3]a[3]a[2]a[2]a[1]a[1]a[0]a[0]
2779___
2780{
2781my ($zero,$carry)=("%rbp","%rcx");
2782my $aaptr=$zero;
2783$code.=<<___;
2784	lea	48+8(%rsp),$tptr
2785	lea	($aptr,$num),$aaptr
2786	mov	$num,0+8(%rsp)			# save $num
2787	mov	$aaptr,8+8(%rsp)		# save end of $aptr
2788	jmp	.Lsqr8x_zero_start
2789
2790.align	32
2791.byte	0x66,0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00
2792.Lsqrx8x_zero:
2793	.byte	0x3e
2794	movdqa	%xmm0,0*8($tptr)
2795	movdqa	%xmm0,2*8($tptr)
2796	movdqa	%xmm0,4*8($tptr)
2797	movdqa	%xmm0,6*8($tptr)
2798.Lsqr8x_zero_start:			# aligned at 32
2799	movdqa	%xmm0,8*8($tptr)
2800	movdqa	%xmm0,10*8($tptr)
2801	movdqa	%xmm0,12*8($tptr)
2802	movdqa	%xmm0,14*8($tptr)
2803	lea	16*8($tptr),$tptr
2804	sub	\$64,$num
2805	jnz	.Lsqrx8x_zero
2806
2807	mov	0*8($aptr),%rdx		# a[0], modulo-scheduled
2808	#xor	%r9,%r9			# t[1], ex-$num, zero already
2809	xor	%r10,%r10
2810	xor	%r11,%r11
2811	xor	%r12,%r12
2812	xor	%r13,%r13
2813	xor	%r14,%r14
2814	xor	%r15,%r15
2815	lea	48+8(%rsp),$tptr
2816	xor	$zero,$zero		# cf=0, cf=0
2817	jmp	.Lsqrx8x_outer_loop
2818
2819.align	32
2820.Lsqrx8x_outer_loop:
2821	mulx	1*8($aptr),%r8,%rax	# a[1]*a[0]
2822	adcx	%r9,%r8			# a[1]*a[0]+=t[1]
2823	adox	%rax,%r10
2824	mulx	2*8($aptr),%r9,%rax	# a[2]*a[0]
2825	adcx	%r10,%r9
2826	adox	%rax,%r11
2827	.byte	0xc4,0xe2,0xab,0xf6,0x86,0x18,0x00,0x00,0x00	# mulx	3*8($aptr),%r10,%rax	# ...
2828	adcx	%r11,%r10
2829	adox	%rax,%r12
2830	.byte	0xc4,0xe2,0xa3,0xf6,0x86,0x20,0x00,0x00,0x00	# mulx	4*8($aptr),%r11,%rax
2831	adcx	%r12,%r11
2832	adox	%rax,%r13
2833	mulx	5*8($aptr),%r12,%rax
2834	adcx	%r13,%r12
2835	adox	%rax,%r14
2836	mulx	6*8($aptr),%r13,%rax
2837	adcx	%r14,%r13
2838	adox	%r15,%rax
2839	mulx	7*8($aptr),%r14,%r15
2840	 mov	1*8($aptr),%rdx		# a[1]
2841	adcx	%rax,%r14
2842	adox	$zero,%r15
2843	adc	8*8($tptr),%r15
2844	mov	%r8,1*8($tptr)		# t[1]
2845	mov	%r9,2*8($tptr)		# t[2]
2846	sbb	$carry,$carry		# mov %cf,$carry
2847	xor	$zero,$zero		# cf=0, of=0
2848
2849
2850	mulx	2*8($aptr),%r8,%rbx	# a[2]*a[1]
2851	mulx	3*8($aptr),%r9,%rax	# a[3]*a[1]
2852	adcx	%r10,%r8
2853	adox	%rbx,%r9
2854	mulx	4*8($aptr),%r10,%rbx	# ...
2855	adcx	%r11,%r9
2856	adox	%rax,%r10
2857	.byte	0xc4,0xe2,0xa3,0xf6,0x86,0x28,0x00,0x00,0x00	# mulx	5*8($aptr),%r11,%rax
2858	adcx	%r12,%r10
2859	adox	%rbx,%r11
2860	.byte	0xc4,0xe2,0x9b,0xf6,0x9e,0x30,0x00,0x00,0x00	# mulx	6*8($aptr),%r12,%rbx
2861	adcx	%r13,%r11
2862	adox	%r14,%r12
2863	.byte	0xc4,0x62,0x93,0xf6,0xb6,0x38,0x00,0x00,0x00	# mulx	7*8($aptr),%r13,%r14
2864	 mov	2*8($aptr),%rdx		# a[2]
2865	adcx	%rax,%r12
2866	adox	%rbx,%r13
2867	adcx	%r15,%r13
2868	adox	$zero,%r14		# of=0
2869	adcx	$zero,%r14		# cf=0
2870
2871	mov	%r8,3*8($tptr)		# t[3]
2872	mov	%r9,4*8($tptr)		# t[4]
2873
2874	mulx	3*8($aptr),%r8,%rbx	# a[3]*a[2]
2875	mulx	4*8($aptr),%r9,%rax	# a[4]*a[2]
2876	adcx	%r10,%r8
2877	adox	%rbx,%r9
2878	mulx	5*8($aptr),%r10,%rbx	# ...
2879	adcx	%r11,%r9
2880	adox	%rax,%r10
2881	.byte	0xc4,0xe2,0xa3,0xf6,0x86,0x30,0x00,0x00,0x00	# mulx	6*8($aptr),%r11,%rax
2882	adcx	%r12,%r10
2883	adox	%r13,%r11
2884	.byte	0xc4,0x62,0x9b,0xf6,0xae,0x38,0x00,0x00,0x00	# mulx	7*8($aptr),%r12,%r13
2885	.byte	0x3e
2886	 mov	3*8($aptr),%rdx		# a[3]
2887	adcx	%rbx,%r11
2888	adox	%rax,%r12
2889	adcx	%r14,%r12
2890	mov	%r8,5*8($tptr)		# t[5]
2891	mov	%r9,6*8($tptr)		# t[6]
2892	 mulx	4*8($aptr),%r8,%rax	# a[4]*a[3]
2893	adox	$zero,%r13		# of=0
2894	adcx	$zero,%r13		# cf=0
2895
2896	mulx	5*8($aptr),%r9,%rbx	# a[5]*a[3]
2897	adcx	%r10,%r8
2898	adox	%rax,%r9
2899	mulx	6*8($aptr),%r10,%rax	# ...
2900	adcx	%r11,%r9
2901	adox	%r12,%r10
2902	mulx	7*8($aptr),%r11,%r12
2903	 mov	4*8($aptr),%rdx		# a[4]
2904	 mov	5*8($aptr),%r14		# a[5]
2905	adcx	%rbx,%r10
2906	adox	%rax,%r11
2907	 mov	6*8($aptr),%r15		# a[6]
2908	adcx	%r13,%r11
2909	adox	$zero,%r12		# of=0
2910	adcx	$zero,%r12		# cf=0
2911
2912	mov	%r8,7*8($tptr)		# t[7]
2913	mov	%r9,8*8($tptr)		# t[8]
2914
2915	mulx	%r14,%r9,%rax		# a[5]*a[4]
2916	 mov	7*8($aptr),%r8		# a[7]
2917	adcx	%r10,%r9
2918	mulx	%r15,%r10,%rbx		# a[6]*a[4]
2919	adox	%rax,%r10
2920	adcx	%r11,%r10
2921	mulx	%r8,%r11,%rax		# a[7]*a[4]
2922	 mov	%r14,%rdx		# a[5]
2923	adox	%rbx,%r11
2924	adcx	%r12,%r11
2925	#adox	$zero,%rax		# of=0
2926	adcx	$zero,%rax		# cf=0
2927
2928	mulx	%r15,%r14,%rbx		# a[6]*a[5]
2929	mulx	%r8,%r12,%r13		# a[7]*a[5]
2930	 mov	%r15,%rdx		# a[6]
2931	 lea	8*8($aptr),$aptr
2932	adcx	%r14,%r11
2933	adox	%rbx,%r12
2934	adcx	%rax,%r12
2935	adox	$zero,%r13
2936
2937	.byte	0x67,0x67
2938	mulx	%r8,%r8,%r14		# a[7]*a[6]
2939	adcx	%r8,%r13
2940	adcx	$zero,%r14
2941
2942	cmp	8+8(%rsp),$aptr
2943	je	.Lsqrx8x_outer_break
2944
2945	neg	$carry			# mov $carry,%cf
2946	mov	\$-8,%rcx
2947	mov	$zero,%r15
2948	mov	8*8($tptr),%r8
2949	adcx	9*8($tptr),%r9		# +=t[9]
2950	adcx	10*8($tptr),%r10	# ...
2951	adcx	11*8($tptr),%r11
2952	adc	12*8($tptr),%r12
2953	adc	13*8($tptr),%r13
2954	adc	14*8($tptr),%r14
2955	adc	15*8($tptr),%r15
2956	lea	($aptr),$aaptr
2957	lea	2*64($tptr),$tptr
2958	sbb	%rax,%rax		# mov %cf,$carry
2959
2960	mov	-64($aptr),%rdx		# a[0]
2961	mov	%rax,16+8(%rsp)		# offload $carry
2962	mov	$tptr,24+8(%rsp)
2963
2964	#lea	8*8($tptr),$tptr	# see 2*8*8($tptr) above
2965	xor	%eax,%eax		# cf=0, of=0
2966	jmp	.Lsqrx8x_loop
2967
2968.align	32
2969.Lsqrx8x_loop:
2970	mov	%r8,%rbx
2971	mulx	0*8($aaptr),%rax,%r8	# a[8]*a[i]
2972	adcx	%rax,%rbx		# +=t[8]
2973	adox	%r9,%r8
2974
2975	mulx	1*8($aaptr),%rax,%r9	# ...
2976	adcx	%rax,%r8
2977	adox	%r10,%r9
2978
2979	mulx	2*8($aaptr),%rax,%r10
2980	adcx	%rax,%r9
2981	adox	%r11,%r10
2982
2983	mulx	3*8($aaptr),%rax,%r11
2984	adcx	%rax,%r10
2985	adox	%r12,%r11
2986
2987	.byte	0xc4,0x62,0xfb,0xf6,0xa5,0x20,0x00,0x00,0x00	# mulx	4*8($aaptr),%rax,%r12
2988	adcx	%rax,%r11
2989	adox	%r13,%r12
2990
2991	mulx	5*8($aaptr),%rax,%r13
2992	adcx	%rax,%r12
2993	adox	%r14,%r13
2994
2995	mulx	6*8($aaptr),%rax,%r14
2996	 mov	%rbx,($tptr,%rcx,8)	# store t[8+i]
2997	 mov	\$0,%ebx
2998	adcx	%rax,%r13
2999	adox	%r15,%r14
3000
3001	.byte	0xc4,0x62,0xfb,0xf6,0xbd,0x38,0x00,0x00,0x00	# mulx	7*8($aaptr),%rax,%r15
3002	 mov	8($aptr,%rcx,8),%rdx	# a[i]
3003	adcx	%rax,%r14
3004	adox	%rbx,%r15		# %rbx is 0, of=0
3005	adcx	%rbx,%r15		# cf=0
3006
3007	.byte	0x67
3008	inc	%rcx			# of=0
3009	jnz	.Lsqrx8x_loop
3010
3011	lea	8*8($aaptr),$aaptr
3012	mov	\$-8,%rcx
3013	cmp	8+8(%rsp),$aaptr	# done?
3014	je	.Lsqrx8x_break
3015
3016	sub	16+8(%rsp),%rbx		# mov 16(%rsp),%cf
3017	.byte	0x66
3018	mov	-64($aptr),%rdx
3019	adcx	0*8($tptr),%r8
3020	adcx	1*8($tptr),%r9
3021	adc	2*8($tptr),%r10
3022	adc	3*8($tptr),%r11
3023	adc	4*8($tptr),%r12
3024	adc	5*8($tptr),%r13
3025	adc	6*8($tptr),%r14
3026	adc	7*8($tptr),%r15
3027	lea	8*8($tptr),$tptr
3028	.byte	0x67
3029	sbb	%rax,%rax		# mov %cf,%rax
3030	xor	%ebx,%ebx		# cf=0, of=0
3031	mov	%rax,16+8(%rsp)		# offload carry
3032	jmp	.Lsqrx8x_loop
3033
3034.align	32
3035.Lsqrx8x_break:
3036	xor	$zero,$zero
3037	sub	16+8(%rsp),%rbx		# mov 16(%rsp),%cf
3038	adcx	$zero,%r8
3039	mov	24+8(%rsp),$carry	# initial $tptr, borrow $carry
3040	adcx	$zero,%r9
3041	mov	0*8($aptr),%rdx		# a[8], modulo-scheduled
3042	adc	\$0,%r10
3043	mov	%r8,0*8($tptr)
3044	adc	\$0,%r11
3045	adc	\$0,%r12
3046	adc	\$0,%r13
3047	adc	\$0,%r14
3048	adc	\$0,%r15
3049	cmp	$carry,$tptr		# cf=0, of=0
3050	je	.Lsqrx8x_outer_loop
3051
3052	mov	%r9,1*8($tptr)
3053	 mov	1*8($carry),%r9
3054	mov	%r10,2*8($tptr)
3055	 mov	2*8($carry),%r10
3056	mov	%r11,3*8($tptr)
3057	 mov	3*8($carry),%r11
3058	mov	%r12,4*8($tptr)
3059	 mov	4*8($carry),%r12
3060	mov	%r13,5*8($tptr)
3061	 mov	5*8($carry),%r13
3062	mov	%r14,6*8($tptr)
3063	 mov	6*8($carry),%r14
3064	mov	%r15,7*8($tptr)
3065	 mov	7*8($carry),%r15
3066	mov	$carry,$tptr
3067	jmp	.Lsqrx8x_outer_loop
3068
3069.align	32
3070.Lsqrx8x_outer_break:
3071	mov	%r9,9*8($tptr)		# t[9]
3072	 movq	%xmm3,%rcx		# -$num
3073	mov	%r10,10*8($tptr)	# ...
3074	mov	%r11,11*8($tptr)
3075	mov	%r12,12*8($tptr)
3076	mov	%r13,13*8($tptr)
3077	mov	%r14,14*8($tptr)
3078___
3079}{
3080my $i="%rcx";
3081$code.=<<___;
3082	lea	48+8(%rsp),$tptr
3083	mov	($aptr,$i),%rdx		# a[0]
3084
3085	mov	8($tptr),$A0[1]		# t[1]
3086	xor	$A0[0],$A0[0]		# t[0], of=0, cf=0
3087	mov	0+8(%rsp),$num		# restore $num
3088	adox	$A0[1],$A0[1]
3089	 mov	16($tptr),$A1[0]	# t[2]	# prefetch
3090	 mov	24($tptr),$A1[1]	# t[3]	# prefetch
3091	#jmp	.Lsqrx4x_shift_n_add	# happens to be aligned
3092
3093.align	32
3094.Lsqrx4x_shift_n_add:
3095	mulx	%rdx,%rax,%rbx
3096	 adox	$A1[0],$A1[0]
3097	adcx	$A0[0],%rax
3098	 .byte	0x48,0x8b,0x94,0x0e,0x08,0x00,0x00,0x00	# mov	8($aptr,$i),%rdx	# a[i+1]	# prefetch
3099	 .byte	0x4c,0x8b,0x97,0x20,0x00,0x00,0x00	# mov	32($tptr),$A0[0]	# t[2*i+4]	# prefetch
3100	 adox	$A1[1],$A1[1]
3101	adcx	$A0[1],%rbx
3102	 mov	40($tptr),$A0[1]		# t[2*i+4+1]	# prefetch
3103	mov	%rax,0($tptr)
3104	mov	%rbx,8($tptr)
3105
3106	mulx	%rdx,%rax,%rbx
3107	 adox	$A0[0],$A0[0]
3108	adcx	$A1[0],%rax
3109	 mov	16($aptr,$i),%rdx	# a[i+2]	# prefetch
3110	 mov	48($tptr),$A1[0]	# t[2*i+6]	# prefetch
3111	 adox	$A0[1],$A0[1]
3112	adcx	$A1[1],%rbx
3113	 mov	56($tptr),$A1[1]	# t[2*i+6+1]	# prefetch
3114	mov	%rax,16($tptr)
3115	mov	%rbx,24($tptr)
3116
3117	mulx	%rdx,%rax,%rbx
3118	 adox	$A1[0],$A1[0]
3119	adcx	$A0[0],%rax
3120	 mov	24($aptr,$i),%rdx	# a[i+3]	# prefetch
3121	 lea	32($i),$i
3122	 mov	64($tptr),$A0[0]	# t[2*i+8]	# prefetch
3123	 adox	$A1[1],$A1[1]
3124	adcx	$A0[1],%rbx
3125	 mov	72($tptr),$A0[1]	# t[2*i+8+1]	# prefetch
3126	mov	%rax,32($tptr)
3127	mov	%rbx,40($tptr)
3128
3129	mulx	%rdx,%rax,%rbx
3130	 adox	$A0[0],$A0[0]
3131	adcx	$A1[0],%rax
3132	jrcxz	.Lsqrx4x_shift_n_add_break
3133	 .byte	0x48,0x8b,0x94,0x0e,0x00,0x00,0x00,0x00	# mov	0($aptr,$i),%rdx	# a[i+4]	# prefetch
3134	 adox	$A0[1],$A0[1]
3135	adcx	$A1[1],%rbx
3136	 mov	80($tptr),$A1[0]	# t[2*i+10]	# prefetch
3137	 mov	88($tptr),$A1[1]	# t[2*i+10+1]	# prefetch
3138	mov	%rax,48($tptr)
3139	mov	%rbx,56($tptr)
3140	lea	64($tptr),$tptr
3141	nop
3142	jmp	.Lsqrx4x_shift_n_add
3143
3144.align	32
3145.Lsqrx4x_shift_n_add_break:
3146	adcx	$A1[1],%rbx
3147	mov	%rax,48($tptr)
3148	mov	%rbx,56($tptr)
3149	lea	64($tptr),$tptr		# end of t[] buffer
3150___
3151}
3152######################################################################
3153# Montgomery reduction part, "word-by-word" algorithm.
3154#
3155# This new path is inspired by multiple submissions from Intel, by
3156# Shay Gueron, Vlad Krasnov, Erdinc Ozturk, James Guilford,
3157# Vinodh Gopal...
3158{
3159my ($nptr,$carry,$m0)=("%rbp","%rsi","%rdx");
3160
3161$code.=<<___;
3162	movq	%xmm2,$nptr
3163__bn_sqrx8x_reduction:
3164	xor	%eax,%eax		# initial top-most carry bit
3165	mov	32+8(%rsp),%rbx		# n0
3166	mov	48+8(%rsp),%rdx		# "%r8", 8*0($tptr)
3167	lea	-8*8($nptr,$num),%rcx	# end of n[]
3168	#lea	48+8(%rsp,$num,2),$tptr	# end of t[] buffer
3169	mov	%rcx, 0+8(%rsp)		# save end of n[]
3170	mov	$tptr,8+8(%rsp)		# save end of t[]
3171
3172	lea	48+8(%rsp),$tptr		# initial t[] window
3173	jmp	.Lsqrx8x_reduction_loop
3174
3175.align	32
3176.Lsqrx8x_reduction_loop:
3177	mov	8*1($tptr),%r9
3178	mov	8*2($tptr),%r10
3179	mov	8*3($tptr),%r11
3180	mov	8*4($tptr),%r12
3181	mov	%rdx,%r8
3182	imulq	%rbx,%rdx		# n0*a[i]
3183	mov	8*5($tptr),%r13
3184	mov	8*6($tptr),%r14
3185	mov	8*7($tptr),%r15
3186	mov	%rax,24+8(%rsp)		# store top-most carry bit
3187
3188	lea	8*8($tptr),$tptr
3189	xor	$carry,$carry		# cf=0,of=0
3190	mov	\$-8,%rcx
3191	jmp	.Lsqrx8x_reduce
3192
3193.align	32
3194.Lsqrx8x_reduce:
3195	mov	%r8, %rbx
3196	mulx	8*0($nptr),%rax,%r8	# n[0]
3197	adcx	%rbx,%rax		# discarded
3198	adox	%r9,%r8
3199
3200	mulx	8*1($nptr),%rbx,%r9	# n[1]
3201	adcx	%rbx,%r8
3202	adox	%r10,%r9
3203
3204	mulx	8*2($nptr),%rbx,%r10
3205	adcx	%rbx,%r9
3206	adox	%r11,%r10
3207
3208	mulx	8*3($nptr),%rbx,%r11
3209	adcx	%rbx,%r10
3210	adox	%r12,%r11
3211
3212	.byte	0xc4,0x62,0xe3,0xf6,0xa5,0x20,0x00,0x00,0x00	# mulx	8*4($nptr),%rbx,%r12
3213	 mov	%rdx,%rax
3214	 mov	%r8,%rdx
3215	adcx	%rbx,%r11
3216	adox	%r13,%r12
3217
3218	 mulx	32+8(%rsp),%rbx,%rdx	# %rdx discarded
3219	 mov	%rax,%rdx
3220	 mov	%rax,64+48+8(%rsp,%rcx,8)	# put aside n0*a[i]
3221
3222	mulx	8*5($nptr),%rax,%r13
3223	adcx	%rax,%r12
3224	adox	%r14,%r13
3225
3226	mulx	8*6($nptr),%rax,%r14
3227	adcx	%rax,%r13
3228	adox	%r15,%r14
3229
3230	mulx	8*7($nptr),%rax,%r15
3231	 mov	%rbx,%rdx
3232	adcx	%rax,%r14
3233	adox	$carry,%r15		# $carry is 0
3234	adcx	$carry,%r15		# cf=0
3235
3236	.byte	0x67,0x67,0x67
3237	inc	%rcx			# of=0
3238	jnz	.Lsqrx8x_reduce
3239
3240	mov	$carry,%rax		# xor	%rax,%rax
3241	cmp	0+8(%rsp),$nptr		# end of n[]?
3242	jae	.Lsqrx8x_no_tail
3243
3244	mov	48+8(%rsp),%rdx		# pull n0*a[0]
3245	add	8*0($tptr),%r8
3246	lea	8*8($nptr),$nptr
3247	mov	\$-8,%rcx
3248	adcx	8*1($tptr),%r9
3249	adcx	8*2($tptr),%r10
3250	adc	8*3($tptr),%r11
3251	adc	8*4($tptr),%r12
3252	adc	8*5($tptr),%r13
3253	adc	8*6($tptr),%r14
3254	adc	8*7($tptr),%r15
3255	lea	8*8($tptr),$tptr
3256	sbb	%rax,%rax		# top carry
3257
3258	xor	$carry,$carry		# of=0, cf=0
3259	mov	%rax,16+8(%rsp)
3260	jmp	.Lsqrx8x_tail
3261
3262.align	32
3263.Lsqrx8x_tail:
3264	mov	%r8,%rbx
3265	mulx	8*0($nptr),%rax,%r8
3266	adcx	%rax,%rbx
3267	adox	%r9,%r8
3268
3269	mulx	8*1($nptr),%rax,%r9
3270	adcx	%rax,%r8
3271	adox	%r10,%r9
3272
3273	mulx	8*2($nptr),%rax,%r10
3274	adcx	%rax,%r9
3275	adox	%r11,%r10
3276
3277	mulx	8*3($nptr),%rax,%r11
3278	adcx	%rax,%r10
3279	adox	%r12,%r11
3280
3281	.byte	0xc4,0x62,0xfb,0xf6,0xa5,0x20,0x00,0x00,0x00	# mulx	8*4($nptr),%rax,%r12
3282	adcx	%rax,%r11
3283	adox	%r13,%r12
3284
3285	mulx	8*5($nptr),%rax,%r13
3286	adcx	%rax,%r12
3287	adox	%r14,%r13
3288
3289	mulx	8*6($nptr),%rax,%r14
3290	adcx	%rax,%r13
3291	adox	%r15,%r14
3292
3293	mulx	8*7($nptr),%rax,%r15
3294	 mov	72+48+8(%rsp,%rcx,8),%rdx	# pull n0*a[i]
3295	adcx	%rax,%r14
3296	adox	$carry,%r15
3297	 mov	%rbx,($tptr,%rcx,8)	# save result
3298	 mov	%r8,%rbx
3299	adcx	$carry,%r15		# cf=0
3300
3301	inc	%rcx			# of=0
3302	jnz	.Lsqrx8x_tail
3303
3304	cmp	0+8(%rsp),$nptr		# end of n[]?
3305	jae	.Lsqrx8x_tail_done	# break out of loop
3306
3307	sub	16+8(%rsp),$carry	# mov 16(%rsp),%cf
3308	 mov	48+8(%rsp),%rdx		# pull n0*a[0]
3309	 lea	8*8($nptr),$nptr
3310	adc	8*0($tptr),%r8
3311	adc	8*1($tptr),%r9
3312	adc	8*2($tptr),%r10
3313	adc	8*3($tptr),%r11
3314	adc	8*4($tptr),%r12
3315	adc	8*5($tptr),%r13
3316	adc	8*6($tptr),%r14
3317	adc	8*7($tptr),%r15
3318	lea	8*8($tptr),$tptr
3319	sbb	%rax,%rax
3320	sub	\$8,%rcx		# mov	\$-8,%rcx
3321
3322	xor	$carry,$carry		# of=0, cf=0
3323	mov	%rax,16+8(%rsp)
3324	jmp	.Lsqrx8x_tail
3325
3326.align	32
3327.Lsqrx8x_tail_done:
3328	xor	%rax,%rax
3329	add	24+8(%rsp),%r8		# can this overflow?
3330	adc	\$0,%r9
3331	adc	\$0,%r10
3332	adc	\$0,%r11
3333	adc	\$0,%r12
3334	adc	\$0,%r13
3335	adc	\$0,%r14
3336	adc	\$0,%r15
3337	adc	\$0,%rax
3338
3339	sub	16+8(%rsp),$carry	# mov 16(%rsp),%cf
3340.Lsqrx8x_no_tail:			# %cf is 0 if jumped here
3341	adc	8*0($tptr),%r8
3342	 movq	%xmm3,%rcx
3343	adc	8*1($tptr),%r9
3344	 mov	8*7($nptr),$carry
3345	 movq	%xmm2,$nptr		# restore $nptr
3346	adc	8*2($tptr),%r10
3347	adc	8*3($tptr),%r11
3348	adc	8*4($tptr),%r12
3349	adc	8*5($tptr),%r13
3350	adc	8*6($tptr),%r14
3351	adc	8*7($tptr),%r15
3352	adc	\$0,%rax		# top-most carry
3353
3354	mov	32+8(%rsp),%rbx		# n0
3355	mov	8*8($tptr,%rcx),%rdx	# modulo-scheduled "%r8"
3356
3357	mov	%r8,8*0($tptr)		# store top 512 bits
3358	 lea	8*8($tptr),%r8		# borrow %r8
3359	mov	%r9,8*1($tptr)
3360	mov	%r10,8*2($tptr)
3361	mov	%r11,8*3($tptr)
3362	mov	%r12,8*4($tptr)
3363	mov	%r13,8*5($tptr)
3364	mov	%r14,8*6($tptr)
3365	mov	%r15,8*7($tptr)
3366
3367	lea	8*8($tptr,%rcx),$tptr	# start of current t[] window
3368	cmp	8+8(%rsp),%r8		# end of t[]?
3369	jb	.Lsqrx8x_reduction_loop
3370	ret
3371.cfi_endproc
3372.size	bn_sqrx8x_internal,.-bn_sqrx8x_internal
3373___
3374}
3375##############################################################
3376# Post-condition, 4x unrolled
3377#
3378{
3379my ($rptr,$nptr)=("%rdx","%rbp");
3380$code.=<<___;
3381.align	32
3382__bn_postx4x_internal:
3383.cfi_startproc
3384	mov	8*0($nptr),%r12
3385	mov	%rcx,%r10		# -$num
3386	mov	%rcx,%r9		# -$num
3387	neg	%rax
3388	sar	\$3+2,%rcx
3389	#lea	48+8(%rsp,%r9),$tptr
3390	movq	%xmm1,$rptr		# restore $rptr
3391	movq	%xmm1,$aptr		# prepare for back-to-back call
3392	dec	%r12			# so that after 'not' we get -n[0]
3393	mov	8*1($nptr),%r13
3394	xor	%r8,%r8
3395	mov	8*2($nptr),%r14
3396	mov	8*3($nptr),%r15
3397	jmp	.Lsqrx4x_sub_entry
3398
3399.align	16
3400.Lsqrx4x_sub:
3401	mov	8*0($nptr),%r12
3402	mov	8*1($nptr),%r13
3403	mov	8*2($nptr),%r14
3404	mov	8*3($nptr),%r15
3405.Lsqrx4x_sub_entry:
3406	andn	%rax,%r12,%r12
3407	lea	8*4($nptr),$nptr
3408	andn	%rax,%r13,%r13
3409	andn	%rax,%r14,%r14
3410	andn	%rax,%r15,%r15
3411
3412	neg	%r8			# mov %r8,%cf
3413	adc	8*0($tptr),%r12
3414	adc	8*1($tptr),%r13
3415	adc	8*2($tptr),%r14
3416	adc	8*3($tptr),%r15
3417	mov	%r12,8*0($rptr)
3418	lea	8*4($tptr),$tptr
3419	mov	%r13,8*1($rptr)
3420	sbb	%r8,%r8			# mov %cf,%r8
3421	mov	%r14,8*2($rptr)
3422	mov	%r15,8*3($rptr)
3423	lea	8*4($rptr),$rptr
3424
3425	inc	%rcx
3426	jnz	.Lsqrx4x_sub
3427
3428	neg	%r9			# restore $num
3429
3430	ret
3431.cfi_endproc
3432.size	__bn_postx4x_internal,.-__bn_postx4x_internal
3433___
3434}
3435}}}
3436{
3437my ($inp,$num,$tbl,$idx)=$win64?("%rcx","%edx","%r8", "%r9d") : # Win64 order
3438				("%rdi","%esi","%rdx","%ecx");  # Unix order
3439my $out=$inp;
3440my $STRIDE=2**5*8;
3441my $N=$STRIDE/4;
3442
3443$code.=<<___;
3444.globl	bn_get_bits5
3445.type	bn_get_bits5,\@abi-omnipotent
3446.align	16
3447bn_get_bits5:
3448.cfi_startproc
3449	lea	0($inp),%r10
3450	lea	1($inp),%r11
3451	mov	$num,%ecx
3452	shr	\$4,$num
3453	and	\$15,%ecx
3454	lea	-8(%ecx),%eax
3455	cmp	\$11,%ecx
3456	cmova	%r11,%r10
3457	cmova	%eax,%ecx
3458	movzw	(%r10,$num,2),%eax
3459	shrl	%cl,%eax
3460	and	\$31,%eax
3461	ret
3462.cfi_endproc
3463.size	bn_get_bits5,.-bn_get_bits5
3464
3465.globl	bn_scatter5
3466.type	bn_scatter5,\@abi-omnipotent
3467.align	16
3468bn_scatter5:
3469.cfi_startproc
3470	cmp	\$0, $num
3471	jz	.Lscatter_epilogue
3472	lea	($tbl,$idx,8),$tbl
3473.Lscatter:
3474	mov	($inp),%rax
3475	lea	8($inp),$inp
3476	mov	%rax,($tbl)
3477	lea	32*8($tbl),$tbl
3478	sub	\$1,$num
3479	jnz	.Lscatter
3480.Lscatter_epilogue:
3481	ret
3482.cfi_endproc
3483.size	bn_scatter5,.-bn_scatter5
3484
3485.globl	bn_gather5
3486.type	bn_gather5,\@abi-omnipotent
3487.align	32
3488bn_gather5:
3489.LSEH_begin_bn_gather5:			# Win64 thing, but harmless in other cases
3490.cfi_startproc
3491	# I can't trust assembler to use specific encoding:-(
3492	.byte	0x4c,0x8d,0x14,0x24			#lea    (%rsp),%r10
3493	.byte	0x48,0x81,0xec,0x08,0x01,0x00,0x00	#sub	$0x108,%rsp
3494	lea	.Linc(%rip),%rax
3495	and	\$-16,%rsp		# shouldn't be formally required
3496
3497	movd	$idx,%xmm5
3498	movdqa	0(%rax),%xmm0		# 00000001000000010000000000000000
3499	movdqa	16(%rax),%xmm1		# 00000002000000020000000200000002
3500	lea	128($tbl),%r11		# size optimization
3501	lea	128(%rsp),%rax		# size optimization
3502
3503	pshufd	\$0,%xmm5,%xmm5		# broadcast $idx
3504	movdqa	%xmm1,%xmm4
3505	movdqa	%xmm1,%xmm2
3506___
3507########################################################################
3508# calculate mask by comparing 0..31 to $idx and save result to stack
3509#
3510for($i=0;$i<$STRIDE/16;$i+=4) {
3511$code.=<<___;
3512	paddd	%xmm0,%xmm1
3513	pcmpeqd	%xmm5,%xmm0		# compare to 1,0
3514___
3515$code.=<<___	if ($i);
3516	movdqa	%xmm3,`16*($i-1)-128`(%rax)
3517___
3518$code.=<<___;
3519	movdqa	%xmm4,%xmm3
3520
3521	paddd	%xmm1,%xmm2
3522	pcmpeqd	%xmm5,%xmm1		# compare to 3,2
3523	movdqa	%xmm0,`16*($i+0)-128`(%rax)
3524	movdqa	%xmm4,%xmm0
3525
3526	paddd	%xmm2,%xmm3
3527	pcmpeqd	%xmm5,%xmm2		# compare to 5,4
3528	movdqa	%xmm1,`16*($i+1)-128`(%rax)
3529	movdqa	%xmm4,%xmm1
3530
3531	paddd	%xmm3,%xmm0
3532	pcmpeqd	%xmm5,%xmm3		# compare to 7,6
3533	movdqa	%xmm2,`16*($i+2)-128`(%rax)
3534	movdqa	%xmm4,%xmm2
3535___
3536}
3537$code.=<<___;
3538	movdqa	%xmm3,`16*($i-1)-128`(%rax)
3539	jmp	.Lgather
3540
3541.align	32
3542.Lgather:
3543	pxor	%xmm4,%xmm4
3544	pxor	%xmm5,%xmm5
3545___
3546for($i=0;$i<$STRIDE/16;$i+=4) {
3547$code.=<<___;
3548	movdqa	`16*($i+0)-128`(%r11),%xmm0
3549	movdqa	`16*($i+1)-128`(%r11),%xmm1
3550	movdqa	`16*($i+2)-128`(%r11),%xmm2
3551	pand	`16*($i+0)-128`(%rax),%xmm0
3552	movdqa	`16*($i+3)-128`(%r11),%xmm3
3553	pand	`16*($i+1)-128`(%rax),%xmm1
3554	por	%xmm0,%xmm4
3555	pand	`16*($i+2)-128`(%rax),%xmm2
3556	por	%xmm1,%xmm5
3557	pand	`16*($i+3)-128`(%rax),%xmm3
3558	por	%xmm2,%xmm4
3559	por	%xmm3,%xmm5
3560___
3561}
3562$code.=<<___;
3563	por	%xmm5,%xmm4
3564	lea	$STRIDE(%r11),%r11
3565	pshufd	\$0x4e,%xmm4,%xmm0
3566	por	%xmm4,%xmm0
3567	movq	%xmm0,($out)		# m0=bp[0]
3568	lea	8($out),$out
3569	sub	\$1,$num
3570	jnz	.Lgather
3571
3572	lea	(%r10),%rsp
3573	ret
3574.LSEH_end_bn_gather5:
3575.cfi_endproc
3576.size	bn_gather5,.-bn_gather5
3577___
3578}
3579$code.=<<___;
3580.align	64
3581.Linc:
3582	.long	0,0, 1,1
3583	.long	2,2, 2,2
3584.asciz	"Montgomery Multiplication with scatter/gather for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
3585___
3586
3587# EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
3588#		CONTEXT *context,DISPATCHER_CONTEXT *disp)
3589if ($win64) {
3590$rec="%rcx";
3591$frame="%rdx";
3592$context="%r8";
3593$disp="%r9";
3594
3595$code.=<<___;
3596.extern	__imp_RtlVirtualUnwind
3597.type	mul_handler,\@abi-omnipotent
3598.align	16
3599mul_handler:
3600	push	%rsi
3601	push	%rdi
3602	push	%rbx
3603	push	%rbp
3604	push	%r12
3605	push	%r13
3606	push	%r14
3607	push	%r15
3608	pushfq
3609	sub	\$64,%rsp
3610
3611	mov	120($context),%rax	# pull context->Rax
3612	mov	248($context),%rbx	# pull context->Rip
3613
3614	mov	8($disp),%rsi		# disp->ImageBase
3615	mov	56($disp),%r11		# disp->HandlerData
3616
3617	mov	0(%r11),%r10d		# HandlerData[0]
3618	lea	(%rsi,%r10),%r10	# end of prologue label
3619	cmp	%r10,%rbx		# context->Rip<end of prologue label
3620	jb	.Lcommon_seh_tail
3621
3622	mov	4(%r11),%r10d		# HandlerData[1]
3623	lea	(%rsi,%r10),%r10	# beginning of body label
3624	cmp	%r10,%rbx		# context->Rip<body label
3625	jb	.Lcommon_pop_regs
3626
3627	mov	152($context),%rax	# pull context->Rsp
3628
3629	mov	8(%r11),%r10d		# HandlerData[2]
3630	lea	(%rsi,%r10),%r10	# epilogue label
3631	cmp	%r10,%rbx		# context->Rip>=epilogue label
3632	jae	.Lcommon_seh_tail
3633
3634	lea	.Lmul_epilogue(%rip),%r10
3635	cmp	%r10,%rbx
3636	ja	.Lbody_40
3637
3638	mov	192($context),%r10	# pull $num
3639	mov	8(%rax,%r10,8),%rax	# pull saved stack pointer
3640
3641	jmp	.Lcommon_pop_regs
3642
3643.Lbody_40:
3644	mov	40(%rax),%rax		# pull saved stack pointer
3645.Lcommon_pop_regs:
3646	mov	-8(%rax),%rbx
3647	mov	-16(%rax),%rbp
3648	mov	-24(%rax),%r12
3649	mov	-32(%rax),%r13
3650	mov	-40(%rax),%r14
3651	mov	-48(%rax),%r15
3652	mov	%rbx,144($context)	# restore context->Rbx
3653	mov	%rbp,160($context)	# restore context->Rbp
3654	mov	%r12,216($context)	# restore context->R12
3655	mov	%r13,224($context)	# restore context->R13
3656	mov	%r14,232($context)	# restore context->R14
3657	mov	%r15,240($context)	# restore context->R15
3658
3659.Lcommon_seh_tail:
3660	mov	8(%rax),%rdi
3661	mov	16(%rax),%rsi
3662	mov	%rax,152($context)	# restore context->Rsp
3663	mov	%rsi,168($context)	# restore context->Rsi
3664	mov	%rdi,176($context)	# restore context->Rdi
3665
3666	mov	40($disp),%rdi		# disp->ContextRecord
3667	mov	$context,%rsi		# context
3668	mov	\$154,%ecx		# sizeof(CONTEXT)
3669	.long	0xa548f3fc		# cld; rep movsq
3670
3671	mov	$disp,%rsi
3672	xor	%rcx,%rcx		# arg1, UNW_FLAG_NHANDLER
3673	mov	8(%rsi),%rdx		# arg2, disp->ImageBase
3674	mov	0(%rsi),%r8		# arg3, disp->ControlPc
3675	mov	16(%rsi),%r9		# arg4, disp->FunctionEntry
3676	mov	40(%rsi),%r10		# disp->ContextRecord
3677	lea	56(%rsi),%r11		# &disp->HandlerData
3678	lea	24(%rsi),%r12		# &disp->EstablisherFrame
3679	mov	%r10,32(%rsp)		# arg5
3680	mov	%r11,40(%rsp)		# arg6
3681	mov	%r12,48(%rsp)		# arg7
3682	mov	%rcx,56(%rsp)		# arg8, (NULL)
3683	call	*__imp_RtlVirtualUnwind(%rip)
3684
3685	mov	\$1,%eax		# ExceptionContinueSearch
3686	add	\$64,%rsp
3687	popfq
3688	pop	%r15
3689	pop	%r14
3690	pop	%r13
3691	pop	%r12
3692	pop	%rbp
3693	pop	%rbx
3694	pop	%rdi
3695	pop	%rsi
3696	ret
3697.size	mul_handler,.-mul_handler
3698
3699.section	.pdata
3700.align	4
3701	.rva	.LSEH_begin_bn_mul_mont_gather5
3702	.rva	.LSEH_end_bn_mul_mont_gather5
3703	.rva	.LSEH_info_bn_mul_mont_gather5
3704
3705	.rva	.LSEH_begin_bn_mul4x_mont_gather5
3706	.rva	.LSEH_end_bn_mul4x_mont_gather5
3707	.rva	.LSEH_info_bn_mul4x_mont_gather5
3708
3709	.rva	.LSEH_begin_bn_power5
3710	.rva	.LSEH_end_bn_power5
3711	.rva	.LSEH_info_bn_power5
3712___
3713$code.=<<___ if ($addx);
3714	.rva	.LSEH_begin_bn_mulx4x_mont_gather5
3715	.rva	.LSEH_end_bn_mulx4x_mont_gather5
3716	.rva	.LSEH_info_bn_mulx4x_mont_gather5
3717
3718	.rva	.LSEH_begin_bn_powerx5
3719	.rva	.LSEH_end_bn_powerx5
3720	.rva	.LSEH_info_bn_powerx5
3721___
3722$code.=<<___;
3723	.rva	.LSEH_begin_bn_gather5
3724	.rva	.LSEH_end_bn_gather5
3725	.rva	.LSEH_info_bn_gather5
3726
3727.section	.xdata
3728.align	8
3729.LSEH_info_bn_mul_mont_gather5:
3730	.byte	9,0,0,0
3731	.rva	mul_handler
3732	.rva	.Lmul_body,.Lmul_body,.Lmul_epilogue		# HandlerData[]
3733.align	8
3734.LSEH_info_bn_mul4x_mont_gather5:
3735	.byte	9,0,0,0
3736	.rva	mul_handler
3737	.rva	.Lmul4x_prologue,.Lmul4x_body,.Lmul4x_epilogue		# HandlerData[]
3738.align	8
3739.LSEH_info_bn_power5:
3740	.byte	9,0,0,0
3741	.rva	mul_handler
3742	.rva	.Lpower5_prologue,.Lpower5_body,.Lpower5_epilogue	# HandlerData[]
3743___
3744$code.=<<___ if ($addx);
3745.align	8
3746.LSEH_info_bn_mulx4x_mont_gather5:
3747	.byte	9,0,0,0
3748	.rva	mul_handler
3749	.rva	.Lmulx4x_prologue,.Lmulx4x_body,.Lmulx4x_epilogue	# HandlerData[]
3750.align	8
3751.LSEH_info_bn_powerx5:
3752	.byte	9,0,0,0
3753	.rva	mul_handler
3754	.rva	.Lpowerx5_prologue,.Lpowerx5_body,.Lpowerx5_epilogue	# HandlerData[]
3755___
3756$code.=<<___;
3757.align	8
3758.LSEH_info_bn_gather5:
3759	.byte	0x01,0x0b,0x03,0x0a
3760	.byte	0x0b,0x01,0x21,0x00	# sub	rsp,0x108
3761	.byte	0x04,0xa3,0x00,0x00	# lea	r10,(rsp)
3762.align	8
3763___
3764}
3765
3766$code =~ s/\`([^\`]*)\`/eval($1)/gem;
3767
3768print $code;
3769close STDOUT or die "error closing STDOUT: $!";
3770