<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#! /usr/bin/env perl
# Copyright 2007-2018 The OpenSSL Project Authors. All Rights Reserved.
#
# Licensed under the OpenSSL license (the "License").  You may not use
# this file except in compliance with the License.  You can obtain a copy
# in the file LICENSE in the source distribution or at
# https://www.openssl.org/source/license.html

#
# ====================================================================
# Written by Andy Polyakov &lt;appro@openssl.org&gt; for the OpenSSL
# project. The module is, however, dual licensed under OpenSSL and
# CRYPTOGAMS licenses depending on where you obtain it. For further
# details see http://www.openssl.org/~appro/cryptogams/.
# ====================================================================
#
# SHA256 block transform for x86. September 2007.
#
# Performance improvement over compiler generated code varies from
# 10% to 40% [see below]. Not very impressive on some Âµ-archs, but
# it's 5 times smaller and optimizes amount of writes.
#
# May 2012.
#
# Optimization including two of Pavel Semjanov's ideas, alternative
# Maj and full unroll, resulted in ~20-25% improvement on most CPUs,
# ~7% on Pentium, ~40% on Atom. As fully unrolled loop body is almost
# 15x larger, 8KB vs. 560B, it's fired only for longer inputs. But not
# on P4, where it kills performance, nor Sandy Bridge, where folded
# loop is approximately as fast...
#
# June 2012.
#
# Add AMD XOP-specific code path, &gt;30% improvement on Bulldozer over
# May version, &gt;60% over original. Add AVX+shrd code path, &gt;25%
# improvement on Sandy Bridge over May version, 60% over original.
#
# May 2013.
#
# Replace AMD XOP code path with SSSE3 to cover more processors.
# (Biggest improvement coefficient is on upcoming Atom Silvermont,
# not shown.) Add AVX+BMI code path.
#
# March 2014.
#
# Add support for Intel SHA Extensions.
#
# Performance in clock cycles per processed byte (less is better):
#
#		gcc	icc	x86 asm(*)	SIMD	x86_64 asm(**)
# Pentium	46	57	40/38		-	-
# PIII		36	33	27/24		-	-
# P4		41	38	28		-	17.3
# AMD K8	27	25	19/15.5		-	14.9
# Core2		26	23	18/15.6		14.3	13.8
# Westmere	27	-	19/15.7		13.4	12.3
# Sandy Bridge	25	-	15.9		12.4	11.6
# Ivy Bridge	24	-	15.0		11.4	10.3
# Haswell	22	-	13.9		9.46	7.80
# Skylake	20	-	14.9		9.50	7.70
# Bulldozer	36	-	27/22		17.0	13.6
# VIA Nano	36	-	25/22		16.8	16.5
# Atom		50	-	30/25		21.9	18.9
# Silvermont	40	-	34/31		22.9	20.6
# Goldmont	29	-	20		16.3(***)
#
# (*)	numbers after slash are for unrolled loop, where applicable;
# (**)	x86_64 assembly performance is presented for reference
#	purposes, results are best-available;
# (***)	SHAEXT result is 4.1, strangely enough better than 64-bit one;

$0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
push(@INC,"${dir}","${dir}../../perlasm");
require "x86asm.pl";

$output=pop;
open STDOUT,"&gt;$output";

&amp;asm_init($ARGV[0],$ARGV[$#ARGV] eq "386");

$xmm=$avx=0;
for (@ARGV) { $xmm=1 if (/-DOPENSSL_IA32_SSE2/); }

if ($xmm &amp;&amp;	`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2&gt;&amp;1`
			=~ /GNU assembler version ([2-9]\.[0-9]+)/) {
	$avx = ($1&gt;=2.19) + ($1&gt;=2.22);
}

if ($xmm &amp;&amp; !$avx &amp;&amp; $ARGV[0] eq "win32n" &amp;&amp;
		`nasm -v 2&gt;&amp;1` =~ /NASM version ([2-9]\.[0-9]+)/) {
	$avx = ($1&gt;=2.03) + ($1&gt;=2.10);
}

if ($xmm &amp;&amp; !$avx &amp;&amp; $ARGV[0] eq "win32" &amp;&amp;
		`ml 2&gt;&amp;1` =~ /Version ([0-9]+)\./) {
	$avx = ($1&gt;=10) + ($1&gt;=11);
}

if ($xmm &amp;&amp; !$avx &amp;&amp; `$ENV{CC} -v 2&gt;&amp;1` =~ /((?:^clang|LLVM) version|based on LLVM) ([3-9]\.[0-9]+)/) {
	$avx = ($2&gt;=3.0) + ($2&gt;3.0);
}

$shaext=$xmm;	### set to zero if compiling for 1.0.1

$unroll_after = 64*4;	# If pre-evicted from L1P cache first spin of
			# fully unrolled loop was measured to run about
			# 3-4x slower. If slowdown coefficient is N and
			# unrolled loop is m times faster, then you break
			# even at (N-1)/(m-1) blocks. Then it needs to be
			# adjusted for probability of code being evicted,
			# code size/cache size=1/4. Typical m is 1.15...

$A="eax";
$E="edx";
$T="ebx";
$Aoff=&amp;DWP(4,"esp");
$Boff=&amp;DWP(8,"esp");
$Coff=&amp;DWP(12,"esp");
$Doff=&amp;DWP(16,"esp");
$Eoff=&amp;DWP(20,"esp");
$Foff=&amp;DWP(24,"esp");
$Goff=&amp;DWP(28,"esp");
$Hoff=&amp;DWP(32,"esp");
$Xoff=&amp;DWP(36,"esp");
$K256="ebp";

sub BODY_16_63() {
	&amp;mov	($T,"ecx");			# "ecx" is preloaded
	 &amp;mov	("esi",&amp;DWP(4*(9+15+16-14),"esp"));
	&amp;ror	("ecx",18-7);
	 &amp;mov	("edi","esi");
	&amp;ror	("esi",19-17);
	 &amp;xor	("ecx",$T);
	 &amp;shr	($T,3);
	&amp;ror	("ecx",7);
	 &amp;xor	("esi","edi");
	 &amp;xor	($T,"ecx");			# T = sigma0(X[-15])
	&amp;ror	("esi",17);
	 &amp;add	($T,&amp;DWP(4*(9+15+16),"esp"));	# T += X[-16]
	&amp;shr	("edi",10);
	 &amp;add	($T,&amp;DWP(4*(9+15+16-9),"esp"));	# T += X[-7]
	#&amp;xor	("edi","esi")			# sigma1(X[-2])
	# &amp;add	($T,"edi");			# T += sigma1(X[-2])
	# &amp;mov	(&amp;DWP(4*(9+15),"esp"),$T);	# save X[0]

	&amp;BODY_00_15(1);
}
sub BODY_00_15() {
    my $in_16_63=shift;

	&amp;mov	("ecx",$E);
	 &amp;xor	("edi","esi")			if ($in_16_63);	# sigma1(X[-2])
	 &amp;mov	("esi",$Foff);
	&amp;ror	("ecx",25-11);
	 &amp;add	($T,"edi")			if ($in_16_63);	# T += sigma1(X[-2])
	 &amp;mov	("edi",$Goff);
	&amp;xor	("ecx",$E);
	 &amp;xor	("esi","edi");
	 &amp;mov	($T,&amp;DWP(4*(9+15),"esp"))	if (!$in_16_63);
	 &amp;mov	(&amp;DWP(4*(9+15),"esp"),$T)	if ($in_16_63);	# save X[0]
	&amp;ror	("ecx",11-6);
	 &amp;and	("esi",$E);
	 &amp;mov	($Eoff,$E);		# modulo-scheduled
	&amp;xor	($E,"ecx");
	 &amp;add	($T,$Hoff);		# T += h
	 &amp;xor	("esi","edi");		# Ch(e,f,g)
	&amp;ror	($E,6);			# Sigma1(e)
	 &amp;mov	("ecx",$A);
	 &amp;add	($T,"esi");		# T += Ch(e,f,g)

	&amp;ror	("ecx",22-13);
	 &amp;add	($T,$E);		# T += Sigma1(e)
	 &amp;mov	("edi",$Boff);
	&amp;xor	("ecx",$A);
	 &amp;mov	($Aoff,$A);		# modulo-scheduled
	 &amp;lea	("esp",&amp;DWP(-4,"esp"));
	&amp;ror	("ecx",13-2);
	 &amp;mov	("esi",&amp;DWP(0,$K256));
	&amp;xor	("ecx",$A);
	 &amp;mov	($E,$Eoff);		# e in next iteration, d in this one
	 &amp;xor	($A,"edi");		# a ^= b
	&amp;ror	("ecx",2);		# Sigma0(a)

	 &amp;add	($T,"esi");		# T+= K[i]
	 &amp;mov	(&amp;DWP(0,"esp"),$A);	# (b^c) in next round
	&amp;add	($E,$T);		# d += T
	 &amp;and	($A,&amp;DWP(4,"esp"));	# a &amp;= (b^c)
	&amp;add	($T,"ecx");		# T += Sigma0(a)
	 &amp;xor	($A,"edi");		# h = Maj(a,b,c) = Ch(a^b,c,b)
	 &amp;mov	("ecx",&amp;DWP(4*(9+15+16-1),"esp"))	if ($in_16_63);	# preload T
	&amp;add	($K256,4);
	 &amp;add	($A,$T);		# h += T
}

&amp;external_label("OPENSSL_ia32cap_P")		if (!$i386);

&amp;function_begin("sha256_block_data_order");
	&amp;mov	("esi",wparam(0));	# ctx
	&amp;mov	("edi",wparam(1));	# inp
	&amp;mov	("eax",wparam(2));	# num
	&amp;mov	("ebx","esp");		# saved sp

	&amp;call	(&amp;label("pic_point"));	# make it PIC!
&amp;set_label("pic_point");
	&amp;blindpop($K256);
	&amp;lea	($K256,&amp;DWP(&amp;label("K256")."-".&amp;label("pic_point"),$K256));

	&amp;sub	("esp",16);
	&amp;and	("esp",-64);

	&amp;shl	("eax",6);
	&amp;add	("eax","edi");
	&amp;mov	(&amp;DWP(0,"esp"),"esi");	# ctx
	&amp;mov	(&amp;DWP(4,"esp"),"edi");	# inp
	&amp;mov	(&amp;DWP(8,"esp"),"eax");	# inp+num*128
	&amp;mov	(&amp;DWP(12,"esp"),"ebx");	# saved sp
						if (!$i386 &amp;&amp; $xmm) {
	&amp;picmeup("edx","OPENSSL_ia32cap_P",$K256,&amp;label("K256"));
	&amp;mov	("ecx",&amp;DWP(0,"edx"));
	&amp;mov	("ebx",&amp;DWP(4,"edx"));
	&amp;test	("ecx",1&lt;&lt;20);		# check for P4
	&amp;jnz	(&amp;label("loop"));
	&amp;mov	("edx",&amp;DWP(8,"edx"))	if ($xmm);
	&amp;test	("ecx",1&lt;&lt;24);		# check for FXSR
	&amp;jz	($unroll_after?&amp;label("no_xmm"):&amp;label("loop"));
	&amp;and	("ecx",1&lt;&lt;30);		# mask "Intel CPU" bit
	&amp;and	("ebx",1&lt;&lt;28|1&lt;&lt;9);	# mask AVX and SSSE3 bits
	&amp;test	("edx",1&lt;&lt;29)		if ($shaext);	# check for SHA
	&amp;jnz	(&amp;label("shaext"))	if ($shaext);
	&amp;or	("ecx","ebx");
	&amp;and	("ecx",1&lt;&lt;28|1&lt;&lt;30);
	&amp;cmp	("ecx",1&lt;&lt;28|1&lt;&lt;30);
					if ($xmm) {
	&amp;je	(&amp;label("AVX"))		if ($avx);
	&amp;test	("ebx",1&lt;&lt;9);		# check for SSSE3
	&amp;jnz	(&amp;label("SSSE3"));
					} else {
	&amp;je	(&amp;label("loop_shrd"));
					}
						if ($unroll_after) {
&amp;set_label("no_xmm");
	&amp;sub	("eax","edi");
	&amp;cmp	("eax",$unroll_after);
	&amp;jae	(&amp;label("unrolled"));
						} }
	&amp;jmp	(&amp;label("loop"));

sub COMPACT_LOOP() {
my $suffix=shift;

&amp;set_label("loop$suffix",$suffix?32:16);
    # copy input block to stack reversing byte and dword order
    for($i=0;$i&lt;4;$i++) {
	&amp;mov	("eax",&amp;DWP($i*16+0,"edi"));
	&amp;mov	("ebx",&amp;DWP($i*16+4,"edi"));
	&amp;mov	("ecx",&amp;DWP($i*16+8,"edi"));
	&amp;bswap	("eax");
	&amp;mov	("edx",&amp;DWP($i*16+12,"edi"));
	&amp;bswap	("ebx");
	&amp;push	("eax");
	&amp;bswap	("ecx");
	&amp;push	("ebx");
	&amp;bswap	("edx");
	&amp;push	("ecx");
	&amp;push	("edx");
    }
	&amp;add	("edi",64);
	&amp;lea	("esp",&amp;DWP(-4*9,"esp"));# place for A,B,C,D,E,F,G,H
	&amp;mov	(&amp;DWP(4*(9+16)+4,"esp"),"edi");

	# copy ctx-&gt;h[0-7] to A,B,C,D,E,F,G,H on stack
	&amp;mov	($A,&amp;DWP(0,"esi"));
	&amp;mov	("ebx",&amp;DWP(4,"esi"));
	&amp;mov	("ecx",&amp;DWP(8,"esi"));
	&amp;mov	("edi",&amp;DWP(12,"esi"));
	# &amp;mov	($Aoff,$A);
	&amp;mov	($Boff,"ebx");
	&amp;xor	("ebx","ecx");
	&amp;mov	($Coff,"ecx");
	&amp;mov	($Doff,"edi");
	&amp;mov	(&amp;DWP(0,"esp"),"ebx");	# magic
	&amp;mov	($E,&amp;DWP(16,"esi"));
	&amp;mov	("ebx",&amp;DWP(20,"esi"));
	&amp;mov	("ecx",&amp;DWP(24,"esi"));
	&amp;mov	("edi",&amp;DWP(28,"esi"));
	# &amp;mov	($Eoff,$E);
	&amp;mov	($Foff,"ebx");
	&amp;mov	($Goff,"ecx");
	&amp;mov	($Hoff,"edi");

&amp;set_label("00_15$suffix",16);

	&amp;BODY_00_15();

	&amp;cmp	("esi",0xc19bf174);
	&amp;jne	(&amp;label("00_15$suffix"));

	&amp;mov	("ecx",&amp;DWP(4*(9+15+16-1),"esp"));	# preloaded in BODY_00_15(1)
	&amp;jmp	(&amp;label("16_63$suffix"));

&amp;set_label("16_63$suffix",16);

	&amp;BODY_16_63();

	&amp;cmp	("esi",0xc67178f2);
	&amp;jne	(&amp;label("16_63$suffix"));

	&amp;mov	("esi",&amp;DWP(4*(9+16+64)+0,"esp"));#ctx
	# &amp;mov	($A,$Aoff);
	&amp;mov	("ebx",$Boff);
	# &amp;mov	("edi",$Coff);
	&amp;mov	("ecx",$Doff);
	&amp;add	($A,&amp;DWP(0,"esi"));
	&amp;add	("ebx",&amp;DWP(4,"esi"));
	&amp;add	("edi",&amp;DWP(8,"esi"));
	&amp;add	("ecx",&amp;DWP(12,"esi"));
	&amp;mov	(&amp;DWP(0,"esi"),$A);
	&amp;mov	(&amp;DWP(4,"esi"),"ebx");
	&amp;mov	(&amp;DWP(8,"esi"),"edi");
	&amp;mov	(&amp;DWP(12,"esi"),"ecx");
	# &amp;mov	($E,$Eoff);
	&amp;mov	("eax",$Foff);
	&amp;mov	("ebx",$Goff);
	&amp;mov	("ecx",$Hoff);
	&amp;mov	("edi",&amp;DWP(4*(9+16+64)+4,"esp"));#inp
	&amp;add	($E,&amp;DWP(16,"esi"));
	&amp;add	("eax",&amp;DWP(20,"esi"));
	&amp;add	("ebx",&amp;DWP(24,"esi"));
	&amp;add	("ecx",&amp;DWP(28,"esi"));
	&amp;mov	(&amp;DWP(16,"esi"),$E);
	&amp;mov	(&amp;DWP(20,"esi"),"eax");
	&amp;mov	(&amp;DWP(24,"esi"),"ebx");
	&amp;mov	(&amp;DWP(28,"esi"),"ecx");

	&amp;lea	("esp",&amp;DWP(4*(9+16+64),"esp"));# destroy frame
	&amp;sub	($K256,4*64);			# rewind K

	&amp;cmp	("edi",&amp;DWP(8,"esp"));		# are we done yet?
	&amp;jb	(&amp;label("loop$suffix"));
}
	&amp;COMPACT_LOOP();
	&amp;mov	("esp",&amp;DWP(12,"esp"));		# restore sp
&amp;function_end_A();
						if (!$i386 &amp;&amp; !$xmm) {
	# ~20% improvement on Sandy Bridge
	local *ror = sub { &amp;shrd(@_[0],@_) };
	&amp;COMPACT_LOOP("_shrd");
	&amp;mov	("esp",&amp;DWP(12,"esp"));		# restore sp
&amp;function_end_A();
						}

&amp;set_label("K256",64);	# Yes! I keep it in the code segment!
@K256=(	0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,
	0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
	0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,
	0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
	0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,
	0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,
	0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,
	0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,
	0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,
	0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,
	0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,
	0xd192e819,0xd6990624,0xf40e3585,0x106aa070,
	0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,
	0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
	0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,
	0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2	);
&amp;data_word(@K256);
&amp;data_word(0x00010203,0x04050607,0x08090a0b,0x0c0d0e0f);	# byte swap mask
&amp;asciz("SHA256 block transform for x86, CRYPTOGAMS by &lt;appro\@openssl.org&gt;");

($a,$b,$c,$d,$e,$f,$g,$h)=(0..7);	# offsets
sub off { &amp;DWP(4*(((shift)-$i)&amp;7),"esp"); }

if (!$i386 &amp;&amp; $unroll_after) {
my @AH=($A,$K256);

&amp;set_label("unrolled",16);
	&amp;lea	("esp",&amp;DWP(-96,"esp"));
	# copy ctx-&gt;h[0-7] to A,B,C,D,E,F,G,H on stack
	&amp;mov	($AH[0],&amp;DWP(0,"esi"));
	&amp;mov	($AH[1],&amp;DWP(4,"esi"));
	&amp;mov	("ecx",&amp;DWP(8,"esi"));
	&amp;mov	("ebx",&amp;DWP(12,"esi"));
	#&amp;mov	(&amp;DWP(0,"esp"),$AH[0]);
	&amp;mov	(&amp;DWP(4,"esp"),$AH[1]);
	&amp;xor	($AH[1],"ecx");		# magic
	&amp;mov	(&amp;DWP(8,"esp"),"ecx");
	&amp;mov	(&amp;DWP(12,"esp"),"ebx");
	&amp;mov	($E,&amp;DWP(16,"esi"));
	&amp;mov	("ebx",&amp;DWP(20,"esi"));
	&amp;mov	("ecx",&amp;DWP(24,"esi"));
	&amp;mov	("esi",&amp;DWP(28,"esi"));
	#&amp;mov	(&amp;DWP(16,"esp"),$E);
	&amp;mov	(&amp;DWP(20,"esp"),"ebx");
	&amp;mov	(&amp;DWP(24,"esp"),"ecx");
	&amp;mov	(&amp;DWP(28,"esp"),"esi");
	&amp;jmp	(&amp;label("grand_loop"));

&amp;set_label("grand_loop",16);
    # copy input block to stack reversing byte order
    for($i=0;$i&lt;5;$i++) {
	&amp;mov	("ebx",&amp;DWP(12*$i+0,"edi"));
	&amp;mov	("ecx",&amp;DWP(12*$i+4,"edi"));
	&amp;bswap	("ebx");
	&amp;mov	("esi",&amp;DWP(12*$i+8,"edi"));
	&amp;bswap	("ecx");
	&amp;mov	(&amp;DWP(32+12*$i+0,"esp"),"ebx");
	&amp;bswap	("esi");
	&amp;mov	(&amp;DWP(32+12*$i+4,"esp"),"ecx");
	&amp;mov	(&amp;DWP(32+12*$i+8,"esp"),"esi");
    }
	&amp;mov	("ebx",&amp;DWP($i*12,"edi"));
	&amp;add	("edi",64);
	&amp;bswap	("ebx");
	&amp;mov	(&amp;DWP(96+4,"esp"),"edi");
	&amp;mov	(&amp;DWP(32+12*$i,"esp"),"ebx");

    my ($t1,$t2) = ("ecx","esi");

    for ($i=0;$i&lt;64;$i++) {

      if ($i&gt;=16) {
	&amp;mov	($T,$t1);			# $t1 is preloaded
	# &amp;mov	($t2,&amp;DWP(32+4*(($i+14)&amp;15),"esp"));
	&amp;ror	($t1,18-7);
	 &amp;mov	("edi",$t2);
	&amp;ror	($t2,19-17);
	 &amp;xor	($t1,$T);
	 &amp;shr	($T,3);
	&amp;ror	($t1,7);
	 &amp;xor	($t2,"edi");
	 &amp;xor	($T,$t1);			# T = sigma0(X[-15])
	&amp;ror	($t2,17);
	 &amp;add	($T,&amp;DWP(32+4*($i&amp;15),"esp"));	# T += X[-16]
	&amp;shr	("edi",10);
	 &amp;add	($T,&amp;DWP(32+4*(($i+9)&amp;15),"esp"));	# T += X[-7]
	#&amp;xor	("edi",$t2)			# sigma1(X[-2])
	# &amp;add	($T,"edi");			# T += sigma1(X[-2])
	# &amp;mov	(&amp;DWP(4*(9+15),"esp"),$T);	# save X[0]
      }
	&amp;mov	($t1,$E);
	 &amp;xor	("edi",$t2)			if ($i&gt;=16);	# sigma1(X[-2])
	 &amp;mov	($t2,&amp;off($f));
	&amp;ror	($E,25-11);
	 &amp;add	($T,"edi")			if ($i&gt;=16);	# T += sigma1(X[-2])
	 &amp;mov	("edi",&amp;off($g));
	&amp;xor	($E,$t1);
	 &amp;mov	($T,&amp;DWP(32+4*($i&amp;15),"esp"))	if ($i&lt;16);	# X[i]
	 &amp;mov	(&amp;DWP(32+4*($i&amp;15),"esp"),$T)	if ($i&gt;=16 &amp;&amp; $i&lt;62);	# save X[0]
	 &amp;xor	($t2,"edi");
	&amp;ror	($E,11-6);
	 &amp;and	($t2,$t1);
	 &amp;mov	(&amp;off($e),$t1);		# save $E, modulo-scheduled
	&amp;xor	($E,$t1);
	 &amp;add	($T,&amp;off($h));		# T += h
	 &amp;xor	("edi",$t2);		# Ch(e,f,g)
	&amp;ror	($E,6);			# Sigma1(e)
	 &amp;mov	($t1,$AH[0]);
	 &amp;add	($T,"edi");		# T += Ch(e,f,g)

	&amp;ror	($t1,22-13);
	 &amp;mov	($t2,$AH[0]);
	 &amp;mov	("edi",&amp;off($b));
	&amp;xor	($t1,$AH[0]);
	 &amp;mov	(&amp;off($a),$AH[0]);	# save $A, modulo-scheduled
	 &amp;xor	($AH[0],"edi");		# a ^= b, (b^c) in next round
	&amp;ror	($t1,13-2);
	 &amp;and	($AH[1],$AH[0]);	# (b^c) &amp;= (a^b)
	 &amp;lea	($E,&amp;DWP(@K256[$i],$T,$E));	# T += Sigma1(1)+K[i]
	&amp;xor	($t1,$t2);
	 &amp;xor	($AH[1],"edi");		# h = Maj(a,b,c) = Ch(a^b,c,b)
	 &amp;mov	($t2,&amp;DWP(32+4*(($i+2)&amp;15),"esp"))	if ($i&gt;=15 &amp;&amp; $i&lt;63);
	&amp;ror	($t1,2);		# Sigma0(a)

	 &amp;add	($AH[1],$E);		# h += T
	 &amp;add	($E,&amp;off($d));		# d += T
	&amp;add	($AH[1],$t1);		# h += Sigma0(a)
	 &amp;mov	($t1,&amp;DWP(32+4*(($i+15)&amp;15),"esp"))	if ($i&gt;=15 &amp;&amp; $i&lt;63);

	@AH = reverse(@AH);		# rotate(a,h)
	($t1,$t2) = ($t2,$t1);		# rotate(t1,t2)
    }
	&amp;mov	("esi",&amp;DWP(96,"esp"));	#ctx
					#&amp;mov	($AH[0],&amp;DWP(0,"esp"));
	&amp;xor	($AH[1],"edi");		#&amp;mov	($AH[1],&amp;DWP(4,"esp"));
					#&amp;mov	("edi", &amp;DWP(8,"esp"));
	&amp;mov	("ecx",&amp;DWP(12,"esp"));
	&amp;add	($AH[0],&amp;DWP(0,"esi"));
	&amp;add	($AH[1],&amp;DWP(4,"esi"));
	&amp;add	("edi",&amp;DWP(8,"esi"));
	&amp;add	("ecx",&amp;DWP(12,"esi"));
	&amp;mov	(&amp;DWP(0,"esi"),$AH[0]);
	&amp;mov	(&amp;DWP(4,"esi"),$AH[1]);
	&amp;mov	(&amp;DWP(8,"esi"),"edi");
	&amp;mov	(&amp;DWP(12,"esi"),"ecx");
	 #&amp;mov	(&amp;DWP(0,"esp"),$AH[0]);
	 &amp;mov	(&amp;DWP(4,"esp"),$AH[1]);
	 &amp;xor	($AH[1],"edi");		# magic
	 &amp;mov	(&amp;DWP(8,"esp"),"edi");
	 &amp;mov	(&amp;DWP(12,"esp"),"ecx");
	#&amp;mov	($E,&amp;DWP(16,"esp"));
	&amp;mov	("edi",&amp;DWP(20,"esp"));
	&amp;mov	("ebx",&amp;DWP(24,"esp"));
	&amp;mov	("ecx",&amp;DWP(28,"esp"));
	&amp;add	($E,&amp;DWP(16,"esi"));
	&amp;add	("edi",&amp;DWP(20,"esi"));
	&amp;add	("ebx",&amp;DWP(24,"esi"));
	&amp;add	("ecx",&amp;DWP(28,"esi"));
	&amp;mov	(&amp;DWP(16,"esi"),$E);
	&amp;mov	(&amp;DWP(20,"esi"),"edi");
	&amp;mov	(&amp;DWP(24,"esi"),"ebx");
	&amp;mov	(&amp;DWP(28,"esi"),"ecx");
	 #&amp;mov	(&amp;DWP(16,"esp"),$E);
	 &amp;mov	(&amp;DWP(20,"esp"),"edi");
	&amp;mov	("edi",&amp;DWP(96+4,"esp"));	# inp
	 &amp;mov	(&amp;DWP(24,"esp"),"ebx");
	 &amp;mov	(&amp;DWP(28,"esp"),"ecx");

	&amp;cmp	("edi",&amp;DWP(96+8,"esp"));	# are we done yet?
	&amp;jb	(&amp;label("grand_loop"));

	&amp;mov	("esp",&amp;DWP(96+12,"esp"));	# restore sp
&amp;function_end_A();
}
						if (!$i386 &amp;&amp; $xmm) {{{
if ($shaext) {
######################################################################
# Intel SHA Extensions implementation of SHA256 update function.
#
my ($ctx,$inp,$end)=("esi","edi","eax");
my ($Wi,$ABEF,$CDGH,$TMP)=map("xmm$_",(0..2,7));
my @MSG=map("xmm$_",(3..6));

sub sha256op38 {
 my ($opcodelet,$dst,$src)=@_;
    if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/)
    {	&amp;data_byte(0x0f,0x38,$opcodelet,0xc0|($1&lt;&lt;3)|$2);	}
}
sub sha256rnds2	{ sha256op38(0xcb,@_); }
sub sha256msg1	{ sha256op38(0xcc,@_); }
sub sha256msg2	{ sha256op38(0xcd,@_); }

&amp;set_label("shaext",32);
	&amp;sub		("esp",32);

	&amp;movdqu		($ABEF,&amp;QWP(0,$ctx));		# DCBA
	&amp;lea		($K256,&amp;DWP(0x80,$K256));
	&amp;movdqu		($CDGH,&amp;QWP(16,$ctx));		# HGFE
	&amp;movdqa		($TMP,&amp;QWP(0x100-0x80,$K256));	# byte swap mask

	&amp;pshufd		($Wi,$ABEF,0x1b);		# ABCD
	&amp;pshufd		($ABEF,$ABEF,0xb1);		# CDAB
	&amp;pshufd		($CDGH,$CDGH,0x1b);		# EFGH
	&amp;palignr	($ABEF,$CDGH,8);		# ABEF
	&amp;punpcklqdq	($CDGH,$Wi);			# CDGH
	&amp;jmp		(&amp;label("loop_shaext"));

&amp;set_label("loop_shaext",16);
	&amp;movdqu		(@MSG[0],&amp;QWP(0,$inp));
	&amp;movdqu		(@MSG[1],&amp;QWP(0x10,$inp));
	&amp;movdqu		(@MSG[2],&amp;QWP(0x20,$inp));
	&amp;pshufb		(@MSG[0],$TMP);
	&amp;movdqu		(@MSG[3],&amp;QWP(0x30,$inp));
	&amp;movdqa		(&amp;QWP(16,"esp"),$CDGH);		# offload

	&amp;movdqa		($Wi,&amp;QWP(0*16-0x80,$K256));
	&amp;paddd		($Wi,@MSG[0]);
	&amp;pshufb		(@MSG[1],$TMP);
	&amp;sha256rnds2	($CDGH,$ABEF);			# 0-3
	&amp;pshufd		($Wi,$Wi,0x0e);
	&amp;nop		();
	&amp;movdqa		(&amp;QWP(0,"esp"),$ABEF);		# offload
	&amp;sha256rnds2	($ABEF,$CDGH);

	&amp;movdqa		($Wi,&amp;QWP(1*16-0x80,$K256));
	&amp;paddd		($Wi,@MSG[1]);
	&amp;pshufb		(@MSG[2],$TMP);
	&amp;sha256rnds2	($CDGH,$ABEF);			# 4-7
	&amp;pshufd		($Wi,$Wi,0x0e);
	&amp;lea		($inp,&amp;DWP(0x40,$inp));
	&amp;sha256msg1	(@MSG[0],@MSG[1]);
	&amp;sha256rnds2	($ABEF,$CDGH);

	&amp;movdqa		($Wi,&amp;QWP(2*16-0x80,$K256));
	&amp;paddd		($Wi,@MSG[2]);
	&amp;pshufb		(@MSG[3],$TMP);
	&amp;sha256rnds2	($CDGH,$ABEF);			# 8-11
	&amp;pshufd		($Wi,$Wi,0x0e);
	&amp;movdqa		($TMP,@MSG[3]);
	&amp;palignr	($TMP,@MSG[2],4);
	&amp;nop		();
	&amp;paddd		(@MSG[0],$TMP);
	&amp;sha256msg1	(@MSG[1],@MSG[2]);
	&amp;sha256rnds2	($ABEF,$CDGH);

	&amp;movdqa		($Wi,&amp;QWP(3*16-0x80,$K256));
	&amp;paddd		($Wi,@MSG[3]);
	&amp;sha256msg2	(@MSG[0],@MSG[3]);
	&amp;sha256rnds2	($CDGH,$ABEF);			# 12-15
	&amp;pshufd		($Wi,$Wi,0x0e);
	&amp;movdqa		($TMP,@MSG[0]);
	&amp;palignr	($TMP,@MSG[3],4);
	&amp;nop		();
	&amp;paddd		(@MSG[1],$TMP);
	&amp;sha256msg1	(@MSG[2],@MSG[3]);
	&amp;sha256rnds2	($ABEF,$CDGH);

for($i=4;$i&lt;16-3;$i++) {
	&amp;movdqa		($Wi,&amp;QWP($i*16-0x80,$K256));
	&amp;paddd		($Wi,@MSG[0]);
	&amp;sha256msg2	(@MSG[1],@MSG[0]);
	&amp;sha256rnds2	($CDGH,$ABEF);			# 16-19...
	&amp;pshufd		($Wi,$Wi,0x0e);
	&amp;movdqa		($TMP,@MSG[1]);
	&amp;palignr	($TMP,@MSG[0],4);
	&amp;nop		();
	&amp;paddd		(@MSG[2],$TMP);
	&amp;sha256msg1	(@MSG[3],@MSG[0]);
	&amp;sha256rnds2	($ABEF,$CDGH);

	push(@MSG,shift(@MSG));
}
	&amp;movdqa		($Wi,&amp;QWP(13*16-0x80,$K256));
	&amp;paddd		($Wi,@MSG[0]);
	&amp;sha256msg2	(@MSG[1],@MSG[0]);
	&amp;sha256rnds2	($CDGH,$ABEF);			# 52-55
	&amp;pshufd		($Wi,$Wi,0x0e);
	&amp;movdqa		($TMP,@MSG[1])
	&amp;palignr	($TMP,@MSG[0],4);
	&amp;sha256rnds2	($ABEF,$CDGH);
	&amp;paddd		(@MSG[2],$TMP);

	&amp;movdqa		($Wi,&amp;QWP(14*16-0x80,$K256));
	&amp;paddd		($Wi,@MSG[1]);
	&amp;sha256rnds2	($CDGH,$ABEF);			# 56-59
	&amp;pshufd		($Wi,$Wi,0x0e);
	&amp;sha256msg2	(@MSG[2],@MSG[1]);
	&amp;movdqa		($TMP,&amp;QWP(0x100-0x80,$K256));	# byte swap mask
	&amp;sha256rnds2	($ABEF,$CDGH);

	&amp;movdqa		($Wi,&amp;QWP(15*16-0x80,$K256));
	&amp;paddd		($Wi,@MSG[2]);
	&amp;nop		();
	&amp;sha256rnds2	($CDGH,$ABEF);			# 60-63
	&amp;pshufd		($Wi,$Wi,0x0e);
	&amp;cmp		($end,$inp);
	&amp;nop		();
	&amp;sha256rnds2	($ABEF,$CDGH);

	&amp;paddd		($CDGH,&amp;QWP(16,"esp"));
	&amp;paddd		($ABEF,&amp;QWP(0,"esp"));
	&amp;jnz		(&amp;label("loop_shaext"));

	&amp;pshufd		($CDGH,$CDGH,0xb1);		# DCHG
	&amp;pshufd		($TMP,$ABEF,0x1b);		# FEBA
	&amp;pshufd		($ABEF,$ABEF,0xb1);		# BAFE
	&amp;punpckhqdq	($ABEF,$CDGH);			# DCBA
	&amp;palignr	($CDGH,$TMP,8);			# HGFE

	&amp;mov		("esp",&amp;DWP(32+12,"esp"));
	&amp;movdqu		(&amp;QWP(0,$ctx),$ABEF);
	&amp;movdqu		(&amp;QWP(16,$ctx),$CDGH);
&amp;function_end_A();
}

my @X = map("xmm$_",(0..3));
my ($t0,$t1,$t2,$t3) = map("xmm$_",(4..7));
my @AH = ($A,$T);

&amp;set_label("SSSE3",32);
	&amp;lea	("esp",&amp;DWP(-96,"esp"));
	# copy ctx-&gt;h[0-7] to A,B,C,D,E,F,G,H on stack
	&amp;mov	($AH[0],&amp;DWP(0,"esi"));
	&amp;mov	($AH[1],&amp;DWP(4,"esi"));
	&amp;mov	("ecx",&amp;DWP(8,"esi"));
	&amp;mov	("edi",&amp;DWP(12,"esi"));
	#&amp;mov	(&amp;DWP(0,"esp"),$AH[0]);
	&amp;mov	(&amp;DWP(4,"esp"),$AH[1]);
	&amp;xor	($AH[1],"ecx");			# magic
	&amp;mov	(&amp;DWP(8,"esp"),"ecx");
	&amp;mov	(&amp;DWP(12,"esp"),"edi");
	&amp;mov	($E,&amp;DWP(16,"esi"));
	&amp;mov	("edi",&amp;DWP(20,"esi"));
	&amp;mov	("ecx",&amp;DWP(24,"esi"));
	&amp;mov	("esi",&amp;DWP(28,"esi"));
	#&amp;mov	(&amp;DWP(16,"esp"),$E);
	&amp;mov	(&amp;DWP(20,"esp"),"edi");
	&amp;mov	("edi",&amp;DWP(96+4,"esp"));	# inp
	&amp;mov	(&amp;DWP(24,"esp"),"ecx");
	&amp;mov	(&amp;DWP(28,"esp"),"esi");
	&amp;movdqa	($t3,&amp;QWP(256,$K256));
	&amp;jmp	(&amp;label("grand_ssse3"));

&amp;set_label("grand_ssse3",16);
	# load input, reverse byte order, add K256[0..15], save to stack
	&amp;movdqu	(@X[0],&amp;QWP(0,"edi"));
	&amp;movdqu	(@X[1],&amp;QWP(16,"edi"));
	&amp;movdqu	(@X[2],&amp;QWP(32,"edi"));
	&amp;movdqu	(@X[3],&amp;QWP(48,"edi"));
	&amp;add	("edi",64);
	&amp;pshufb	(@X[0],$t3);
	&amp;mov	(&amp;DWP(96+4,"esp"),"edi");
	&amp;pshufb	(@X[1],$t3);
	&amp;movdqa	($t0,&amp;QWP(0,$K256));
	&amp;pshufb	(@X[2],$t3);
	&amp;movdqa	($t1,&amp;QWP(16,$K256));
	&amp;paddd	($t0,@X[0]);
	&amp;pshufb	(@X[3],$t3);
	&amp;movdqa	($t2,&amp;QWP(32,$K256));
	&amp;paddd	($t1,@X[1]);
	&amp;movdqa	($t3,&amp;QWP(48,$K256));
	&amp;movdqa	(&amp;QWP(32+0,"esp"),$t0);
	&amp;paddd	($t2,@X[2]);
	&amp;movdqa	(&amp;QWP(32+16,"esp"),$t1);
	&amp;paddd	($t3,@X[3]);
	&amp;movdqa	(&amp;QWP(32+32,"esp"),$t2);
	&amp;movdqa	(&amp;QWP(32+48,"esp"),$t3);
	&amp;jmp	(&amp;label("ssse3_00_47"));

&amp;set_label("ssse3_00_47",16);
	&amp;add		($K256,64);

sub SSSE3_00_47 () {
my $j = shift;
my $body = shift;
my @X = @_;
my @insns = (&amp;$body,&amp;$body,&amp;$body,&amp;$body);	# 120 instructions

	  eval(shift(@insns));
	&amp;movdqa		($t0,@X[1]);
	  eval(shift(@insns));			# @
	  eval(shift(@insns));
	&amp;movdqa		($t3,@X[3]);
	  eval(shift(@insns));
	  eval(shift(@insns));
	&amp;palignr	($t0,@X[0],4);		# X[1..4]
	  eval(shift(@insns));
	  eval(shift(@insns));			# @
	  eval(shift(@insns));
	 &amp;palignr	($t3,@X[2],4);		# X[9..12]
	  eval(shift(@insns));
	  eval(shift(@insns));
	  eval(shift(@insns));
	&amp;movdqa		($t1,$t0);
	  eval(shift(@insns));			# @
	  eval(shift(@insns));
	&amp;movdqa		($t2,$t0);
	  eval(shift(@insns));
	  eval(shift(@insns));
	&amp;psrld		($t0,3);
	  eval(shift(@insns));
	  eval(shift(@insns));			# @
	 &amp;paddd		(@X[0],$t3);		# X[0..3] += X[9..12]
	  eval(shift(@insns));
	  eval(shift(@insns));
	&amp;psrld		($t2,7);
	  eval(shift(@insns));
	  eval(shift(@insns));
	  eval(shift(@insns));			# @
	  eval(shift(@insns));
	 &amp;pshufd	($t3,@X[3],0b11111010);	# X[14..15]
	  eval(shift(@insns));
	  eval(shift(@insns));
	&amp;pslld		($t1,32-18);
	  eval(shift(@insns));
	  eval(shift(@insns));			# @
	&amp;pxor		($t0,$t2);
	  eval(shift(@insns));
	  eval(shift(@insns));
	&amp;psrld		($t2,18-7);
	  eval(shift(@insns));
	  eval(shift(@insns));
	  eval(shift(@insns));			# @
	&amp;pxor		($t0,$t1);
	  eval(shift(@insns));
	  eval(shift(@insns));
	&amp;pslld		($t1,18-7);
	  eval(shift(@insns));
	  eval(shift(@insns));
	  eval(shift(@insns));			# @
	&amp;pxor		($t0,$t2);
	  eval(shift(@insns));
	  eval(shift(@insns));
	 &amp;movdqa	($t2,$t3);
	  eval(shift(@insns));
	  eval(shift(@insns));
	  eval(shift(@insns));			# @
	&amp;pxor		($t0,$t1);		# sigma0(X[1..4])
	  eval(shift(@insns));
	  eval(shift(@insns));
	 &amp;psrld		($t3,10);
	  eval(shift(@insns));
	  eval(shift(@insns));
	  eval(shift(@insns));			# @
	&amp;paddd		(@X[0],$t0);		# X[0..3] += sigma0(X[1..4])
	  eval(shift(@insns));
	  eval(shift(@insns));
	 &amp;psrlq		($t2,17);
	  eval(shift(@insns));
	  eval(shift(@insns));
	  eval(shift(@insns));			# @
	 &amp;pxor		($t3,$t2);
	  eval(shift(@insns));
	  eval(shift(@insns));
	 &amp;psrlq		($t2,19-17);
	  eval(shift(@insns));
	  eval(shift(@insns));
	  eval(shift(@insns));			# @
	 &amp;pxor		($t3,$t2);
	  eval(shift(@insns));
	  eval(shift(@insns));
	 &amp;pshufd	($t3,$t3,0b10000000);
	  eval(shift(@insns));
	  eval(shift(@insns));
	  eval(shift(@insns));			# @
	  eval(shift(@insns));
	  eval(shift(@insns));
	  eval(shift(@insns));
	  eval(shift(@insns));
	  eval(shift(@insns));			# @
	  eval(shift(@insns));
	 &amp;psrldq	($t3,8);
	  eval(shift(@insns));
	  eval(shift(@insns));
	  eval(shift(@insns));
	&amp;paddd		(@X[0],$t3);		# X[0..1] += sigma1(X[14..15])
	  eval(shift(@insns));			# @
	  eval(shift(@insns));
	  eval(shift(@insns));
	  eval(shift(@insns));
	  eval(shift(@insns));
	  eval(shift(@insns));			# @
	  eval(shift(@insns));
	 &amp;pshufd	($t3,@X[0],0b01010000);	# X[16..17]
	  eval(shift(@insns));
	  eval(shift(@insns));
	  eval(shift(@insns));
	 &amp;movdqa	($t2,$t3);
	  eval(shift(@insns));			# @
	 &amp;psrld		($t3,10);
	  eval(shift(@insns));
	 &amp;psrlq		($t2,17);
	  eval(shift(@insns));
	  eval(shift(@insns));
	  eval(shift(@insns));
	  eval(shift(@insns));			# @
	 &amp;pxor		($t3,$t2);
	  eval(shift(@insns));
	  eval(shift(@insns));
	 &amp;psrlq		($t2,19-17);
	  eval(shift(@insns));
	  eval(shift(@insns));
	  eval(shift(@insns));			# @
	 &amp;pxor		($t3,$t2);
	  eval(shift(@insns));
	  eval(shift(@insns));
	  eval(shift(@insns));
	 &amp;pshufd	($t3,$t3,0b00001000);
	  eval(shift(@insns));
	  eval(shift(@insns));			# @
	&amp;movdqa		($t2,&amp;QWP(16*$j,$K256));
	  eval(shift(@insns));
	  eval(shift(@insns));
	 &amp;pslldq	($t3,8);
	  eval(shift(@insns));
	  eval(shift(@insns));
	  eval(shift(@insns));			# @
	  eval(shift(@insns));
	  eval(shift(@insns));
	  eval(shift(@insns));
	  eval(shift(@insns));
	  eval(shift(@insns));			# @
	&amp;paddd		(@X[0],$t3);		# X[2..3] += sigma1(X[16..17])
	  eval(shift(@insns));
	  eval(shift(@insns));
	  eval(shift(@insns));
	  eval(shift(@insns));
	&amp;paddd		($t2,@X[0]);
	  eval(shift(@insns));			# @

	foreach (@insns) { eval; }		# remaining instructions

	&amp;movdqa		(&amp;QWP(32+16*$j,"esp"),$t2);
}

sub body_00_15 () {
	(
	'&amp;mov	("ecx",$E);',
	'&amp;ror	($E,25-11);',
	 '&amp;mov	("esi",&amp;off($f));',
	'&amp;xor	($E,"ecx");',
	 '&amp;mov	("edi",&amp;off($g));',
	 '&amp;xor	("esi","edi");',
	'&amp;ror	($E,11-6);',
	 '&amp;and	("esi","ecx");',
	 '&amp;mov	(&amp;off($e),"ecx");',	# save $E, modulo-scheduled
	'&amp;xor	($E,"ecx");',
	 '&amp;xor	("edi","esi");',	# Ch(e,f,g)
	'&amp;ror	($E,6);',		# T = Sigma1(e)
	 '&amp;mov	("ecx",$AH[0]);',
	 '&amp;add	($E,"edi");',		# T += Ch(e,f,g)
	 '&amp;mov	("edi",&amp;off($b));',
	'&amp;mov	("esi",$AH[0]);',

	'&amp;ror	("ecx",22-13);',
	 '&amp;mov	(&amp;off($a),$AH[0]);',	# save $A, modulo-scheduled
	'&amp;xor	("ecx",$AH[0]);',
	 '&amp;xor	($AH[0],"edi");',	# a ^= b, (b^c) in next round
	 '&amp;add	($E,&amp;off($h));',	# T += h
	'&amp;ror	("ecx",13-2);',
	 '&amp;and	($AH[1],$AH[0]);',	# (b^c) &amp;= (a^b)
	'&amp;xor	("ecx","esi");',
	 '&amp;add	($E,&amp;DWP(32+4*($i&amp;15),"esp"));',	# T += K[i]+X[i]
	 '&amp;xor	($AH[1],"edi");',	# h = Maj(a,b,c) = Ch(a^b,c,b)
	'&amp;ror	("ecx",2);',		# Sigma0(a)

	 '&amp;add	($AH[1],$E);',		# h += T
	 '&amp;add	($E,&amp;off($d));',	# d += T
	'&amp;add	($AH[1],"ecx");'.	# h += Sigma0(a)

	'@AH = reverse(@AH); $i++;'	# rotate(a,h)
	);
}

    for ($i=0,$j=0; $j&lt;4; $j++) {
	&amp;SSSE3_00_47($j,\&amp;body_00_15,@X);
	push(@X,shift(@X));		# rotate(@X)
    }
	&amp;cmp	(&amp;DWP(16*$j,$K256),0x00010203);
	&amp;jne	(&amp;label("ssse3_00_47"));

    for ($i=0; $i&lt;16; ) {
	foreach(body_00_15()) { eval; }
    }

	&amp;mov	("esi",&amp;DWP(96,"esp"));	#ctx
					#&amp;mov	($AH[0],&amp;DWP(0,"esp"));
	&amp;xor	($AH[1],"edi");		#&amp;mov	($AH[1],&amp;DWP(4,"esp"));
					#&amp;mov	("edi", &amp;DWP(8,"esp"));
	&amp;mov	("ecx",&amp;DWP(12,"esp"));
	&amp;add	($AH[0],&amp;DWP(0,"esi"));
	&amp;add	($AH[1],&amp;DWP(4,"esi"));
	&amp;add	("edi",&amp;DWP(8,"esi"));
	&amp;add	("ecx",&amp;DWP(12,"esi"));
	&amp;mov	(&amp;DWP(0,"esi"),$AH[0]);
	&amp;mov	(&amp;DWP(4,"esi"),$AH[1]);
	&amp;mov	(&amp;DWP(8,"esi"),"edi");
	&amp;mov	(&amp;DWP(12,"esi"),"ecx");
	 #&amp;mov	(&amp;DWP(0,"esp"),$AH[0]);
	 &amp;mov	(&amp;DWP(4,"esp"),$AH[1]);
	 &amp;xor	($AH[1],"edi");			# magic
	 &amp;mov	(&amp;DWP(8,"esp"),"edi");
	 &amp;mov	(&amp;DWP(12,"esp"),"ecx");
	#&amp;mov	($E,&amp;DWP(16,"esp"));
	&amp;mov	("edi",&amp;DWP(20,"esp"));
	&amp;mov	("ecx",&amp;DWP(24,"esp"));
	&amp;add	($E,&amp;DWP(16,"esi"));
	&amp;add	("edi",&amp;DWP(20,"esi"));
	&amp;add	("ecx",&amp;DWP(24,"esi"));
	&amp;mov	(&amp;DWP(16,"esi"),$E);
	&amp;mov	(&amp;DWP(20,"esi"),"edi");
	 &amp;mov	(&amp;DWP(20,"esp"),"edi");
	&amp;mov	("edi",&amp;DWP(28,"esp"));
	&amp;mov	(&amp;DWP(24,"esi"),"ecx");
	 #&amp;mov	(&amp;DWP(16,"esp"),$E);
	&amp;add	("edi",&amp;DWP(28,"esi"));
	 &amp;mov	(&amp;DWP(24,"esp"),"ecx");
	&amp;mov	(&amp;DWP(28,"esi"),"edi");
	 &amp;mov	(&amp;DWP(28,"esp"),"edi");
	&amp;mov	("edi",&amp;DWP(96+4,"esp"));	# inp

	&amp;movdqa	($t3,&amp;QWP(64,$K256));
	&amp;sub	($K256,3*64);			# rewind K
	&amp;cmp	("edi",&amp;DWP(96+8,"esp"));	# are we done yet?
	&amp;jb	(&amp;label("grand_ssse3"));

	&amp;mov	("esp",&amp;DWP(96+12,"esp"));	# restore sp
&amp;function_end_A();
						if ($avx) {
&amp;set_label("AVX",32);
						if ($avx&gt;1) {
	&amp;and	("edx",1&lt;&lt;8|1&lt;&lt;3);		# check for BMI2+BMI1
	&amp;cmp	("edx",1&lt;&lt;8|1&lt;&lt;3);
	&amp;je	(&amp;label("AVX_BMI"));
						}
	&amp;lea	("esp",&amp;DWP(-96,"esp"));
	&amp;vzeroall	();
	# copy ctx-&gt;h[0-7] to A,B,C,D,E,F,G,H on stack
	&amp;mov	($AH[0],&amp;DWP(0,"esi"));
	&amp;mov	($AH[1],&amp;DWP(4,"esi"));
	&amp;mov	("ecx",&amp;DWP(8,"esi"));
	&amp;mov	("edi",&amp;DWP(12,"esi"));
	#&amp;mov	(&amp;DWP(0,"esp"),$AH[0]);
	&amp;mov	(&amp;DWP(4,"esp"),$AH[1]);
	&amp;xor	($AH[1],"ecx");			# magic
	&amp;mov	(&amp;DWP(8,"esp"),"ecx");
	&amp;mov	(&amp;DWP(12,"esp"),"edi");
	&amp;mov	($E,&amp;DWP(16,"esi"));
	&amp;mov	("edi",&amp;DWP(20,"esi"));
	&amp;mov	("ecx",&amp;DWP(24,"esi"));
	&amp;mov	("esi",&amp;DWP(28,"esi"));
	#&amp;mov	(&amp;DWP(16,"esp"),$E);
	&amp;mov	(&amp;DWP(20,"esp"),"edi");
	&amp;mov	("edi",&amp;DWP(96+4,"esp"));	# inp
	&amp;mov	(&amp;DWP(24,"esp"),"ecx");
	&amp;mov	(&amp;DWP(28,"esp"),"esi");
	&amp;vmovdqa	($t3,&amp;QWP(256,$K256));
	&amp;jmp	(&amp;label("grand_avx"));

&amp;set_label("grand_avx",32);
	# load input, reverse byte order, add K256[0..15], save to stack
	&amp;vmovdqu	(@X[0],&amp;QWP(0,"edi"));
	&amp;vmovdqu	(@X[1],&amp;QWP(16,"edi"));
	&amp;vmovdqu	(@X[2],&amp;QWP(32,"edi"));
	&amp;vmovdqu	(@X[3],&amp;QWP(48,"edi"));
	&amp;add		("edi",64);
	&amp;vpshufb	(@X[0],@X[0],$t3);
	&amp;mov		(&amp;DWP(96+4,"esp"),"edi");
	&amp;vpshufb	(@X[1],@X[1],$t3);
	&amp;vpshufb	(@X[2],@X[2],$t3);
	&amp;vpaddd		($t0,@X[0],&amp;QWP(0,$K256));
	&amp;vpshufb	(@X[3],@X[3],$t3);
	&amp;vpaddd		($t1,@X[1],&amp;QWP(16,$K256));
	&amp;vpaddd		($t2,@X[2],&amp;QWP(32,$K256));
	&amp;vpaddd		($t3,@X[3],&amp;QWP(48,$K256));
	&amp;vmovdqa	(&amp;QWP(32+0,"esp"),$t0);
	&amp;vmovdqa	(&amp;QWP(32+16,"esp"),$t1);
	&amp;vmovdqa	(&amp;QWP(32+32,"esp"),$t2);
	&amp;vmovdqa	(&amp;QWP(32+48,"esp"),$t3);
	&amp;jmp		(&amp;label("avx_00_47"));

&amp;set_label("avx_00_47",16);
	&amp;add		($K256,64);

sub Xupdate_AVX () {
	(
	'&amp;vpalignr	($t0,@X[1],@X[0],4);',	# X[1..4]
	 '&amp;vpalignr	($t3,@X[3],@X[2],4);',	# X[9..12]
	'&amp;vpsrld	($t2,$t0,7);',
	 '&amp;vpaddd	(@X[0],@X[0],$t3);',	# X[0..3] += X[9..16]
	'&amp;vpsrld	($t3,$t0,3);',
	'&amp;vpslld	($t1,$t0,14);',
	'&amp;vpxor		($t0,$t3,$t2);',
	 '&amp;vpshufd	($t3,@X[3],0b11111010)',# X[14..15]
	'&amp;vpsrld	($t2,$t2,18-7);',
	'&amp;vpxor		($t0,$t0,$t1);',
	'&amp;vpslld	($t1,$t1,25-14);',
	'&amp;vpxor		($t0,$t0,$t2);',
	 '&amp;vpsrld	($t2,$t3,10);',
	'&amp;vpxor		($t0,$t0,$t1);',	# sigma0(X[1..4])
	 '&amp;vpsrlq	($t1,$t3,17);',
	'&amp;vpaddd	(@X[0],@X[0],$t0);',	# X[0..3] += sigma0(X[1..4])
	 '&amp;vpxor	($t2,$t2,$t1);',
	 '&amp;vpsrlq	($t3,$t3,19);',
	 '&amp;vpxor	($t2,$t2,$t3);',	# sigma1(X[14..15]
	 '&amp;vpshufd	($t3,$t2,0b10000100);',
	'&amp;vpsrldq	($t3,$t3,8);',
	'&amp;vpaddd	(@X[0],@X[0],$t3);',	# X[0..1] += sigma1(X[14..15])
	 '&amp;vpshufd	($t3,@X[0],0b01010000)',# X[16..17]
	 '&amp;vpsrld	($t2,$t3,10);',
	 '&amp;vpsrlq	($t1,$t3,17);',
	 '&amp;vpxor	($t2,$t2,$t1);',
	 '&amp;vpsrlq	($t3,$t3,19);',
	 '&amp;vpxor	($t2,$t2,$t3);',	# sigma1(X[16..17]
	 '&amp;vpshufd	($t3,$t2,0b11101000);',
	'&amp;vpslldq	($t3,$t3,8);',
	'&amp;vpaddd	(@X[0],@X[0],$t3);'	# X[2..3] += sigma1(X[16..17])
	);
}

local *ror = sub { &amp;shrd(@_[0],@_) };
sub AVX_00_47 () {
my $j = shift;
my $body = shift;
my @X = @_;
my @insns = (&amp;$body,&amp;$body,&amp;$body,&amp;$body);	# 120 instructions
my $insn;

	foreach (Xupdate_AVX()) {		# 31 instructions
	    eval;
	    eval(shift(@insns));
	    eval(shift(@insns));
	    eval($insn = shift(@insns));
	    eval(shift(@insns)) if ($insn =~ /rorx/ &amp;&amp; @insns[0] =~ /rorx/);
	}
	&amp;vpaddd		($t2,@X[0],&amp;QWP(16*$j,$K256));
	foreach (@insns) { eval; }		# remaining instructions
	&amp;vmovdqa	(&amp;QWP(32+16*$j,"esp"),$t2);
}

    for ($i=0,$j=0; $j&lt;4; $j++) {
	&amp;AVX_00_47($j,\&amp;body_00_15,@X);
	push(@X,shift(@X));		# rotate(@X)
    }
	&amp;cmp	(&amp;DWP(16*$j,$K256),0x00010203);
	&amp;jne	(&amp;label("avx_00_47"));

    for ($i=0; $i&lt;16; ) {
	foreach(body_00_15()) { eval; }
    }

	&amp;mov	("esi",&amp;DWP(96,"esp"));	#ctx
					#&amp;mov	($AH[0],&amp;DWP(0,"esp"));
	&amp;xor	($AH[1],"edi");		#&amp;mov	($AH[1],&amp;DWP(4,"esp"));
					#&amp;mov	("edi", &amp;DWP(8,"esp"));
	&amp;mov	("ecx",&amp;DWP(12,"esp"));
	&amp;add	($AH[0],&amp;DWP(0,"esi"));
	&amp;add	($AH[1],&amp;DWP(4,"esi"));
	&amp;add	("edi",&amp;DWP(8,"esi"));
	&amp;add	("ecx",&amp;DWP(12,"esi"));
	&amp;mov	(&amp;DWP(0,"esi"),$AH[0]);
	&amp;mov	(&amp;DWP(4,"esi"),$AH[1]);
	&amp;mov	(&amp;DWP(8,"esi"),"edi");
	&amp;mov	(&amp;DWP(12,"esi"),"ecx");
	 #&amp;mov	(&amp;DWP(0,"esp"),$AH[0]);
	 &amp;mov	(&amp;DWP(4,"esp"),$AH[1]);
	 &amp;xor	($AH[1],"edi");			# magic
	 &amp;mov	(&amp;DWP(8,"esp"),"edi");
	 &amp;mov	(&amp;DWP(12,"esp"),"ecx");
	#&amp;mov	($E,&amp;DWP(16,"esp"));
	&amp;mov	("edi",&amp;DWP(20,"esp"));
	&amp;mov	("ecx",&amp;DWP(24,"esp"));
	&amp;add	($E,&amp;DWP(16,"esi"));
	&amp;add	("edi",&amp;DWP(20,"esi"));
	&amp;add	("ecx",&amp;DWP(24,"esi"));
	&amp;mov	(&amp;DWP(16,"esi"),$E);
	&amp;mov	(&amp;DWP(20,"esi"),"edi");
	 &amp;mov	(&amp;DWP(20,"esp"),"edi");
	&amp;mov	("edi",&amp;DWP(28,"esp"));
	&amp;mov	(&amp;DWP(24,"esi"),"ecx");
	 #&amp;mov	(&amp;DWP(16,"esp"),$E);
	&amp;add	("edi",&amp;DWP(28,"esi"));
	 &amp;mov	(&amp;DWP(24,"esp"),"ecx");
	&amp;mov	(&amp;DWP(28,"esi"),"edi");
	 &amp;mov	(&amp;DWP(28,"esp"),"edi");
	&amp;mov	("edi",&amp;DWP(96+4,"esp"));	# inp

	&amp;vmovdqa	($t3,&amp;QWP(64,$K256));
	&amp;sub	($K256,3*64);			# rewind K
	&amp;cmp	("edi",&amp;DWP(96+8,"esp"));	# are we done yet?
	&amp;jb	(&amp;label("grand_avx"));

	&amp;mov	("esp",&amp;DWP(96+12,"esp"));	# restore sp
	&amp;vzeroall	();
&amp;function_end_A();
						if ($avx&gt;1) {
sub bodyx_00_15 () {			# +10%
	(
	'&amp;rorx	("ecx",$E,6)',
	'&amp;rorx	("esi",$E,11)',
	 '&amp;mov	(&amp;off($e),$E)',		# save $E, modulo-scheduled
	'&amp;rorx	("edi",$E,25)',
	'&amp;xor	("ecx","esi")',
	 '&amp;andn	("esi",$E,&amp;off($g))',
	'&amp;xor	("ecx","edi")',		# Sigma1(e)
	 '&amp;and	($E,&amp;off($f))',
	 '&amp;mov	(&amp;off($a),$AH[0]);',	# save $A, modulo-scheduled
	 '&amp;or	($E,"esi")',		# T = Ch(e,f,g)

	'&amp;rorx	("edi",$AH[0],2)',
	'&amp;rorx	("esi",$AH[0],13)',
	 '&amp;lea	($E,&amp;DWP(0,$E,"ecx"))',	# T += Sigma1(e)
	'&amp;rorx	("ecx",$AH[0],22)',
	'&amp;xor	("esi","edi")',
	 '&amp;mov	("edi",&amp;off($b))',
	'&amp;xor	("ecx","esi")',		# Sigma0(a)

	 '&amp;xor	($AH[0],"edi")',	# a ^= b, (b^c) in next round
	 '&amp;add	($E,&amp;off($h))',		# T += h
	 '&amp;and	($AH[1],$AH[0])',	# (b^c) &amp;= (a^b)
	 '&amp;add	($E,&amp;DWP(32+4*($i&amp;15),"esp"))',	# T += K[i]+X[i]
	 '&amp;xor	($AH[1],"edi")',	# h = Maj(a,b,c) = Ch(a^b,c,b)

	 '&amp;add	("ecx",$E)',		# h += T
	 '&amp;add	($E,&amp;off($d))',		# d += T
	'&amp;lea	($AH[1],&amp;DWP(0,$AH[1],"ecx"));'.	# h += Sigma0(a)

	'@AH = reverse(@AH); $i++;'	# rotate(a,h)
	);
}

&amp;set_label("AVX_BMI",32);
	&amp;lea	("esp",&amp;DWP(-96,"esp"));
	&amp;vzeroall	();
	# copy ctx-&gt;h[0-7] to A,B,C,D,E,F,G,H on stack
	&amp;mov	($AH[0],&amp;DWP(0,"esi"));
	&amp;mov	($AH[1],&amp;DWP(4,"esi"));
	&amp;mov	("ecx",&amp;DWP(8,"esi"));
	&amp;mov	("edi",&amp;DWP(12,"esi"));
	#&amp;mov	(&amp;DWP(0,"esp"),$AH[0]);
	&amp;mov	(&amp;DWP(4,"esp"),$AH[1]);
	&amp;xor	($AH[1],"ecx");			# magic
	&amp;mov	(&amp;DWP(8,"esp"),"ecx");
	&amp;mov	(&amp;DWP(12,"esp"),"edi");
	&amp;mov	($E,&amp;DWP(16,"esi"));
	&amp;mov	("edi",&amp;DWP(20,"esi"));
	&amp;mov	("ecx",&amp;DWP(24,"esi"));
	&amp;mov	("esi",&amp;DWP(28,"esi"));
	#&amp;mov	(&amp;DWP(16,"esp"),$E);
	&amp;mov	(&amp;DWP(20,"esp"),"edi");
	&amp;mov	("edi",&amp;DWP(96+4,"esp"));	# inp
	&amp;mov	(&amp;DWP(24,"esp"),"ecx");
	&amp;mov	(&amp;DWP(28,"esp"),"esi");
	&amp;vmovdqa	($t3,&amp;QWP(256,$K256));
	&amp;jmp	(&amp;label("grand_avx_bmi"));

&amp;set_label("grand_avx_bmi",32);
	# load input, reverse byte order, add K256[0..15], save to stack
	&amp;vmovdqu	(@X[0],&amp;QWP(0,"edi"));
	&amp;vmovdqu	(@X[1],&amp;QWP(16,"edi"));
	&amp;vmovdqu	(@X[2],&amp;QWP(32,"edi"));
	&amp;vmovdqu	(@X[3],&amp;QWP(48,"edi"));
	&amp;add		("edi",64);
	&amp;vpshufb	(@X[0],@X[0],$t3);
	&amp;mov		(&amp;DWP(96+4,"esp"),"edi");
	&amp;vpshufb	(@X[1],@X[1],$t3);
	&amp;vpshufb	(@X[2],@X[2],$t3);
	&amp;vpaddd		($t0,@X[0],&amp;QWP(0,$K256));
	&amp;vpshufb	(@X[3],@X[3],$t3);
	&amp;vpaddd		($t1,@X[1],&amp;QWP(16,$K256));
	&amp;vpaddd		($t2,@X[2],&amp;QWP(32,$K256));
	&amp;vpaddd		($t3,@X[3],&amp;QWP(48,$K256));
	&amp;vmovdqa	(&amp;QWP(32+0,"esp"),$t0);
	&amp;vmovdqa	(&amp;QWP(32+16,"esp"),$t1);
	&amp;vmovdqa	(&amp;QWP(32+32,"esp"),$t2);
	&amp;vmovdqa	(&amp;QWP(32+48,"esp"),$t3);
	&amp;jmp		(&amp;label("avx_bmi_00_47"));

&amp;set_label("avx_bmi_00_47",16);
	&amp;add		($K256,64);

    for ($i=0,$j=0; $j&lt;4; $j++) {
	&amp;AVX_00_47($j,\&amp;bodyx_00_15,@X);
	push(@X,shift(@X));		# rotate(@X)
    }
	&amp;cmp	(&amp;DWP(16*$j,$K256),0x00010203);
	&amp;jne	(&amp;label("avx_bmi_00_47"));

    for ($i=0; $i&lt;16; ) {
	foreach(bodyx_00_15()) { eval; }
    }

	&amp;mov	("esi",&amp;DWP(96,"esp"));	#ctx
					#&amp;mov	($AH[0],&amp;DWP(0,"esp"));
	&amp;xor	($AH[1],"edi");		#&amp;mov	($AH[1],&amp;DWP(4,"esp"));
					#&amp;mov	("edi", &amp;DWP(8,"esp"));
	&amp;mov	("ecx",&amp;DWP(12,"esp"));
	&amp;add	($AH[0],&amp;DWP(0,"esi"));
	&amp;add	($AH[1],&amp;DWP(4,"esi"));
	&amp;add	("edi",&amp;DWP(8,"esi"));
	&amp;add	("ecx",&amp;DWP(12,"esi"));
	&amp;mov	(&amp;DWP(0,"esi"),$AH[0]);
	&amp;mov	(&amp;DWP(4,"esi"),$AH[1]);
	&amp;mov	(&amp;DWP(8,"esi"),"edi");
	&amp;mov	(&amp;DWP(12,"esi"),"ecx");
	 #&amp;mov	(&amp;DWP(0,"esp"),$AH[0]);
	 &amp;mov	(&amp;DWP(4,"esp"),$AH[1]);
	 &amp;xor	($AH[1],"edi");			# magic
	 &amp;mov	(&amp;DWP(8,"esp"),"edi");
	 &amp;mov	(&amp;DWP(12,"esp"),"ecx");
	#&amp;mov	($E,&amp;DWP(16,"esp"));
	&amp;mov	("edi",&amp;DWP(20,"esp"));
	&amp;mov	("ecx",&amp;DWP(24,"esp"));
	&amp;add	($E,&amp;DWP(16,"esi"));
	&amp;add	("edi",&amp;DWP(20,"esi"));
	&amp;add	("ecx",&amp;DWP(24,"esi"));
	&amp;mov	(&amp;DWP(16,"esi"),$E);
	&amp;mov	(&amp;DWP(20,"esi"),"edi");
	 &amp;mov	(&amp;DWP(20,"esp"),"edi");
	&amp;mov	("edi",&amp;DWP(28,"esp"));
	&amp;mov	(&amp;DWP(24,"esi"),"ecx");
	 #&amp;mov	(&amp;DWP(16,"esp"),$E);
	&amp;add	("edi",&amp;DWP(28,"esi"));
	 &amp;mov	(&amp;DWP(24,"esp"),"ecx");
	&amp;mov	(&amp;DWP(28,"esi"),"edi");
	 &amp;mov	(&amp;DWP(28,"esp"),"edi");
	&amp;mov	("edi",&amp;DWP(96+4,"esp"));	# inp

	&amp;vmovdqa	($t3,&amp;QWP(64,$K256));
	&amp;sub	($K256,3*64);			# rewind K
	&amp;cmp	("edi",&amp;DWP(96+8,"esp"));	# are we done yet?
	&amp;jb	(&amp;label("grand_avx_bmi"));

	&amp;mov	("esp",&amp;DWP(96+12,"esp"));	# restore sp
	&amp;vzeroall	();
&amp;function_end_A();
						}
						}
						}}}
&amp;function_end_B("sha256_block_data_order");

&amp;asm_finish();

close STDOUT;
</pre></body></html>