<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 2011-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/.
# ====================================================================
#
# May 2011
#
# The module implements bn_GF2m_mul_2x2 polynomial multiplication used
# in bn_gf2m.c. It's kind of low-hanging mechanical port from C for
# the time being... Except that it has three code paths: pure integer
# code suitable for any x86 CPU, MMX code suitable for PIII and later
# and PCLMULQDQ suitable for Westmere and later. Improvement varies
# from one benchmark and 碌-arch to another. Below are interval values
# for 163- and 571-bit ECDH benchmarks relative to compiler-generated
# code:
#
# PIII		16%-30%
# P4		12%-12%
# Opteron	18%-40%
# Core2		19%-44%
# Atom		38%-64%
# Westmere	53%-121%(PCLMULQDQ)/20%-32%(MMX)
# Sandy Bridge	72%-127%(PCLMULQDQ)/27%-23%(MMX)
#
# Note that above improvement coefficients are not coefficients for
# bn_GF2m_mul_2x2 itself. For example 120% ECDH improvement is result
# of bn_GF2m_mul_2x2 being &gt;4x faster. As it gets faster, benchmark
# is more and more dominated by other subroutines, most notably by
# BN_GF2m_mod[_mul]_arr...

$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/); }

&amp;external_label("OPENSSL_ia32cap_P") if ($sse2);

$a="eax";
$b="ebx";
($a1,$a2,$a4)=("ecx","edx","ebp");

$R="mm0";
@T=("mm1","mm2");
($A,$B,$B30,$B31)=("mm2","mm3","mm4","mm5");
@i=("esi","edi");

					if (!$x86only) {
&amp;function_begin_B("_mul_1x1_mmx");
	&amp;sub	("esp",32+4);
	 &amp;mov	($a1,$a);
	 &amp;lea	($a2,&amp;DWP(0,$a,$a));
	 &amp;and	($a1,0x3fffffff);
	 &amp;lea	($a4,&amp;DWP(0,$a2,$a2));
	 &amp;mov	(&amp;DWP(0*4,"esp"),0);
	 &amp;and	($a2,0x7fffffff);
	&amp;movd	($A,$a);
	&amp;movd	($B,$b);
	 &amp;mov	(&amp;DWP(1*4,"esp"),$a1);	# a1
	 &amp;xor	($a1,$a2);		# a1^a2
	&amp;pxor	($B31,$B31);
	&amp;pxor	($B30,$B30);
	 &amp;mov	(&amp;DWP(2*4,"esp"),$a2);	# a2
	 &amp;xor	($a2,$a4);		# a2^a4
	 &amp;mov	(&amp;DWP(3*4,"esp"),$a1);	# a1^a2
	&amp;pcmpgtd($B31,$A);		# broadcast 31st bit
	&amp;paddd	($A,$A);		# $A&lt;&lt;=1
	 &amp;xor	($a1,$a2);		# a1^a4=a1^a2^a2^a4
	 &amp;mov	(&amp;DWP(4*4,"esp"),$a4);	# a4
	 &amp;xor	($a4,$a2);		# a2=a4^a2^a4
	&amp;pand	($B31,$B);
	&amp;pcmpgtd($B30,$A);		# broadcast 30th bit
	 &amp;mov	(&amp;DWP(5*4,"esp"),$a1);	# a1^a4
	 &amp;xor	($a4,$a1);		# a1^a2^a4
	&amp;psllq	($B31,31);
	&amp;pand	($B30,$B);
	 &amp;mov	(&amp;DWP(6*4,"esp"),$a2);	# a2^a4
	&amp;mov	(@i[0],0x7);
	 &amp;mov	(&amp;DWP(7*4,"esp"),$a4);	# a1^a2^a4
	 &amp;mov	($a4,@i[0]);
	&amp;and	(@i[0],$b);
	&amp;shr	($b,3);
	&amp;mov	(@i[1],$a4);
	&amp;psllq	($B30,30);
	&amp;and	(@i[1],$b);
	&amp;shr	($b,3);
	&amp;movd	($R,&amp;DWP(0,"esp",@i[0],4));
	&amp;mov	(@i[0],$a4);
	&amp;and	(@i[0],$b);
	&amp;shr	($b,3);
	for($n=1;$n&lt;9;$n++) {
		&amp;movd	(@T[1],&amp;DWP(0,"esp",@i[1],4));
		&amp;mov	(@i[1],$a4);
		&amp;psllq	(@T[1],3*$n);
		&amp;and	(@i[1],$b);
		&amp;shr	($b,3);
		&amp;pxor	($R,@T[1]);

		push(@i,shift(@i)); push(@T,shift(@T));
	}
	&amp;movd	(@T[1],&amp;DWP(0,"esp",@i[1],4));
	&amp;pxor	($R,$B30);
	&amp;psllq	(@T[1],3*$n++);
	&amp;pxor	($R,@T[1]);

	&amp;movd	(@T[0],&amp;DWP(0,"esp",@i[0],4));
	&amp;pxor	($R,$B31);
	&amp;psllq	(@T[0],3*$n);
	&amp;add	("esp",32+4);
	&amp;pxor	($R,@T[0]);
	&amp;ret	();
&amp;function_end_B("_mul_1x1_mmx");
					}

($lo,$hi)=("eax","edx");
@T=("ecx","ebp");

&amp;function_begin_B("_mul_1x1_ialu");
	&amp;sub	("esp",32+4);
	 &amp;mov	($a1,$a);
	 &amp;lea	($a2,&amp;DWP(0,$a,$a));
	 &amp;lea	($a4,&amp;DWP(0,"",$a,4));
	 &amp;and	($a1,0x3fffffff);
	&amp;lea	(@i[1],&amp;DWP(0,$lo,$lo));
	&amp;sar	($lo,31);		# broadcast 31st bit
	 &amp;mov	(&amp;DWP(0*4,"esp"),0);
	 &amp;and	($a2,0x7fffffff);
	 &amp;mov	(&amp;DWP(1*4,"esp"),$a1);	# a1
	 &amp;xor	($a1,$a2);		# a1^a2
	 &amp;mov	(&amp;DWP(2*4,"esp"),$a2);	# a2
	 &amp;xor	($a2,$a4);		# a2^a4
	 &amp;mov	(&amp;DWP(3*4,"esp"),$a1);	# a1^a2
	 &amp;xor	($a1,$a2);		# a1^a4=a1^a2^a2^a4
	 &amp;mov	(&amp;DWP(4*4,"esp"),$a4);	# a4
	 &amp;xor	($a4,$a2);		# a2=a4^a2^a4
	 &amp;mov	(&amp;DWP(5*4,"esp"),$a1);	# a1^a4
	 &amp;xor	($a4,$a1);		# a1^a2^a4
	&amp;sar	(@i[1],31);		# broadcast 30th bit
	&amp;and	($lo,$b);
	 &amp;mov	(&amp;DWP(6*4,"esp"),$a2);	# a2^a4
	&amp;and	(@i[1],$b);
	 &amp;mov	(&amp;DWP(7*4,"esp"),$a4);	# a1^a2^a4
	&amp;mov	($hi,$lo);
	&amp;shl	($lo,31);
	&amp;mov	(@T[0],@i[1]);
	&amp;shr	($hi,1);

	 &amp;mov	(@i[0],0x7);
	&amp;shl	(@i[1],30);
	 &amp;and	(@i[0],$b);
	&amp;shr	(@T[0],2);
	&amp;xor	($lo,@i[1]);

	&amp;shr	($b,3);
	&amp;mov	(@i[1],0x7);		# 5-byte instruction!?
	&amp;and	(@i[1],$b);
	&amp;shr	($b,3);
	 &amp;xor	($hi,@T[0]);
	&amp;xor	($lo,&amp;DWP(0,"esp",@i[0],4));
	&amp;mov	(@i[0],0x7);
	&amp;and	(@i[0],$b);
	&amp;shr	($b,3);
	for($n=1;$n&lt;9;$n++) {
		&amp;mov	(@T[1],&amp;DWP(0,"esp",@i[1],4));
		&amp;mov	(@i[1],0x7);
		&amp;mov	(@T[0],@T[1]);
		&amp;shl	(@T[1],3*$n);
		&amp;and	(@i[1],$b);
		&amp;shr	(@T[0],32-3*$n);
		&amp;xor	($lo,@T[1]);
		&amp;shr	($b,3);
		&amp;xor	($hi,@T[0]);

		push(@i,shift(@i)); push(@T,shift(@T));
	}
	&amp;mov	(@T[1],&amp;DWP(0,"esp",@i[1],4));
	&amp;mov	(@T[0],@T[1]);
	&amp;shl	(@T[1],3*$n);
	&amp;mov	(@i[1],&amp;DWP(0,"esp",@i[0],4));
	&amp;shr	(@T[0],32-3*$n);	$n++;
	&amp;mov	(@i[0],@i[1]);
	&amp;xor	($lo,@T[1]);
	&amp;shl	(@i[1],3*$n);
	&amp;xor	($hi,@T[0]);
	&amp;shr	(@i[0],32-3*$n);
	&amp;xor	($lo,@i[1]);
	&amp;xor	($hi,@i[0]);

	&amp;add	("esp",32+4);
	&amp;ret	();
&amp;function_end_B("_mul_1x1_ialu");

# void bn_GF2m_mul_2x2(BN_ULONG *r, BN_ULONG a1, BN_ULONG a0, BN_ULONG b1, BN_ULONG b0);
&amp;function_begin_B("bn_GF2m_mul_2x2");
if (!$x86only) {
	&amp;picmeup("edx","OPENSSL_ia32cap_P");
	&amp;mov	("eax",&amp;DWP(0,"edx"));
	&amp;mov	("edx",&amp;DWP(4,"edx"));
	&amp;test	("eax",1&lt;&lt;23);		# check MMX bit
	&amp;jz	(&amp;label("ialu"));
if ($sse2) {
	&amp;test	("eax",1&lt;&lt;24);		# check FXSR bit
	&amp;jz	(&amp;label("mmx"));
	&amp;test	("edx",1&lt;&lt;1);		# check PCLMULQDQ bit
	&amp;jz	(&amp;label("mmx"));

	&amp;movups		("xmm0",&amp;QWP(8,"esp"));
	&amp;shufps		("xmm0","xmm0",0b10110001);
	&amp;pclmulqdq	("xmm0","xmm0",1);
	&amp;mov		("eax",&amp;DWP(4,"esp"));
	&amp;movups		(&amp;QWP(0,"eax"),"xmm0");
	&amp;ret	();

&amp;set_label("mmx",16);
}
	&amp;push	("ebp");
	&amp;push	("ebx");
	&amp;push	("esi");
	&amp;push	("edi");
	&amp;mov	($a,&amp;wparam(1));
	&amp;mov	($b,&amp;wparam(3));
	&amp;call	("_mul_1x1_mmx");	# a1路b1
	&amp;movq	("mm7",$R);

	&amp;mov	($a,&amp;wparam(2));
	&amp;mov	($b,&amp;wparam(4));
	&amp;call	("_mul_1x1_mmx");	# a0路b0
	&amp;movq	("mm6",$R);

	&amp;mov	($a,&amp;wparam(1));
	&amp;mov	($b,&amp;wparam(3));
	&amp;xor	($a,&amp;wparam(2));
	&amp;xor	($b,&amp;wparam(4));
	&amp;call	("_mul_1x1_mmx");	# (a0+a1)路(b0+b1)
	&amp;pxor	($R,"mm7");
	&amp;mov	($a,&amp;wparam(0));
	&amp;pxor	($R,"mm6");		# (a0+a1)路(b0+b1)-a1路b1-a0路b0

	&amp;movq	($A,$R);
	&amp;psllq	($R,32);
	&amp;pop	("edi");
	&amp;psrlq	($A,32);
	&amp;pop	("esi");
	&amp;pxor	($R,"mm6");
	&amp;pop	("ebx");
	&amp;pxor	($A,"mm7");
	&amp;movq	(&amp;QWP(0,$a),$R);
	&amp;pop	("ebp");
	&amp;movq	(&amp;QWP(8,$a),$A);
	&amp;emms	();
	&amp;ret	();
&amp;set_label("ialu",16);
}
	&amp;push	("ebp");
	&amp;push	("ebx");
	&amp;push	("esi");
	&amp;push	("edi");
	&amp;stack_push(4+1);

	&amp;mov	($a,&amp;wparam(1));
	&amp;mov	($b,&amp;wparam(3));
	&amp;call	("_mul_1x1_ialu");	# a1路b1
	&amp;mov	(&amp;DWP(8,"esp"),$lo);
	&amp;mov	(&amp;DWP(12,"esp"),$hi);

	&amp;mov	($a,&amp;wparam(2));
	&amp;mov	($b,&amp;wparam(4));
	&amp;call	("_mul_1x1_ialu");	# a0路b0
	&amp;mov	(&amp;DWP(0,"esp"),$lo);
	&amp;mov	(&amp;DWP(4,"esp"),$hi);

	&amp;mov	($a,&amp;wparam(1));
	&amp;mov	($b,&amp;wparam(3));
	&amp;xor	($a,&amp;wparam(2));
	&amp;xor	($b,&amp;wparam(4));
	&amp;call	("_mul_1x1_ialu");	# (a0+a1)路(b0+b1)

	&amp;mov	("ebp",&amp;wparam(0));
		 @r=("ebx","ecx","edi","esi");
	&amp;mov	(@r[0],&amp;DWP(0,"esp"));
	&amp;mov	(@r[1],&amp;DWP(4,"esp"));
	&amp;mov	(@r[2],&amp;DWP(8,"esp"));
	&amp;mov	(@r[3],&amp;DWP(12,"esp"));

	&amp;xor	($lo,$hi);
	&amp;xor	($hi,@r[1]);
	&amp;xor	($lo,@r[0]);
	&amp;mov	(&amp;DWP(0,"ebp"),@r[0]);
	&amp;xor	($hi,@r[2]);
	&amp;mov	(&amp;DWP(12,"ebp"),@r[3]);
	&amp;xor	($lo,@r[3]);
	&amp;stack_pop(4+1);
	&amp;xor	($hi,@r[3]);
	&amp;pop	("edi");
	&amp;xor	($lo,$hi);
	&amp;pop	("esi");
	&amp;mov	(&amp;DWP(8,"ebp"),$hi);
	&amp;pop	("ebx");
	&amp;mov	(&amp;DWP(4,"ebp"),$lo);
	&amp;pop	("ebp");
	&amp;ret	();
&amp;function_end_B("bn_GF2m_mul_2x2");

&amp;asciz	("GF(2^m) Multiplication for x86, CRYPTOGAMS by &lt;appro\@openssl.org&gt;");

&amp;asm_finish();

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