x86[_64] assembly pack: update benchmark results.
[openssl.git] / crypto / sha / asm / sha512-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 # SHA512 block transform for x86. September 2007.
11 #
12 # Performance in clock cycles per processed byte (less is better):
13 #
14 #               PIII    P4      AMD K8  Core2   SB      Atom    Bldzr
15 # gcc           75      116     54      66      58      126     121
16 # icc           77      95      55      57      -       -       -
17 # x86 asm       56      82      36      40      35      68      50
18 # SSE2 asm      -       38      24      20      16      64(**)  18
19 # x86_64 asm(*) -       33      9.6     10.3    11.3    14.7    13.5
20 #
21 # (*)   x86_64 assembler performance is presented for reference
22 #       purposes.
23 # (**)  paddq is increadibly slow on Atom.
24 #
25 # IALU code-path is optimized for elder Pentiums. On vanilla Pentium
26 # performance improvement over compiler generated code reaches ~60%,
27 # while on PIII - ~35%. On newer ยต-archs improvement varies from 15%
28 # to 50%, but it's less important as they are expected to execute SSE2
29 # code-path, which is commonly ~2-3x faster [than compiler generated
30 # code]. SSE2 code-path is as fast as original sha512-sse2.pl, even
31 # though it does not use 128-bit operations. The latter means that
32 # SSE2-aware kernel is no longer required to execute the code. Another
33 # difference is that new code optimizes amount of writes, but at the
34 # cost of increased data cache "footprint" by 1/2KB.
35
36 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
37 push(@INC,"${dir}","${dir}../../perlasm");
38 require "x86asm.pl";
39
40 &asm_init($ARGV[0],"sha512-586.pl",$ARGV[$#ARGV] eq "386");
41
42 $sse2=0;
43 for (@ARGV) { $sse2=1 if (/-DOPENSSL_IA32_SSE2/); }
44
45 &external_label("OPENSSL_ia32cap_P") if ($sse2);
46
47 $Tlo=&DWP(0,"esp");     $Thi=&DWP(4,"esp");
48 $Alo=&DWP(8,"esp");     $Ahi=&DWP(8+4,"esp");
49 $Blo=&DWP(16,"esp");    $Bhi=&DWP(16+4,"esp");
50 $Clo=&DWP(24,"esp");    $Chi=&DWP(24+4,"esp");
51 $Dlo=&DWP(32,"esp");    $Dhi=&DWP(32+4,"esp");
52 $Elo=&DWP(40,"esp");    $Ehi=&DWP(40+4,"esp");
53 $Flo=&DWP(48,"esp");    $Fhi=&DWP(48+4,"esp");
54 $Glo=&DWP(56,"esp");    $Ghi=&DWP(56+4,"esp");
55 $Hlo=&DWP(64,"esp");    $Hhi=&DWP(64+4,"esp");
56 $K512="ebp";
57
58 $Asse2=&QWP(0,"esp");
59 $Bsse2=&QWP(8,"esp");
60 $Csse2=&QWP(16,"esp");
61 $Dsse2=&QWP(24,"esp");
62 $Esse2=&QWP(32,"esp");
63 $Fsse2=&QWP(40,"esp");
64 $Gsse2=&QWP(48,"esp");
65 $Hsse2=&QWP(56,"esp");
66
67 $A="mm0";       # B-D and
68 $E="mm4";       # F-H are commonly loaded to respectively mm1-mm3 and
69                 # mm5-mm7, but it's done on on-demand basis...
70
71 sub BODY_00_15_sse2 {
72     my $prefetch=shift;
73
74         &movq   ("mm5",$Fsse2);                 # load f
75         &movq   ("mm6",$Gsse2);                 # load g
76         &movq   ("mm7",$Hsse2);                 # load h
77
78         &movq   ("mm1",$E);                     # %mm1 is sliding right
79         &movq   ("mm2",$E);                     # %mm2 is sliding left
80         &psrlq  ("mm1",14);
81         &movq   ($Esse2,$E);                    # modulo-scheduled save e
82         &psllq  ("mm2",23);
83         &movq   ("mm3","mm1");                  # %mm3 is T1
84         &psrlq  ("mm1",4);
85         &pxor   ("mm3","mm2");
86         &psllq  ("mm2",23);
87         &pxor   ("mm3","mm1");
88         &psrlq  ("mm1",23);
89         &pxor   ("mm3","mm2");
90         &psllq  ("mm2",4);
91         &pxor   ("mm3","mm1");
92         &paddq  ("mm7",QWP(0,$K512));           # h+=K512[i]
93         &pxor   ("mm3","mm2");                  # T1=Sigma1_512(e)
94
95         &pxor   ("mm5","mm6");                  # f^=g
96         &movq   ("mm1",$Bsse2);                 # load b
97         &pand   ("mm5",$E);                     # f&=e
98         &movq   ("mm2",$Csse2);                 # load c
99         &pxor   ("mm5","mm6");                  # f^=g
100         &movq   ($E,$Dsse2);                    # e = load d
101         &paddq  ("mm3","mm5");                  # T1+=Ch(e,f,g)
102         &movq   (&QWP(0,"esp"),$A);             # modulo-scheduled save a
103         &paddq  ("mm3","mm7");                  # T1+=h
104
105         &movq   ("mm5",$A);                     # %mm5 is sliding right
106         &movq   ("mm6",$A);                     # %mm6 is sliding left
107         &paddq  ("mm3",&QWP(8*9,"esp"));        # T1+=X[0]
108         &psrlq  ("mm5",28);
109         &paddq  ($E,"mm3");                     # e += T1
110         &psllq  ("mm6",25);
111         &movq   ("mm7","mm5");                  # %mm7 is T2
112         &psrlq  ("mm5",6);
113         &pxor   ("mm7","mm6");
114         &psllq  ("mm6",5);
115         &pxor   ("mm7","mm5");
116         &psrlq  ("mm5",5);
117         &pxor   ("mm7","mm6");
118         &psllq  ("mm6",6);
119         &pxor   ("mm7","mm5");
120         &sub    ("esp",8);
121         &pxor   ("mm7","mm6");                  # T2=Sigma0_512(a)
122
123         &movq   ("mm5",$A);                     # %mm5=a
124         &por    ($A,"mm2");                     # a=a|c
125         &movq   ("mm6",&QWP(8*(9+16-14),"esp")) if ($prefetch);
126         &pand   ("mm5","mm2");                  # %mm5=a&c
127         &pand   ($A,"mm1");                     # a=(a|c)&b
128         &movq   ("mm2",&QWP(8*(9+16-1),"esp"))  if ($prefetch);
129         &por    ("mm5",$A);                     # %mm5=(a&c)|((a|c)&b)
130         &paddq  ("mm7","mm5");                  # T2+=Maj(a,b,c)
131         &movq   ($A,"mm3");                     # a=T1
132
133         &mov    (&LB("edx"),&BP(0,$K512));
134         &paddq  ($A,"mm7");                     # a+=T2
135         &add    ($K512,8);
136 }
137
138 sub BODY_00_15_x86 {
139         #define Sigma1(x)       (ROTR((x),14) ^ ROTR((x),18)  ^ ROTR((x),41))
140         #       LO              lo>>14^hi<<18 ^ lo>>18^hi<<14 ^ hi>>9^lo<<23
141         #       HI              hi>>14^lo<<18 ^ hi>>18^lo<<14 ^ lo>>9^hi<<23
142         &mov    ("ecx",$Elo);
143         &mov    ("edx",$Ehi);
144         &mov    ("esi","ecx");
145
146         &shr    ("ecx",9);      # lo>>9
147         &mov    ("edi","edx");
148         &shr    ("edx",9);      # hi>>9
149         &mov    ("ebx","ecx");
150         &shl    ("esi",14);     # lo<<14
151         &mov    ("eax","edx");
152         &shl    ("edi",14);     # hi<<14
153         &xor    ("ebx","esi");
154
155         &shr    ("ecx",14-9);   # lo>>14
156         &xor    ("eax","edi");
157         &shr    ("edx",14-9);   # hi>>14
158         &xor    ("eax","ecx");
159         &shl    ("esi",18-14);  # lo<<18
160         &xor    ("ebx","edx");
161         &shl    ("edi",18-14);  # hi<<18
162         &xor    ("ebx","esi");
163
164         &shr    ("ecx",18-14);  # lo>>18
165         &xor    ("eax","edi");
166         &shr    ("edx",18-14);  # hi>>18
167         &xor    ("eax","ecx");
168         &shl    ("esi",23-18);  # lo<<23
169         &xor    ("ebx","edx");
170         &shl    ("edi",23-18);  # hi<<23
171         &xor    ("eax","esi");
172         &xor    ("ebx","edi");                  # T1 = Sigma1(e)
173
174         &mov    ("ecx",$Flo);
175         &mov    ("edx",$Fhi);
176         &mov    ("esi",$Glo);
177         &mov    ("edi",$Ghi);
178          &add   ("eax",$Hlo);
179          &adc   ("ebx",$Hhi);                   # T1 += h
180         &xor    ("ecx","esi");
181         &xor    ("edx","edi");
182         &and    ("ecx",$Elo);
183         &and    ("edx",$Ehi);
184          &add   ("eax",&DWP(8*(9+15)+0,"esp"));
185          &adc   ("ebx",&DWP(8*(9+15)+4,"esp")); # T1 += X[0]
186         &xor    ("ecx","esi");
187         &xor    ("edx","edi");                  # Ch(e,f,g) = (f^g)&e)^g
188
189         &mov    ("esi",&DWP(0,$K512));
190         &mov    ("edi",&DWP(4,$K512));          # K[i]
191         &add    ("eax","ecx");
192         &adc    ("ebx","edx");                  # T1 += Ch(e,f,g)
193         &mov    ("ecx",$Dlo);
194         &mov    ("edx",$Dhi);
195         &add    ("eax","esi");
196         &adc    ("ebx","edi");                  # T1 += K[i]
197         &mov    ($Tlo,"eax");
198         &mov    ($Thi,"ebx");                   # put T1 away
199         &add    ("eax","ecx");
200         &adc    ("ebx","edx");                  # d += T1
201
202         #define Sigma0(x)       (ROTR((x),28) ^ ROTR((x),34) ^ ROTR((x),39))
203         #       LO              lo>>28^hi<<4  ^ hi>>2^lo<<30 ^ hi>>7^lo<<25
204         #       HI              hi>>28^lo<<4  ^ lo>>2^hi<<30 ^ lo>>7^hi<<25
205         &mov    ("ecx",$Alo);
206         &mov    ("edx",$Ahi);
207         &mov    ($Dlo,"eax");
208         &mov    ($Dhi,"ebx");
209         &mov    ("esi","ecx");
210
211         &shr    ("ecx",2);      # lo>>2
212         &mov    ("edi","edx");
213         &shr    ("edx",2);      # hi>>2
214         &mov    ("ebx","ecx");
215         &shl    ("esi",4);      # lo<<4
216         &mov    ("eax","edx");
217         &shl    ("edi",4);      # hi<<4
218         &xor    ("ebx","esi");
219
220         &shr    ("ecx",7-2);    # lo>>7
221         &xor    ("eax","edi");
222         &shr    ("edx",7-2);    # hi>>7
223         &xor    ("ebx","ecx");
224         &shl    ("esi",25-4);   # lo<<25
225         &xor    ("eax","edx");
226         &shl    ("edi",25-4);   # hi<<25
227         &xor    ("eax","esi");
228
229         &shr    ("ecx",28-7);   # lo>>28
230         &xor    ("ebx","edi");
231         &shr    ("edx",28-7);   # hi>>28
232         &xor    ("eax","ecx");
233         &shl    ("esi",30-25);  # lo<<30
234         &xor    ("ebx","edx");
235         &shl    ("edi",30-25);  # hi<<30
236         &xor    ("eax","esi");
237         &xor    ("ebx","edi");                  # Sigma0(a)
238
239         &mov    ("ecx",$Alo);
240         &mov    ("edx",$Ahi);
241         &mov    ("esi",$Blo);
242         &mov    ("edi",$Bhi);
243         &add    ("eax",$Tlo);
244         &adc    ("ebx",$Thi);                   # T1 = Sigma0(a)+T1
245         &or     ("ecx","esi");
246         &or     ("edx","edi");
247         &and    ("ecx",$Clo);
248         &and    ("edx",$Chi);
249         &and    ("esi",$Alo);
250         &and    ("edi",$Ahi);
251         &or     ("ecx","esi");
252         &or     ("edx","edi");                  # Maj(a,b,c) = ((a|b)&c)|(a&b)
253
254         &add    ("eax","ecx");
255         &adc    ("ebx","edx");                  # T1 += Maj(a,b,c)
256         &mov    ($Tlo,"eax");
257         &mov    ($Thi,"ebx");
258
259         &mov    (&LB("edx"),&BP(0,$K512));      # pre-fetch LSB of *K
260         &sub    ("esp",8);
261         &lea    ($K512,&DWP(8,$K512));          # K++
262 }
263
264
265 &function_begin("sha512_block_data_order");
266         &mov    ("esi",wparam(0));      # ctx
267         &mov    ("edi",wparam(1));      # inp
268         &mov    ("eax",wparam(2));      # num
269         &mov    ("ebx","esp");          # saved sp
270
271         &call   (&label("pic_point"));  # make it PIC!
272 &set_label("pic_point");
273         &blindpop($K512);
274         &lea    ($K512,&DWP(&label("K512")."-".&label("pic_point"),$K512));
275
276         &sub    ("esp",16);
277         &and    ("esp",-64);
278
279         &shl    ("eax",7);
280         &add    ("eax","edi");
281         &mov    (&DWP(0,"esp"),"esi");  # ctx
282         &mov    (&DWP(4,"esp"),"edi");  # inp
283         &mov    (&DWP(8,"esp"),"eax");  # inp+num*128
284         &mov    (&DWP(12,"esp"),"ebx"); # saved sp
285
286 if ($sse2) {
287         &picmeup("edx","OPENSSL_ia32cap_P",$K512,&label("K512"));
288         &bt     (&DWP(0,"edx"),26);
289         &jnc    (&label("loop_x86"));
290
291         # load ctx->h[0-7]
292         &movq   ($A,&QWP(0,"esi"));
293         &movq   ("mm1",&QWP(8,"esi"));
294         &movq   ("mm2",&QWP(16,"esi"));
295         &movq   ("mm3",&QWP(24,"esi"));
296         &movq   ($E,&QWP(32,"esi"));
297         &movq   ("mm5",&QWP(40,"esi"));
298         &movq   ("mm6",&QWP(48,"esi"));
299         &movq   ("mm7",&QWP(56,"esi"));
300         &sub    ("esp",8*10);
301
302 &set_label("loop_sse2",16);
303         # &movq ($Asse2,$A);
304         &movq   ($Bsse2,"mm1");
305         &movq   ($Csse2,"mm2");
306         &movq   ($Dsse2,"mm3");
307         # &movq ($Esse2,$E);
308         &movq   ($Fsse2,"mm5");
309         &movq   ($Gsse2,"mm6");
310         &movq   ($Hsse2,"mm7");
311
312         &mov    ("ecx",&DWP(0,"edi"));
313         &mov    ("edx",&DWP(4,"edi"));
314         &add    ("edi",8);
315         &bswap  ("ecx");
316         &bswap  ("edx");
317         &mov    (&DWP(8*9+4,"esp"),"ecx");
318         &mov    (&DWP(8*9+0,"esp"),"edx");
319         &jmp    (&label("00_14_sse2"));
320
321 &set_label("00_14_sse2",16);
322         &mov    ("eax",&DWP(0,"edi"));
323         &mov    ("ebx",&DWP(4,"edi"));
324         &add    ("edi",8);
325         &bswap  ("eax");
326         &bswap  ("ebx");
327         &mov    (&DWP(8*8+4,"esp"),"eax");
328         &mov    (&DWP(8*8+0,"esp"),"ebx");
329
330         &BODY_00_15_sse2();
331
332         &cmp    (&LB("edx"),0x35);
333         &jne    (&label("00_14_sse2"));
334
335         &BODY_00_15_sse2(1);
336
337 &set_label("16_79_sse2",16);
338         #&movq  ("mm2",&QWP(8*(9+16-1),"esp")); #prefetched in BODY_00_15 
339         #&movq  ("mm6",&QWP(8*(9+16-14),"esp"));
340         &movq   ("mm1","mm2");
341
342         &psrlq  ("mm2",1);
343         &movq   ("mm7","mm6");
344         &psrlq  ("mm6",6);
345         &movq   ("mm3","mm2");
346
347         &psrlq  ("mm2",7-1);
348         &movq   ("mm5","mm6");
349         &psrlq  ("mm6",19-6);
350         &pxor   ("mm3","mm2");
351
352         &psrlq  ("mm2",8-7);
353         &pxor   ("mm5","mm6");
354         &psrlq  ("mm6",61-19);
355         &pxor   ("mm3","mm2");
356
357         &movq   ("mm2",&QWP(8*(9+16),"esp"));
358
359         &psllq  ("mm1",56);
360         &pxor   ("mm5","mm6");
361         &psllq  ("mm7",3);
362         &pxor   ("mm3","mm1");
363
364         &paddq  ("mm2",&QWP(8*(9+16-9),"esp"));
365
366         &psllq  ("mm1",63-56);
367         &pxor   ("mm5","mm7");
368         &psllq  ("mm7",45-3);
369         &pxor   ("mm3","mm1");
370         &pxor   ("mm5","mm7");
371
372         &paddq  ("mm3","mm5");
373         &paddq  ("mm3","mm2");
374         &movq   (&QWP(8*9,"esp"),"mm3");
375
376         &BODY_00_15_sse2(1);
377
378         &cmp    (&LB("edx"),0x17);
379         &jne    (&label("16_79_sse2"));
380
381         # &movq ($A,$Asse2);
382         &movq   ("mm1",$Bsse2);
383         &movq   ("mm2",$Csse2);
384         &movq   ("mm3",$Dsse2);
385         # &movq ($E,$Esse2);
386         &movq   ("mm5",$Fsse2);
387         &movq   ("mm6",$Gsse2);
388         &movq   ("mm7",$Hsse2);
389
390         &paddq  ($A,&QWP(0,"esi"));
391         &paddq  ("mm1",&QWP(8,"esi"));
392         &paddq  ("mm2",&QWP(16,"esi"));
393         &paddq  ("mm3",&QWP(24,"esi"));
394         &paddq  ($E,&QWP(32,"esi"));
395         &paddq  ("mm5",&QWP(40,"esi"));
396         &paddq  ("mm6",&QWP(48,"esi"));
397         &paddq  ("mm7",&QWP(56,"esi"));
398
399         &movq   (&QWP(0,"esi"),$A);
400         &movq   (&QWP(8,"esi"),"mm1");
401         &movq   (&QWP(16,"esi"),"mm2");
402         &movq   (&QWP(24,"esi"),"mm3");
403         &movq   (&QWP(32,"esi"),$E);
404         &movq   (&QWP(40,"esi"),"mm5");
405         &movq   (&QWP(48,"esi"),"mm6");
406         &movq   (&QWP(56,"esi"),"mm7");
407
408         &add    ("esp",8*80);                   # destroy frame
409         &sub    ($K512,8*80);                   # rewind K
410
411         &cmp    ("edi",&DWP(8*10+8,"esp"));     # are we done yet?
412         &jb     (&label("loop_sse2"));
413
414         &emms   ();
415         &mov    ("esp",&DWP(8*10+12,"esp"));    # restore sp
416 &function_end_A();
417 }
418 &set_label("loop_x86",16);
419     # copy input block to stack reversing byte and qword order
420     for ($i=0;$i<8;$i++) {
421         &mov    ("eax",&DWP($i*16+0,"edi"));
422         &mov    ("ebx",&DWP($i*16+4,"edi"));
423         &mov    ("ecx",&DWP($i*16+8,"edi"));
424         &mov    ("edx",&DWP($i*16+12,"edi"));
425         &bswap  ("eax");
426         &bswap  ("ebx");
427         &bswap  ("ecx");
428         &bswap  ("edx");
429         &push   ("eax");
430         &push   ("ebx");
431         &push   ("ecx");
432         &push   ("edx");
433     }
434         &add    ("edi",128);
435         &sub    ("esp",9*8);            # place for T,A,B,C,D,E,F,G,H
436         &mov    (&DWP(8*(9+16)+4,"esp"),"edi");
437
438         # copy ctx->h[0-7] to A,B,C,D,E,F,G,H on stack
439         &lea    ("edi",&DWP(8,"esp"));
440         &mov    ("ecx",16);
441         &data_word(0xA5F3F689);         # rep movsd
442
443 &set_label("00_15_x86",16);
444         &BODY_00_15_x86();
445
446         &cmp    (&LB("edx"),0x94);
447         &jne    (&label("00_15_x86"));
448
449 &set_label("16_79_x86",16);
450         #define sigma0(x)       (ROTR((x),1)  ^ ROTR((x),8)  ^ ((x)>>7))
451         #       LO              lo>>1^hi<<31  ^ lo>>8^hi<<24 ^ lo>>7^hi<<25
452         #       HI              hi>>1^lo<<31  ^ hi>>8^lo<<24 ^ hi>>7
453         &mov    ("ecx",&DWP(8*(9+15+16-1)+0,"esp"));
454         &mov    ("edx",&DWP(8*(9+15+16-1)+4,"esp"));
455         &mov    ("esi","ecx");
456
457         &shr    ("ecx",1);      # lo>>1
458         &mov    ("edi","edx");
459         &shr    ("edx",1);      # hi>>1
460         &mov    ("eax","ecx");
461         &shl    ("esi",24);     # lo<<24
462         &mov    ("ebx","edx");
463         &shl    ("edi",24);     # hi<<24
464         &xor    ("ebx","esi");
465
466         &shr    ("ecx",7-1);    # lo>>7
467         &xor    ("eax","edi");
468         &shr    ("edx",7-1);    # hi>>7
469         &xor    ("eax","ecx");
470         &shl    ("esi",31-24);  # lo<<31
471         &xor    ("ebx","edx");
472         &shl    ("edi",25-24);  # hi<<25
473         &xor    ("ebx","esi");
474
475         &shr    ("ecx",8-7);    # lo>>8
476         &xor    ("eax","edi");
477         &shr    ("edx",8-7);    # hi>>8
478         &xor    ("eax","ecx");
479         &shl    ("edi",31-25);  # hi<<31
480         &xor    ("ebx","edx");
481         &xor    ("eax","edi");                  # T1 = sigma0(X[-15])
482
483         &mov    (&DWP(0,"esp"),"eax");
484         &mov    (&DWP(4,"esp"),"ebx");          # put T1 away
485
486         #define sigma1(x)       (ROTR((x),19) ^ ROTR((x),61) ^ ((x)>>6))
487         #       LO              lo>>19^hi<<13 ^ hi>>29^lo<<3 ^ lo>>6^hi<<26
488         #       HI              hi>>19^lo<<13 ^ lo>>29^hi<<3 ^ hi>>6
489         &mov    ("ecx",&DWP(8*(9+15+16-14)+0,"esp"));
490         &mov    ("edx",&DWP(8*(9+15+16-14)+4,"esp"));
491         &mov    ("esi","ecx");
492
493         &shr    ("ecx",6);      # lo>>6
494         &mov    ("edi","edx");
495         &shr    ("edx",6);      # hi>>6
496         &mov    ("eax","ecx");
497         &shl    ("esi",3);      # lo<<3
498         &mov    ("ebx","edx");
499         &shl    ("edi",3);      # hi<<3
500         &xor    ("eax","esi");
501
502         &shr    ("ecx",19-6);   # lo>>19
503         &xor    ("ebx","edi");
504         &shr    ("edx",19-6);   # hi>>19
505         &xor    ("eax","ecx");
506         &shl    ("esi",13-3);   # lo<<13
507         &xor    ("ebx","edx");
508         &shl    ("edi",13-3);   # hi<<13
509         &xor    ("ebx","esi");
510
511         &shr    ("ecx",29-19);  # lo>>29
512         &xor    ("eax","edi");
513         &shr    ("edx",29-19);  # hi>>29
514         &xor    ("ebx","ecx");
515         &shl    ("edi",26-13);  # hi<<26
516         &xor    ("eax","edx");
517         &xor    ("eax","edi");                  # sigma1(X[-2])
518
519         &mov    ("ecx",&DWP(8*(9+15+16)+0,"esp"));
520         &mov    ("edx",&DWP(8*(9+15+16)+4,"esp"));
521         &add    ("eax",&DWP(0,"esp"));
522         &adc    ("ebx",&DWP(4,"esp"));          # T1 = sigma1(X[-2])+T1
523         &mov    ("esi",&DWP(8*(9+15+16-9)+0,"esp"));
524         &mov    ("edi",&DWP(8*(9+15+16-9)+4,"esp"));
525         &add    ("eax","ecx");
526         &adc    ("ebx","edx");                  # T1 += X[-16]
527         &add    ("eax","esi");
528         &adc    ("ebx","edi");                  # T1 += X[-7]
529         &mov    (&DWP(8*(9+15)+0,"esp"),"eax");
530         &mov    (&DWP(8*(9+15)+4,"esp"),"ebx"); # save X[0]
531
532         &BODY_00_15_x86();
533
534         &cmp    (&LB("edx"),0x17);
535         &jne    (&label("16_79_x86"));
536
537         &mov    ("esi",&DWP(8*(9+16+80)+0,"esp"));# ctx
538         &mov    ("edi",&DWP(8*(9+16+80)+4,"esp"));# inp
539     for($i=0;$i<4;$i++) {
540         &mov    ("eax",&DWP($i*16+0,"esi"));
541         &mov    ("ebx",&DWP($i*16+4,"esi"));
542         &mov    ("ecx",&DWP($i*16+8,"esi"));
543         &mov    ("edx",&DWP($i*16+12,"esi"));
544         &add    ("eax",&DWP(8+($i*16)+0,"esp"));
545         &adc    ("ebx",&DWP(8+($i*16)+4,"esp"));
546         &mov    (&DWP($i*16+0,"esi"),"eax");
547         &mov    (&DWP($i*16+4,"esi"),"ebx");
548         &add    ("ecx",&DWP(8+($i*16)+8,"esp"));
549         &adc    ("edx",&DWP(8+($i*16)+12,"esp"));
550         &mov    (&DWP($i*16+8,"esi"),"ecx");
551         &mov    (&DWP($i*16+12,"esi"),"edx");
552     }
553         &add    ("esp",8*(9+16+80));            # destroy frame
554         &sub    ($K512,8*80);                   # rewind K
555
556         &cmp    ("edi",&DWP(8,"esp"));          # are we done yet?
557         &jb     (&label("loop_x86"));
558
559         &mov    ("esp",&DWP(12,"esp"));         # restore sp
560 &function_end_A();
561
562 &set_label("K512",64);  # Yes! I keep it in the code segment!
563         &data_word(0xd728ae22,0x428a2f98);      # u64
564         &data_word(0x23ef65cd,0x71374491);      # u64
565         &data_word(0xec4d3b2f,0xb5c0fbcf);      # u64
566         &data_word(0x8189dbbc,0xe9b5dba5);      # u64
567         &data_word(0xf348b538,0x3956c25b);      # u64
568         &data_word(0xb605d019,0x59f111f1);      # u64
569         &data_word(0xaf194f9b,0x923f82a4);      # u64
570         &data_word(0xda6d8118,0xab1c5ed5);      # u64
571         &data_word(0xa3030242,0xd807aa98);      # u64
572         &data_word(0x45706fbe,0x12835b01);      # u64
573         &data_word(0x4ee4b28c,0x243185be);      # u64
574         &data_word(0xd5ffb4e2,0x550c7dc3);      # u64
575         &data_word(0xf27b896f,0x72be5d74);      # u64
576         &data_word(0x3b1696b1,0x80deb1fe);      # u64
577         &data_word(0x25c71235,0x9bdc06a7);      # u64
578         &data_word(0xcf692694,0xc19bf174);      # u64
579         &data_word(0x9ef14ad2,0xe49b69c1);      # u64
580         &data_word(0x384f25e3,0xefbe4786);      # u64
581         &data_word(0x8b8cd5b5,0x0fc19dc6);      # u64
582         &data_word(0x77ac9c65,0x240ca1cc);      # u64
583         &data_word(0x592b0275,0x2de92c6f);      # u64
584         &data_word(0x6ea6e483,0x4a7484aa);      # u64
585         &data_word(0xbd41fbd4,0x5cb0a9dc);      # u64
586         &data_word(0x831153b5,0x76f988da);      # u64
587         &data_word(0xee66dfab,0x983e5152);      # u64
588         &data_word(0x2db43210,0xa831c66d);      # u64
589         &data_word(0x98fb213f,0xb00327c8);      # u64
590         &data_word(0xbeef0ee4,0xbf597fc7);      # u64
591         &data_word(0x3da88fc2,0xc6e00bf3);      # u64
592         &data_word(0x930aa725,0xd5a79147);      # u64
593         &data_word(0xe003826f,0x06ca6351);      # u64
594         &data_word(0x0a0e6e70,0x14292967);      # u64
595         &data_word(0x46d22ffc,0x27b70a85);      # u64
596         &data_word(0x5c26c926,0x2e1b2138);      # u64
597         &data_word(0x5ac42aed,0x4d2c6dfc);      # u64
598         &data_word(0x9d95b3df,0x53380d13);      # u64
599         &data_word(0x8baf63de,0x650a7354);      # u64
600         &data_word(0x3c77b2a8,0x766a0abb);      # u64
601         &data_word(0x47edaee6,0x81c2c92e);      # u64
602         &data_word(0x1482353b,0x92722c85);      # u64
603         &data_word(0x4cf10364,0xa2bfe8a1);      # u64
604         &data_word(0xbc423001,0xa81a664b);      # u64
605         &data_word(0xd0f89791,0xc24b8b70);      # u64
606         &data_word(0x0654be30,0xc76c51a3);      # u64
607         &data_word(0xd6ef5218,0xd192e819);      # u64
608         &data_word(0x5565a910,0xd6990624);      # u64
609         &data_word(0x5771202a,0xf40e3585);      # u64
610         &data_word(0x32bbd1b8,0x106aa070);      # u64
611         &data_word(0xb8d2d0c8,0x19a4c116);      # u64
612         &data_word(0x5141ab53,0x1e376c08);      # u64
613         &data_word(0xdf8eeb99,0x2748774c);      # u64
614         &data_word(0xe19b48a8,0x34b0bcb5);      # u64
615         &data_word(0xc5c95a63,0x391c0cb3);      # u64
616         &data_word(0xe3418acb,0x4ed8aa4a);      # u64
617         &data_word(0x7763e373,0x5b9cca4f);      # u64
618         &data_word(0xd6b2b8a3,0x682e6ff3);      # u64
619         &data_word(0x5defb2fc,0x748f82ee);      # u64
620         &data_word(0x43172f60,0x78a5636f);      # u64
621         &data_word(0xa1f0ab72,0x84c87814);      # u64
622         &data_word(0x1a6439ec,0x8cc70208);      # u64
623         &data_word(0x23631e28,0x90befffa);      # u64
624         &data_word(0xde82bde9,0xa4506ceb);      # u64
625         &data_word(0xb2c67915,0xbef9a3f7);      # u64
626         &data_word(0xe372532b,0xc67178f2);      # u64
627         &data_word(0xea26619c,0xca273ece);      # u64
628         &data_word(0x21c0c207,0xd186b8c7);      # u64
629         &data_word(0xcde0eb1e,0xeada7dd6);      # u64
630         &data_word(0xee6ed178,0xf57d4f7f);      # u64
631         &data_word(0x72176fba,0x06f067aa);      # u64
632         &data_word(0xa2c898a6,0x0a637dc5);      # u64
633         &data_word(0xbef90dae,0x113f9804);      # u64
634         &data_word(0x131c471b,0x1b710b35);      # u64
635         &data_word(0x23047d84,0x28db77f5);      # u64
636         &data_word(0x40c72493,0x32caab7b);      # u64
637         &data_word(0x15c9bebc,0x3c9ebe0a);      # u64
638         &data_word(0x9c100d4c,0x431d67c4);      # u64
639         &data_word(0xcb3e42b6,0x4cc5d4be);      # u64
640         &data_word(0xfc657e2a,0x597f299c);      # u64
641         &data_word(0x3ad6faec,0x5fcb6fab);      # u64
642         &data_word(0x4a475817,0x6c44198c);      # u64
643 &function_end_B("sha512_block_data_order");
644 &asciz("SHA512 block transform for x86, CRYPTOGAMS by <appro\@openssl.org>");
645
646 &asm_finish();