3 $0 =~ m/(.*[\/\\])[^\
/\\]+$/; $dir=$1;
4 push(@INC,"${dir}","${dir}../../perlasm");
7 &asm_init
($ARGV[0],$0);
10 for (@ARGV) { $sse2=1 if (/-DOPENSSL_IA32_SSE2/); }
12 &external_label
("OPENSSL_ia32cap_P") if ($sse2);
14 &bn_mul_add_words
("bn_mul_add_words");
15 &bn_mul_words
("bn_mul_words");
16 &bn_sqr_words
("bn_sqr_words");
17 &bn_div_words
("bn_div_words");
18 &bn_add_words
("bn_add_words");
19 &bn_sub_words
("bn_sub_words");
20 &bn_sub_part_words
("bn_sub_part_words");
28 &function_begin_B
($name,$sse2?
"EXTRN\t_OPENSSL_ia32cap_P:DWORD":"");
35 &picmeup
("eax","OPENSSL_ia32cap_P");
36 &bt
(&DWP
(0,"eax"),26);
37 &jnc
(&label
("maw_non_sse2"));
42 &movd
("mm0",&wparam
(3)); # mm0 = w
43 &pxor
("mm1","mm1"); # mm1 = carry_in
44 &jmp
(&label
("maw_sse2_entry"));
46 &set_label
("maw_sse2_unrolled",16);
47 &movd
("mm3",&DWP
(0,$r,"",0)); # mm3 = r[0]
48 &paddq
("mm1","mm3"); # mm1 = carry_in + r[0]
49 &movd
("mm2",&DWP
(0,$a,"",0)); # mm2 = a[0]
50 &pmuludq
("mm2","mm0"); # mm2 = w*a[0]
51 &movd
("mm4",&DWP
(4,$a,"",0)); # mm4 = a[1]
52 &pmuludq
("mm4","mm0"); # mm4 = w*a[1]
53 &movd
("mm6",&DWP
(8,$a,"",0)); # mm6 = a[2]
54 &pmuludq
("mm6","mm0"); # mm6 = w*a[2]
55 &movd
("mm7",&DWP
(12,$a,"",0)); # mm7 = a[3]
56 &pmuludq
("mm7","mm0"); # mm7 = w*a[3]
57 &paddq
("mm1","mm2"); # mm1 = carry_in + r[0] + w*a[0]
58 &movd
("mm3",&DWP
(4,$r,"",0)); # mm3 = r[1]
59 &paddq
("mm3","mm4"); # mm3 = r[1] + w*a[1]
60 &movd
("mm5",&DWP
(8,$r,"",0)); # mm5 = r[2]
61 &paddq
("mm5","mm6"); # mm5 = r[2] + w*a[2]
62 &movd
("mm4",&DWP
(12,$r,"",0)); # mm4 = r[3]
63 &paddq
("mm7","mm4"); # mm7 = r[3] + w*a[3]
64 &movd
(&DWP
(0,$r,"",0),"mm1");
65 &movd
("mm2",&DWP
(16,$a,"",0)); # mm2 = a[4]
66 &pmuludq
("mm2","mm0"); # mm2 = w*a[4]
67 &psrlq
("mm1",32); # mm1 = carry0
68 &movd
("mm4",&DWP
(20,$a,"",0)); # mm4 = a[5]
69 &pmuludq
("mm4","mm0"); # mm4 = w*a[5]
70 &paddq
("mm1","mm3"); # mm1 = carry0 + r[1] + w*a[1]
71 &movd
("mm6",&DWP
(24,$a,"",0)); # mm6 = a[6]
72 &pmuludq
("mm6","mm0"); # mm6 = w*a[6]
73 &movd
(&DWP
(4,$r,"",0),"mm1");
74 &psrlq
("mm1",32); # mm1 = carry1
75 &movd
("mm3",&DWP
(28,$a,"",0)); # mm3 = a[7]
77 &pmuludq
("mm3","mm0"); # mm3 = w*a[7]
78 &paddq
("mm1","mm5"); # mm1 = carry1 + r[2] + w*a[2]
79 &movd
("mm5",&DWP
(16,$r,"",0)); # mm5 = r[4]
80 &paddq
("mm2","mm5"); # mm2 = r[4] + w*a[4]
81 &movd
(&DWP
(8,$r,"",0),"mm1");
82 &psrlq
("mm1",32); # mm1 = carry2
83 &paddq
("mm1","mm7"); # mm1 = carry2 + r[3] + w*a[3]
84 &movd
("mm5",&DWP
(20,$r,"",0)); # mm5 = r[5]
85 &paddq
("mm4","mm5"); # mm4 = r[5] + w*a[5]
86 &movd
(&DWP
(12,$r,"",0),"mm1");
87 &psrlq
("mm1",32); # mm1 = carry3
88 &paddq
("mm1","mm2"); # mm1 = carry3 + r[4] + w*a[4]
89 &movd
("mm5",&DWP
(24,$r,"",0)); # mm5 = r[6]
90 &paddq
("mm6","mm5"); # mm6 = r[6] + w*a[6]
91 &movd
(&DWP
(16,$r,"",0),"mm1");
92 &psrlq
("mm1",32); # mm1 = carry4
93 &paddq
("mm1","mm4"); # mm1 = carry4 + r[5] + w*a[5]
94 &movd
("mm5",&DWP
(28,$r,"",0)); # mm5 = r[7]
95 &paddq
("mm3","mm5"); # mm3 = r[7] + w*a[7]
96 &movd
(&DWP
(20,$r,"",0),"mm1");
97 &psrlq
("mm1",32); # mm1 = carry5
98 &paddq
("mm1","mm6"); # mm1 = carry5 + r[6] + w*a[6]
99 &movd
(&DWP
(24,$r,"",0),"mm1");
100 &psrlq
("mm1",32); # mm1 = carry6
101 &paddq
("mm1","mm3"); # mm1 = carry6 + r[7] + w*a[7]
102 &movd
(&DWP
(28,$r,"",0),"mm1");
103 &lea
($r,&DWP
(32,$r));
104 &psrlq
("mm1",32); # mm1 = carry_out
107 &jz
(&label
("maw_sse2_exit"));
108 &set_label
("maw_sse2_entry");
109 &test
($c,0xfffffff8);
110 &jnz
(&label
("maw_sse2_unrolled"));
112 &set_label
("maw_sse2_loop",4);
113 &movd
("mm2",&DWP
(0,$a)); # mm2 = a[i]
114 &movd
("mm3",&DWP
(0,$r)); # mm3 = r[i]
115 &pmuludq
("mm2","mm0"); # a[i] *= w
117 &paddq
("mm1","mm3"); # carry += r[i]
118 &paddq
("mm1","mm2"); # carry += a[i]*w
119 &movd
(&DWP
(0,$r),"mm1"); # r[i] = carry_low
121 &psrlq
("mm1",32); # carry = carry_high
123 &jnz
(&label
("maw_sse2_loop"));
124 &set_label
("maw_sse2_exit");
125 &movd
("eax","mm1"); # c = carry_out
129 &set_label
("maw_non_sse2",16);
132 # function_begin prologue
146 &xor($c,$c); # clear carry
147 &mov
($r,&wparam
(0)); #
149 &mov
("ecx",&wparam
(2)); #
150 &mov
($a,&wparam
(1)); #
152 &and("ecx",0xfffffff8); # num / 8
153 &mov
($w,&wparam
(3)); #
155 &push("ecx"); # Up the stack for a tmp variable
157 &jz
(&label
("maw_finish"));
159 &set_label
("maw_loop",16);
161 for ($i=0; $i<32; $i+=4)
163 &comment
("Round $i");
165 &mov
("eax",&DWP
($i,$a)); # *a
167 &add
("eax",$c); # L(t)+= c
168 &adc
("edx",0); # H(t)+=carry
169 &add
("eax",&DWP
($i,$r)); # L(t)+= *r
170 &adc
("edx",0); # H(t)+=carry
171 &mov
(&DWP
($i,$r),"eax"); # *r= L(t);
172 &mov
($c,"edx"); # c= H(t);
177 &lea
($a,&DWP
(32,$a));
178 &lea
($r,&DWP
(32,$r));
179 &jnz
(&label
("maw_loop"));
181 &set_label
("maw_finish",0);
182 &mov
("ecx",&wparam
(2)); # get num
184 &jnz
(&label
("maw_finish2")); # helps branch prediction
185 &jmp
(&label
("maw_end"));
187 &set_label
("maw_finish2",1);
188 for ($i=0; $i<7; $i++)
190 &comment
("Tail Round $i");
191 &mov
("eax",&DWP
($i*4,$a)); # *a
193 &add
("eax",$c); # L(t)+=c
194 &adc
("edx",0); # H(t)+=carry
195 &add
("eax",&DWP
($i*4,$r)); # L(t)+= *r
196 &adc
("edx",0); # H(t)+=carry
197 &dec
("ecx") if ($i != 7-1);
198 &mov
(&DWP
($i*4,$r),"eax"); # *r= L(t);
199 &mov
($c,"edx"); # c= H(t);
200 &jz
(&label
("maw_end")) if ($i != 7-1);
202 &set_label
("maw_end",0);
205 &pop("ecx"); # clear variable from
207 &function_end
($name);
214 &function_begin_B
($name,$sse2?
"EXTRN\t_OPENSSL_ia32cap_P:DWORD":"");
221 &picmeup
("eax","OPENSSL_ia32cap_P");
222 &bt
(&DWP
(0,"eax"),26);
223 &jnc
(&label
("mw_non_sse2"));
228 &movd
("mm0",&wparam
(3)); # mm0 = w
229 &pxor
("mm1","mm1"); # mm1 = carry = 0
231 &set_label
("mw_sse2_loop",16);
232 &movd
("mm2",&DWP
(0,$a)); # mm2 = a[i]
233 &pmuludq
("mm2","mm0"); # a[i] *= w
235 &paddq
("mm1","mm2"); # carry += a[i]*w
236 &movd
(&DWP
(0,$r),"mm1"); # r[i] = carry_low
238 &psrlq
("mm1",32); # carry = carry_high
240 &jnz
(&label
("mw_sse2_loop"));
242 &movd
("eax","mm1"); # return carry
245 &set_label
("mw_non_sse2",16);
248 # function_begin prologue
263 &xor($c,$c); # clear carry
264 &mov
($r,&wparam
(0)); #
265 &mov
($a,&wparam
(1)); #
266 &mov
($num,&wparam
(2)); #
267 &mov
($w,&wparam
(3)); #
269 &and($num,0xfffffff8); # num / 8
270 &jz
(&label
("mw_finish"));
272 &set_label
("mw_loop",0);
273 for ($i=0; $i<32; $i+=4)
275 &comment
("Round $i");
277 &mov
("eax",&DWP
($i,$a,"",0)); # *a
279 &add
("eax",$c); # L(t)+=c
282 &adc
("edx",0); # H(t)+=carry
283 &mov
(&DWP
($i,$r,"",0),"eax"); # *r= L(t);
285 &mov
($c,"edx"); # c= H(t);
292 &jz
(&label
("mw_finish"));
293 &jmp
(&label
("mw_loop"));
295 &set_label
("mw_finish",0);
296 &mov
($num,&wparam
(2)); # get num
298 &jnz
(&label
("mw_finish2"));
299 &jmp
(&label
("mw_end"));
301 &set_label
("mw_finish2",1);
302 for ($i=0; $i<7; $i++)
304 &comment
("Tail Round $i");
305 &mov
("eax",&DWP
($i*4,$a,"",0));# *a
307 &add
("eax",$c); # L(t)+=c
309 &adc
("edx",0); # H(t)+=carry
310 &mov
(&DWP
($i*4,$r,"",0),"eax");# *r= L(t);
311 &mov
($c,"edx"); # c= H(t);
312 &dec
($num) if ($i != 7-1);
313 &jz
(&label
("mw_end")) if ($i != 7-1);
315 &set_label
("mw_end",0);
318 &function_end
($name);
325 &function_begin_B
($name,$sse2?
"EXTRN\t_OPENSSL_ia32cap_P:DWORD":"");
332 &picmeup
("eax","OPENSSL_ia32cap_P");
333 &bt
(&DWP
(0,"eax"),26);
334 &jnc
(&label
("sqr_non_sse2"));
340 &set_label
("sqr_sse2_loop",16);
341 &movd
("mm0",&DWP
(0,$a)); # mm0 = a[i]
342 &pmuludq
("mm0","mm0"); # a[i] *= a[i]
343 &lea
($a,&DWP
(4,$a)); # a++
344 &movq
(&QWP
(0,$r),"mm0"); # r[i] = a[i]*a[i]
346 &lea
($r,&DWP
(8,$r)); # r += 2
347 &jnz
(&label
("sqr_sse2_loop"));
351 &set_label
("sqr_non_sse2",16);
354 # function_begin prologue
365 &mov
($r,&wparam
(0)); #
366 &mov
($a,&wparam
(1)); #
367 &mov
($num,&wparam
(2)); #
369 &and($num,0xfffffff8); # num / 8
370 &jz
(&label
("sw_finish"));
372 &set_label
("sw_loop",0);
373 for ($i=0; $i<32; $i+=4)
375 &comment
("Round $i");
376 &mov
("eax",&DWP
($i,$a,"",0)); # *a
378 &mul
("eax"); # *a * *a
379 &mov
(&DWP
($i*2,$r,"",0),"eax"); #
380 &mov
(&DWP
($i*2+4,$r,"",0),"edx");#
387 &jnz
(&label
("sw_loop"));
389 &set_label
("sw_finish",0);
390 &mov
($num,&wparam
(2)); # get num
392 &jz
(&label
("sw_end"));
394 for ($i=0; $i<7; $i++)
396 &comment
("Tail Round $i");
397 &mov
("eax",&DWP
($i*4,$a,"",0)); # *a
399 &mul
("eax"); # *a * *a
400 &mov
(&DWP
($i*8,$r,"",0),"eax"); #
401 &dec
($num) if ($i != 7-1);
402 &mov
(&DWP
($i*8+4,$r,"",0),"edx");
403 &jz
(&label
("sw_end")) if ($i != 7-1);
405 &set_label
("sw_end",0);
407 &function_end
($name);
414 &function_begin_B
($name,"");
415 &mov
("edx",&wparam
(0)); #
416 &mov
("eax",&wparam
(1)); #
417 &mov
("ecx",&wparam
(2)); #
420 &function_end_B
($name);
427 &function_begin
($name,"");
438 &mov
($r,&wparam
(0)); # get r
439 &mov
($a,&wparam
(1)); # get a
440 &mov
($b,&wparam
(2)); # get b
441 &mov
($num,&wparam
(3)); # get num
442 &xor($c,$c); # clear carry
443 &and($num,0xfffffff8); # num / 8
445 &jz
(&label
("aw_finish"));
447 &set_label
("aw_loop",0);
448 for ($i=0; $i<8; $i++)
450 &comment
("Round $i");
452 &mov
($tmp1,&DWP
($i*4,$a,"",0)); # *a
453 &mov
($tmp2,&DWP
($i*4,$b,"",0)); # *b
459 &mov
(&DWP
($i*4,$r,"",0),$tmp1); # *r
467 &jnz
(&label
("aw_loop"));
469 &set_label
("aw_finish",0);
470 &mov
($num,&wparam
(3)); # get num
472 &jz
(&label
("aw_end"));
474 for ($i=0; $i<7; $i++)
476 &comment
("Tail Round $i");
477 &mov
($tmp1,&DWP
($i*4,$a,"",0)); # *a
478 &mov
($tmp2,&DWP
($i*4,$b,"",0));# *b
484 &dec
($num) if ($i != 6);
485 &mov
(&DWP
($i*4,$r,"",0),$tmp1); # *r
486 &jz
(&label
("aw_end")) if ($i != 6);
488 &set_label
("aw_end",0);
490 # &mov("eax",$c); # $c is "eax"
492 &function_end
($name);
499 &function_begin
($name,"");
510 &mov
($r,&wparam
(0)); # get r
511 &mov
($a,&wparam
(1)); # get a
512 &mov
($b,&wparam
(2)); # get b
513 &mov
($num,&wparam
(3)); # get num
514 &xor($c,$c); # clear carry
515 &and($num,0xfffffff8); # num / 8
517 &jz
(&label
("aw_finish"));
519 &set_label
("aw_loop",0);
520 for ($i=0; $i<8; $i++)
522 &comment
("Round $i");
524 &mov
($tmp1,&DWP
($i*4,$a,"",0)); # *a
525 &mov
($tmp2,&DWP
($i*4,$b,"",0)); # *b
531 &mov
(&DWP
($i*4,$r,"",0),$tmp1); # *r
539 &jnz
(&label
("aw_loop"));
541 &set_label
("aw_finish",0);
542 &mov
($num,&wparam
(3)); # get num
544 &jz
(&label
("aw_end"));
546 for ($i=0; $i<7; $i++)
548 &comment
("Tail Round $i");
549 &mov
($tmp1,&DWP
($i*4,$a,"",0)); # *a
550 &mov
($tmp2,&DWP
($i*4,$b,"",0));# *b
556 &dec
($num) if ($i != 6);
557 &mov
(&DWP
($i*4,$r,"",0),$tmp1); # *r
558 &jz
(&label
("aw_end")) if ($i != 6);
560 &set_label
("aw_end",0);
562 # &mov("eax",$c); # $c is "eax"
564 &function_end
($name);
567 sub bn_sub_part_words
571 &function_begin
($name,"");
582 &mov
($r,&wparam
(0)); # get r
583 &mov
($a,&wparam
(1)); # get a
584 &mov
($b,&wparam
(2)); # get b
585 &mov
($num,&wparam
(3)); # get num
586 &xor($c,$c); # clear carry
587 &and($num,0xfffffff8); # num / 8
589 &jz
(&label
("aw_finish"));
591 &set_label
("aw_loop",0);
592 for ($i=0; $i<8; $i++)
594 &comment
("Round $i");
596 &mov
($tmp1,&DWP
($i*4,$a,"",0)); # *a
597 &mov
($tmp2,&DWP
($i*4,$b,"",0)); # *b
603 &mov
(&DWP
($i*4,$r,"",0),$tmp1); # *r
611 &jnz
(&label
("aw_loop"));
613 &set_label
("aw_finish",0);
614 &mov
($num,&wparam
(3)); # get num
616 &jz
(&label
("aw_end"));
618 for ($i=0; $i<7; $i++)
620 &comment
("Tail Round $i");
621 &mov
($tmp1,&DWP
(0,$a,"",0)); # *a
622 &mov
($tmp2,&DWP
(0,$b,"",0));# *b
628 &mov
(&DWP
(0,$r,"",0),$tmp1); # *r
632 &dec
($num) if ($i != 6);
633 &jz
(&label
("aw_end")) if ($i != 6);
635 &set_label
("aw_end",0);
638 &je
(&label
("pw_end"));
640 &mov
($num,&wparam
(4)); # get dl
642 &je
(&label
("pw_end"));
643 &jge
(&label
("pw_pos"));
649 &and($num,0xfffffff8); # num / 8
650 &jz
(&label
("pw_neg_finish"));
652 &set_label
("pw_neg_loop",0);
653 for ($i=0; $i<8; $i++)
655 &comment
("dl<0 Round $i");
658 &mov
($tmp2,&DWP
($i*4,$b,"",0)); # *b
664 &mov
(&DWP
($i*4,$r,"",0),$tmp1); # *r
671 &jnz
(&label
("pw_neg_loop"));
673 &set_label
("pw_neg_finish",0);
674 &mov
($tmp2,&wparam
(4)); # get dl
678 &jz
(&label
("pw_end"));
680 for ($i=0; $i<7; $i++)
682 &comment
("dl<0 Tail Round $i");
684 &mov
($tmp2,&DWP
($i*4,$b,"",0));# *b
690 &dec
($num) if ($i != 6);
691 &mov
(&DWP
($i*4,$r,"",0),$tmp1); # *r
692 &jz
(&label
("pw_end")) if ($i != 6);
695 &jmp
(&label
("pw_end"));
697 &set_label
("pw_pos",0);
699 &and($num,0xfffffff8); # num / 8
700 &jz
(&label
("pw_pos_finish"));
702 &set_label
("pw_pos_loop",0);
704 for ($i=0; $i<8; $i++)
706 &comment
("dl>0 Round $i");
708 &mov
($tmp1,&DWP
($i*4,$a,"",0)); # *a
710 &mov
(&DWP
($i*4,$r,"",0),$tmp1); # *r
711 &jnc
(&label
("pw_nc".$i));
718 &jnz
(&label
("pw_pos_loop"));
720 &set_label
("pw_pos_finish",0);
721 &mov
($num,&wparam
(4)); # get dl
723 &jz
(&label
("pw_end"));
725 for ($i=0; $i<7; $i++)
727 &comment
("dl>0 Tail Round $i");
728 &mov
($tmp1,&DWP
($i*4,$a,"",0)); # *a
730 &mov
(&DWP
($i*4,$r,"",0),$tmp1); # *r
731 &jnc
(&label
("pw_tail_nc".$i));
732 &dec
($num) if ($i != 6);
733 &jz
(&label
("pw_end")) if ($i != 6);
736 &jmp
(&label
("pw_end"));
738 &set_label
("pw_nc_loop",0);
739 for ($i=0; $i<8; $i++)
741 &mov
($tmp1,&DWP
($i*4,$a,"",0)); # *a
742 &mov
(&DWP
($i*4,$r,"",0),$tmp1); # *r
743 &set_label
("pw_nc".$i,0);
750 &jnz
(&label
("pw_nc_loop"));
752 &mov
($num,&wparam
(4)); # get dl
754 &jz
(&label
("pw_nc_end"));
756 for ($i=0; $i<7; $i++)
758 &mov
($tmp1,&DWP
($i*4,$a,"",0)); # *a
759 &mov
(&DWP
($i*4,$r,"",0),$tmp1); # *r
760 &set_label
("pw_tail_nc".$i,0);
761 &dec
($num) if ($i != 6);
762 &jz
(&label
("pw_nc_end")) if ($i != 6);
765 &set_label
("pw_nc_end",0);
768 &set_label
("pw_end",0);
770 # &mov("eax",$c); # $c is "eax"
772 &function_end
($name);