2 # Copyright 2011-2016 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the Apache License 2.0 (the "License"). You may not use
5 # this file except in compliance with the License. You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
10 # ====================================================================
11 # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
12 # project. The module is, however, dual licensed under OpenSSL and
13 # CRYPTOGAMS licenses depending on where you obtain it. For further
14 # details see http://www.openssl.org/~appro/cryptogams/.
15 # ====================================================================
19 # The module implements bn_GF2m_mul_2x2 polynomial multiplication used
20 # in bn_gf2m.c. It's kind of low-hanging mechanical port from C for
21 # the time being... Except that it has two code paths: code suitable
22 # for any x86_64 CPU and PCLMULQDQ one suitable for Westmere and
23 # later. Improvement varies from one benchmark and µ-arch to another.
24 # Vanilla code path is at most 20% faster than compiler-generated code
25 # [not very impressive], while PCLMULQDQ - whole 85%-160% better on
26 # 163- and 571-bit ECDH benchmarks on Intel CPUs. Keep in mind that
27 # these coefficients are not ones for bn_GF2m_mul_2x2 itself, as not
28 # all CPU time is burnt in it...
30 # $output is the last argument if it looks like a file (it has an extension)
31 # $flavour is the first argument if it doesn't look like a file
32 $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
33 $flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
35 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
37 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
38 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
39 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
40 die "can't locate x86_64-xlate.pl";
42 open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\""
43 or die "can't call $xlate: $!";
46 ($lo,$hi)=("%rax","%rdx"); $a=$lo;
47 ($i0,$i1)=("%rsi","%rdi");
48 ($t0,$t1)=("%rbx","%rcx");
49 ($b,$mask)=("%rbp","%r8");
50 ($a1,$a2,$a4,$a8,$a12,$a48)=map("%r$_",(9..15));
51 ($R,$Tx)=("%xmm0","%xmm1");
56 .type _mul_1x1,\@abi-omnipotent
61 .cfi_adjust_cfa_offset 128+8
66 and $a,$a1 # a1=a&0x1fffffffffffffff
68 sar \$63,$a # broadcast 63rd bit
70 sar \$63,$i0 # broadcast 62nd bit
73 sar \$63,$i1 # broadcast 61st bit
74 mov $a,$hi # $a is $lo
91 movq \$0,0(%rsp) # tab[0]=0
93 mov $a1,8(%rsp) # tab[1]=a1
95 mov $a2,16(%rsp) # tab[2]=a2
97 mov $a12,24(%rsp) # tab[3]=a1^a2
100 mov $a4,32(%rsp) # tab[4]=a4
102 mov $a1,40(%rsp) # tab[5]=a1^a4
104 mov $a2,48(%rsp) # tab[6]=a2^a4
105 xor $a48,$a1 # a1^a4^a4^a8=a1^a8
106 mov $a12,56(%rsp) # tab[7]=a1^a2^a4
107 xor $a48,$a2 # a2^a4^a4^a8=a1^a8
109 mov $a8,64(%rsp) # tab[8]=a8
110 xor $a48,$a12 # a1^a2^a4^a4^a8=a1^a2^a8
111 mov $a1,72(%rsp) # tab[9]=a1^a8
112 xor $a4,$a1 # a1^a8^a4
113 mov $a2,80(%rsp) # tab[10]=a2^a8
114 xor $a4,$a2 # a2^a8^a4
115 mov $a12,88(%rsp) # tab[11]=a1^a2^a8
117 xor $a4,$a12 # a1^a2^a8^a4
118 mov $a48,96(%rsp) # tab[12]=a4^a8
120 mov $a1,104(%rsp) # tab[13]=a1^a4^a8
122 mov $a2,112(%rsp) # tab[14]=a2^a4^a8
124 mov $a12,120(%rsp) # tab[15]=a1^a2^a4^a8
129 movq (%rsp,$i0,8),$R # half of calculations is done in SSE2
134 for ($n=1;$n<8;$n++) {
141 movq (%rsp,$i0,8),$Tx
142 shr \$`64-(8*$n-4)`,$t0
158 shr \$`64-(8*$n-4)`,$t0
167 .cfi_adjust_cfa_offset -128-8
171 .size _mul_1x1,.-_mul_1x1
174 ($rp,$a1,$a0,$b1,$b0) = $win64? ("%rcx","%rdx","%r8", "%r9","%r10") : # Win64 order
175 ("%rdi","%rsi","%rdx","%rcx","%r8"); # Unix order
178 .extern OPENSSL_ia32cap_P
179 .globl bn_GF2m_mul_2x2
180 .type bn_GF2m_mul_2x2,\@abi-omnipotent
185 mov OPENSSL_ia32cap_P(%rip),%r10
187 jnc .Lvanilla_mul_2x2
193 $code.=<<___ if ($win64);
196 $code.=<<___ if (!$win64);
202 pclmulqdq \$0,%xmm1,%xmm0 # a1·b1
205 pclmulqdq \$0,%xmm3,%xmm2 # a0·b0
206 pclmulqdq \$0,%xmm5,%xmm4 # (a0+a1)·(b0+b1)
208 xorps %xmm2,%xmm4 # (a0+a1)·(b0+b1)-a0·b0-a1·b1
221 .cfi_adjust_cfa_offset 8*17
223 $code.=<<___ if ($win64);
224 mov `8*17+40`(%rsp),$b0
230 .cfi_rel_offset %r14,8*10
232 .cfi_rel_offset %r13,8*11
234 .cfi_rel_offset %r12,8*12
236 .cfi_rel_offset %rbp,8*13
238 .cfi_rel_offset %rbx,8*14
240 mov $rp,32(%rsp) # save the arguments
249 call _mul_1x1 # a1·b1
255 call _mul_1x1 # a0·b0
263 call _mul_1x1 # (a0+a1)·(b0+b1)
265 @r=("%rbx","%rcx","%rdi","%rsi");
296 $code.=<<___ if ($win64);
302 .cfi_adjust_cfa_offset -8*17
307 .size bn_GF2m_mul_2x2,.-bn_GF2m_mul_2x2
308 .asciz "GF(2^m) Multiplication for x86_64, CRYPTOGAMS by <appro\@openssl.org>"
312 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
313 # CONTEXT *context,DISPATCHER_CONTEXT *disp)
321 .extern __imp_RtlVirtualUnwind
323 .type se_handler,\@abi-omnipotent
337 mov 120($context),%rax # pull context->Rax
338 mov 248($context),%rbx # pull context->Rip
340 lea .Lbody_mul_2x2(%rip),%r10
341 cmp %r10,%rbx # context->Rip<"prologue" label
344 mov 152($context),%rax # pull context->Rsp
346 lea .Lepilogue_mul_2x2(%rip),%r10
347 cmp %r10,%rbx # context->Rip>="epilogue" label
350 mov 8*10(%rax),%r14 # mimic epilogue
358 mov %rbx,144($context) # restore context->Rbx
359 mov %rbp,160($context) # restore context->Rbp
360 mov %rsi,168($context) # restore context->Rsi
361 mov %rdi,176($context) # restore context->Rdi
362 mov %r12,216($context) # restore context->R12
363 mov %r13,224($context) # restore context->R13
364 mov %r14,232($context) # restore context->R14
369 mov %rax,152($context) # restore context->Rsp
371 mov 40($disp),%rdi # disp->ContextRecord
372 mov $context,%rsi # context
373 mov \$154,%ecx # sizeof(CONTEXT)
374 .long 0xa548f3fc # cld; rep movsq
377 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
378 mov 8(%rsi),%rdx # arg2, disp->ImageBase
379 mov 0(%rsi),%r8 # arg3, disp->ControlPc
380 mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
381 mov 40(%rsi),%r10 # disp->ContextRecord
382 lea 56(%rsi),%r11 # &disp->HandlerData
383 lea 24(%rsi),%r12 # &disp->EstablisherFrame
384 mov %r10,32(%rsp) # arg5
385 mov %r11,40(%rsp) # arg6
386 mov %r12,48(%rsp) # arg7
387 mov %rcx,56(%rsp) # arg8, (NULL)
388 call *__imp_RtlVirtualUnwind(%rip)
390 mov \$1,%eax # ExceptionContinueSearch
402 .size se_handler,.-se_handler
410 .rva .Lvanilla_mul_2x2
416 .byte 0x01,0x07,0x02,0x00
417 .byte 0x07,0x01,0x11,0x00 # sub rsp,128+8
424 $code =~ s/\`([^\`]*)\`/eval($1)/gem;