4ca82b0e5bcd94105f798358016dad4ff4647296
[openssl.git] / crypto / sha / asm / sha256-586.pl
1 #!/usr/bin/env perl
2 #
3 # ====================================================================
4 # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
5 # project. The module is, however, dual licensed under OpenSSL and
6 # CRYPTOGAMS licenses depending on where you obtain it. For further
7 # details see http://www.openssl.org/~appro/cryptogams/.
8 # ====================================================================
9 #
10 # SHA256 block transform for x86. September 2007.
11 #
12 # Performance improvement over compiler generated code varies from
13 # 10% to 40% [see below]. Not very impressive on some ยต-archs, but
14 # it's 5 times smaller and optimizies amount of writes.
15 #
16 # May 2012.
17 #
18 # Optimization including two of Pavel Semjanov's ideas, alternative
19 # Maj and full unroll, resulted in ~20-25% improvement on most CPUs,
20 # ~10% on Pentium and P4, ~37% on Atom. As fully unrolled loop body is
21 # almost 15x larger, 8KB vs. 560B, it's fired only for longer inputs.
22 # But not on P4, where it kills performance, nor Sandy Bridge, where
23 # folded loop is just as fast...
24 #
25 # Performance in clock cycles per processed byte (less is better):
26 #
27 #               Pentium PIII    P4      AMD K8  Core2   SB(**)  Atom
28 # gcc           46      36      41      27      26      25      50
29 # icc           57      33      38      25      23      -       -
30 # x86 asm(*)    39/36   27/24   30      19/15.5 18/16   16(**)  30/26
31 # x86_64 asm(***)       -       17.5    15      16      17.5    23
32 #
33 # (*)   numbers after slash are for unrolled loop, where available;
34 # (**)  for Sandy Bridge executing code path with ror replaced with
35 #       equivalent shrd;
36 # (***) x86_64 assembly performance is presented for reference
37 #       purposes.
38
39 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
40 push(@INC,"${dir}","${dir}../../perlasm");
41 require "x86asm.pl";
42
43 &asm_init($ARGV[0],"sha512-586.pl",$ARGV[$#ARGV] eq "386");
44
45 $unroll_after = 1024;
46
47 $A="eax";
48 $E="edx";
49 $T="ebx";
50 $Aoff=&DWP(4,"esp");
51 $Boff=&DWP(8,"esp");
52 $Coff=&DWP(12,"esp");
53 $Doff=&DWP(16,"esp");
54 $Eoff=&DWP(20,"esp");
55 $Foff=&DWP(24,"esp");
56 $Goff=&DWP(28,"esp");
57 $Hoff=&DWP(32,"esp");
58 $Xoff=&DWP(36,"esp");
59 $K256="ebp";
60
61 sub BODY_16_63() {
62         &mov    ($T,"ecx");                     # "ecx" is preloaded
63          &mov   ("esi",&DWP(4*(9+15+16-14),"esp"));
64         &ror    ("ecx",18-7);
65          &mov   ("edi","esi");
66         &ror    ("esi",19-17);
67          &xor   ("ecx",$T);
68         &shr    ($T,3);
69          &xor   ("esi","edi");
70         &ror    ("ecx",7);
71          &xor   ($T,"ecx");                     # T = sigma0(X[-15])
72         &ror    ("esi",17);
73          &add   ($T,&DWP(4*(9+15+16),"esp"));   # T += X[-16]
74         &shr    ("edi",10);
75          &add   ($T,&DWP(4*(9+15+16-9),"esp")); # T += X[-7]
76         #&xor   ("edi","esi")                   # sigma1(X[-2])
77         # &add  ($T,"edi");                     # T += sigma1(X[-2])
78         # &mov  (&DWP(4*(9+15),"esp"),$T);      # save X[0]
79
80         &BODY_00_15(1);
81 }
82 sub BODY_00_15() {
83     my $in_16_63=shift;
84
85         &mov    ("ecx",$E);
86          &xor   ("edi","esi")                   if ($in_16_63); # sigma1(X[-2])
87          &mov   ("esi",$Foff);
88         &ror    ("ecx",25-11);
89          &add   ($T,"edi")                      if ($in_16_63); # T += sigma1(X[-2])
90          &mov   ("edi",$Goff);
91         &xor    ("ecx",$E);
92          &xor   ("esi","edi");
93          &mov   ($T,&DWP(4*(9+15),"esp"))       if (!$in_16_63);
94          &mov   (&DWP(4*(9+15),"esp"),$T)       if ($in_16_63); # save X[0]
95         &ror    ("ecx",11-6);
96          &and   ("esi",$E);
97          &mov   ($Eoff,$E);             # modulo-scheduled
98         &xor    ($E,"ecx");
99          &xor   ("esi","edi");          # Ch(e,f,g)
100          &add   ($T,$Hoff);             # T += h
101         &ror    ($E,6);                 # Sigma1(e)
102          &mov   ("ecx",$A);
103          &add   ($T,"esi");             # T += Ch(e,f,g)
104
105         &ror    ("ecx",22-13);
106          &add   ($T,$E);                # T += Sigma1(e)
107          &mov   ("edi",$Boff);
108         &xor    ("ecx",$A);
109          &mov   ($Aoff,$A);             # modulo-scheduled
110          &lea   ("esp",&DWP(-4,"esp"));
111         &ror    ("ecx",13-2);
112          &mov   ("esi",&DWP(0,$K256));
113         &xor    ("ecx",$A);
114          &mov   ($E,$Eoff);             # e in next iteration, d in this one
115          &xor   ($A,"edi");             # a ^= b
116         &ror    ("ecx",2);              # Sigma0(a)
117
118          &add   ($T,"esi");             # T+= K[i]
119          &mov   (&DWP(0,"esp"),$A);     # (b^c) in next round
120         &add    ($E,$T);                # d += T
121          &and   ($A,&DWP(4,"esp"));     # a &= (b^c)
122         &add    ($T,"ecx");             # T += Sigma0(a)
123          &xor   ($A,"edi");             # h = Maj(a,b,c) = Ch(a^b,c,b)
124          &mov   ("ecx",&DWP(4*(9+15+16-1),"esp"))       if ($in_16_63); # preload T
125         &add    ($K256,4);
126          &add   ($A,$T);                # h += T
127 }
128
129 &external_label("OPENSSL_ia32cap_P")            if (!$i386);
130
131 &function_begin("sha256_block_data_order");
132         &mov    ("esi",wparam(0));      # ctx
133         &mov    ("edi",wparam(1));      # inp
134         &mov    ("eax",wparam(2));      # num
135         &mov    ("ebx","esp");          # saved sp
136
137         &call   (&label("pic_point"));  # make it PIC!
138 &set_label("pic_point");
139         &blindpop($K256);
140         &lea    ($K256,&DWP(&label("K256")."-".&label("pic_point"),$K256));
141
142         &sub    ("esp",16);
143         &and    ("esp",-64);
144
145         &shl    ("eax",6);
146         &add    ("eax","edi");
147         &mov    (&DWP(0,"esp"),"esi");  # ctx
148         &mov    (&DWP(4,"esp"),"edi");  # inp
149         &mov    (&DWP(8,"esp"),"eax");  # inp+num*128
150         &mov    (&DWP(12,"esp"),"ebx"); # saved sp
151                                                 if (!$i386) {
152         &picmeup("edx","OPENSSL_ia32cap_P",$K256,&label("K256"));
153         &mov    ("ecx",&DWP(0,"edx"));
154         &mov    ("edx",&DWP(4,"edx"));
155         &test   ("ecx",1<<20);          # check for P4
156         &jnz    (&label("loop"));
157         &and    ("ecx",1<<30);          # mask "Intel CPU" bit
158         &and    ("edx",1<<28);          # mask AVX bit
159         &or     ("ecx","edx");
160         &cmp    ("ecx",1<<28|1<<30);
161         &je     (&label("loop_shrd"));
162                                                 if ($unroll_after) {
163         &sub    ("eax","edi");
164         &cmp    ("eax",$unroll_after);
165         &jge    (&label("unrolled"));
166                                                 } }
167         &jmp    (&label("loop"));
168
169 sub COMPACT_LOOP() {
170 my $suffix=shift;
171
172 &set_label("loop$suffix",16);
173     # copy input block to stack reversing byte and dword order
174     for($i=0;$i<4;$i++) {
175         &mov    ("eax",&DWP($i*16+0,"edi"));
176         &mov    ("ebx",&DWP($i*16+4,"edi"));
177         &mov    ("ecx",&DWP($i*16+8,"edi"));
178         &bswap  ("eax");
179         &mov    ("edx",&DWP($i*16+12,"edi"));
180         &bswap  ("ebx");
181         &push   ("eax");
182         &bswap  ("ecx");
183         &push   ("ebx");
184         &bswap  ("edx");
185         &push   ("ecx");
186         &push   ("edx");
187     }
188         &add    ("edi",64);
189         &lea    ("esp",&DWP(-4*9,"esp"));# place for A,B,C,D,E,F,G,H
190         &mov    (&DWP(4*(9+16)+4,"esp"),"edi");
191
192         # copy ctx->h[0-7] to A,B,C,D,E,F,G,H on stack
193         &mov    ($A,&DWP(0,"esi"));
194         &mov    ("ebx",&DWP(4,"esi"));
195         &mov    ("ecx",&DWP(8,"esi"));
196         &mov    ("edi",&DWP(12,"esi"));
197         # &mov  ($Aoff,$A);
198         &mov    ($Boff,"ebx");
199         &xor    ("ebx","ecx");
200         &mov    ($Coff,"ecx");
201         &mov    ($Doff,"edi");
202         &mov    (&DWP(0,"esp"),"ebx");  # magic
203         &mov    ($E,&DWP(16,"esi"));    
204         &mov    ("ebx",&DWP(20,"esi"));
205         &mov    ("ecx",&DWP(24,"esi"));
206         &mov    ("edi",&DWP(28,"esi"));
207         # &mov  ($Eoff,$E);
208         &mov    ($Foff,"ebx");
209         &mov    ($Goff,"ecx");
210         &mov    ($Hoff,"edi");
211
212 &set_label("00_15$suffix",16);
213
214         &BODY_00_15();
215
216         &cmp    ("esi",0xc19bf174);
217         &jne    (&label("00_15$suffix"));
218
219         &mov    ("ecx",&DWP(4*(9+15+16-1),"esp"));      # preloaded in BODY_00_15(1)
220         &jmp    (&label("16_63$suffix"));
221
222 &set_label("16_63$suffix",16);
223
224         &BODY_16_63();
225
226         &cmp    ("esi",0xc67178f2);
227         &jne    (&label("16_63$suffix"));
228
229         &mov    ("esi",&DWP(4*(9+16+64)+0,"esp"));#ctx
230         # &mov  ($A,$Aoff);
231         &mov    ("ebx",$Boff);
232         # &mov  ("edi",$Coff);
233         &mov    ("ecx",$Doff);
234         &add    ($A,&DWP(0,"esi"));
235         &add    ("ebx",&DWP(4,"esi"));
236         &add    ("edi",&DWP(8,"esi"));
237         &add    ("ecx",&DWP(12,"esi"));
238         &mov    (&DWP(0,"esi"),$A);
239         &mov    (&DWP(4,"esi"),"ebx");
240         &mov    (&DWP(8,"esi"),"edi");
241         &mov    (&DWP(12,"esi"),"ecx");
242         # &mov  ($E,$Eoff);
243         &mov    ("eax",$Foff);
244         &mov    ("ebx",$Goff);
245         &mov    ("ecx",$Hoff);
246         &mov    ("edi",&DWP(4*(9+16+64)+4,"esp"));#inp
247         &add    ($E,&DWP(16,"esi"));
248         &add    ("eax",&DWP(20,"esi"));
249         &add    ("ebx",&DWP(24,"esi"));
250         &add    ("ecx",&DWP(28,"esi"));
251         &mov    (&DWP(16,"esi"),$E);
252         &mov    (&DWP(20,"esi"),"eax");
253         &mov    (&DWP(24,"esi"),"ebx");
254         &mov    (&DWP(28,"esi"),"ecx");
255
256         &lea    ("esp",&DWP(4*(9+16+64),"esp"));# destroy frame
257         &sub    ($K256,4*64);                   # rewind K
258
259         &cmp    ("edi",&DWP(8,"esp"));          # are we done yet?
260         &jb     (&label("loop$suffix"));
261 }
262         &COMPACT_LOOP();
263         &mov    ("esp",&DWP(12,"esp"));         # restore sp
264 &function_end_A();
265                                                 if (!$i386) {
266         # ~20% improvement on Sandy Bridge
267         local *ror = sub { &shrd(@_[0],@_) };
268         &COMPACT_LOOP("_shrd");
269         &mov    ("esp",&DWP(12,"esp"));         # restore sp
270 &function_end_A();
271                                                 }
272
273 &set_label("K256",64);  # Yes! I keep it in the code segment!
274 @K256=( 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,
275         0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
276         0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,
277         0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
278         0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,
279         0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,
280         0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,
281         0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,
282         0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,
283         0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,
284         0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,
285         0xd192e819,0xd6990624,0xf40e3585,0x106aa070,
286         0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,
287         0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
288         0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,
289         0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2     );
290 &data_word(@K256);
291
292 if (!$i386 && $unroll_after) {
293 my @AH=($A,$K256);
294
295 &set_label("unrolled",16);
296         &lea    ("esp",&DWP(-96,"esp"));
297         # copy ctx->h[0-7] to A,B,C,D,E,F,G,H on stack
298         &mov    ($AH[0],&DWP(0,"esi"));
299         &mov    ($AH[1],&DWP(4,"esi"));
300         &mov    ("ecx",&DWP(8,"esi"));
301         &mov    ("ebx",&DWP(12,"esi"));
302         #&mov   (&DWP(0,"esp"),$AH[0]);
303         &mov    (&DWP(4,"esp"),$AH[1]);
304         &xor    ($AH[1],"ecx");         # magic
305         &mov    (&DWP(8,"esp"),"ecx");
306         &mov    (&DWP(12,"esp"),"ebx");
307         &mov    ($E,&DWP(16,"esi"));    
308         &mov    ("ebx",&DWP(20,"esi"));
309         &mov    ("ecx",&DWP(24,"esi"));
310         &mov    ("esi",&DWP(28,"esi"));
311         #&mov   (&DWP(16,"esp"),$E);
312         &mov    (&DWP(20,"esp"),"ebx");
313         &mov    (&DWP(24,"esp"),"ecx");
314         &mov    (&DWP(28,"esp"),"esi");
315         &jmp    (&label("grand_loop"));
316
317 &set_label("grand_loop",16);
318     # copy input block to stack reversing byte order
319     for($i=0;$i<5;$i++) {
320         &mov    ("ebx",&DWP(12*$i+0,"edi"));
321         &mov    ("ecx",&DWP(12*$i+4,"edi"));
322         &bswap  ("ebx");
323         &mov    ("esi",&DWP(12*$i+8,"edi"));
324         &bswap  ("ecx");
325         &mov    (&DWP(32+12*$i+0,"esp"),"ebx");
326         &bswap  ("esi");
327         &mov    (&DWP(32+12*$i+4,"esp"),"ecx");
328         &mov    (&DWP(32+12*$i+8,"esp"),"esi");
329     }
330         &mov    ("ebx",&DWP($i*12,"edi"));
331         &add    ("edi",64);
332         &bswap  ("ebx");
333         &mov    (&DWP(96+4,"esp"),"edi");
334         &mov    (&DWP(32+12*$i,"esp"),"ebx");
335
336     my ($a,$b,$c,$d,$e,$f,$g,$h)=(0..7);        # offsets
337     sub off { &DWP(4*(((shift)-$i)&7),"esp"); }
338
339     for ($i=0;$i<64;$i++) {
340
341       if ($i>=16) {
342         &mov    ($T,"ecx");                     # "ecx" is preloaded
343         # &mov  ("esi",&DWP(32+4*(($i+14)&15),"esp"));
344         &ror    ("ecx",18-7);
345          &mov   ("edi","esi");
346         &ror    ("esi",19-17);
347          &xor   ("ecx",$T);
348         &shr    ($T,3);
349          &xor   ("esi","edi");
350         &ror    ("ecx",7);
351          &xor   ($T,"ecx");                     # T = sigma0(X[-15])
352         &ror    ("esi",17);
353          &add   ($T,&DWP(32+4*($i&15),"esp"));  # T += X[-16]
354         &shr    ("edi",10);
355          &add   ($T,&DWP(32+4*(($i+9)&15),"esp"));      # T += X[-7]
356         #&xor   ("edi","esi")                   # sigma1(X[-2])
357         # &add  ($T,"edi");                     # T += sigma1(X[-2])
358         # &mov  (&DWP(4*(9+15),"esp"),$T);      # save X[0]
359       }
360         &mov    ("ecx",$E);
361          &xor   ("edi","esi")                   if ($i>=16);    # sigma1(X[-2])
362          &mov   ("esi",&off($f));
363         &ror    ("ecx",25-11);
364          &add   ($T,"edi")                      if ($i>=16);    # T += sigma1(X[-2])
365          &mov   ("edi",&off($g));
366         &xor    ("ecx",$E);
367          &xor   ("esi","edi");
368          &mov   ($T,&DWP(32+4*($i&15),"esp"))   if ($i<16);     # X[i]
369          &mov   (&DWP(32+4*($i&15),"esp"),$T)   if ($i>=16);    # save X[0]
370         &ror    ("ecx",11-6);
371          &and   ("esi",$E);
372          &mov   (&off($e),$E);          # modulo-scheduled
373         &xor    ($E,"ecx");
374          &xor   ("esi","edi");          # Ch(e,f,g)
375          &add   ($T,&off($h));          # T += h
376         &ror    ($E,6);                 # Sigma1(e)
377          &mov   ("ecx",$AH[0]);
378          &add   ($T,"esi");             # T += Ch(e,f,g)
379
380         &ror    ("ecx",22-13);
381          &mov   ("edi",&off($b));
382         &xor    ("ecx",$AH[0]);
383          &mov   (&off($a),$AH[0]);      # modulo-scheduled
384         &ror    ("ecx",13-2);
385          &lea   ($T,&DWP(@K256[$i],$T,$E));     # T += Sigma1(1)+K[i]
386          &mov   ($E,&off($d));          # e in next iteration, d in this one
387         &xor    ("ecx",$AH[0]);
388          &xor   ($AH[0],"edi");         # a ^= b, (b^c) in next round
389         &ror    ("ecx",2);              # Sigma0(a)
390
391         &add    ($E,$T);                # d += T
392          &and   ($AH[1],$AH[0]);        # a &= (b^c)
393         &add    ($T,"ecx");             # T += Sigma0(a)
394          &mov   ("ecx",&DWP(32+4*(($i+2)&15),"esp"))    if ($i>=15 && $i<63);
395         &xor    ($AH[1],"edi");         # h = Maj(a,b,c) = Ch(a^b,c,b)
396          &mov   ("esi",&DWP(32+4*(($i+15)&15),"esp"))   if ($i>=15 && $i<63);
397         &add    ($AH[1],$T);            # h += T
398
399         unshift(@AH,pop(@AH));          # rotate(a,h)
400     }
401         &mov    ("esi",&DWP(96,"esp")); #ctx
402                                         #&mov   ($AH[0],&DWP(0,"esp"));
403         &xor    ($AH[1],"edi");         #&mov   ($AH[1],&DWP(4,"esp"));
404                                         #&mov   ("edi", &DWP(8,"esp"));
405         &mov    ("ecx",&DWP(12,"esp"));
406         &add    ($AH[0],&DWP(0,"esi"));
407         &add    ($AH[1],&DWP(4,"esi"));
408         &add    ("edi",&DWP(8,"esi"));
409         &add    ("ecx",&DWP(12,"esi"));
410         &mov    (&DWP(0,"esi"),$AH[0]);
411         &mov    (&DWP(4,"esi"),$AH[1]);
412         &mov    (&DWP(8,"esi"),"edi");
413         &mov    (&DWP(12,"esi"),"ecx");
414          #&mov  (&DWP(0,"esp"),$AH[0]);
415          &mov   (&DWP(4,"esp"),$AH[1]);
416          &xor   ($AH[1],"edi");         # magic
417          &mov   (&DWP(8,"esp"),"edi");
418          &mov   (&DWP(12,"esp"),"ecx");
419         #&mov   ($E,&DWP(16,"esp"));
420         &mov    ("edi",&DWP(20,"esp"));
421         &mov    ("ebx",&DWP(24,"esp"));
422         &mov    ("ecx",&DWP(28,"esp"));
423         &add    ($E,&DWP(16,"esi"));
424         &add    ("edi",&DWP(20,"esi"));
425         &add    ("ebx",&DWP(24,"esi"));
426         &add    ("ecx",&DWP(28,"esi"));
427         &mov    (&DWP(16,"esi"),$E);
428         &mov    (&DWP(20,"esi"),"edi");
429         &mov    (&DWP(24,"esi"),"ebx");
430         &mov    (&DWP(28,"esi"),"ecx");
431          #&mov  (&DWP(16,"esp"),$E);
432          &mov   (&DWP(20,"esp"),"edi");
433         &mov    ("edi",&DWP(96+4,"esp"));       # inp
434          &mov   (&DWP(24,"esp"),"ebx");
435          &mov   (&DWP(28,"esp"),"ecx");
436
437         &cmp    ("edi",&DWP(96+8,"esp"));       # are we done yet?
438         &jb     (&label("grand_loop"));
439
440         &mov    ("esp",&DWP(96+12,"esp"));      # restore sp
441 &function_end_A();
442 }
443 &function_end_B("sha256_block_data_order");
444 &asciz("SHA256 block transform for x86, CRYPTOGAMS by <appro\@openssl.org>");
445
446 &asm_finish();