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