x86-mont.pl 16.2 KB
Newer Older
1 2 3 4
#!/usr/bin/env perl

# ====================================================================
# Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
5 6 7
# project. The module is, however, dual licensed under OpenSSL and
# CRYPTOGAMS licenses depending on where you obtain it. For further
# details see http://www.openssl.org/~appro/cryptogams/.
8 9
# ====================================================================

A
Andy Polyakov 已提交
10 11
# October 2005
#
12 13 14 15 16 17 18 19 20 21 22
# This is a "teaser" code, as it can be improved in several ways...
# First of all non-SSE2 path should be implemented (yes, for now it
# performs Montgomery multiplication/convolution only on SSE2-capable
# CPUs such as P4, others fall down to original code). Then inner loop
# can be unrolled and modulo-scheduled to improve ILP and possibly
# moved to 128-bit XMM register bank (though it would require input
# rearrangement and/or increase bus bandwidth utilization). Dedicated
# squaring procedure should give further performance improvement...
# Yet, for being draft, the code improves rsa512 *sign* benchmark by
# 110%(!), rsa1024 one - by 70% and rsa4096 - by 20%:-)

23 24 25 26
# December 2006
#
# Modulo-scheduling SSE2 loops results in further 15-20% improvement.
# Integer-only code [being equipped with dedicated squaring procedure]
27
# gives ~40% on rsa512 sign benchmark...
28 29 30

$0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
push(@INC,"${dir}","${dir}../../perlasm");
31 32 33 34 35 36 37 38 39
require "x86asm.pl";

&asm_init($ARGV[0],$0);

$sse2=0;
for (@ARGV) { $sse2=1 if (/-DOPENSSL_IA32_SSE2/); }

&external_label("OPENSSL_ia32cap_P") if ($sse2);

40
&function_begin("bn_mul_mont");
41

42
$i="edx";
43 44 45
$j="ecx";
$ap="esi";
$rp="edi";	$bp="edi";		# overlapping variables!!!
46 47
$np="ebp";
$num="ebx";
48

49 50 51 52 53 54
$_num=&DWP(4*0,"esp");			# stack top layout
$_rp=&DWP(4*1,"esp");
$_ap=&DWP(4*2,"esp");
$_bp=&DWP(4*3,"esp");
$_np=&DWP(4*4,"esp");
$_n0=&DWP(4*5,"esp");
55
$_sp=&DWP(4*6,"esp");
56
$_bpend=&DWP(4*7,"esp");
57
$frame=32;				# size of above frame rounded up to 16n
58

59 60
	&xor	("eax","eax");
	&mov	("edi",&wparam(5));	# int num
61 62
	&cmp	("edi",4);
	&jl	(&label("just_leave"));
63

64 65
	&lea	("esi",&wparam(0));	# put aside pointer to argument block
	&lea	("edx",&wparam(1));	# load ap
66 67 68 69 70
	&mov	("ebp","esp");		# saved stack pointer!
	&add	("edi",2);		# extra two words on top of tp
	&neg	("edi");
	&lea	("esp",&DWP(-$frame,"esp","edi",4));	# alloca($frame+4*(num+2))
	&neg	("edi");
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

	# minimize cache contention by arraning 2K window between stack
	# pointer and ap argument [np is also position sensitive vector,
	# but it's assumed to be near ap, as it's allocated at ~same
	# time].
	&mov	("eax","esp");
	&sub	("eax","edx");
	&and	("eax",2047);
	&sub	("esp","eax");		# this aligns sp and ap modulo 2048

	&xor	("edx","esp");
	&and	("edx",2048);
	&xor	("edx",2048);
	&sub	("esp","edx");		# this splits them apart modulo 4096

	&and	("esp",-64);		# align to cache line

	################################# load argument block...
	&mov	("eax",&DWP(0*4,"esi"));# BN_ULONG *rp
	&mov	("ebx",&DWP(1*4,"esi"));# const BN_ULONG *ap
	&mov	("ecx",&DWP(2*4,"esi"));# const BN_ULONG *bp
	&mov	("edx",&DWP(3*4,"esi"));# const BN_ULONG *np
	&mov	("esi",&DWP(4*4,"esi"));# const BN_ULONG *n0
	#&mov	("edi",&DWP(5*4,"esi"));# int num
95

96
	&mov	("esi",&DWP(0,"esi"));	# pull n0[0]
97 98 99 100 101
	&mov	($_rp,"eax");		# ... save a copy of argument block
	&mov	($_ap,"ebx");
	&mov	($_bp,"ecx");
	&mov	($_np,"edx");
	&mov	($_n0,"esi");
102
	&lea	($num,&DWP(-3,"edi"));	# num=num-1 to assist modulo-scheduling
103 104
	#&mov	($_num,$num);		# redundant as $num is not reused
	&mov	($_sp,"ebp");		# saved stack pointer!
105

106 107 108 109 110 111 112 113 114 115 116 117 118
if($sse2) {
$acc0="mm0";	# mmx register bank layout
$acc1="mm1";
$car0="mm2";
$car1="mm3";
$mul0="mm4";
$mul1="mm5";
$temp="mm6";
$mask="mm7";

	&picmeup("eax","OPENSSL_ia32cap_P");
	&bt	(&DWP(0,"eax"),26);
	&jnc	(&label("non_sse2"));
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

	&mov	("eax",-1);
	&movd	($mask,"eax");		# mask 32 lower bits

	&mov	($ap,$_ap);		# load input pointers
	&mov	($bp,$_bp);
	&mov	($np,$_np);

	&xor	($i,$i);		# i=0
	&xor	($j,$j);		# j=0

	&movd	($mul0,&DWP(0,$bp));		# bp[0]
	&movd	($mul1,&DWP(0,$ap));		# ap[0]
	&movd	($car1,&DWP(0,$np));		# np[0]

	&pmuludq($mul1,$mul0);			# ap[0]*bp[0]
	&movq	($car0,$mul1);
	&movq	($acc0,$mul1);			# I wish movd worked for
	&pand	($acc0,$mask);			# inter-register transfers

	&pmuludq($mul1,$_n0);			# *=n0

	&pmuludq($car1,$mul1);			# "t[0]"*np[0]*n0
	&paddq	($car1,$acc0);

144 145 146
	&movd	($acc1,&DWP(4,$np));		# np[1]
	&movd	($acc0,&DWP(4,$ap));		# ap[1]

147 148 149 150
	&psrlq	($car0,32);
	&psrlq	($car1,32);

	&inc	($j);				# j++
151
&set_label("1st",16);
152 153 154
	&pmuludq($acc0,$mul0);			# ap[j]*bp[0]
	&pmuludq($acc1,$mul1);			# np[j]*m1
	&paddq	($car0,$acc0);			# +=c0
155 156
	&paddq	($car1,$acc1);			# +=c1

157 158
	&movq	($acc0,$car0);
	&pand	($acc0,$mask);
159
	&movd	($acc1,&DWP(4,$np,$j,4));	# np[j+1]
160
	&paddq	($car1,$acc0);			# +=ap[j]*bp[0];
161
	&movd	($acc0,&DWP(4,$ap,$j,4));	# ap[j+1]
162
	&psrlq	($car0,32);
163
	&movd	(&DWP($frame-4,"esp",$j,4),$car1);	# tp[j-1]=
164 165 166 167 168 169
	&psrlq	($car1,32);

	&lea	($j,&DWP(1,$j));
	&cmp	($j,$num);
	&jl	(&label("1st"));

170 171 172 173
	&pmuludq($acc0,$mul0);			# ap[num-1]*bp[0]
	&pmuludq($acc1,$mul1);			# np[num-1]*m1
	&paddq	($car0,$acc0);			# +=c0
	&paddq	($car1,$acc1);			# +=c1
174

175 176 177 178 179 180 181 182 183 184 185
	&movq	($acc0,$car0);
	&pand	($acc0,$mask);
	&paddq	($car1,$acc0);			# +=ap[num-1]*bp[0];
	&movd	(&DWP($frame-4,"esp",$j,4),$car1);	# tp[num-2]=

	&psrlq	($car0,32);
	&psrlq	($car1,32);

	&paddq	($car1,$car0);
	&movq	(&DWP($frame,"esp",$num,4),$car1);	# tp[num].tp[num-1]

186 187 188 189 190 191
	&inc	($i);				# i++
&set_label("outer");
	&xor	($j,$j);			# j=0

	&movd	($mul0,&DWP(0,$bp,$i,4));	# bp[i]
	&movd	($mul1,&DWP(0,$ap));		# ap[0]
192 193
	&movd	($temp,&DWP($frame,"esp"));	# tp[0]
	&movd	($car1,&DWP(0,$np));		# np[0]
194 195 196 197 198 199 200 201 202 203 204 205
	&pmuludq($mul1,$mul0);			# ap[0]*bp[i]

	&paddq	($mul1,$temp);			# +=tp[0]
	&movq	($acc0,$mul1);
	&movq	($car0,$mul1);
	&pand	($acc0,$mask);

	&pmuludq($mul1,$_n0);			# *=n0

	&pmuludq($car1,$mul1);
	&paddq	($car1,$acc0);

206 207 208 209
	&movd	($temp,&DWP($frame+4,"esp"));	# tp[1]
	&movd	($acc1,&DWP(4,$np));		# np[1]
	&movd	($acc0,&DWP(4,$ap));		# ap[1]

210 211
	&psrlq	($car0,32);
	&psrlq	($car1,32);
212
	&paddq	($car0,$temp);			# +=tp[1]
213 214

	&inc	($j);				# j++
215
	&dec	($num);
216 217 218 219
&set_label("inner");
	&pmuludq($acc0,$mul0);			# ap[j]*bp[i]
	&pmuludq($acc1,$mul1);			# np[j]*m1
	&paddq	($car0,$acc0);			# +=c0
220 221
	&paddq	($car1,$acc1);			# +=c1

222
	&movq	($acc0,$car0);
223
	&movd	($temp,&DWP($frame+4,"esp",$j,4));# tp[j+1]
224
	&pand	($acc0,$mask);
225
	&movd	($acc1,&DWP(4,$np,$j,4));	# np[j+1]
226
	&paddq	($car1,$acc0);			# +=ap[j]*bp[i]+tp[j]
227
	&movd	($acc0,&DWP(4,$ap,$j,4));	# ap[j+1]
228
	&psrlq	($car0,32);
229
	&movd	(&DWP($frame-4,"esp",$j,4),$car1);# tp[j-1]=
230
	&psrlq	($car1,32);
231
	&paddq	($car0,$temp);			# +=tp[j+1]
232

233
	&dec	($num);
234
	&lea	($j,&DWP(1,$j));		# j++
235 236 237 238 239 240 241 242 243 244 245 246 247 248
	&jnz	(&label("inner"));

	&mov	($num,$j);
	&pmuludq($acc0,$mul0);			# ap[num-1]*bp[i]
	&pmuludq($acc1,$mul1);			# np[num-1]*m1
	&paddq	($car0,$acc0);			# +=c0
	&paddq	($car1,$acc1);			# +=c1

	&movq	($acc0,$car0);
	&pand	($acc0,$mask);
	&paddq	($car1,$acc0);			# +=ap[num-1]*bp[i]+tp[num-1]
	&movd	(&DWP($frame-4,"esp",$j,4),$car1);	# tp[num-2]=
	&psrlq	($car0,32);
	&psrlq	($car1,32);
249

250
	&movd	($temp,&DWP($frame+4,"esp",$num,4));	# += tp[num]
251 252
	&paddq	($car1,$car0);
	&paddq	($car1,$temp);
253
	&movq	(&DWP($frame,"esp",$num,4),$car1);	# tp[num].tp[num-1]
254 255 256

	&lea	($i,&DWP(1,$i));		# i++
	&cmp	($i,$num);
257
	&jle	(&label("outer"));
258 259

	&emms	();				# done with mmx bank
260 261 262 263
	&jmp	(&label("common_tail"));

&set_label("non_sse2",16);
}
264 265

if (0) {
266 267 268
	&mov	("esp",$_sp);
	&xor	("eax","eax");	# signal "not fast enough [yet]"
	&jmp	(&label("just_leave"));
269
	# While the below code provides competitive performance for
270 271 272 273 274
	# all key lengthes on modern Intel cores, it's still more
	# than 10% slower for 4096-bit key elsewhere:-( "Competitive"
	# means compared to the original integer-only assembler.
	# 512-bit RSA sign is better by ~40%, but that's about all
	# one can say about all CPUs...
275 276 277 278 279 280
} else {
$inp="esi";	# integer path uses these registers differently
$word="edi";
$carry="ebp";

	&mov	($inp,$_ap);
281
	&lea	($carry,&DWP(1,$num));
282
	&mov	($word,$_bp);
283 284 285 286
	&xor	($j,$j);				# j=0
	&mov	("edx",$inp);
	&and	($carry,1);				# see if num is even
	&sub	("edx",$word);				# see if ap==bp
287
	&lea	("eax",&DWP(4,$word,$num,4));		# &bp[num]
288
	&or	($carry,"edx");
289
	&mov	($word,&DWP(0,$word));			# bp[0]
290
	&jz	(&label("bn_sqr_mont"));
291
	&mov	($_bpend,"eax");
292
	&mov	("eax",&DWP(0,$inp));
293 294 295 296 297
	&xor	("edx","edx");

&set_label("mull",16);
	&mov	($carry,"edx");
	&mul	($word);				# ap[j]*bp[0]
298
	&add	($carry,"eax");
299 300
	&lea	($j,&DWP(1,$j));
	&adc	("edx",0);
301
	&mov	("eax",&DWP(0,$inp,$j,4));		# ap[j+1]
302
	&cmp	($j,$num);
303 304
	&mov	(&DWP($frame-4,"esp",$j,4),$carry);	# tp[j]=
	&jl	(&label("mull"));
305 306 307

	&mov	($carry,"edx");
	&mul	($word);				# ap[num-1]*bp[0]
308
	 &mov	($word,$_n0);
309
	&add	("eax",$carry);
310
	 &mov	($inp,$_np);
311
	&adc	("edx",0);
312
	 &imul	($word,&DWP($frame,"esp"));		# n0*tp[0]
313 314 315 316 317 318 319 320 321

	&mov	(&DWP($frame,"esp",$num,4),"eax");	# tp[num-1]=
	&xor	($j,$j);
	&mov	(&DWP($frame+4,"esp",$num,4),"edx");	# tp[num]=
	&mov	(&DWP($frame+8,"esp",$num,4),$j);	# tp[num+1]=

	&mov	("eax",&DWP(0,$inp));			# np[0]
	&mul	($word);				# np[0]*m
	&add	("eax",&DWP($frame,"esp"));		# +=tp[0]
322
	&mov	("eax",&DWP(4,$inp));			# np[1]
323
	&adc	("edx",0);
324
	&inc	($j);
325 326

	&jmp	(&label("2ndmadd"));
327

328 329 330
&set_label("1stmadd",16);
	&mov	($carry,"edx");
	&mul	($word);				# ap[j]*bp[i]
331
	&add	($carry,&DWP($frame,"esp",$j,4));	# +=tp[j]
332 333
	&lea	($j,&DWP(1,$j));
	&adc	("edx",0);
334 335
	&add	($carry,"eax");
	&mov	("eax",&DWP(0,$inp,$j,4));		# ap[j+1]
336 337
	&adc	("edx",0);
	&cmp	($j,$num);
338 339
	&mov	(&DWP($frame-4,"esp",$j,4),$carry);	# tp[j]=
	&jl	(&label("1stmadd"));
340 341 342 343

	&mov	($carry,"edx");
	&mul	($word);				# ap[num-1]*bp[i]
	&add	("eax",&DWP($frame,"esp",$num,4));	# +=tp[num-1]
344
	 &mov	($word,$_n0);
345
	&adc	("edx",0);
346 347
	 &mov	($inp,$_np);
	&add	($carry,"eax");
348
	&adc	("edx",0);
349
	 &imul	($word,&DWP($frame,"esp"));		# n0*tp[0]
350 351 352

	&xor	($j,$j);
	&add	("edx",&DWP($frame+4,"esp",$num,4));	# carry+=tp[num]
353
	&mov	(&DWP($frame,"esp",$num,4),$carry);	# tp[num-1]=
354
	&adc	($j,0);
355
	 &mov	("eax",&DWP(0,$inp));			# np[0]
356 357 358 359 360
	&mov	(&DWP($frame+4,"esp",$num,4),"edx");	# tp[num]=
	&mov	(&DWP($frame+8,"esp",$num,4),$j);	# tp[num+1]=

	&mul	($word);				# np[0]*m
	&add	("eax",&DWP($frame,"esp"));		# +=tp[0]
361
	&mov	("eax",&DWP(4,$inp));			# np[1]
362 363
	&adc	("edx",0);
	&mov	($j,1);
364

365 366 367
&set_label("2ndmadd",16);
	&mov	($carry,"edx");
	&mul	($word);				# np[j]*m
368
	&add	($carry,&DWP($frame,"esp",$j,4));	# +=tp[j]
369 370
	&lea	($j,&DWP(1,$j));
	&adc	("edx",0);
371 372
	&add	($carry,"eax");
	&mov	("eax",&DWP(0,$inp,$j,4));		# np[j+1]
373 374
	&adc	("edx",0);
	&cmp	($j,$num);
375 376
	&mov	(&DWP($frame-8,"esp",$j,4),$carry);	# tp[j-1]=
	&jl	(&label("2ndmadd"));
377 378

	&mov	($carry,"edx");
379
	&mul	($word);				# np[j]*m
380
	&add	($carry,&DWP($frame,"esp",$num,4));	# +=tp[num-1]
381
	&adc	("edx",0);
382
	&add	($carry,"eax");
383
	&adc	("edx",0);
384
	&mov	(&DWP($frame-4,"esp",$num,4),$carry);	# tp[num-2]=
385 386

	&xor	("eax","eax");
387
	 &mov	($j,$_bp);				# &bp[i]
388 389
	&add	("edx",&DWP($frame+4,"esp",$num,4));	# carry+=tp[num]
	&adc	("eax",&DWP($frame+8,"esp",$num,4));	# +=tp[num+1]
390
	 &lea	($j,&DWP(4,$j));
391
	&mov	(&DWP($frame,"esp",$num,4),"edx");	# tp[num-1]=
392
	 &cmp	($j,$_bpend);
393
	&mov	(&DWP($frame+4,"esp",$num,4),"eax");	# tp[num]=
394
	&je	(&label("common_tail"));
395

396
	&mov	($word,&DWP(0,$j));			# bp[i+1]
397
	&mov	($inp,$_ap);
398
	&mov	($_bp,$j);				# &bp[++i]
399 400
	&xor	($j,$j);
	&xor	("edx","edx");
401
	&mov	("eax",&DWP(0,$inp));
402
	&jmp	(&label("1stmadd"));
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492

&set_label("bn_sqr_mont",16);
$sbit=$num;
	&mov	($_num,$num);
	&mov	($_bp,$j);				# i=0

	&mov	("eax",$word);				# ap[0]
	&mul	($word);				# ap[0]*ap[0]
	&mov	(&DWP($frame,"esp"),"eax");		# tp[0]=
	&mov	($sbit,"edx");
	&shr	("edx",1);
	&and	($sbit,1);
	&inc	($j);
&set_label("sqr",16);
	&mov	("eax",&DWP(0,$inp,$j,4));		# ap[j]
	&mov	($carry,"edx");
	&mul	($word);				# ap[j]*ap[0]
	&add	("eax",$carry);
	&lea	($j,&DWP(1,$j));
	&adc	("edx",0);
	&lea	($carry,&DWP(0,$sbit,"eax",2));
	&shr	("eax",31);
	&cmp	($j,$_num);
	&mov	($sbit,"eax");
	&mov	(&DWP($frame-4,"esp",$j,4),$carry);	# tp[j]=
	&jl	(&label("sqr"));

	&mov	("eax",&DWP(0,$inp,$j,4));		# ap[num-1]
	&mov	($carry,"edx");
	&mul	($word);				# ap[num-1]*ap[0]
	&add	("eax",$carry);
	 &mov	($word,$_n0);
	&adc	("edx",0);
	 &mov	($inp,$_np);
	&lea	($carry,&DWP(0,$sbit,"eax",2));
	 &imul	($word,&DWP($frame,"esp"));		# n0*tp[0]
	&shr	("eax",31);
	&mov	(&DWP($frame,"esp",$j,4),$carry);	# tp[num-1]=

	&lea	($carry,&DWP(0,"eax","edx",2));
	 &mov	("eax",&DWP(0,$inp));			# np[0]
	&shr	("edx",31);
	&mov	(&DWP($frame+4,"esp",$j,4),$carry);	# tp[num]=
	&mov	(&DWP($frame+8,"esp",$j,4),"edx");	# tp[num+1]=

	&mul	($word);				# np[0]*m
	&add	("eax",&DWP($frame,"esp"));		# +=tp[0]
	&mov	($num,$j);
	&adc	("edx",0);
	&mov	("eax",&DWP(4,$inp));			# np[1]
	&mov	($j,1);

&set_label("3rdmadd",16);
	&mov	($carry,"edx");
	&mul	($word);				# np[j]*m
	&add	($carry,&DWP($frame,"esp",$j,4));	# +=tp[j]
	&adc	("edx",0);
	&add	($carry,"eax");
	&mov	("eax",&DWP(4,$inp,$j,4));		# np[j+1]
	&adc	("edx",0);
	&mov	(&DWP($frame-4,"esp",$j,4),$carry);	# tp[j-1]=

	&mov	($carry,"edx");
	&mul	($word);				# np[j+1]*m
	&add	($carry,&DWP($frame+4,"esp",$j,4));	# +=tp[j+1]
	&lea	($j,&DWP(2,$j));
	&adc	("edx",0);
	&add	($carry,"eax");
	&mov	("eax",&DWP(0,$inp,$j,4));		# np[j+2]
	&adc	("edx",0);
	&cmp	($j,$num);
	&mov	(&DWP($frame-8,"esp",$j,4),$carry);	# tp[j]=
	&jl	(&label("3rdmadd"));

	&mov	($carry,"edx");
	&mul	($word);				# np[j]*m
	&add	($carry,&DWP($frame,"esp",$num,4));	# +=tp[num-1]
	&adc	("edx",0);
	&add	($carry,"eax");
	&adc	("edx",0);
	&mov	(&DWP($frame-4,"esp",$num,4),$carry);	# tp[num-2]=

	&mov	($j,$_bp);				# i
	&xor	("eax","eax");
	&mov	($inp,$_ap);
	&add	("edx",&DWP($frame+4,"esp",$num,4));	# carry+=tp[num]
	&adc	("eax",&DWP($frame+8,"esp",$num,4));	# +=tp[num+1]
	&mov	(&DWP($frame,"esp",$num,4),"edx");	# tp[num-1]=
	&cmp	($j,$num);
	&mov	(&DWP($frame+4,"esp",$num,4),"eax");	# tp[num]=
493
	&je	(&label("common_tail"));
494 495 496 497 498 499 500 501 502 503 504 505 506

	&mov	($word,&DWP(4,$inp,$j,4));		# ap[i]
	&lea	($j,&DWP(1,$j));
	&mov	("eax",$word);
	&mov	($_bp,$j);				# ++i
	&mul	($word);				# ap[i]*ap[i]
	&add	("eax",&DWP($frame,"esp",$j,4));	# +=tp[i]
	&adc	("edx",0);
	&mov	(&DWP($frame,"esp",$j,4),"eax");	# tp[i]=
	&xor	($carry,$carry);
	&cmp	($j,$num);
	&lea	($j,&DWP(1,$j));
	&je	(&label("sqrlast"));
507

508 509 510 511 512 513 514 515
	&mov	($sbit,"edx");				# zaps $num
	&shr	("edx",1);
	&and	($sbit,1);
&set_label("sqradd",16);
	&mov	("eax",&DWP(0,$inp,$j,4));		# ap[j]
	&mov	($carry,"edx");
	&mul	($word);				# ap[j]*ap[i]
	&add	("eax",$carry);
516
	&lea	($carry,&DWP(0,"eax","eax"));
517 518
	&adc	("edx",0);
	&shr	("eax",31);
519 520
	&add	($carry,&DWP($frame,"esp",$j,4));	# +=tp[j]
	&lea	($j,&DWP(1,$j));
521
	&adc	("eax",0);
522
	&add	($carry,$sbit);
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
	&adc	("eax",0);
	&cmp	($j,$_num);
	&mov	(&DWP($frame-4,"esp",$j,4),$carry);	# tp[j]=
	&mov	($sbit,"eax");
	&jle	(&label("sqradd"));

	&mov	($carry,"edx");
	&lea	("edx",&DWP(0,$sbit,"edx",2));
	&shr	($carry,31);
&set_label("sqrlast");
	&mov	($word,$_n0);
	&mov	($inp,$_np);
	&imul	($word,&DWP($frame,"esp"));		# n0*tp[0]

	&add	("edx",&DWP($frame,"esp",$j,4));	# +=tp[num]
	&mov	("eax",&DWP(0,$inp));			# np[0]
	&adc	($carry,0);
	&mov	(&DWP($frame,"esp",$j,4),"edx");	# tp[num]=
	&mov	(&DWP($frame+4,"esp",$j,4),$carry);	# tp[num+1]=

	&mul	($word);				# np[0]*m
	&add	("eax",&DWP($frame,"esp"));		# +=tp[0]
	&lea	($num,&DWP(-1,$j));
	&adc	("edx",0);
	&mov	($j,1);
	&mov	("eax",&DWP(4,$inp));			# np[1]

	&jmp	(&label("3rdmadd"));
551
}
552

553
&set_label("common_tail",16);
554
	&mov	($np,$_np);
555
	&mov	("esi",&DWP($frame+4,"esp",$num,4));# load upmost overflow bit
556 557 558
	&mov	($rp,$_rp);			# load result pointer
						# [$ap and $bp are zapped]
	&xor	($i,$i);			# i=0
559
	&mov	($j,$num);			# j=num-1
560 561
	&cmp	("esi",0);			# clears CF unconditionally
	&jnz	(&label("sub"));
562
	&mov	("eax",&DWP($frame,"esp",$j,4));
563 564
	&cmp	("eax",&DWP(0,$np,$j,4));	# tp[num-1]-np[num-1]?
	&jae	(&label("sub"));		# if taken CF is cleared
565
&set_label("copy",16);
566
	&mov	("eax",&DWP($frame,"esp",$j,4));
567
	&mov	(&DWP(0,$rp,$j,4),"eax");	# rp[i]=tp[i]
568
	&mov	(&DWP($frame,"esp",$j,4),$j);	# zap temporary vector
569 570
	&dec	($j);
	&jge	(&label("copy"));
571
	&jmp	(&label("exit"));
572

573
&set_label("sub",16);
574
	&mov	("eax",&DWP($frame,"esp",$i,4));
575 576 577 578 579
	&sbb	("eax",&DWP(0,$np,$i,4));
	&mov	(&DWP(0,$rp,$i,4),"eax");	# rp[i]=tp[i]-np[i]
	&lea	($i,&DWP(1,$i));		# i++
	&dec	($j);				# doesn't affect CF!
	&jge	(&label("sub"));
580
	&mov	($j,$num);			# j=num-1
581 582
	&sbb	("esi",0);			# esi holds upmost overflow bit
	&jc	(&label("copy"));
583
&set_label("zap",8);
584
	&mov	(&DWP($frame,"esp",$j,4),$i);	# zap temporary vector
585 586 587
	&dec	($j);
	&jge	(&label("zap"));

588
&set_label("exit",8);
589 590
	&mov	("esp",$_sp);		# pull saved stack pointer
	&mov	("eax",1);
591
&set_label("just_leave");
592 593
&function_end("bn_mul_mont");

594 595
&asciz("Montgomery Multiplication for x86, CRYPTOGAMS by <appro\@openssl.org>");

596
&asm_finish();