Also check for errors in x86_64-xlate.pl.
[oweals/openssl.git] / crypto / whrlpool / asm / wp-x86_64.pl
1 #! /usr/bin/env perl
2 # Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved.
3 #
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
8
9 #
10 # ====================================================================
11 # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
12 # project. Rights for redistribution and usage in source and binary
13 # forms are granted according to the License.
14 # ====================================================================
15 #
16 # whirlpool_block for x86_64.
17 #
18 # 2500 cycles per 64-byte input block on AMD64, which is *identical*
19 # to 32-bit MMX version executed on same CPU. So why did I bother?
20 # Well, it's faster than gcc 3.3.2 generated code by over 50%, and
21 # over 80% faster than PathScale 1.4, an "ambitious" commercial
22 # compiler. Furthermore it surpasses gcc 3.4.3 by 170% and Sun Studio
23 # 10 - by 360%[!]... What is it with x86_64 compilers? It's not the
24 # first example when they fail to generate more optimal code, when
25 # I believe they had *all* chances to...
26 #
27 # Note that register and stack frame layout are virtually identical
28 # to 32-bit MMX version, except that %r8-15 are used instead of
29 # %mm0-8. You can even notice that K[i] and S[i] are loaded to
30 # %eax:%ebx as pair of 32-bit values and not as single 64-bit one.
31 # This is done in order to avoid 64-bit shift penalties on Intel
32 # EM64T core. Speaking of which! I bet it's possible to improve
33 # Opteron performance by compressing the table to 2KB and replacing
34 # unaligned references with complementary rotations [which would
35 # incidentally replace lea instructions], but it would definitely
36 # just "kill" EM64T, because it has only 1 shifter/rotator [against
37 # 3 on Opteron] and which is *unacceptably* slow with 64-bit
38 # operand.
39
40 # $output is the last argument if it looks like a file (it has an extension)
41 # $flavour is the first argument if it doesn't look like a file
42 $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
43 $flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
44
45 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
46
47 $0 =~ m/(.*[\/\\])[^\/\\]+$/; my $dir=$1; my $xlate;
48 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
49 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
50 die "can't locate x86_64-xlate.pl";
51
52 open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\""
53     or die "can't call $xlate: $!";
54 *STDOUT=*OUT;
55
56 sub L() { $code.=".byte ".join(',',@_)."\n"; }
57 sub LL(){ $code.=".byte ".join(',',@_).",".join(',',@_)."\n"; }
58
59 @mm=("%r8","%r9","%r10","%r11","%r12","%r13","%r14","%r15");
60
61 $func="whirlpool_block";
62 $table=".Ltable";
63
64 $code=<<___;
65 .text
66
67 .globl  $func
68 .type   $func,\@function,3
69 .align  16
70 $func:
71 .cfi_startproc
72         mov     %rsp,%rax
73 .cfi_def_cfa_register   %rax
74         push    %rbx
75 .cfi_push       %rbx
76         push    %rbp
77 .cfi_push       %rbp
78         push    %r12
79 .cfi_push       %r12
80         push    %r13
81 .cfi_push       %r13
82         push    %r14
83 .cfi_push       %r14
84         push    %r15
85 .cfi_push       %r15
86
87         sub     \$128+40,%rsp
88         and     \$-64,%rsp
89
90         lea     128(%rsp),%r10
91         mov     %rdi,0(%r10)            # save parameter block
92         mov     %rsi,8(%r10)
93         mov     %rdx,16(%r10)
94         mov     %rax,32(%r10)           # saved stack pointer
95 .cfi_cfa_expression     %rsp+`128+32`,deref,+8
96 .Lprologue:
97
98         mov     %r10,%rbx
99         lea     $table(%rip),%rbp
100
101         xor     %rcx,%rcx
102         xor     %rdx,%rdx
103 ___
104 for($i=0;$i<8;$i++) { $code.="mov $i*8(%rdi),@mm[$i]\n"; }      # L=H
105 $code.=".Louterloop:\n";
106 for($i=0;$i<8;$i++) { $code.="mov @mm[$i],$i*8(%rsp)\n"; }      # K=L
107 for($i=0;$i<8;$i++) { $code.="xor $i*8(%rsi),@mm[$i]\n"; }      # L^=inp
108 for($i=0;$i<8;$i++) { $code.="mov @mm[$i],64+$i*8(%rsp)\n"; }   # S=L
109 $code.=<<___;
110         xor     %rsi,%rsi
111         mov     %rsi,24(%rbx)           # zero round counter
112         jmp     .Lround
113 .align  16
114 .Lround:
115         mov     4096(%rbp,%rsi,8),@mm[0]        # rc[r]
116         mov     0(%rsp),%eax
117         mov     4(%rsp),%ebx
118         movz    %al,%ecx
119         movz    %ah,%edx
120 ___
121 for($i=0;$i<8;$i++) {
122     my $func = ($i==0)? "mov" : "xor";
123     $code.=<<___;
124         shr     \$16,%eax
125         lea     (%rcx,%rcx),%rsi
126         movz    %al,%ecx
127         lea     (%rdx,%rdx),%rdi
128         movz    %ah,%edx
129         xor     0(%rbp,%rsi,8),@mm[0]
130         $func   7(%rbp,%rdi,8),@mm[1]
131         mov     $i*8+8(%rsp),%eax               # ($i+1)*8
132         lea     (%rcx,%rcx),%rsi
133         movz    %bl,%ecx
134         lea     (%rdx,%rdx),%rdi
135         movz    %bh,%edx
136         $func   6(%rbp,%rsi,8),@mm[2]
137         $func   5(%rbp,%rdi,8),@mm[3]
138         shr     \$16,%ebx
139         lea     (%rcx,%rcx),%rsi
140         movz    %bl,%ecx
141         lea     (%rdx,%rdx),%rdi
142         movz    %bh,%edx
143         $func   4(%rbp,%rsi,8),@mm[4]
144         $func   3(%rbp,%rdi,8),@mm[5]
145         mov     $i*8+8+4(%rsp),%ebx             # ($i+1)*8+4
146         lea     (%rcx,%rcx),%rsi
147         movz    %al,%ecx
148         lea     (%rdx,%rdx),%rdi
149         movz    %ah,%edx
150         $func   2(%rbp,%rsi,8),@mm[6]
151         $func   1(%rbp,%rdi,8),@mm[7]
152 ___
153     push(@mm,shift(@mm));
154 }
155 for($i=0;$i<8;$i++) { $code.="mov @mm[$i],$i*8(%rsp)\n"; }      # K=L
156 for($i=0;$i<8;$i++) {
157     $code.=<<___;
158         shr     \$16,%eax
159         lea     (%rcx,%rcx),%rsi
160         movz    %al,%ecx
161         lea     (%rdx,%rdx),%rdi
162         movz    %ah,%edx
163         xor     0(%rbp,%rsi,8),@mm[0]
164         xor     7(%rbp,%rdi,8),@mm[1]
165         `"mov   64+$i*8+8(%rsp),%eax"   if($i<7);`      # 64+($i+1)*8
166         lea     (%rcx,%rcx),%rsi
167         movz    %bl,%ecx
168         lea     (%rdx,%rdx),%rdi
169         movz    %bh,%edx
170         xor     6(%rbp,%rsi,8),@mm[2]
171         xor     5(%rbp,%rdi,8),@mm[3]
172         shr     \$16,%ebx
173         lea     (%rcx,%rcx),%rsi
174         movz    %bl,%ecx
175         lea     (%rdx,%rdx),%rdi
176         movz    %bh,%edx
177         xor     4(%rbp,%rsi,8),@mm[4]
178         xor     3(%rbp,%rdi,8),@mm[5]
179         `"mov   64+$i*8+8+4(%rsp),%ebx" if($i<7);`      # 64+($i+1)*8+4
180         lea     (%rcx,%rcx),%rsi
181         movz    %al,%ecx
182         lea     (%rdx,%rdx),%rdi
183         movz    %ah,%edx
184         xor     2(%rbp,%rsi,8),@mm[6]
185         xor     1(%rbp,%rdi,8),@mm[7]
186 ___
187     push(@mm,shift(@mm));
188 }
189 $code.=<<___;
190         lea     128(%rsp),%rbx
191         mov     24(%rbx),%rsi           # pull round counter
192         add     \$1,%rsi
193         cmp     \$10,%rsi
194         je      .Lroundsdone
195
196         mov     %rsi,24(%rbx)           # update round counter
197 ___
198 for($i=0;$i<8;$i++) { $code.="mov @mm[$i],64+$i*8(%rsp)\n"; }   # S=L
199 $code.=<<___;
200         jmp     .Lround
201 .align  16
202 .Lroundsdone:
203         mov     0(%rbx),%rdi            # reload argument block
204         mov     8(%rbx),%rsi
205         mov     16(%rbx),%rax
206 ___
207 for($i=0;$i<8;$i++) { $code.="xor $i*8(%rsi),@mm[$i]\n"; }      # L^=inp
208 for($i=0;$i<8;$i++) { $code.="xor $i*8(%rdi),@mm[$i]\n"; }      # L^=H
209 for($i=0;$i<8;$i++) { $code.="mov @mm[$i],$i*8(%rdi)\n"; }      # H=L
210 $code.=<<___;
211         lea     64(%rsi),%rsi           # inp+=64
212         sub     \$1,%rax                # num--
213         jz      .Lalldone
214         mov     %rsi,8(%rbx)            # update parameter block
215         mov     %rax,16(%rbx)
216         jmp     .Louterloop
217 .Lalldone:
218         mov     32(%rbx),%rsi           # restore saved pointer
219 .cfi_def_cfa    %rsi,8
220         mov     -48(%rsi),%r15
221 .cfi_restore    %r15
222         mov     -40(%rsi),%r14
223 .cfi_restore    %r14
224         mov     -32(%rsi),%r13
225 .cfi_restore    %r13
226         mov     -24(%rsi),%r12
227 .cfi_restore    %r12
228         mov     -16(%rsi),%rbp
229 .cfi_restore    %rbp
230         mov     -8(%rsi),%rbx
231 .cfi_restore    %rbx
232         lea     (%rsi),%rsp
233 .cfi_def_cfa_register   %rsp
234 .Lepilogue:
235         ret
236 .cfi_endproc
237 .size   $func,.-$func
238
239 .align  64
240 .type   $table,\@object
241 $table:
242 ___
243         &LL(0x18,0x18,0x60,0x18,0xc0,0x78,0x30,0xd8);
244         &LL(0x23,0x23,0x8c,0x23,0x05,0xaf,0x46,0x26);
245         &LL(0xc6,0xc6,0x3f,0xc6,0x7e,0xf9,0x91,0xb8);
246         &LL(0xe8,0xe8,0x87,0xe8,0x13,0x6f,0xcd,0xfb);
247         &LL(0x87,0x87,0x26,0x87,0x4c,0xa1,0x13,0xcb);
248         &LL(0xb8,0xb8,0xda,0xb8,0xa9,0x62,0x6d,0x11);
249         &LL(0x01,0x01,0x04,0x01,0x08,0x05,0x02,0x09);
250         &LL(0x4f,0x4f,0x21,0x4f,0x42,0x6e,0x9e,0x0d);
251         &LL(0x36,0x36,0xd8,0x36,0xad,0xee,0x6c,0x9b);
252         &LL(0xa6,0xa6,0xa2,0xa6,0x59,0x04,0x51,0xff);
253         &LL(0xd2,0xd2,0x6f,0xd2,0xde,0xbd,0xb9,0x0c);
254         &LL(0xf5,0xf5,0xf3,0xf5,0xfb,0x06,0xf7,0x0e);
255         &LL(0x79,0x79,0xf9,0x79,0xef,0x80,0xf2,0x96);
256         &LL(0x6f,0x6f,0xa1,0x6f,0x5f,0xce,0xde,0x30);
257         &LL(0x91,0x91,0x7e,0x91,0xfc,0xef,0x3f,0x6d);
258         &LL(0x52,0x52,0x55,0x52,0xaa,0x07,0xa4,0xf8);
259         &LL(0x60,0x60,0x9d,0x60,0x27,0xfd,0xc0,0x47);
260         &LL(0xbc,0xbc,0xca,0xbc,0x89,0x76,0x65,0x35);
261         &LL(0x9b,0x9b,0x56,0x9b,0xac,0xcd,0x2b,0x37);
262         &LL(0x8e,0x8e,0x02,0x8e,0x04,0x8c,0x01,0x8a);
263         &LL(0xa3,0xa3,0xb6,0xa3,0x71,0x15,0x5b,0xd2);
264         &LL(0x0c,0x0c,0x30,0x0c,0x60,0x3c,0x18,0x6c);
265         &LL(0x7b,0x7b,0xf1,0x7b,0xff,0x8a,0xf6,0x84);
266         &LL(0x35,0x35,0xd4,0x35,0xb5,0xe1,0x6a,0x80);
267         &LL(0x1d,0x1d,0x74,0x1d,0xe8,0x69,0x3a,0xf5);
268         &LL(0xe0,0xe0,0xa7,0xe0,0x53,0x47,0xdd,0xb3);
269         &LL(0xd7,0xd7,0x7b,0xd7,0xf6,0xac,0xb3,0x21);
270         &LL(0xc2,0xc2,0x2f,0xc2,0x5e,0xed,0x99,0x9c);
271         &LL(0x2e,0x2e,0xb8,0x2e,0x6d,0x96,0x5c,0x43);
272         &LL(0x4b,0x4b,0x31,0x4b,0x62,0x7a,0x96,0x29);
273         &LL(0xfe,0xfe,0xdf,0xfe,0xa3,0x21,0xe1,0x5d);
274         &LL(0x57,0x57,0x41,0x57,0x82,0x16,0xae,0xd5);
275         &LL(0x15,0x15,0x54,0x15,0xa8,0x41,0x2a,0xbd);
276         &LL(0x77,0x77,0xc1,0x77,0x9f,0xb6,0xee,0xe8);
277         &LL(0x37,0x37,0xdc,0x37,0xa5,0xeb,0x6e,0x92);
278         &LL(0xe5,0xe5,0xb3,0xe5,0x7b,0x56,0xd7,0x9e);
279         &LL(0x9f,0x9f,0x46,0x9f,0x8c,0xd9,0x23,0x13);
280         &LL(0xf0,0xf0,0xe7,0xf0,0xd3,0x17,0xfd,0x23);
281         &LL(0x4a,0x4a,0x35,0x4a,0x6a,0x7f,0x94,0x20);
282         &LL(0xda,0xda,0x4f,0xda,0x9e,0x95,0xa9,0x44);
283         &LL(0x58,0x58,0x7d,0x58,0xfa,0x25,0xb0,0xa2);
284         &LL(0xc9,0xc9,0x03,0xc9,0x06,0xca,0x8f,0xcf);
285         &LL(0x29,0x29,0xa4,0x29,0x55,0x8d,0x52,0x7c);
286         &LL(0x0a,0x0a,0x28,0x0a,0x50,0x22,0x14,0x5a);
287         &LL(0xb1,0xb1,0xfe,0xb1,0xe1,0x4f,0x7f,0x50);
288         &LL(0xa0,0xa0,0xba,0xa0,0x69,0x1a,0x5d,0xc9);
289         &LL(0x6b,0x6b,0xb1,0x6b,0x7f,0xda,0xd6,0x14);
290         &LL(0x85,0x85,0x2e,0x85,0x5c,0xab,0x17,0xd9);
291         &LL(0xbd,0xbd,0xce,0xbd,0x81,0x73,0x67,0x3c);
292         &LL(0x5d,0x5d,0x69,0x5d,0xd2,0x34,0xba,0x8f);
293         &LL(0x10,0x10,0x40,0x10,0x80,0x50,0x20,0x90);
294         &LL(0xf4,0xf4,0xf7,0xf4,0xf3,0x03,0xf5,0x07);
295         &LL(0xcb,0xcb,0x0b,0xcb,0x16,0xc0,0x8b,0xdd);
296         &LL(0x3e,0x3e,0xf8,0x3e,0xed,0xc6,0x7c,0xd3);
297         &LL(0x05,0x05,0x14,0x05,0x28,0x11,0x0a,0x2d);
298         &LL(0x67,0x67,0x81,0x67,0x1f,0xe6,0xce,0x78);
299         &LL(0xe4,0xe4,0xb7,0xe4,0x73,0x53,0xd5,0x97);
300         &LL(0x27,0x27,0x9c,0x27,0x25,0xbb,0x4e,0x02);
301         &LL(0x41,0x41,0x19,0x41,0x32,0x58,0x82,0x73);
302         &LL(0x8b,0x8b,0x16,0x8b,0x2c,0x9d,0x0b,0xa7);
303         &LL(0xa7,0xa7,0xa6,0xa7,0x51,0x01,0x53,0xf6);
304         &LL(0x7d,0x7d,0xe9,0x7d,0xcf,0x94,0xfa,0xb2);
305         &LL(0x95,0x95,0x6e,0x95,0xdc,0xfb,0x37,0x49);
306         &LL(0xd8,0xd8,0x47,0xd8,0x8e,0x9f,0xad,0x56);
307         &LL(0xfb,0xfb,0xcb,0xfb,0x8b,0x30,0xeb,0x70);
308         &LL(0xee,0xee,0x9f,0xee,0x23,0x71,0xc1,0xcd);
309         &LL(0x7c,0x7c,0xed,0x7c,0xc7,0x91,0xf8,0xbb);
310         &LL(0x66,0x66,0x85,0x66,0x17,0xe3,0xcc,0x71);
311         &LL(0xdd,0xdd,0x53,0xdd,0xa6,0x8e,0xa7,0x7b);
312         &LL(0x17,0x17,0x5c,0x17,0xb8,0x4b,0x2e,0xaf);
313         &LL(0x47,0x47,0x01,0x47,0x02,0x46,0x8e,0x45);
314         &LL(0x9e,0x9e,0x42,0x9e,0x84,0xdc,0x21,0x1a);
315         &LL(0xca,0xca,0x0f,0xca,0x1e,0xc5,0x89,0xd4);
316         &LL(0x2d,0x2d,0xb4,0x2d,0x75,0x99,0x5a,0x58);
317         &LL(0xbf,0xbf,0xc6,0xbf,0x91,0x79,0x63,0x2e);
318         &LL(0x07,0x07,0x1c,0x07,0x38,0x1b,0x0e,0x3f);
319         &LL(0xad,0xad,0x8e,0xad,0x01,0x23,0x47,0xac);
320         &LL(0x5a,0x5a,0x75,0x5a,0xea,0x2f,0xb4,0xb0);
321         &LL(0x83,0x83,0x36,0x83,0x6c,0xb5,0x1b,0xef);
322         &LL(0x33,0x33,0xcc,0x33,0x85,0xff,0x66,0xb6);
323         &LL(0x63,0x63,0x91,0x63,0x3f,0xf2,0xc6,0x5c);
324         &LL(0x02,0x02,0x08,0x02,0x10,0x0a,0x04,0x12);
325         &LL(0xaa,0xaa,0x92,0xaa,0x39,0x38,0x49,0x93);
326         &LL(0x71,0x71,0xd9,0x71,0xaf,0xa8,0xe2,0xde);
327         &LL(0xc8,0xc8,0x07,0xc8,0x0e,0xcf,0x8d,0xc6);
328         &LL(0x19,0x19,0x64,0x19,0xc8,0x7d,0x32,0xd1);
329         &LL(0x49,0x49,0x39,0x49,0x72,0x70,0x92,0x3b);
330         &LL(0xd9,0xd9,0x43,0xd9,0x86,0x9a,0xaf,0x5f);
331         &LL(0xf2,0xf2,0xef,0xf2,0xc3,0x1d,0xf9,0x31);
332         &LL(0xe3,0xe3,0xab,0xe3,0x4b,0x48,0xdb,0xa8);
333         &LL(0x5b,0x5b,0x71,0x5b,0xe2,0x2a,0xb6,0xb9);
334         &LL(0x88,0x88,0x1a,0x88,0x34,0x92,0x0d,0xbc);
335         &LL(0x9a,0x9a,0x52,0x9a,0xa4,0xc8,0x29,0x3e);
336         &LL(0x26,0x26,0x98,0x26,0x2d,0xbe,0x4c,0x0b);
337         &LL(0x32,0x32,0xc8,0x32,0x8d,0xfa,0x64,0xbf);
338         &LL(0xb0,0xb0,0xfa,0xb0,0xe9,0x4a,0x7d,0x59);
339         &LL(0xe9,0xe9,0x83,0xe9,0x1b,0x6a,0xcf,0xf2);
340         &LL(0x0f,0x0f,0x3c,0x0f,0x78,0x33,0x1e,0x77);
341         &LL(0xd5,0xd5,0x73,0xd5,0xe6,0xa6,0xb7,0x33);
342         &LL(0x80,0x80,0x3a,0x80,0x74,0xba,0x1d,0xf4);
343         &LL(0xbe,0xbe,0xc2,0xbe,0x99,0x7c,0x61,0x27);
344         &LL(0xcd,0xcd,0x13,0xcd,0x26,0xde,0x87,0xeb);
345         &LL(0x34,0x34,0xd0,0x34,0xbd,0xe4,0x68,0x89);
346         &LL(0x48,0x48,0x3d,0x48,0x7a,0x75,0x90,0x32);
347         &LL(0xff,0xff,0xdb,0xff,0xab,0x24,0xe3,0x54);
348         &LL(0x7a,0x7a,0xf5,0x7a,0xf7,0x8f,0xf4,0x8d);
349         &LL(0x90,0x90,0x7a,0x90,0xf4,0xea,0x3d,0x64);
350         &LL(0x5f,0x5f,0x61,0x5f,0xc2,0x3e,0xbe,0x9d);
351         &LL(0x20,0x20,0x80,0x20,0x1d,0xa0,0x40,0x3d);
352         &LL(0x68,0x68,0xbd,0x68,0x67,0xd5,0xd0,0x0f);
353         &LL(0x1a,0x1a,0x68,0x1a,0xd0,0x72,0x34,0xca);
354         &LL(0xae,0xae,0x82,0xae,0x19,0x2c,0x41,0xb7);
355         &LL(0xb4,0xb4,0xea,0xb4,0xc9,0x5e,0x75,0x7d);
356         &LL(0x54,0x54,0x4d,0x54,0x9a,0x19,0xa8,0xce);
357         &LL(0x93,0x93,0x76,0x93,0xec,0xe5,0x3b,0x7f);
358         &LL(0x22,0x22,0x88,0x22,0x0d,0xaa,0x44,0x2f);
359         &LL(0x64,0x64,0x8d,0x64,0x07,0xe9,0xc8,0x63);
360         &LL(0xf1,0xf1,0xe3,0xf1,0xdb,0x12,0xff,0x2a);
361         &LL(0x73,0x73,0xd1,0x73,0xbf,0xa2,0xe6,0xcc);
362         &LL(0x12,0x12,0x48,0x12,0x90,0x5a,0x24,0x82);
363         &LL(0x40,0x40,0x1d,0x40,0x3a,0x5d,0x80,0x7a);
364         &LL(0x08,0x08,0x20,0x08,0x40,0x28,0x10,0x48);
365         &LL(0xc3,0xc3,0x2b,0xc3,0x56,0xe8,0x9b,0x95);
366         &LL(0xec,0xec,0x97,0xec,0x33,0x7b,0xc5,0xdf);
367         &LL(0xdb,0xdb,0x4b,0xdb,0x96,0x90,0xab,0x4d);
368         &LL(0xa1,0xa1,0xbe,0xa1,0x61,0x1f,0x5f,0xc0);
369         &LL(0x8d,0x8d,0x0e,0x8d,0x1c,0x83,0x07,0x91);
370         &LL(0x3d,0x3d,0xf4,0x3d,0xf5,0xc9,0x7a,0xc8);
371         &LL(0x97,0x97,0x66,0x97,0xcc,0xf1,0x33,0x5b);
372         &LL(0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00);
373         &LL(0xcf,0xcf,0x1b,0xcf,0x36,0xd4,0x83,0xf9);
374         &LL(0x2b,0x2b,0xac,0x2b,0x45,0x87,0x56,0x6e);
375         &LL(0x76,0x76,0xc5,0x76,0x97,0xb3,0xec,0xe1);
376         &LL(0x82,0x82,0x32,0x82,0x64,0xb0,0x19,0xe6);
377         &LL(0xd6,0xd6,0x7f,0xd6,0xfe,0xa9,0xb1,0x28);
378         &LL(0x1b,0x1b,0x6c,0x1b,0xd8,0x77,0x36,0xc3);
379         &LL(0xb5,0xb5,0xee,0xb5,0xc1,0x5b,0x77,0x74);
380         &LL(0xaf,0xaf,0x86,0xaf,0x11,0x29,0x43,0xbe);
381         &LL(0x6a,0x6a,0xb5,0x6a,0x77,0xdf,0xd4,0x1d);
382         &LL(0x50,0x50,0x5d,0x50,0xba,0x0d,0xa0,0xea);
383         &LL(0x45,0x45,0x09,0x45,0x12,0x4c,0x8a,0x57);
384         &LL(0xf3,0xf3,0xeb,0xf3,0xcb,0x18,0xfb,0x38);
385         &LL(0x30,0x30,0xc0,0x30,0x9d,0xf0,0x60,0xad);
386         &LL(0xef,0xef,0x9b,0xef,0x2b,0x74,0xc3,0xc4);
387         &LL(0x3f,0x3f,0xfc,0x3f,0xe5,0xc3,0x7e,0xda);
388         &LL(0x55,0x55,0x49,0x55,0x92,0x1c,0xaa,0xc7);
389         &LL(0xa2,0xa2,0xb2,0xa2,0x79,0x10,0x59,0xdb);
390         &LL(0xea,0xea,0x8f,0xea,0x03,0x65,0xc9,0xe9);
391         &LL(0x65,0x65,0x89,0x65,0x0f,0xec,0xca,0x6a);
392         &LL(0xba,0xba,0xd2,0xba,0xb9,0x68,0x69,0x03);
393         &LL(0x2f,0x2f,0xbc,0x2f,0x65,0x93,0x5e,0x4a);
394         &LL(0xc0,0xc0,0x27,0xc0,0x4e,0xe7,0x9d,0x8e);
395         &LL(0xde,0xde,0x5f,0xde,0xbe,0x81,0xa1,0x60);
396         &LL(0x1c,0x1c,0x70,0x1c,0xe0,0x6c,0x38,0xfc);
397         &LL(0xfd,0xfd,0xd3,0xfd,0xbb,0x2e,0xe7,0x46);
398         &LL(0x4d,0x4d,0x29,0x4d,0x52,0x64,0x9a,0x1f);
399         &LL(0x92,0x92,0x72,0x92,0xe4,0xe0,0x39,0x76);
400         &LL(0x75,0x75,0xc9,0x75,0x8f,0xbc,0xea,0xfa);
401         &LL(0x06,0x06,0x18,0x06,0x30,0x1e,0x0c,0x36);
402         &LL(0x8a,0x8a,0x12,0x8a,0x24,0x98,0x09,0xae);
403         &LL(0xb2,0xb2,0xf2,0xb2,0xf9,0x40,0x79,0x4b);
404         &LL(0xe6,0xe6,0xbf,0xe6,0x63,0x59,0xd1,0x85);
405         &LL(0x0e,0x0e,0x38,0x0e,0x70,0x36,0x1c,0x7e);
406         &LL(0x1f,0x1f,0x7c,0x1f,0xf8,0x63,0x3e,0xe7);
407         &LL(0x62,0x62,0x95,0x62,0x37,0xf7,0xc4,0x55);
408         &LL(0xd4,0xd4,0x77,0xd4,0xee,0xa3,0xb5,0x3a);
409         &LL(0xa8,0xa8,0x9a,0xa8,0x29,0x32,0x4d,0x81);
410         &LL(0x96,0x96,0x62,0x96,0xc4,0xf4,0x31,0x52);
411         &LL(0xf9,0xf9,0xc3,0xf9,0x9b,0x3a,0xef,0x62);
412         &LL(0xc5,0xc5,0x33,0xc5,0x66,0xf6,0x97,0xa3);
413         &LL(0x25,0x25,0x94,0x25,0x35,0xb1,0x4a,0x10);
414         &LL(0x59,0x59,0x79,0x59,0xf2,0x20,0xb2,0xab);
415         &LL(0x84,0x84,0x2a,0x84,0x54,0xae,0x15,0xd0);
416         &LL(0x72,0x72,0xd5,0x72,0xb7,0xa7,0xe4,0xc5);
417         &LL(0x39,0x39,0xe4,0x39,0xd5,0xdd,0x72,0xec);
418         &LL(0x4c,0x4c,0x2d,0x4c,0x5a,0x61,0x98,0x16);
419         &LL(0x5e,0x5e,0x65,0x5e,0xca,0x3b,0xbc,0x94);
420         &LL(0x78,0x78,0xfd,0x78,0xe7,0x85,0xf0,0x9f);
421         &LL(0x38,0x38,0xe0,0x38,0xdd,0xd8,0x70,0xe5);
422         &LL(0x8c,0x8c,0x0a,0x8c,0x14,0x86,0x05,0x98);
423         &LL(0xd1,0xd1,0x63,0xd1,0xc6,0xb2,0xbf,0x17);
424         &LL(0xa5,0xa5,0xae,0xa5,0x41,0x0b,0x57,0xe4);
425         &LL(0xe2,0xe2,0xaf,0xe2,0x43,0x4d,0xd9,0xa1);
426         &LL(0x61,0x61,0x99,0x61,0x2f,0xf8,0xc2,0x4e);
427         &LL(0xb3,0xb3,0xf6,0xb3,0xf1,0x45,0x7b,0x42);
428         &LL(0x21,0x21,0x84,0x21,0x15,0xa5,0x42,0x34);
429         &LL(0x9c,0x9c,0x4a,0x9c,0x94,0xd6,0x25,0x08);
430         &LL(0x1e,0x1e,0x78,0x1e,0xf0,0x66,0x3c,0xee);
431         &LL(0x43,0x43,0x11,0x43,0x22,0x52,0x86,0x61);
432         &LL(0xc7,0xc7,0x3b,0xc7,0x76,0xfc,0x93,0xb1);
433         &LL(0xfc,0xfc,0xd7,0xfc,0xb3,0x2b,0xe5,0x4f);
434         &LL(0x04,0x04,0x10,0x04,0x20,0x14,0x08,0x24);
435         &LL(0x51,0x51,0x59,0x51,0xb2,0x08,0xa2,0xe3);
436         &LL(0x99,0x99,0x5e,0x99,0xbc,0xc7,0x2f,0x25);
437         &LL(0x6d,0x6d,0xa9,0x6d,0x4f,0xc4,0xda,0x22);
438         &LL(0x0d,0x0d,0x34,0x0d,0x68,0x39,0x1a,0x65);
439         &LL(0xfa,0xfa,0xcf,0xfa,0x83,0x35,0xe9,0x79);
440         &LL(0xdf,0xdf,0x5b,0xdf,0xb6,0x84,0xa3,0x69);
441         &LL(0x7e,0x7e,0xe5,0x7e,0xd7,0x9b,0xfc,0xa9);
442         &LL(0x24,0x24,0x90,0x24,0x3d,0xb4,0x48,0x19);
443         &LL(0x3b,0x3b,0xec,0x3b,0xc5,0xd7,0x76,0xfe);
444         &LL(0xab,0xab,0x96,0xab,0x31,0x3d,0x4b,0x9a);
445         &LL(0xce,0xce,0x1f,0xce,0x3e,0xd1,0x81,0xf0);
446         &LL(0x11,0x11,0x44,0x11,0x88,0x55,0x22,0x99);
447         &LL(0x8f,0x8f,0x06,0x8f,0x0c,0x89,0x03,0x83);
448         &LL(0x4e,0x4e,0x25,0x4e,0x4a,0x6b,0x9c,0x04);
449         &LL(0xb7,0xb7,0xe6,0xb7,0xd1,0x51,0x73,0x66);
450         &LL(0xeb,0xeb,0x8b,0xeb,0x0b,0x60,0xcb,0xe0);
451         &LL(0x3c,0x3c,0xf0,0x3c,0xfd,0xcc,0x78,0xc1);
452         &LL(0x81,0x81,0x3e,0x81,0x7c,0xbf,0x1f,0xfd);
453         &LL(0x94,0x94,0x6a,0x94,0xd4,0xfe,0x35,0x40);
454         &LL(0xf7,0xf7,0xfb,0xf7,0xeb,0x0c,0xf3,0x1c);
455         &LL(0xb9,0xb9,0xde,0xb9,0xa1,0x67,0x6f,0x18);
456         &LL(0x13,0x13,0x4c,0x13,0x98,0x5f,0x26,0x8b);
457         &LL(0x2c,0x2c,0xb0,0x2c,0x7d,0x9c,0x58,0x51);
458         &LL(0xd3,0xd3,0x6b,0xd3,0xd6,0xb8,0xbb,0x05);
459         &LL(0xe7,0xe7,0xbb,0xe7,0x6b,0x5c,0xd3,0x8c);
460         &LL(0x6e,0x6e,0xa5,0x6e,0x57,0xcb,0xdc,0x39);
461         &LL(0xc4,0xc4,0x37,0xc4,0x6e,0xf3,0x95,0xaa);
462         &LL(0x03,0x03,0x0c,0x03,0x18,0x0f,0x06,0x1b);
463         &LL(0x56,0x56,0x45,0x56,0x8a,0x13,0xac,0xdc);
464         &LL(0x44,0x44,0x0d,0x44,0x1a,0x49,0x88,0x5e);
465         &LL(0x7f,0x7f,0xe1,0x7f,0xdf,0x9e,0xfe,0xa0);
466         &LL(0xa9,0xa9,0x9e,0xa9,0x21,0x37,0x4f,0x88);
467         &LL(0x2a,0x2a,0xa8,0x2a,0x4d,0x82,0x54,0x67);
468         &LL(0xbb,0xbb,0xd6,0xbb,0xb1,0x6d,0x6b,0x0a);
469         &LL(0xc1,0xc1,0x23,0xc1,0x46,0xe2,0x9f,0x87);
470         &LL(0x53,0x53,0x51,0x53,0xa2,0x02,0xa6,0xf1);
471         &LL(0xdc,0xdc,0x57,0xdc,0xae,0x8b,0xa5,0x72);
472         &LL(0x0b,0x0b,0x2c,0x0b,0x58,0x27,0x16,0x53);
473         &LL(0x9d,0x9d,0x4e,0x9d,0x9c,0xd3,0x27,0x01);
474         &LL(0x6c,0x6c,0xad,0x6c,0x47,0xc1,0xd8,0x2b);
475         &LL(0x31,0x31,0xc4,0x31,0x95,0xf5,0x62,0xa4);
476         &LL(0x74,0x74,0xcd,0x74,0x87,0xb9,0xe8,0xf3);
477         &LL(0xf6,0xf6,0xff,0xf6,0xe3,0x09,0xf1,0x15);
478         &LL(0x46,0x46,0x05,0x46,0x0a,0x43,0x8c,0x4c);
479         &LL(0xac,0xac,0x8a,0xac,0x09,0x26,0x45,0xa5);
480         &LL(0x89,0x89,0x1e,0x89,0x3c,0x97,0x0f,0xb5);
481         &LL(0x14,0x14,0x50,0x14,0xa0,0x44,0x28,0xb4);
482         &LL(0xe1,0xe1,0xa3,0xe1,0x5b,0x42,0xdf,0xba);
483         &LL(0x16,0x16,0x58,0x16,0xb0,0x4e,0x2c,0xa6);
484         &LL(0x3a,0x3a,0xe8,0x3a,0xcd,0xd2,0x74,0xf7);
485         &LL(0x69,0x69,0xb9,0x69,0x6f,0xd0,0xd2,0x06);
486         &LL(0x09,0x09,0x24,0x09,0x48,0x2d,0x12,0x41);
487         &LL(0x70,0x70,0xdd,0x70,0xa7,0xad,0xe0,0xd7);
488         &LL(0xb6,0xb6,0xe2,0xb6,0xd9,0x54,0x71,0x6f);
489         &LL(0xd0,0xd0,0x67,0xd0,0xce,0xb7,0xbd,0x1e);
490         &LL(0xed,0xed,0x93,0xed,0x3b,0x7e,0xc7,0xd6);
491         &LL(0xcc,0xcc,0x17,0xcc,0x2e,0xdb,0x85,0xe2);
492         &LL(0x42,0x42,0x15,0x42,0x2a,0x57,0x84,0x68);
493         &LL(0x98,0x98,0x5a,0x98,0xb4,0xc2,0x2d,0x2c);
494         &LL(0xa4,0xa4,0xaa,0xa4,0x49,0x0e,0x55,0xed);
495         &LL(0x28,0x28,0xa0,0x28,0x5d,0x88,0x50,0x75);
496         &LL(0x5c,0x5c,0x6d,0x5c,0xda,0x31,0xb8,0x86);
497         &LL(0xf8,0xf8,0xc7,0xf8,0x93,0x3f,0xed,0x6b);
498         &LL(0x86,0x86,0x22,0x86,0x44,0xa4,0x11,0xc2);
499
500         &L(0x18,0x23,0xc6,0xe8,0x87,0xb8,0x01,0x4f);    # rc[ROUNDS]
501         &L(0x36,0xa6,0xd2,0xf5,0x79,0x6f,0x91,0x52);
502         &L(0x60,0xbc,0x9b,0x8e,0xa3,0x0c,0x7b,0x35);
503         &L(0x1d,0xe0,0xd7,0xc2,0x2e,0x4b,0xfe,0x57);
504         &L(0x15,0x77,0x37,0xe5,0x9f,0xf0,0x4a,0xda);
505         &L(0x58,0xc9,0x29,0x0a,0xb1,0xa0,0x6b,0x85);
506         &L(0xbd,0x5d,0x10,0xf4,0xcb,0x3e,0x05,0x67);
507         &L(0xe4,0x27,0x41,0x8b,0xa7,0x7d,0x95,0xd8);
508         &L(0xfb,0xee,0x7c,0x66,0xdd,0x17,0x47,0x9e);
509         &L(0xca,0x2d,0xbf,0x07,0xad,0x5a,0x83,0x33);
510
511 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
512 #               CONTEXT *context,DISPATCHER_CONTEXT *disp)
513 if ($win64) {
514 $rec="%rcx";
515 $frame="%rdx";
516 $context="%r8";
517 $disp="%r9";
518
519 $code.=<<___;
520 .extern __imp_RtlVirtualUnwind
521 .type   se_handler,\@abi-omnipotent
522 .align  16
523 se_handler:
524         push    %rsi
525         push    %rdi
526         push    %rbx
527         push    %rbp
528         push    %r12
529         push    %r13
530         push    %r14
531         push    %r15
532         pushfq
533         sub     \$64,%rsp
534
535         mov     120($context),%rax      # pull context->Rax
536         mov     248($context),%rbx      # pull context->Rip
537
538         lea     .Lprologue(%rip),%r10
539         cmp     %r10,%rbx               # context->Rip<.Lprologue
540         jb      .Lin_prologue
541
542         mov     152($context),%rax      # pull context->Rsp
543
544         lea     .Lepilogue(%rip),%r10
545         cmp     %r10,%rbx               # context->Rip>=.Lepilogue
546         jae     .Lin_prologue
547
548         mov     128+32(%rax),%rax       # pull saved stack pointer
549
550         mov     -8(%rax),%rbx
551         mov     -16(%rax),%rbp
552         mov     -24(%rax),%r12
553         mov     -32(%rax),%r13
554         mov     -40(%rax),%r14
555         mov     -48(%rax),%r15
556         mov     %rbx,144($context)      # restore context->Rbx
557         mov     %rbp,160($context)      # restore context->Rbp
558         mov     %r12,216($context)      # restore context->R12
559         mov     %r13,224($context)      # restore context->R13
560         mov     %r14,232($context)      # restore context->R14
561         mov     %r15,240($context)      # restore context->R15
562
563 .Lin_prologue:
564         mov     8(%rax),%rdi
565         mov     16(%rax),%rsi
566         mov     %rax,152($context)      # restore context->Rsp
567         mov     %rsi,168($context)      # restore context->Rsi
568         mov     %rdi,176($context)      # restore context->Rdi
569
570         mov     40($disp),%rdi          # disp->ContextRecord
571         mov     $context,%rsi           # context
572         mov     \$154,%ecx              # sizeof(CONTEXT)
573         .long   0xa548f3fc              # cld; rep movsq
574
575         mov     $disp,%rsi
576         xor     %rcx,%rcx               # arg1, UNW_FLAG_NHANDLER
577         mov     8(%rsi),%rdx            # arg2, disp->ImageBase
578         mov     0(%rsi),%r8             # arg3, disp->ControlPc
579         mov     16(%rsi),%r9            # arg4, disp->FunctionEntry
580         mov     40(%rsi),%r10           # disp->ContextRecord
581         lea     56(%rsi),%r11           # &disp->HandlerData
582         lea     24(%rsi),%r12           # &disp->EstablisherFrame
583         mov     %r10,32(%rsp)           # arg5
584         mov     %r11,40(%rsp)           # arg6
585         mov     %r12,48(%rsp)           # arg7
586         mov     %rcx,56(%rsp)           # arg8, (NULL)
587         call    *__imp_RtlVirtualUnwind(%rip)
588
589         mov     \$1,%eax                # ExceptionContinueSearch
590         add     \$64,%rsp
591         popfq
592         pop     %r15
593         pop     %r14
594         pop     %r13
595         pop     %r12
596         pop     %rbp
597         pop     %rbx
598         pop     %rdi
599         pop     %rsi
600         ret
601 .size   se_handler,.-se_handler
602
603 .section        .pdata
604 .align  4
605         .rva    .LSEH_begin_$func
606         .rva    .LSEH_end_$func
607         .rva    .LSEH_info_$func
608
609 .section        .xdata
610 .align  8
611 .LSEH_info_$func:
612         .byte   9,0,0,0
613         .rva    se_handler
614 ___
615 }
616
617 $code =~ s/\`([^\`]*)\`/eval $1/gem;
618 print $code;
619 close STDOUT or die "error closing STDOUT: $!";