3236a39551a963cb8107aa0db79af5b4a8cbf301
[openssl.git] / crypto / sha / asm / sha256-586.pl
1 #!/usr/bin/env perl
2 #
3 # ====================================================================
4 # Written by Andy Polyakov <appro@fy.chalmers.se> 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 one of Pavel Semjanov's ideas resulted in
19 # ~5% improvement on P4, AMD and Sandy Bridge, and ~13% on Atom.
20 # Pavel also suggested full unroll. While his code runs ~20%/13%/6%
21 # faster on K8/Core2/Sandy Bridge, it's 9.6x larger and ~6%/18%/24%
22 # slower on P4/Atom/Pentium...
23 #
24 # Performance in clock cycles per processed byte (less is better):
25 #
26 #               Pentium PIII    P4      AMD K8  Core2   SB(**)  Atom
27 # gcc           46      36      41      27      26
28 # icc           57      33      38      25      23      
29 # x86 asm       39      29      31      19      18      19(**)  31
30 # x86_64 asm(*) -       -       21      16      16      18      25
31 #
32 # (*)   x86_64 assembler performance is presented for reference
33 #       purposes.
34 # (**)  Sandy Bridge results can be improved by ~20% by replacing
35 #       ror with equivalent shrd.
36
37 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
38 push(@INC,"${dir}","${dir}../../perlasm");
39 require "x86asm.pl";
40
41 &asm_init($ARGV[0],"sha512-586.pl",$ARGV[$#ARGV] eq "386");
42
43 $A="eax";
44 $E="edx";
45 $T="ebx";
46 $Aoff=&DWP(4,"esp");
47 $Boff=&DWP(8,"esp");
48 $Coff=&DWP(12,"esp");
49 $Doff=&DWP(16,"esp");
50 $Eoff=&DWP(20,"esp");
51 $Foff=&DWP(24,"esp");
52 $Goff=&DWP(28,"esp");
53 $Hoff=&DWP(32,"esp");
54 $Xoff=&DWP(36,"esp");
55 $K256="ebp";
56
57 # *ror = sub { &shrd(@_[0],@_) };
58
59 sub BODY_00_15() {
60     my $in_16_63=shift;
61
62         &mov    ("ecx",$E);
63          &mov   ("esi",$Foff);
64         &ror    ("ecx",25-11);
65          &add   ($T,"edi")                      if ($in_16_63); # T += sigma1(X[-2])
66         &xor    ("ecx",$E);
67          &mov   ("edi",$Goff);
68         &ror    ("ecx",11-6);
69          &xor   ("esi","edi");
70         &xor    ("ecx",$E);
71          &mov   (&DWP(4*(9+15),"esp"),$T)       if ($in_16_63); # save X[0]
72         &ror    ("ecx",6);      # Sigma1(e)
73          &and   ("esi",$E);
74         &add    ($T,"ecx");     # T += Sigma1(e)
75          &mov   ($Eoff,$E);     # modulo-scheduled
76         &xor    ("esi","edi");  # Ch(e,f,g)
77          &add   ($T,$Hoff);     # T += h
78
79         &mov    ("ecx",$A);
80          &mov   ($E,$Doff);     # e becomes d, which is e in next iteration
81         &ror    ("ecx",22-13);
82          &add   ($T,"esi");     # T += Ch(e,f,g)
83         &xor    ("ecx",$A);
84          &mov   ("esi",&DWP(0,$K256));
85         &ror    ("ecx",13-2);
86          &mov   ("edi",$Boff);
87         &xor    ("ecx",$A);
88          &mov   ($Aoff,$A);     # modulo-scheduled
89         &ror    ("ecx",2);      # Sigma0(a)
90
91          &xor   ($A,"edi");     # a ^= b
92         &add    ($T,"esi");
93          &push  ($A);           # (b^c) in next round
94         &add    ($E,$T);        # d += T
95          &and   ($A,&DWP(4,"esp"));     # a &= (b^c)
96         &add    ($T,"ecx");     # T += Sigma0(a)
97          &xor   ($A,"edi");     # h = Maj(a,b,c) = Ch(a^b,c,b)
98
99         &add    ($K256,4);
100          &add   ($A,$T);        # h += T
101         &mov    ($T,&DWP(4*(9+15+16-1),"esp"))  if ($in_16_63); # preload T
102 }
103
104 &function_begin("sha256_block_data_order");
105         &mov    ("esi",wparam(0));      # ctx
106         &mov    ("edi",wparam(1));      # inp
107         &mov    ("eax",wparam(2));      # num
108         &mov    ("ebx","esp");          # saved sp
109
110         &call   (&label("pic_point"));  # make it PIC!
111 &set_label("pic_point");
112         &blindpop($K256);
113         &lea    ($K256,&DWP(&label("K256")."-".&label("pic_point"),$K256));
114
115         &sub    ("esp",16);
116         &and    ("esp",-64);
117
118         &shl    ("eax",6);
119         &add    ("eax","edi");
120         &mov    (&DWP(0,"esp"),"esi");  # ctx
121         &mov    (&DWP(4,"esp"),"edi");  # inp
122         &mov    (&DWP(8,"esp"),"eax");  # inp+num*128
123         &mov    (&DWP(12,"esp"),"ebx"); # saved sp
124
125 &set_label("loop",16);
126     # copy input block to stack reversing byte and dword order
127     for($i=0;$i<4;$i++) {
128         &mov    ("eax",&DWP($i*16+0,"edi"));
129         &mov    ("ebx",&DWP($i*16+4,"edi"));
130         &mov    ("ecx",&DWP($i*16+8,"edi"));
131         &bswap  ("eax");
132         &mov    ("edx",&DWP($i*16+12,"edi"));
133         &bswap  ("ebx");
134         &push   ("eax");
135         &bswap  ("ecx");
136         &push   ("ebx");
137         &bswap  ("edx");
138         &push   ("ecx");
139         &push   ("edx");
140     }
141         &add    ("edi",64);
142         &lea    ("esp",&DWP(-4*9,"esp"));# place for A,B,C,D,E,F,G,H
143         &mov    (&DWP(4*(9+16)+4,"esp"),"edi");
144
145         # copy ctx->h[0-7] to A,B,C,D,E,F,G,H on stack
146         &mov    ($A,&DWP(0,"esi"));
147         &mov    ("ebx",&DWP(4,"esi"));
148         &mov    ("ecx",&DWP(8,"esi"));
149         &mov    ("edi",&DWP(12,"esi"));
150         # &mov  ($Aoff,$A);
151         &mov    ($Boff,"ebx");
152         &xor    ("ebx","ecx");
153         &mov    ($Coff,"ecx");
154         &mov    ($Doff,"edi");
155         &mov    (&DWP(0,"esp"),"ebx");  # magic
156         &mov    ($E,&DWP(16,"esi"));    
157         &mov    ("ebx",&DWP(20,"esi"));
158         &mov    ("ecx",&DWP(24,"esi"));
159         &mov    ("edi",&DWP(28,"esi"));
160         # &mov  ($Eoff,$E);
161         &mov    ($Foff,"ebx");
162         &mov    ($Goff,"ecx");
163         &mov    ($Hoff,"edi");
164
165 &set_label("00_15",16);
166         &mov    ($T,&DWP(4*(9+15),"esp"));
167
168         &BODY_00_15();
169
170         &cmp    ("esi",0xc19bf174);
171         &jne    (&label("00_15"));
172
173         &mov    ($T,&DWP(4*(9+15+16-1),"esp")); # preloaded in BODY_00_15(1)
174 &set_label("16_63",16);
175         &mov    ("esi",$T);
176          &mov   ("ecx",&DWP(4*(9+15+16-14),"esp"));
177         &ror    ("esi",18-7);
178          &mov   ("edi","ecx");
179         &xor    ("esi",$T);
180          &shr   ($T,3);
181         &ror    ("esi",7);
182
183         &ror    ("edi",19-17);
184          &xor   ($T,"esi");                     # T = sigma0(X[-15])
185         &xor    ("edi","ecx");
186          &shr   ("ecx",10);
187         &ror    ("edi",17);
188          &add   ($T,&DWP(4*(9+15+16),"esp"));   # T += X[-16]
189         &xor    ("edi","ecx");                  # sigma1(X[-2])
190
191          &add   ($T,&DWP(4*(9+15+16-9),"esp")); # T += X[-7]
192         # &add  ($T,"edi");                     # T += sigma1(X[-2])
193         # &mov  (&DWP(4*(9+15),"esp"),$T);      # save X[0]
194
195         &BODY_00_15(1);
196
197         &cmp    ("esi",0xc67178f2);
198         &jne    (&label("16_63"));
199
200         &mov    ("esi",&DWP(4*(9+16+64)+0,"esp"));#ctx
201         # &mov  ($A,$Aoff);
202         &mov    ("ebx",$Boff);
203         &mov    ("ecx",$Coff);
204         &mov    ("edi",$Doff);
205         &add    ($A,&DWP(0,"esi"));
206         &add    ("ebx",&DWP(4,"esi"));
207         &add    ("ecx",&DWP(8,"esi"));
208         &add    ("edi",&DWP(12,"esi"));
209         &mov    (&DWP(0,"esi"),$A);
210         &mov    (&DWP(4,"esi"),"ebx");
211         &mov    (&DWP(8,"esi"),"ecx");
212         &mov    (&DWP(12,"esi"),"edi");
213         # &mov  ($E,$Eoff);
214         &mov    ("eax",$Foff);
215         &mov    ("ebx",$Goff);
216         &mov    ("ecx",$Hoff);
217         &mov    ("edi",&DWP(4*(9+16+64)+4,"esp"));#inp
218         &add    ($E,&DWP(16,"esi"));
219         &add    ("eax",&DWP(20,"esi"));
220         &add    ("ebx",&DWP(24,"esi"));
221         &add    ("ecx",&DWP(28,"esi"));
222         &mov    (&DWP(16,"esi"),$E);
223         &mov    (&DWP(20,"esi"),"eax");
224         &mov    (&DWP(24,"esi"),"ebx");
225         &mov    (&DWP(28,"esi"),"ecx");
226
227         &lea    ("esp",&DWP(4*(9+16+64),"esp"));# destroy frame
228         &sub    ($K256,4*64);                   # rewind K
229
230         &cmp    ("edi",&DWP(8,"esp"));          # are we done yet?
231         &jb     (&label("loop"));
232
233         &mov    ("esp",&DWP(12,"esp"));         # restore sp
234 &function_end_A();
235
236 &set_label("K256",64);  # Yes! I keep it in the code segment!
237         &data_word(0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5);
238         &data_word(0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5);
239         &data_word(0xd807aa98,0x12835b01,0x243185be,0x550c7dc3);
240         &data_word(0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174);
241         &data_word(0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc);
242         &data_word(0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da);
243         &data_word(0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7);
244         &data_word(0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967);
245         &data_word(0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13);
246         &data_word(0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85);
247         &data_word(0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3);
248         &data_word(0xd192e819,0xd6990624,0xf40e3585,0x106aa070);
249         &data_word(0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5);
250         &data_word(0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3);
251         &data_word(0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208);
252         &data_word(0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2);
253 &function_end_B("sha256_block_data_order");
254 &asciz("SHA256 block transform for x86, CRYPTOGAMS by <appro\@openssl.org>");
255
256 &asm_finish();