<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 2010-2016 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/.
# ====================================================================
#
# March, May, June 2010
#
# The module implements "4-bit" GCM GHASH function and underlying
# single multiplication operation in GF(2^128). "4-bit" means that it
# uses 256 bytes per-key table [+64/128 bytes fixed table]. It has two
# code paths: vanilla x86 and vanilla SSE. Former will be executed on
# 486 and Pentium, latter on all others. SSE GHASH features so called
# "528B" variant of "4-bit" method utilizing additional 256+16 bytes
# of per-key storage [+512 bytes shared table]. Performance results
# are for streamed GHASH subroutine and are expressed in cycles per
# processed byte, less is better:
#
#		gcc 2.95.3(*)	SSE assembler	x86 assembler
#
# Pentium	105/111(**)	-		50
# PIII		68 /75		12.2		24
# P4		125/125		17.8		84(***)
# Opteron	66 /70		10.1		30
# Core2		54 /67		8.4		18
# Atom		105/105		16.8		53
# VIA Nano	69 /71		13.0		27
#
# (*)	gcc 3.4.x was observed to generate few percent slower code,
#	which is one of reasons why 2.95.3 results were chosen,
#	another reason is lack of 3.4.x results for older CPUs;
#	comparison with SSE results is not completely fair, because C
#	results are for vanilla "256B" implementation, while
#	assembler results are for "528B";-)
# (**)	second number is result for code compiled with -fPIC flag,
#	which is actually more relevant, because assembler code is
#	position-independent;
# (***)	see comment in non-MMX routine for further details;
#
# To summarize, it's &gt;2-5 times faster than gcc-generated code. To
# anchor it to something else SHA1 assembler processes one byte in
# ~7 cycles on contemporary x86 cores. As for choice of MMX/SSE
# in particular, see comment at the end of the file...

# May 2010
#
# Add PCLMULQDQ version performing at 2.10 cycles per processed byte.
# The question is how close is it to theoretical limit? The pclmulqdq
# instruction latency appears to be 14 cycles and there can't be more
# than 2 of them executing at any given time. This means that single
# Karatsuba multiplication would take 28 cycles *plus* few cycles for
# pre- and post-processing. Then multiplication has to be followed by
# modulo-reduction. Given that aggregated reduction method [see
# "Carry-less Multiplication and Its Usage for Computing the GCM Mode"
# white paper by Intel] allows you to perform reduction only once in
# a while we can assume that asymptotic performance can be estimated
# as (28+Tmod/Naggr)/16, where Tmod is time to perform reduction
# and Naggr is the aggregation factor.
#
# Before we proceed to this implementation let's have closer look at
# the best-performing code suggested by Intel in their white paper.
# By tracing inter-register dependencies Tmod is estimated as ~19
# cycles and Naggr chosen by Intel is 4, resulting in 2.05 cycles per
# processed byte. As implied, this is quite optimistic estimate,
# because it does not account for Karatsuba pre- and post-processing,
# which for a single multiplication is ~5 cycles. Unfortunately Intel
# does not provide performance data for GHASH alone. But benchmarking
# AES_GCM_encrypt ripped out of Fig. 15 of the white paper with aadt
# alone resulted in 2.46 cycles per byte of out 16KB buffer. Note that
# the result accounts even for pre-computing of degrees of the hash
# key H, but its portion is negligible at 16KB buffer size.
#
# Moving on to the implementation in question. Tmod is estimated as
# ~13 cycles and Naggr is 2, giving asymptotic performance of ...
# 2.16. How is it possible that measured performance is better than
# optimistic theoretical estimate? There is one thing Intel failed
# to recognize. By serializing GHASH with CTR in same subroutine
# former's performance is really limited to above (Tmul + Tmod/Naggr)
# equation. But if GHASH procedure is detached, the modulo-reduction
# can be interleaved with Naggr-1 multiplications at instruction level
# and under ideal conditions even disappear from the equation. So that
# optimistic theoretical estimate for this implementation is ...
# 28/16=1.75, and not 2.16. Well, it's probably way too optimistic,
# at least for such small Naggr. I'd argue that (28+Tproc/Naggr),
# where Tproc is time required for Karatsuba pre- and post-processing,
# is more realistic estimate. In this case it gives ... 1.91 cycles.
# Or in other words, depending on how well we can interleave reduction
# and one of the two multiplications the performance should be between
# 1.91 and 2.16. As already mentioned, this implementation processes
# one byte out of 8KB buffer in 2.10 cycles, while x86_64 counterpart
# - in 2.02. x86_64 performance is better, because larger register
# bank allows to interleave reduction and multiplication better.
#
# Does it make sense to increase Naggr? To start with it's virtually
# impossible in 32-bit mode, because of limited register bank
# capacity. Otherwise improvement has to be weighed against slower
# setup, as well as code size and complexity increase. As even
# optimistic estimate doesn't promise 30% performance improvement,
# there are currently no plans to increase Naggr.
#
# Special thanks to David Woodhouse for providing access to a
# Westmere-based system on behalf of Intel Open Source Technology Centre.

# January 2010
#
# Tweaked to optimize transitions between integer and FP operations
# on same XMM register, PCLMULQDQ subroutine was measured to process
# one byte in 2.07 cycles on Sandy Bridge, and in 2.12 - on Westmere.
# The minor regression on Westmere is outweighed by ~15% improvement
# on Sandy Bridge. Strangely enough attempt to modify 64-bit code in
# similar manner resulted in almost 20% degradation on Sandy Bridge,
# where original 64-bit code processes one byte in 1.95 cycles.

#####################################################################
# For reference, AMD Bulldozer processes one byte in 1.98 cycles in
# 32-bit mode and 1.89 in 64-bit.

# February 2013
#
# Overhaul: aggregate Karatsuba post-processing, improve ILP in
# reduction_alg9. Resulting performance is 1.96 cycles per byte on
# Westmere, 1.95 - on Sandy/Ivy Bridge, 1.76 - on Bulldozer.

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

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

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

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

($Zhh,$Zhl,$Zlh,$Zll) = ("ebp","edx","ecx","ebx");
$inp  = "edi";
$Htbl = "esi";

$unroll = 0;	# Affects x86 loop. Folded loop performs ~7% worse
		# than unrolled, which has to be weighted against
		# 2.5x x86-specific code size reduction.

sub x86_loop {
    my $off = shift;
    my $rem = "eax";

	&amp;mov	($Zhh,&amp;DWP(4,$Htbl,$Zll));
	&amp;mov	($Zhl,&amp;DWP(0,$Htbl,$Zll));
	&amp;mov	($Zlh,&amp;DWP(12,$Htbl,$Zll));
	&amp;mov	($Zll,&amp;DWP(8,$Htbl,$Zll));
	&amp;xor	($rem,$rem);	# avoid partial register stalls on PIII

	# shrd practically kills P4, 2.5x deterioration, but P4 has
	# MMX code-path to execute. shrd runs tad faster [than twice
	# the shifts, move's and or's] on pre-MMX Pentium (as well as
	# PIII and Core2), *but* minimizes code size, spares register
	# and thus allows to fold the loop...
	if (!$unroll) {
	my $cnt = $inp;
	&amp;mov	($cnt,15);
	&amp;jmp	(&amp;label("x86_loop"));
	&amp;set_label("x86_loop",16);
	    for($i=1;$i&lt;=2;$i++) {
		&amp;mov	(&amp;LB($rem),&amp;LB($Zll));
		&amp;shrd	($Zll,$Zlh,4);
		&amp;and	(&amp;LB($rem),0xf);
		&amp;shrd	($Zlh,$Zhl,4);
		&amp;shrd	($Zhl,$Zhh,4);
		&amp;shr	($Zhh,4);
		&amp;xor	($Zhh,&amp;DWP($off+16,"esp",$rem,4));

		&amp;mov	(&amp;LB($rem),&amp;BP($off,"esp",$cnt));
		if ($i&amp;1) {
			&amp;and	(&amp;LB($rem),0xf0);
		} else {
			&amp;shl	(&amp;LB($rem),4);
		}

		&amp;xor	($Zll,&amp;DWP(8,$Htbl,$rem));
		&amp;xor	($Zlh,&amp;DWP(12,$Htbl,$rem));
		&amp;xor	($Zhl,&amp;DWP(0,$Htbl,$rem));
		&amp;xor	($Zhh,&amp;DWP(4,$Htbl,$rem));

		if ($i&amp;1) {
			&amp;dec	($cnt);
			&amp;js	(&amp;label("x86_break"));
		} else {
			&amp;jmp	(&amp;label("x86_loop"));
		}
	    }
	&amp;set_label("x86_break",16);
	} else {
	    for($i=1;$i&lt;32;$i++) {
		&amp;comment($i);
		&amp;mov	(&amp;LB($rem),&amp;LB($Zll));
		&amp;shrd	($Zll,$Zlh,4);
		&amp;and	(&amp;LB($rem),0xf);
		&amp;shrd	($Zlh,$Zhl,4);
		&amp;shrd	($Zhl,$Zhh,4);
		&amp;shr	($Zhh,4);
		&amp;xor	($Zhh,&amp;DWP($off+16,"esp",$rem,4));

		if ($i&amp;1) {
			&amp;mov	(&amp;LB($rem),&amp;BP($off+15-($i&gt;&gt;1),"esp"));
			&amp;and	(&amp;LB($rem),0xf0);
		} else {
			&amp;mov	(&amp;LB($rem),&amp;BP($off+15-($i&gt;&gt;1),"esp"));
			&amp;shl	(&amp;LB($rem),4);
		}

		&amp;xor	($Zll,&amp;DWP(8,$Htbl,$rem));
		&amp;xor	($Zlh,&amp;DWP(12,$Htbl,$rem));
		&amp;xor	($Zhl,&amp;DWP(0,$Htbl,$rem));
		&amp;xor	($Zhh,&amp;DWP(4,$Htbl,$rem));
	    }
	}
	&amp;bswap	($Zll);
	&amp;bswap	($Zlh);
	&amp;bswap	($Zhl);
	if (!$x86only) {
		&amp;bswap	($Zhh);
	} else {
		&amp;mov	("eax",$Zhh);
		&amp;bswap	("eax");
		&amp;mov	($Zhh,"eax");
	}
}

if ($unroll) {
    &amp;function_begin_B("_x86_gmult_4bit_inner");
	&amp;x86_loop(4);
	&amp;ret	();
    &amp;function_end_B("_x86_gmult_4bit_inner");
}

sub deposit_rem_4bit {
    my $bias = shift;

	&amp;mov	(&amp;DWP($bias+0, "esp"),0x0000&lt;&lt;16);
	&amp;mov	(&amp;DWP($bias+4, "esp"),0x1C20&lt;&lt;16);
	&amp;mov	(&amp;DWP($bias+8, "esp"),0x3840&lt;&lt;16);
	&amp;mov	(&amp;DWP($bias+12,"esp"),0x2460&lt;&lt;16);
	&amp;mov	(&amp;DWP($bias+16,"esp"),0x7080&lt;&lt;16);
	&amp;mov	(&amp;DWP($bias+20,"esp"),0x6CA0&lt;&lt;16);
	&amp;mov	(&amp;DWP($bias+24,"esp"),0x48C0&lt;&lt;16);
	&amp;mov	(&amp;DWP($bias+28,"esp"),0x54E0&lt;&lt;16);
	&amp;mov	(&amp;DWP($bias+32,"esp"),0xE100&lt;&lt;16);
	&amp;mov	(&amp;DWP($bias+36,"esp"),0xFD20&lt;&lt;16);
	&amp;mov	(&amp;DWP($bias+40,"esp"),0xD940&lt;&lt;16);
	&amp;mov	(&amp;DWP($bias+44,"esp"),0xC560&lt;&lt;16);
	&amp;mov	(&amp;DWP($bias+48,"esp"),0x9180&lt;&lt;16);
	&amp;mov	(&amp;DWP($bias+52,"esp"),0x8DA0&lt;&lt;16);
	&amp;mov	(&amp;DWP($bias+56,"esp"),0xA9C0&lt;&lt;16);
	&amp;mov	(&amp;DWP($bias+60,"esp"),0xB5E0&lt;&lt;16);
}

$suffix = $x86only ? "" : "_x86";

&amp;function_begin("gcm_gmult_4bit".$suffix);
	&amp;stack_push(16+4+1);			# +1 for stack alignment
	&amp;mov	($inp,&amp;wparam(0));		# load Xi
	&amp;mov	($Htbl,&amp;wparam(1));		# load Htable

	&amp;mov	($Zhh,&amp;DWP(0,$inp));		# load Xi[16]
	&amp;mov	($Zhl,&amp;DWP(4,$inp));
	&amp;mov	($Zlh,&amp;DWP(8,$inp));
	&amp;mov	($Zll,&amp;DWP(12,$inp));

	&amp;deposit_rem_4bit(16);

	&amp;mov	(&amp;DWP(0,"esp"),$Zhh);		# copy Xi[16] on stack
	&amp;mov	(&amp;DWP(4,"esp"),$Zhl);
	&amp;mov	(&amp;DWP(8,"esp"),$Zlh);
	&amp;mov	(&amp;DWP(12,"esp"),$Zll);
	&amp;shr	($Zll,20);
	&amp;and	($Zll,0xf0);

	if ($unroll) {
		&amp;call	("_x86_gmult_4bit_inner");
	} else {
		&amp;x86_loop(0);
		&amp;mov	($inp,&amp;wparam(0));
	}

	&amp;mov	(&amp;DWP(12,$inp),$Zll);
	&amp;mov	(&amp;DWP(8,$inp),$Zlh);
	&amp;mov	(&amp;DWP(4,$inp),$Zhl);
	&amp;mov	(&amp;DWP(0,$inp),$Zhh);
	&amp;stack_pop(16+4+1);
&amp;function_end("gcm_gmult_4bit".$suffix);

&amp;function_begin("gcm_ghash_4bit".$suffix);
	&amp;stack_push(16+4+1);			# +1 for 64-bit alignment
	&amp;mov	($Zll,&amp;wparam(0));		# load Xi
	&amp;mov	($Htbl,&amp;wparam(1));		# load Htable
	&amp;mov	($inp,&amp;wparam(2));		# load in
	&amp;mov	("ecx",&amp;wparam(3));		# load len
	&amp;add	("ecx",$inp);
	&amp;mov	(&amp;wparam(3),"ecx");

	&amp;mov	($Zhh,&amp;DWP(0,$Zll));		# load Xi[16]
	&amp;mov	($Zhl,&amp;DWP(4,$Zll));
	&amp;mov	($Zlh,&amp;DWP(8,$Zll));
	&amp;mov	($Zll,&amp;DWP(12,$Zll));

	&amp;deposit_rem_4bit(16);

    &amp;set_label("x86_outer_loop",16);
	&amp;xor	($Zll,&amp;DWP(12,$inp));		# xor with input
	&amp;xor	($Zlh,&amp;DWP(8,$inp));
	&amp;xor	($Zhl,&amp;DWP(4,$inp));
	&amp;xor	($Zhh,&amp;DWP(0,$inp));
	&amp;mov	(&amp;DWP(12,"esp"),$Zll);		# dump it on stack
	&amp;mov	(&amp;DWP(8,"esp"),$Zlh);
	&amp;mov	(&amp;DWP(4,"esp"),$Zhl);
	&amp;mov	(&amp;DWP(0,"esp"),$Zhh);

	&amp;shr	($Zll,20);
	&amp;and	($Zll,0xf0);

	if ($unroll) {
		&amp;call	("_x86_gmult_4bit_inner");
	} else {
		&amp;x86_loop(0);
		&amp;mov	($inp,&amp;wparam(2));
	}
	&amp;lea	($inp,&amp;DWP(16,$inp));
	&amp;cmp	($inp,&amp;wparam(3));
	&amp;mov	(&amp;wparam(2),$inp)	if (!$unroll);
	&amp;jb	(&amp;label("x86_outer_loop"));

	&amp;mov	($inp,&amp;wparam(0));	# load Xi
	&amp;mov	(&amp;DWP(12,$inp),$Zll);
	&amp;mov	(&amp;DWP(8,$inp),$Zlh);
	&amp;mov	(&amp;DWP(4,$inp),$Zhl);
	&amp;mov	(&amp;DWP(0,$inp),$Zhh);
	&amp;stack_pop(16+4+1);
&amp;function_end("gcm_ghash_4bit".$suffix);

if (!$x86only) {{{

&amp;static_label("rem_4bit");

if (!$sse2) {{	# pure-MMX "May" version...

$S=12;		# shift factor for rem_4bit

&amp;function_begin_B("_mmx_gmult_4bit_inner");
# MMX version performs 3.5 times better on P4 (see comment in non-MMX
# routine for further details), 100% better on Opteron, ~70% better
# on Core2 and PIII... In other words effort is considered to be well
# spent... Since initial release the loop was unrolled in order to
# "liberate" register previously used as loop counter. Instead it's
# used to optimize critical path in 'Z.hi ^= rem_4bit[Z.lo&amp;0xf]'.
# The path involves move of Z.lo from MMX to integer register,
# effective address calculation and finally merge of value to Z.hi.
# Reference to rem_4bit is scheduled so late that I had to &gt;&gt;4
# rem_4bit elements. This resulted in 20-45% procent improvement
# on contemporary Âµ-archs.
{
    my $cnt;
    my $rem_4bit = "eax";
    my @rem = ($Zhh,$Zll);
    my $nhi = $Zhl;
    my $nlo = $Zlh;

    my ($Zlo,$Zhi) = ("mm0","mm1");
    my $tmp = "mm2";

	&amp;xor	($nlo,$nlo);	# avoid partial register stalls on PIII
	&amp;mov	($nhi,$Zll);
	&amp;mov	(&amp;LB($nlo),&amp;LB($nhi));
	&amp;shl	(&amp;LB($nlo),4);
	&amp;and	($nhi,0xf0);
	&amp;movq	($Zlo,&amp;QWP(8,$Htbl,$nlo));
	&amp;movq	($Zhi,&amp;QWP(0,$Htbl,$nlo));
	&amp;movd	($rem[0],$Zlo);

	for ($cnt=28;$cnt&gt;=-2;$cnt--) {
	    my $odd = $cnt&amp;1;
	    my $nix = $odd ? $nlo : $nhi;

		&amp;shl	(&amp;LB($nlo),4)			if ($odd);
		&amp;psrlq	($Zlo,4);
		&amp;movq	($tmp,$Zhi);
		&amp;psrlq	($Zhi,4);
		&amp;pxor	($Zlo,&amp;QWP(8,$Htbl,$nix));
		&amp;mov	(&amp;LB($nlo),&amp;BP($cnt/2,$inp))	if (!$odd &amp;&amp; $cnt&gt;=0);
		&amp;psllq	($tmp,60);
		&amp;and	($nhi,0xf0)			if ($odd);
		&amp;pxor	($Zhi,&amp;QWP(0,$rem_4bit,$rem[1],8)) if ($cnt&lt;28);
		&amp;and	($rem[0],0xf);
		&amp;pxor	($Zhi,&amp;QWP(0,$Htbl,$nix));
		&amp;mov	($nhi,$nlo)			if (!$odd &amp;&amp; $cnt&gt;=0);
		&amp;movd	($rem[1],$Zlo);
		&amp;pxor	($Zlo,$tmp);

		push	(@rem,shift(@rem));		# "rotate" registers
	}

	&amp;mov	($inp,&amp;DWP(4,$rem_4bit,$rem[1],8));	# last rem_4bit[rem]

	&amp;psrlq	($Zlo,32);	# lower part of Zlo is already there
	&amp;movd	($Zhl,$Zhi);
	&amp;psrlq	($Zhi,32);
	&amp;movd	($Zlh,$Zlo);
	&amp;movd	($Zhh,$Zhi);
	&amp;shl	($inp,4);	# compensate for rem_4bit[i] being &gt;&gt;4

	&amp;bswap	($Zll);
	&amp;bswap	($Zhl);
	&amp;bswap	($Zlh);
	&amp;xor	($Zhh,$inp);
	&amp;bswap	($Zhh);

	&amp;ret	();
}
&amp;function_end_B("_mmx_gmult_4bit_inner");

&amp;function_begin("gcm_gmult_4bit_mmx");
	&amp;mov	($inp,&amp;wparam(0));	# load Xi
	&amp;mov	($Htbl,&amp;wparam(1));	# load Htable

	&amp;call	(&amp;label("pic_point"));
	&amp;set_label("pic_point");
	&amp;blindpop("eax");
	&amp;lea	("eax",&amp;DWP(&amp;label("rem_4bit")."-".&amp;label("pic_point"),"eax"));

	&amp;movz	($Zll,&amp;BP(15,$inp));

	&amp;call	("_mmx_gmult_4bit_inner");

	&amp;mov	($inp,&amp;wparam(0));	# load Xi
	&amp;emms	();
	&amp;mov	(&amp;DWP(12,$inp),$Zll);
	&amp;mov	(&amp;DWP(4,$inp),$Zhl);
	&amp;mov	(&amp;DWP(8,$inp),$Zlh);
	&amp;mov	(&amp;DWP(0,$inp),$Zhh);
&amp;function_end("gcm_gmult_4bit_mmx");

# Streamed version performs 20% better on P4, 7% on Opteron,
# 10% on Core2 and PIII...
&amp;function_begin("gcm_ghash_4bit_mmx");
	&amp;mov	($Zhh,&amp;wparam(0));	# load Xi
	&amp;mov	($Htbl,&amp;wparam(1));	# load Htable
	&amp;mov	($inp,&amp;wparam(2));	# load in
	&amp;mov	($Zlh,&amp;wparam(3));	# load len

	&amp;call	(&amp;label("pic_point"));
	&amp;set_label("pic_point");
	&amp;blindpop("eax");
	&amp;lea	("eax",&amp;DWP(&amp;label("rem_4bit")."-".&amp;label("pic_point"),"eax"));

	&amp;add	($Zlh,$inp);
	&amp;mov	(&amp;wparam(3),$Zlh);	# len to point at the end of input
	&amp;stack_push(4+1);		# +1 for stack alignment

	&amp;mov	($Zll,&amp;DWP(12,$Zhh));	# load Xi[16]
	&amp;mov	($Zhl,&amp;DWP(4,$Zhh));
	&amp;mov	($Zlh,&amp;DWP(8,$Zhh));
	&amp;mov	($Zhh,&amp;DWP(0,$Zhh));
	&amp;jmp	(&amp;label("mmx_outer_loop"));

    &amp;set_label("mmx_outer_loop",16);
	&amp;xor	($Zll,&amp;DWP(12,$inp));
	&amp;xor	($Zhl,&amp;DWP(4,$inp));
	&amp;xor	($Zlh,&amp;DWP(8,$inp));
	&amp;xor	($Zhh,&amp;DWP(0,$inp));
	&amp;mov	(&amp;wparam(2),$inp);
	&amp;mov	(&amp;DWP(12,"esp"),$Zll);
	&amp;mov	(&amp;DWP(4,"esp"),$Zhl);
	&amp;mov	(&amp;DWP(8,"esp"),$Zlh);
	&amp;mov	(&amp;DWP(0,"esp"),$Zhh);

	&amp;mov	($inp,"esp");
	&amp;shr	($Zll,24);

	&amp;call	("_mmx_gmult_4bit_inner");

	&amp;mov	($inp,&amp;wparam(2));
	&amp;lea	($inp,&amp;DWP(16,$inp));
	&amp;cmp	($inp,&amp;wparam(3));
	&amp;jb	(&amp;label("mmx_outer_loop"));

	&amp;mov	($inp,&amp;wparam(0));	# load Xi
	&amp;emms	();
	&amp;mov	(&amp;DWP(12,$inp),$Zll);
	&amp;mov	(&amp;DWP(4,$inp),$Zhl);
	&amp;mov	(&amp;DWP(8,$inp),$Zlh);
	&amp;mov	(&amp;DWP(0,$inp),$Zhh);

	&amp;stack_pop(4+1);
&amp;function_end("gcm_ghash_4bit_mmx");

}} else {{	# "June" MMX version...
		# ... has slower "April" gcm_gmult_4bit_mmx with folded
		# loop. This is done to conserve code size...
$S=16;		# shift factor for rem_4bit

sub mmx_loop() {
# MMX version performs 2.8 times better on P4 (see comment in non-MMX
# routine for further details), 40% better on Opteron and Core2, 50%
# better on PIII... In other words effort is considered to be well
# spent...
    my $inp = shift;
    my $rem_4bit = shift;
    my $cnt = $Zhh;
    my $nhi = $Zhl;
    my $nlo = $Zlh;
    my $rem = $Zll;

    my ($Zlo,$Zhi) = ("mm0","mm1");
    my $tmp = "mm2";

	&amp;xor	($nlo,$nlo);	# avoid partial register stalls on PIII
	&amp;mov	($nhi,$Zll);
	&amp;mov	(&amp;LB($nlo),&amp;LB($nhi));
	&amp;mov	($cnt,14);
	&amp;shl	(&amp;LB($nlo),4);
	&amp;and	($nhi,0xf0);
	&amp;movq	($Zlo,&amp;QWP(8,$Htbl,$nlo));
	&amp;movq	($Zhi,&amp;QWP(0,$Htbl,$nlo));
	&amp;movd	($rem,$Zlo);
	&amp;jmp	(&amp;label("mmx_loop"));

    &amp;set_label("mmx_loop",16);
	&amp;psrlq	($Zlo,4);
	&amp;and	($rem,0xf);
	&amp;movq	($tmp,$Zhi);
	&amp;psrlq	($Zhi,4);
	&amp;pxor	($Zlo,&amp;QWP(8,$Htbl,$nhi));
	&amp;mov	(&amp;LB($nlo),&amp;BP(0,$inp,$cnt));
	&amp;psllq	($tmp,60);
	&amp;pxor	($Zhi,&amp;QWP(0,$rem_4bit,$rem,8));
	&amp;dec	($cnt);
	&amp;movd	($rem,$Zlo);
	&amp;pxor	($Zhi,&amp;QWP(0,$Htbl,$nhi));
	&amp;mov	($nhi,$nlo);
	&amp;pxor	($Zlo,$tmp);
	&amp;js	(&amp;label("mmx_break"));

	&amp;shl	(&amp;LB($nlo),4);
	&amp;and	($rem,0xf);
	&amp;psrlq	($Zlo,4);
	&amp;and	($nhi,0xf0);
	&amp;movq	($tmp,$Zhi);
	&amp;psrlq	($Zhi,4);
	&amp;pxor	($Zlo,&amp;QWP(8,$Htbl,$nlo));
	&amp;psllq	($tmp,60);
	&amp;pxor	($Zhi,&amp;QWP(0,$rem_4bit,$rem,8));
	&amp;movd	($rem,$Zlo);
	&amp;pxor	($Zhi,&amp;QWP(0,$Htbl,$nlo));
	&amp;pxor	($Zlo,$tmp);
	&amp;jmp	(&amp;label("mmx_loop"));

    &amp;set_label("mmx_break",16);
	&amp;shl	(&amp;LB($nlo),4);
	&amp;and	($rem,0xf);
	&amp;psrlq	($Zlo,4);
	&amp;and	($nhi,0xf0);
	&amp;movq	($tmp,$Zhi);
	&amp;psrlq	($Zhi,4);
	&amp;pxor	($Zlo,&amp;QWP(8,$Htbl,$nlo));
	&amp;psllq	($tmp,60);
	&amp;pxor	($Zhi,&amp;QWP(0,$rem_4bit,$rem,8));
	&amp;movd	($rem,$Zlo);
	&amp;pxor	($Zhi,&amp;QWP(0,$Htbl,$nlo));
	&amp;pxor	($Zlo,$tmp);

	&amp;psrlq	($Zlo,4);
	&amp;and	($rem,0xf);
	&amp;movq	($tmp,$Zhi);
	&amp;psrlq	($Zhi,4);
	&amp;pxor	($Zlo,&amp;QWP(8,$Htbl,$nhi));
	&amp;psllq	($tmp,60);
	&amp;pxor	($Zhi,&amp;QWP(0,$rem_4bit,$rem,8));
	&amp;movd	($rem,$Zlo);
	&amp;pxor	($Zhi,&amp;QWP(0,$Htbl,$nhi));
	&amp;pxor	($Zlo,$tmp);

	&amp;psrlq	($Zlo,32);	# lower part of Zlo is already there
	&amp;movd	($Zhl,$Zhi);
	&amp;psrlq	($Zhi,32);
	&amp;movd	($Zlh,$Zlo);
	&amp;movd	($Zhh,$Zhi);

	&amp;bswap	($Zll);
	&amp;bswap	($Zhl);
	&amp;bswap	($Zlh);
	&amp;bswap	($Zhh);
}

&amp;function_begin("gcm_gmult_4bit_mmx");
	&amp;mov	($inp,&amp;wparam(0));	# load Xi
	&amp;mov	($Htbl,&amp;wparam(1));	# load Htable

	&amp;call	(&amp;label("pic_point"));
	&amp;set_label("pic_point");
	&amp;blindpop("eax");
	&amp;lea	("eax",&amp;DWP(&amp;label("rem_4bit")."-".&amp;label("pic_point"),"eax"));

	&amp;movz	($Zll,&amp;BP(15,$inp));

	&amp;mmx_loop($inp,"eax");

	&amp;emms	();
	&amp;mov	(&amp;DWP(12,$inp),$Zll);
	&amp;mov	(&amp;DWP(4,$inp),$Zhl);
	&amp;mov	(&amp;DWP(8,$inp),$Zlh);
	&amp;mov	(&amp;DWP(0,$inp),$Zhh);
&amp;function_end("gcm_gmult_4bit_mmx");

######################################################################
# Below subroutine is "528B" variant of "4-bit" GCM GHASH function
# (see gcm128.c for details). It provides further 20-40% performance
# improvement over above mentioned "May" version.

&amp;static_label("rem_8bit");

&amp;function_begin("gcm_ghash_4bit_mmx");
{ my ($Zlo,$Zhi) = ("mm7","mm6");
  my $rem_8bit = "esi";
  my $Htbl = "ebx";

    # parameter block
    &amp;mov	("eax",&amp;wparam(0));		# Xi
    &amp;mov	("ebx",&amp;wparam(1));		# Htable
    &amp;mov	("ecx",&amp;wparam(2));		# inp
    &amp;mov	("edx",&amp;wparam(3));		# len
    &amp;mov	("ebp","esp");			# original %esp
    &amp;call	(&amp;label("pic_point"));
    &amp;set_label	("pic_point");
    &amp;blindpop	($rem_8bit);
    &amp;lea	($rem_8bit,&amp;DWP(&amp;label("rem_8bit")."-".&amp;label("pic_point"),$rem_8bit));

    &amp;sub	("esp",512+16+16);		# allocate stack frame...
    &amp;and	("esp",-64);			# ...and align it
    &amp;sub	("esp",16);			# place for (u8)(H[]&lt;&lt;4)

    &amp;add	("edx","ecx");			# pointer to the end of input
    &amp;mov	(&amp;DWP(528+16+0,"esp"),"eax");	# save Xi
    &amp;mov	(&amp;DWP(528+16+8,"esp"),"edx");	# save inp+len
    &amp;mov	(&amp;DWP(528+16+12,"esp"),"ebp");	# save original %esp

    { my @lo  = ("mm0","mm1","mm2");
      my @hi  = ("mm3","mm4","mm5");
      my @tmp = ("mm6","mm7");
      my ($off1,$off2,$i) = (0,0,);

      &amp;add	($Htbl,128);			# optimize for size
      &amp;lea	("edi",&amp;DWP(16+128,"esp"));
      &amp;lea	("ebp",&amp;DWP(16+256+128,"esp"));

      # decompose Htable (low and high parts are kept separately),
      # generate Htable[]&gt;&gt;4, (u8)(Htable[]&lt;&lt;4), save to stack...
      for ($i=0;$i&lt;18;$i++) {

	&amp;mov	("edx",&amp;DWP(16*$i+8-128,$Htbl))		if ($i&lt;16);
	&amp;movq	($lo[0],&amp;QWP(16*$i+8-128,$Htbl))	if ($i&lt;16);
	&amp;psllq	($tmp[1],60)				if ($i&gt;1);
	&amp;movq	($hi[0],&amp;QWP(16*$i+0-128,$Htbl))	if ($i&lt;16);
	&amp;por	($lo[2],$tmp[1])			if ($i&gt;1);
	&amp;movq	(&amp;QWP($off1-128,"edi"),$lo[1])		if ($i&gt;0 &amp;&amp; $i&lt;17);
	&amp;psrlq	($lo[1],4)				if ($i&gt;0 &amp;&amp; $i&lt;17);
	&amp;movq	(&amp;QWP($off1,"edi"),$hi[1])		if ($i&gt;0 &amp;&amp; $i&lt;17);
	&amp;movq	($tmp[0],$hi[1])			if ($i&gt;0 &amp;&amp; $i&lt;17);
	&amp;movq	(&amp;QWP($off2-128,"ebp"),$lo[2])		if ($i&gt;1);
	&amp;psrlq	($hi[1],4)				if ($i&gt;0 &amp;&amp; $i&lt;17);
	&amp;movq	(&amp;QWP($off2,"ebp"),$hi[2])		if ($i&gt;1);
	&amp;shl	("edx",4)				if ($i&lt;16);
	&amp;mov	(&amp;BP($i,"esp"),&amp;LB("edx"))		if ($i&lt;16);

	unshift	(@lo,pop(@lo));			# "rotate" registers
	unshift	(@hi,pop(@hi));
	unshift	(@tmp,pop(@tmp));
	$off1 += 8	if ($i&gt;0);
	$off2 += 8	if ($i&gt;1);
      }
    }

    &amp;movq	($Zhi,&amp;QWP(0,"eax"));
    &amp;mov	("ebx",&amp;DWP(8,"eax"));
    &amp;mov	("edx",&amp;DWP(12,"eax"));		# load Xi

&amp;set_label("outer",16);
  { my $nlo = "eax";
    my $dat = "edx";
    my @nhi = ("edi","ebp");
    my @rem = ("ebx","ecx");
    my @red = ("mm0","mm1","mm2");
    my $tmp = "mm3";

    &amp;xor	($dat,&amp;DWP(12,"ecx"));		# merge input data
    &amp;xor	("ebx",&amp;DWP(8,"ecx"));
    &amp;pxor	($Zhi,&amp;QWP(0,"ecx"));
    &amp;lea	("ecx",&amp;DWP(16,"ecx"));		# inp+=16
    #&amp;mov	(&amp;DWP(528+12,"esp"),$dat);	# save inp^Xi
    &amp;mov	(&amp;DWP(528+8,"esp"),"ebx");
    &amp;movq	(&amp;QWP(528+0,"esp"),$Zhi);
    &amp;mov	(&amp;DWP(528+16+4,"esp"),"ecx");	# save inp

    &amp;xor	($nlo,$nlo);
    &amp;rol	($dat,8);
    &amp;mov	(&amp;LB($nlo),&amp;LB($dat));
    &amp;mov	($nhi[1],$nlo);
    &amp;and	(&amp;LB($nlo),0x0f);
    &amp;shr	($nhi[1],4);
    &amp;pxor	($red[0],$red[0]);
    &amp;rol	($dat,8);			# next byte
    &amp;pxor	($red[1],$red[1]);
    &amp;pxor	($red[2],$red[2]);

    # Just like in "May" version modulo-schedule for critical path in
    # 'Z.hi ^= rem_8bit[Z.lo&amp;0xff^((u8)H[nhi]&lt;&lt;4)]&lt;&lt;48'. Final 'pxor'
    # is scheduled so late that rem_8bit[] has to be shifted *right*
    # by 16, which is why last argument to pinsrw is 2, which
    # corresponds to &lt;&lt;32=&lt;&lt;48&gt;&gt;16...
    for ($j=11,$i=0;$i&lt;15;$i++) {

      if ($i&gt;0) {
	&amp;pxor	($Zlo,&amp;QWP(16,"esp",$nlo,8));		# Z^=H[nlo]
	&amp;rol	($dat,8);				# next byte
	&amp;pxor	($Zhi,&amp;QWP(16+128,"esp",$nlo,8));

	&amp;pxor	($Zlo,$tmp);
	&amp;pxor	($Zhi,&amp;QWP(16+256+128,"esp",$nhi[0],8));
	&amp;xor	(&amp;LB($rem[1]),&amp;BP(0,"esp",$nhi[0]));	# rem^(H[nhi]&lt;&lt;4)
      } else {
	&amp;movq	($Zlo,&amp;QWP(16,"esp",$nlo,8));
	&amp;movq	($Zhi,&amp;QWP(16+128,"esp",$nlo,8));
      }

	&amp;mov	(&amp;LB($nlo),&amp;LB($dat));
	&amp;mov	($dat,&amp;DWP(528+$j,"esp"))		if (--$j%4==0);

	&amp;movd	($rem[0],$Zlo);
	&amp;movz	($rem[1],&amp;LB($rem[1]))			if ($i&gt;0);
	&amp;psrlq	($Zlo,8);				# Z&gt;&gt;=8

	&amp;movq	($tmp,$Zhi);
	&amp;mov	($nhi[0],$nlo);
	&amp;psrlq	($Zhi,8);

	&amp;pxor	($Zlo,&amp;QWP(16+256+0,"esp",$nhi[1],8));	# Z^=H[nhi]&gt;&gt;4
	&amp;and	(&amp;LB($nlo),0x0f);
	&amp;psllq	($tmp,56);

	&amp;pxor	($Zhi,$red[1])				if ($i&gt;1);
	&amp;shr	($nhi[0],4);
	&amp;pinsrw	($red[0],&amp;WP(0,$rem_8bit,$rem[1],2),2)	if ($i&gt;0);

	unshift	(@red,pop(@red));			# "rotate" registers
	unshift	(@rem,pop(@rem));
	unshift	(@nhi,pop(@nhi));
    }

    &amp;pxor	($Zlo,&amp;QWP(16,"esp",$nlo,8));		# Z^=H[nlo]
    &amp;pxor	($Zhi,&amp;QWP(16+128,"esp",$nlo,8));
    &amp;xor	(&amp;LB($rem[1]),&amp;BP(0,"esp",$nhi[0]));	# rem^(H[nhi]&lt;&lt;4)

    &amp;pxor	($Zlo,$tmp);
    &amp;pxor	($Zhi,&amp;QWP(16+256+128,"esp",$nhi[0],8));
    &amp;movz	($rem[1],&amp;LB($rem[1]));

    &amp;pxor	($red[2],$red[2]);			# clear 2nd word
    &amp;psllq	($red[1],4);

    &amp;movd	($rem[0],$Zlo);
    &amp;psrlq	($Zlo,4);				# Z&gt;&gt;=4

    &amp;movq	($tmp,$Zhi);
    &amp;psrlq	($Zhi,4);
    &amp;shl	($rem[0],4);				# rem&lt;&lt;4

    &amp;pxor	($Zlo,&amp;QWP(16,"esp",$nhi[1],8));	# Z^=H[nhi]
    &amp;psllq	($tmp,60);
    &amp;movz	($rem[0],&amp;LB($rem[0]));

    &amp;pxor	($Zlo,$tmp);
    &amp;pxor	($Zhi,&amp;QWP(16+128,"esp",$nhi[1],8));

    &amp;pinsrw	($red[0],&amp;WP(0,$rem_8bit,$rem[1],2),2);
    &amp;pxor	($Zhi,$red[1]);

    &amp;movd	($dat,$Zlo);
    &amp;pinsrw	($red[2],&amp;WP(0,$rem_8bit,$rem[0],2),3);	# last is &lt;&lt;48

    &amp;psllq	($red[0],12);				# correct by &lt;&lt;16&gt;&gt;4
    &amp;pxor	($Zhi,$red[0]);
    &amp;psrlq	($Zlo,32);
    &amp;pxor	($Zhi,$red[2]);

    &amp;mov	("ecx",&amp;DWP(528+16+4,"esp"));	# restore inp
    &amp;movd	("ebx",$Zlo);
    &amp;movq	($tmp,$Zhi);			# 01234567
    &amp;psllw	($Zhi,8);			# 1.3.5.7.
    &amp;psrlw	($tmp,8);			# .0.2.4.6
    &amp;por	($Zhi,$tmp);			# 10325476
    &amp;bswap	($dat);
    &amp;pshufw	($Zhi,$Zhi,0b00011011);		# 76543210
    &amp;bswap	("ebx");

    &amp;cmp	("ecx",&amp;DWP(528+16+8,"esp"));	# are we done?
    &amp;jne	(&amp;label("outer"));
  }

    &amp;mov	("eax",&amp;DWP(528+16+0,"esp"));	# restore Xi
    &amp;mov	(&amp;DWP(12,"eax"),"edx");
    &amp;mov	(&amp;DWP(8,"eax"),"ebx");
    &amp;movq	(&amp;QWP(0,"eax"),$Zhi);

    &amp;mov	("esp",&amp;DWP(528+16+12,"esp"));	# restore original %esp
    &amp;emms	();
}
&amp;function_end("gcm_ghash_4bit_mmx");
}}

if ($sse2) {{
######################################################################
# PCLMULQDQ version.

$Xip="eax";
$Htbl="edx";
$const="ecx";
$inp="esi";
$len="ebx";

($Xi,$Xhi)=("xmm0","xmm1");	$Hkey="xmm2";
($T1,$T2,$T3)=("xmm3","xmm4","xmm5");
($Xn,$Xhn)=("xmm6","xmm7");

&amp;static_label("bswap");

sub clmul64x64_T2 {	# minimal "register" pressure
my ($Xhi,$Xi,$Hkey,$HK)=@_;

	&amp;movdqa		($Xhi,$Xi);		#
	&amp;pshufd		($T1,$Xi,0b01001110);
	&amp;pshufd		($T2,$Hkey,0b01001110)	if (!defined($HK));
	&amp;pxor		($T1,$Xi);		#
	&amp;pxor		($T2,$Hkey)		if (!defined($HK));
			$HK=$T2			if (!defined($HK));

	&amp;pclmulqdq	($Xi,$Hkey,0x00);	#######
	&amp;pclmulqdq	($Xhi,$Hkey,0x11);	#######
	&amp;pclmulqdq	($T1,$HK,0x00);		#######
	&amp;xorps		($T1,$Xi);		#
	&amp;xorps		($T1,$Xhi);		#

	&amp;movdqa		($T2,$T1);		#
	&amp;psrldq		($T1,8);
	&amp;pslldq		($T2,8);		#
	&amp;pxor		($Xhi,$T1);
	&amp;pxor		($Xi,$T2);		#
}

sub clmul64x64_T3 {
# Even though this subroutine offers visually better ILP, it
# was empirically found to be a tad slower than above version.
# At least in gcm_ghash_clmul context. But it's just as well,
# because loop modulo-scheduling is possible only thanks to
# minimized "register" pressure...
my ($Xhi,$Xi,$Hkey)=@_;

	&amp;movdqa		($T1,$Xi);		#
	&amp;movdqa		($Xhi,$Xi);
	&amp;pclmulqdq	($Xi,$Hkey,0x00);	#######
	&amp;pclmulqdq	($Xhi,$Hkey,0x11);	#######
	&amp;pshufd		($T2,$T1,0b01001110);	#
	&amp;pshufd		($T3,$Hkey,0b01001110);
	&amp;pxor		($T2,$T1);		#
	&amp;pxor		($T3,$Hkey);
	&amp;pclmulqdq	($T2,$T3,0x00);		#######
	&amp;pxor		($T2,$Xi);		#
	&amp;pxor		($T2,$Xhi);		#

	&amp;movdqa		($T3,$T2);		#
	&amp;psrldq		($T2,8);
	&amp;pslldq		($T3,8);		#
	&amp;pxor		($Xhi,$T2);
	&amp;pxor		($Xi,$T3);		#
}

if (1) {		# Algorithm 9 with &lt;&lt;1 twist.
			# Reduction is shorter and uses only two
			# temporary registers, which makes it better
			# candidate for interleaving with 64x64
			# multiplication. Pre-modulo-scheduled loop
			# was found to be ~20% faster than Algorithm 5
			# below. Algorithm 9 was therefore chosen for
			# further optimization...

sub reduction_alg9 {	# 17/11 times faster than Intel version
my ($Xhi,$Xi) = @_;

	# 1st phase
	&amp;movdqa		($T2,$Xi);		#
	&amp;movdqa		($T1,$Xi);
	&amp;psllq		($Xi,5);
	&amp;pxor		($T1,$Xi);		#
	&amp;psllq		($Xi,1);
	&amp;pxor		($Xi,$T1);		#
	&amp;psllq		($Xi,57);		#
	&amp;movdqa		($T1,$Xi);		#
	&amp;pslldq		($Xi,8);
	&amp;psrldq		($T1,8);		#
	&amp;pxor		($Xi,$T2);
	&amp;pxor		($Xhi,$T1);		#

	# 2nd phase
	&amp;movdqa		($T2,$Xi);
	&amp;psrlq		($Xi,1);
	&amp;pxor		($Xhi,$T2);		#
	&amp;pxor		($T2,$Xi);
	&amp;psrlq		($Xi,5);
	&amp;pxor		($Xi,$T2);		#
	&amp;psrlq		($Xi,1);		#
	&amp;pxor		($Xi,$Xhi)		#
}

&amp;function_begin_B("gcm_init_clmul");
	&amp;mov		($Htbl,&amp;wparam(0));
	&amp;mov		($Xip,&amp;wparam(1));

	&amp;call		(&amp;label("pic"));
&amp;set_label("pic");
	&amp;blindpop	($const);
	&amp;lea		($const,&amp;DWP(&amp;label("bswap")."-".&amp;label("pic"),$const));

	&amp;movdqu		($Hkey,&amp;QWP(0,$Xip));
	&amp;pshufd		($Hkey,$Hkey,0b01001110);# dword swap

	# &lt;&lt;1 twist
	&amp;pshufd		($T2,$Hkey,0b11111111);	# broadcast uppermost dword
	&amp;movdqa		($T1,$Hkey);
	&amp;psllq		($Hkey,1);
	&amp;pxor		($T3,$T3);		#
	&amp;psrlq		($T1,63);
	&amp;pcmpgtd	($T3,$T2);		# broadcast carry bit
	&amp;pslldq		($T1,8);
	&amp;por		($Hkey,$T1);		# H&lt;&lt;=1

	# magic reduction
	&amp;pand		($T3,&amp;QWP(16,$const));	# 0x1c2_polynomial
	&amp;pxor		($Hkey,$T3);		# if(carry) H^=0x1c2_polynomial

	# calculate H^2
	&amp;movdqa		($Xi,$Hkey);
	&amp;clmul64x64_T2	($Xhi,$Xi,$Hkey);
	&amp;reduction_alg9	($Xhi,$Xi);

	&amp;pshufd		($T1,$Hkey,0b01001110);
	&amp;pshufd		($T2,$Xi,0b01001110);
	&amp;pxor		($T1,$Hkey);		# Karatsuba pre-processing
	&amp;movdqu		(&amp;QWP(0,$Htbl),$Hkey);	# save H
	&amp;pxor		($T2,$Xi);		# Karatsuba pre-processing
	&amp;movdqu		(&amp;QWP(16,$Htbl),$Xi);	# save H^2
	&amp;palignr	($T2,$T1,8);		# low part is H.lo^H.hi
	&amp;movdqu		(&amp;QWP(32,$Htbl),$T2);	# save Karatsuba "salt"

	&amp;ret		();
&amp;function_end_B("gcm_init_clmul");

&amp;function_begin_B("gcm_gmult_clmul");
	&amp;mov		($Xip,&amp;wparam(0));
	&amp;mov		($Htbl,&amp;wparam(1));

	&amp;call		(&amp;label("pic"));
&amp;set_label("pic");
	&amp;blindpop	($const);
	&amp;lea		($const,&amp;DWP(&amp;label("bswap")."-".&amp;label("pic"),$const));

	&amp;movdqu		($Xi,&amp;QWP(0,$Xip));
	&amp;movdqa		($T3,&amp;QWP(0,$const));
	&amp;movups		($Hkey,&amp;QWP(0,$Htbl));
	&amp;pshufb		($Xi,$T3);
	&amp;movups		($T2,&amp;QWP(32,$Htbl));

	&amp;clmul64x64_T2	($Xhi,$Xi,$Hkey,$T2);
	&amp;reduction_alg9	($Xhi,$Xi);

	&amp;pshufb		($Xi,$T3);
	&amp;movdqu		(&amp;QWP(0,$Xip),$Xi);

	&amp;ret	();
&amp;function_end_B("gcm_gmult_clmul");

&amp;function_begin("gcm_ghash_clmul");
	&amp;mov		($Xip,&amp;wparam(0));
	&amp;mov		($Htbl,&amp;wparam(1));
	&amp;mov		($inp,&amp;wparam(2));
	&amp;mov		($len,&amp;wparam(3));

	&amp;call		(&amp;label("pic"));
&amp;set_label("pic");
	&amp;blindpop	($const);
	&amp;lea		($const,&amp;DWP(&amp;label("bswap")."-".&amp;label("pic"),$const));

	&amp;movdqu		($Xi,&amp;QWP(0,$Xip));
	&amp;movdqa		($T3,&amp;QWP(0,$const));
	&amp;movdqu		($Hkey,&amp;QWP(0,$Htbl));
	&amp;pshufb		($Xi,$T3);

	&amp;sub		($len,0x10);
	&amp;jz		(&amp;label("odd_tail"));

	#######
	# Xi+2 =[H*(Ii+1 + Xi+1)] mod P =
	#	[(H*Ii+1) + (H*Xi+1)] mod P =
	#	[(H*Ii+1) + H^2*(Ii+Xi)] mod P
	#
	&amp;movdqu		($T1,&amp;QWP(0,$inp));	# Ii
	&amp;movdqu		($Xn,&amp;QWP(16,$inp));	# Ii+1
	&amp;pshufb		($T1,$T3);
	&amp;pshufb		($Xn,$T3);
	&amp;movdqu		($T3,&amp;QWP(32,$Htbl));
	&amp;pxor		($Xi,$T1);		# Ii+Xi

	&amp;pshufd		($T1,$Xn,0b01001110);	# H*Ii+1
	&amp;movdqa		($Xhn,$Xn);
	&amp;pxor		($T1,$Xn);		#
	&amp;lea		($inp,&amp;DWP(32,$inp));	# i+=2

	&amp;pclmulqdq	($Xn,$Hkey,0x00);	#######
	&amp;pclmulqdq	($Xhn,$Hkey,0x11);	#######
	&amp;pclmulqdq	($T1,$T3,0x00);		#######
	&amp;movups		($Hkey,&amp;QWP(16,$Htbl));	# load H^2
	&amp;nop		();

	&amp;sub		($len,0x20);
	&amp;jbe		(&amp;label("even_tail"));
	&amp;jmp		(&amp;label("mod_loop"));

&amp;set_label("mod_loop",32);
	&amp;pshufd		($T2,$Xi,0b01001110);	# H^2*(Ii+Xi)
	&amp;movdqa		($Xhi,$Xi);
	&amp;pxor		($T2,$Xi);		#
	&amp;nop		();

	&amp;pclmulqdq	($Xi,$Hkey,0x00);	#######
	&amp;pclmulqdq	($Xhi,$Hkey,0x11);	#######
	&amp;pclmulqdq	($T2,$T3,0x10);		#######
	&amp;movups		($Hkey,&amp;QWP(0,$Htbl));	# load H

	&amp;xorps		($Xi,$Xn);		# (H*Ii+1) + H^2*(Ii+Xi)
	&amp;movdqa		($T3,&amp;QWP(0,$const));
	&amp;xorps		($Xhi,$Xhn);
	 &amp;movdqu	($Xhn,&amp;QWP(0,$inp));	# Ii
	&amp;pxor		($T1,$Xi);		# aggregated Karatsuba post-processing
	 &amp;movdqu	($Xn,&amp;QWP(16,$inp));	# Ii+1
	&amp;pxor		($T1,$Xhi);		#

	 &amp;pshufb	($Xhn,$T3);
	&amp;pxor		($T2,$T1);		#

	&amp;movdqa		($T1,$T2);		#
	&amp;psrldq		($T2,8);
	&amp;pslldq		($T1,8);		#
	&amp;pxor		($Xhi,$T2);
	&amp;pxor		($Xi,$T1);		#
	 &amp;pshufb	($Xn,$T3);
	 &amp;pxor		($Xhi,$Xhn);		# "Ii+Xi", consume early

	&amp;movdqa		($Xhn,$Xn);		#&amp;clmul64x64_TX	($Xhn,$Xn,$Hkey); H*Ii+1
	  &amp;movdqa	($T2,$Xi);		#&amp;reduction_alg9($Xhi,$Xi); 1st phase
	  &amp;movdqa	($T1,$Xi);
	  &amp;psllq	($Xi,5);
	  &amp;pxor		($T1,$Xi);		#
	  &amp;psllq	($Xi,1);
	  &amp;pxor		($Xi,$T1);		#
	&amp;pclmulqdq	($Xn,$Hkey,0x00);	#######
	&amp;movups		($T3,&amp;QWP(32,$Htbl));
	  &amp;psllq	($Xi,57);		#
	  &amp;movdqa	($T1,$Xi);		#
	  &amp;pslldq	($Xi,8);
	  &amp;psrldq	($T1,8);		#
	  &amp;pxor		($Xi,$T2);
	  &amp;pxor		($Xhi,$T1);		#
	&amp;pshufd		($T1,$Xhn,0b01001110);
	  &amp;movdqa	($T2,$Xi);		# 2nd phase
	  &amp;psrlq	($Xi,1);
	&amp;pxor		($T1,$Xhn);
	  &amp;pxor		($Xhi,$T2);		#
	&amp;pclmulqdq	($Xhn,$Hkey,0x11);	#######
	&amp;movups		($Hkey,&amp;QWP(16,$Htbl));	# load H^2
	  &amp;pxor		($T2,$Xi);
	  &amp;psrlq	($Xi,5);
	  &amp;pxor		($Xi,$T2);		#
	  &amp;psrlq	($Xi,1);		#
	  &amp;pxor		($Xi,$Xhi)		#
	&amp;pclmulqdq	($T1,$T3,0x00);		#######

	&amp;lea		($inp,&amp;DWP(32,$inp));
	&amp;sub		($len,0x20);
	&amp;ja		(&amp;label("mod_loop"));

&amp;set_label("even_tail");
	&amp;pshufd		($T2,$Xi,0b01001110);	# H^2*(Ii+Xi)
	&amp;movdqa		($Xhi,$Xi);
	&amp;pxor		($T2,$Xi);		#

	&amp;pclmulqdq	($Xi,$Hkey,0x00);	#######
	&amp;pclmulqdq	($Xhi,$Hkey,0x11);	#######
	&amp;pclmulqdq	($T2,$T3,0x10);		#######
	&amp;movdqa		($T3,&amp;QWP(0,$const));

	&amp;xorps		($Xi,$Xn);		# (H*Ii+1) + H^2*(Ii+Xi)
	&amp;xorps		($Xhi,$Xhn);
	&amp;pxor		($T1,$Xi);		# aggregated Karatsuba post-processing
	&amp;pxor		($T1,$Xhi);		#

	&amp;pxor		($T2,$T1);		#

	&amp;movdqa		($T1,$T2);		#
	&amp;psrldq		($T2,8);
	&amp;pslldq		($T1,8);		#
	&amp;pxor		($Xhi,$T2);
	&amp;pxor		($Xi,$T1);		#

	&amp;reduction_alg9	($Xhi,$Xi);

	&amp;test		($len,$len);
	&amp;jnz		(&amp;label("done"));

	&amp;movups		($Hkey,&amp;QWP(0,$Htbl));	# load H
&amp;set_label("odd_tail");
	&amp;movdqu		($T1,&amp;QWP(0,$inp));	# Ii
	&amp;pshufb		($T1,$T3);
	&amp;pxor		($Xi,$T1);		# Ii+Xi

	&amp;clmul64x64_T2	($Xhi,$Xi,$Hkey);	# H*(Ii+Xi)
	&amp;reduction_alg9	($Xhi,$Xi);

&amp;set_label("done");
	&amp;pshufb		($Xi,$T3);
	&amp;movdqu		(&amp;QWP(0,$Xip),$Xi);
&amp;function_end("gcm_ghash_clmul");

} else {		# Algorithm 5. Kept for reference purposes.

sub reduction_alg5 {	# 19/16 times faster than Intel version
my ($Xhi,$Xi)=@_;

	# &lt;&lt;1
	&amp;movdqa		($T1,$Xi);		#
	&amp;movdqa		($T2,$Xhi);
	&amp;pslld		($Xi,1);
	&amp;pslld		($Xhi,1);		#
	&amp;psrld		($T1,31);
	&amp;psrld		($T2,31);		#
	&amp;movdqa		($T3,$T1);
	&amp;pslldq		($T1,4);
	&amp;psrldq		($T3,12);		#
	&amp;pslldq		($T2,4);
	&amp;por		($Xhi,$T3);		#
	&amp;por		($Xi,$T1);
	&amp;por		($Xhi,$T2);		#

	# 1st phase
	&amp;movdqa		($T1,$Xi);
	&amp;movdqa		($T2,$Xi);
	&amp;movdqa		($T3,$Xi);		#
	&amp;pslld		($T1,31);
	&amp;pslld		($T2,30);
	&amp;pslld		($Xi,25);		#
	&amp;pxor		($T1,$T2);
	&amp;pxor		($T1,$Xi);		#
	&amp;movdqa		($T2,$T1);		#
	&amp;pslldq		($T1,12);
	&amp;psrldq		($T2,4);		#
	&amp;pxor		($T3,$T1);

	# 2nd phase
	&amp;pxor		($Xhi,$T3);		#
	&amp;movdqa		($Xi,$T3);
	&amp;movdqa		($T1,$T3);
	&amp;psrld		($Xi,1);		#
	&amp;psrld		($T1,2);
	&amp;psrld		($T3,7);		#
	&amp;pxor		($Xi,$T1);
	&amp;pxor		($Xhi,$T2);
	&amp;pxor		($Xi,$T3);		#
	&amp;pxor		($Xi,$Xhi);		#
}

&amp;function_begin_B("gcm_init_clmul");
	&amp;mov		($Htbl,&amp;wparam(0));
	&amp;mov		($Xip,&amp;wparam(1));

	&amp;call		(&amp;label("pic"));
&amp;set_label("pic");
	&amp;blindpop	($const);
	&amp;lea		($const,&amp;DWP(&amp;label("bswap")."-".&amp;label("pic"),$const));

	&amp;movdqu		($Hkey,&amp;QWP(0,$Xip));
	&amp;pshufd		($Hkey,$Hkey,0b01001110);# dword swap

	# calculate H^2
	&amp;movdqa		($Xi,$Hkey);
	&amp;clmul64x64_T3	($Xhi,$Xi,$Hkey);
	&amp;reduction_alg5	($Xhi,$Xi);

	&amp;movdqu		(&amp;QWP(0,$Htbl),$Hkey);	# save H
	&amp;movdqu		(&amp;QWP(16,$Htbl),$Xi);	# save H^2

	&amp;ret		();
&amp;function_end_B("gcm_init_clmul");

&amp;function_begin_B("gcm_gmult_clmul");
	&amp;mov		($Xip,&amp;wparam(0));
	&amp;mov		($Htbl,&amp;wparam(1));

	&amp;call		(&amp;label("pic"));
&amp;set_label("pic");
	&amp;blindpop	($const);
	&amp;lea		($const,&amp;DWP(&amp;label("bswap")."-".&amp;label("pic"),$const));

	&amp;movdqu		($Xi,&amp;QWP(0,$Xip));
	&amp;movdqa		($Xn,&amp;QWP(0,$const));
	&amp;movdqu		($Hkey,&amp;QWP(0,$Htbl));
	&amp;pshufb		($Xi,$Xn);

	&amp;clmul64x64_T3	($Xhi,$Xi,$Hkey);
	&amp;reduction_alg5	($Xhi,$Xi);

	&amp;pshufb		($Xi,$Xn);
	&amp;movdqu		(&amp;QWP(0,$Xip),$Xi);

	&amp;ret	();
&amp;function_end_B("gcm_gmult_clmul");

&amp;function_begin("gcm_ghash_clmul");
	&amp;mov		($Xip,&amp;wparam(0));
	&amp;mov		($Htbl,&amp;wparam(1));
	&amp;mov		($inp,&amp;wparam(2));
	&amp;mov		($len,&amp;wparam(3));

	&amp;call		(&amp;label("pic"));
&amp;set_label("pic");
	&amp;blindpop	($const);
	&amp;lea		($const,&amp;DWP(&amp;label("bswap")."-".&amp;label("pic"),$const));

	&amp;movdqu		($Xi,&amp;QWP(0,$Xip));
	&amp;movdqa		($T3,&amp;QWP(0,$const));
	&amp;movdqu		($Hkey,&amp;QWP(0,$Htbl));
	&amp;pshufb		($Xi,$T3);

	&amp;sub		($len,0x10);
	&amp;jz		(&amp;label("odd_tail"));

	#######
	# Xi+2 =[H*(Ii+1 + Xi+1)] mod P =
	#	[(H*Ii+1) + (H*Xi+1)] mod P =
	#	[(H*Ii+1) + H^2*(Ii+Xi)] mod P
	#
	&amp;movdqu		($T1,&amp;QWP(0,$inp));	# Ii
	&amp;movdqu		($Xn,&amp;QWP(16,$inp));	# Ii+1
	&amp;pshufb		($T1,$T3);
	&amp;pshufb		($Xn,$T3);
	&amp;pxor		($Xi,$T1);		# Ii+Xi

	&amp;clmul64x64_T3	($Xhn,$Xn,$Hkey);	# H*Ii+1
	&amp;movdqu		($Hkey,&amp;QWP(16,$Htbl));	# load H^2

	&amp;sub		($len,0x20);
	&amp;lea		($inp,&amp;DWP(32,$inp));	# i+=2
	&amp;jbe		(&amp;label("even_tail"));

&amp;set_label("mod_loop");
	&amp;clmul64x64_T3	($Xhi,$Xi,$Hkey);	# H^2*(Ii+Xi)
	&amp;movdqu		($Hkey,&amp;QWP(0,$Htbl));	# load H

	&amp;pxor		($Xi,$Xn);		# (H*Ii+1) + H^2*(Ii+Xi)
	&amp;pxor		($Xhi,$Xhn);

	&amp;reduction_alg5	($Xhi,$Xi);

	#######
	&amp;movdqa		($T3,&amp;QWP(0,$const));
	&amp;movdqu		($T1,&amp;QWP(0,$inp));	# Ii
	&amp;movdqu		($Xn,&amp;QWP(16,$inp));	# Ii+1
	&amp;pshufb		($T1,$T3);
	&amp;pshufb		($Xn,$T3);
	&amp;pxor		($Xi,$T1);		# Ii+Xi

	&amp;clmul64x64_T3	($Xhn,$Xn,$Hkey);	# H*Ii+1
	&amp;movdqu		($Hkey,&amp;QWP(16,$Htbl));	# load H^2

	&amp;sub		($len,0x20);
	&amp;lea		($inp,&amp;DWP(32,$inp));
	&amp;ja		(&amp;label("mod_loop"));

&amp;set_label("even_tail");
	&amp;clmul64x64_T3	($Xhi,$Xi,$Hkey);	# H^2*(Ii+Xi)

	&amp;pxor		($Xi,$Xn);		# (H*Ii+1) + H^2*(Ii+Xi)
	&amp;pxor		($Xhi,$Xhn);

	&amp;reduction_alg5	($Xhi,$Xi);

	&amp;movdqa		($T3,&amp;QWP(0,$const));
	&amp;test		($len,$len);
	&amp;jnz		(&amp;label("done"));

	&amp;movdqu		($Hkey,&amp;QWP(0,$Htbl));	# load H
&amp;set_label("odd_tail");
	&amp;movdqu		($T1,&amp;QWP(0,$inp));	# Ii
	&amp;pshufb		($T1,$T3);
	&amp;pxor		($Xi,$T1);		# Ii+Xi

	&amp;clmul64x64_T3	($Xhi,$Xi,$Hkey);	# H*(Ii+Xi)
	&amp;reduction_alg5	($Xhi,$Xi);

	&amp;movdqa		($T3,&amp;QWP(0,$const));
&amp;set_label("done");
	&amp;pshufb		($Xi,$T3);
	&amp;movdqu		(&amp;QWP(0,$Xip),$Xi);
&amp;function_end("gcm_ghash_clmul");

}

&amp;set_label("bswap",64);
	&amp;data_byte(15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0);
	&amp;data_byte(1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xc2);	# 0x1c2_polynomial
&amp;set_label("rem_8bit",64);
	&amp;data_short(0x0000,0x01C2,0x0384,0x0246,0x0708,0x06CA,0x048C,0x054E);
	&amp;data_short(0x0E10,0x0FD2,0x0D94,0x0C56,0x0918,0x08DA,0x0A9C,0x0B5E);
	&amp;data_short(0x1C20,0x1DE2,0x1FA4,0x1E66,0x1B28,0x1AEA,0x18AC,0x196E);
	&amp;data_short(0x1230,0x13F2,0x11B4,0x1076,0x1538,0x14FA,0x16BC,0x177E);
	&amp;data_short(0x3840,0x3982,0x3BC4,0x3A06,0x3F48,0x3E8A,0x3CCC,0x3D0E);
	&amp;data_short(0x3650,0x3792,0x35D4,0x3416,0x3158,0x309A,0x32DC,0x331E);
	&amp;data_short(0x2460,0x25A2,0x27E4,0x2626,0x2368,0x22AA,0x20EC,0x212E);
	&amp;data_short(0x2A70,0x2BB2,0x29F4,0x2836,0x2D78,0x2CBA,0x2EFC,0x2F3E);
	&amp;data_short(0x7080,0x7142,0x7304,0x72C6,0x7788,0x764A,0x740C,0x75CE);
	&amp;data_short(0x7E90,0x7F52,0x7D14,0x7CD6,0x7998,0x785A,0x7A1C,0x7BDE);
	&amp;data_short(0x6CA0,0x6D62,0x6F24,0x6EE6,0x6BA8,0x6A6A,0x682C,0x69EE);
	&amp;data_short(0x62B0,0x6372,0x6134,0x60F6,0x65B8,0x647A,0x663C,0x67FE);
	&amp;data_short(0x48C0,0x4902,0x4B44,0x4A86,0x4FC8,0x4E0A,0x4C4C,0x4D8E);
	&amp;data_short(0x46D0,0x4712,0x4554,0x4496,0x41D8,0x401A,0x425C,0x439E);
	&amp;data_short(0x54E0,0x5522,0x5764,0x56A6,0x53E8,0x522A,0x506C,0x51AE);
	&amp;data_short(0x5AF0,0x5B32,0x5974,0x58B6,0x5DF8,0x5C3A,0x5E7C,0x5FBE);
	&amp;data_short(0xE100,0xE0C2,0xE284,0xE346,0xE608,0xE7CA,0xE58C,0xE44E);
	&amp;data_short(0xEF10,0xEED2,0xEC94,0xED56,0xE818,0xE9DA,0xEB9C,0xEA5E);
	&amp;data_short(0xFD20,0xFCE2,0xFEA4,0xFF66,0xFA28,0xFBEA,0xF9AC,0xF86E);
	&amp;data_short(0xF330,0xF2F2,0xF0B4,0xF176,0xF438,0xF5FA,0xF7BC,0xF67E);
	&amp;data_short(0xD940,0xD882,0xDAC4,0xDB06,0xDE48,0xDF8A,0xDDCC,0xDC0E);
	&amp;data_short(0xD750,0xD692,0xD4D4,0xD516,0xD058,0xD19A,0xD3DC,0xD21E);
	&amp;data_short(0xC560,0xC4A2,0xC6E4,0xC726,0xC268,0xC3AA,0xC1EC,0xC02E);
	&amp;data_short(0xCB70,0xCAB2,0xC8F4,0xC936,0xCC78,0xCDBA,0xCFFC,0xCE3E);
	&amp;data_short(0x9180,0x9042,0x9204,0x93C6,0x9688,0x974A,0x950C,0x94CE);
	&amp;data_short(0x9F90,0x9E52,0x9C14,0x9DD6,0x9898,0x995A,0x9B1C,0x9ADE);
	&amp;data_short(0x8DA0,0x8C62,0x8E24,0x8FE6,0x8AA8,0x8B6A,0x892C,0x88EE);
	&amp;data_short(0x83B0,0x8272,0x8034,0x81F6,0x84B8,0x857A,0x873C,0x86FE);
	&amp;data_short(0xA9C0,0xA802,0xAA44,0xAB86,0xAEC8,0xAF0A,0xAD4C,0xAC8E);
	&amp;data_short(0xA7D0,0xA612,0xA454,0xA596,0xA0D8,0xA11A,0xA35C,0xA29E);
	&amp;data_short(0xB5E0,0xB422,0xB664,0xB7A6,0xB2E8,0xB32A,0xB16C,0xB0AE);
	&amp;data_short(0xBBF0,0xBA32,0xB874,0xB9B6,0xBCF8,0xBD3A,0xBF7C,0xBEBE);
}}	# $sse2

&amp;set_label("rem_4bit",64);
	&amp;data_word(0,0x0000&lt;&lt;$S,0,0x1C20&lt;&lt;$S,0,0x3840&lt;&lt;$S,0,0x2460&lt;&lt;$S);
	&amp;data_word(0,0x7080&lt;&lt;$S,0,0x6CA0&lt;&lt;$S,0,0x48C0&lt;&lt;$S,0,0x54E0&lt;&lt;$S);
	&amp;data_word(0,0xE100&lt;&lt;$S,0,0xFD20&lt;&lt;$S,0,0xD940&lt;&lt;$S,0,0xC560&lt;&lt;$S);
	&amp;data_word(0,0x9180&lt;&lt;$S,0,0x8DA0&lt;&lt;$S,0,0xA9C0&lt;&lt;$S,0,0xB5E0&lt;&lt;$S);
}}}	# !$x86only

&amp;asciz("GHASH for x86, CRYPTOGAMS by &lt;appro\@openssl.org&gt;");
&amp;asm_finish();

close STDOUT;

# A question was risen about choice of vanilla MMX. Or rather why wasn't
# SSE2 chosen instead? In addition to the fact that MMX runs on legacy
# CPUs such as PIII, "4-bit" MMX version was observed to provide better
# performance than *corresponding* SSE2 one even on contemporary CPUs.
# SSE2 results were provided by Peter-Michael Hager. He maintains SSE2
# implementation featuring full range of lookup-table sizes, but with
# per-invocation lookup table setup. Latter means that table size is
# chosen depending on how much data is to be hashed in every given call,
# more data - larger table. Best reported result for Core2 is ~4 cycles
# per processed byte out of 64KB block. This number accounts even for
# 64KB table setup overhead. As discussed in gcm128.c we choose to be
# more conservative in respect to lookup table sizes, but how do the
# results compare? Minimalistic "256B" MMX version delivers ~11 cycles
# on same platform. As also discussed in gcm128.c, next in line "8-bit
# Shoup's" or "4KB" method should deliver twice the performance of
# "256B" one, in other words not worse than ~6 cycles per byte. It
# should be also be noted that in SSE2 case improvement can be "super-
# linear," i.e. more than twice, mostly because &gt;&gt;8 maps to single
# instruction on SSE2 register. This is unlike "4-bit" case when &gt;&gt;4
# maps to same amount of instructions in both MMX and SSE2 cases.
# Bottom line is that switch to SSE2 is considered to be justifiable
# only in case we choose to implement "8-bit" method...
</pre></body></html>