SHA-256/-512 x86_64 assembler module.
[openssl.git] / crypto / sha / asm / sha512-x86_64.pl
1 #!/usr/bin/env perl
2 #
3 # ====================================================================
4 # Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
5 # project. Rights for redistribution and usage in source and binary
6 # forms are granted according to the OpenSSL license.
7 # ====================================================================
8 #
9 # sha256/512_block procedure for x86_64.
10 #
11 # 40% improvement over compiler-generated code on Opteron. No magical
12 # tricks, just straight implementation... I really wonder why gcc
13 # [being armed with inline assembler] fails to generate as fast code.
14 # The only thing which is cool about this module is that it's very
15 # same instruction sequence used for both SHA-256 and SHA-512. In
16 # former case the instructions operate on 32-bit operands, while in
17 # latter - on 64-bit ones. All I had to do is to get one flavor right,
18 # the other one passed the test right away:-)
19 #
20 # sha256_block runs in ~1005 cycles on Opteron, which gives you
21 # asymptotic performance of 64*1000/1005=63.7MBps times CPU clock
22 # frequency in GHz. sha512_block runs in ~1275 cycles, which results
23 # in 128*1000/1275=100MBps per GHz. Is there room for improvement?
24 # Well, if you compare it to IA-64 implementation, which maintains
25 # X[16] in register bank[!], tends to 4 instructions per CPU clock
26 # cycle and runs in 1003 cycles, 1275 is very good result for 3-way
27 # issue Opteron pipeline and X[16] maintained in memory. So that *if*
28 # there is a way to improve it, *then* the only way would be to try to
29 # offload X[16] updates to SSE unit, but that would require "deeper"
30 # loop unroll, which in turn would naturally cause size blow-up, not
31 # to mention increased complexity! And once again, only *if* it's
32 # actually possible to noticeably improve overall ILP, instruction
33 # level parallelism, on a given CPU implementation in this case.
34 #
35 # Special note on Intel EM64T. While Opteron CPU exhibits perfect
36 # perfromance ratio of 1.5 between 64- and 32-bit flavors [see above],
37 # [currently available] EM64T CPUs apparently are far from it. 64-bit
38 # version, sha512_block, is hardly faster than 32-bit one. This is
39 # presumably because 64-bit shifts/rotates apparently are not atomic
40 # instructions, but implemented in microcode.
41
42 $output=shift;
43 open STDOUT,"| $^X ../perlasm/x86_64-xlate.pl $output";
44
45 if ($output =~ /512/) {
46         $func="sha512_block";
47         $TABLE="K512";
48         $SZ=8;
49         @ROT=($A,$B,$C,$D,$E,$F,$G,$H)=("%rax","%rbx","%rcx","%rdx",
50                                         "%r8", "%r9", "%r10","%r11");
51         ($T1,$a0,$a1,$a2)=("%r12","%r13","%r14","%r15");
52         @Sigma0=(28,34,39);
53         @Sigma1=(14,18,41);
54         @sigma0=(1,  8, 7);
55         @sigma1=(19,61, 6);
56         $rounds=80;
57 } else {
58         $func="sha256_block";
59         $TABLE="K256";
60         $SZ=4;
61         @ROT=($A,$B,$C,$D,$E,$F,$G,$H)=("%eax","%ebx","%ecx","%edx",
62                                         "%r8d","%r9d","%r10d","%r11d");
63         ($T1,$a0,$a1,$a2)=("%r12d","%r13d","%r14d","%r15d");
64         @Sigma0=( 2,13,22);
65         @Sigma1=( 6,11,25);
66         @sigma0=( 7,18, 3);
67         @sigma1=(17,19,10);
68         $rounds=64;
69 }
70
71 $ctx="%rdi";    # 1st arg
72 $round="%rdi";  # zaps $ctx
73 $inp="%rsi";    # 2nd arg
74 $Tbl="%rbp";
75
76 $_ctx="16*$SZ+0*8(%rsp)";
77 $_inp="16*$SZ+1*8(%rsp)";
78 $_end="16*$SZ+2*8(%rsp)";
79 $_ord="16*$SZ+3*8(%rsp)";
80 $_rsp="16*$SZ+4*8(%rsp)";
81 $framesz="16*$SZ+5*8";
82
83
84 sub ROUND_00_15()
85 { my ($i,$a,$b,$c,$d,$e,$f,$g,$h) = @_;
86
87 $code.=<<___;
88         mov     $e,$a0
89         mov     $e,$a1
90         mov     $f,$a2
91
92         ror     \$$Sigma1[0],$a0
93         ror     \$$Sigma1[1],$a1
94         xor     $g,$a2                  # f^g
95
96         xor     $a1,$a0
97         ror     \$`$Sigma1[2]-$Sigma1[1]`,$a1
98         and     $e,$a2                  # (f^g)&e
99         mov     $T1,`$SZ*($i&0xf)`(%rsp)
100
101         xor     $a1,$a0                 # Sigma1(e)
102         xor     $g,$a2                  # Ch(e,f,g)=((f^g)&e)^g
103         add     $h,$T1                  # T1+=h
104
105         mov     $a,$h
106         add     $a0,$T1                 # T1+=Sigma1(e)
107
108         add     $a2,$T1                 # T1+=Ch(e,f,g)
109         mov     $a,$a0
110         mov     $a,$a1
111
112         ror     \$$Sigma0[0],$h
113         ror     \$$Sigma0[1],$a0
114         mov     $a,$a2
115         add     ($Tbl,$round,$SZ),$T1   # T1+=K[round]
116
117         xor     $a0,$h
118         ror     \$`$Sigma0[2]-$Sigma0[1]`,$a0
119         or      $c,$a1                  # a|c
120
121         xor     $a0,$h                  # h=Sigma0(a)
122         and     $c,$a2                  # a&c
123         add     $T1,$d                  # d+=T1
124
125         and     $b,$a1                  # (a|c)&b
126         add     $T1,$h                  # h+=T1
127
128         or      $a2,$a1                 # Maj(a,b,c)=((a|c)&b)|(a&c)
129         lea     1($round),$round        # round++
130
131         add     $a1,$h                  # h+=Maj(a,b,c)
132 ___
133 }
134
135 sub ROUND_16_XX()
136 { my ($i,$a,$b,$c,$d,$e,$f,$g,$h) = @_;
137
138 $code.=<<___;
139         mov     `$SZ*(($i+1)&0xf)`(%rsp),$a0
140         mov     `$SZ*(($i+14)&0xf)`(%rsp),$T1
141
142         mov     $a0,$a2
143
144         shr     \$$sigma0[2],$a0
145         ror     \$$sigma0[0],$a2
146
147         xor     $a2,$a0
148         ror     \$`$sigma0[1]-$sigma0[0]`,$a2
149
150         xor     $a2,$a0                 # sigma0(X[(i+1)&0xf])
151         mov     $T1,$a1
152
153         shr     \$$sigma1[2],$T1
154         ror     \$$sigma1[0],$a1
155
156         xor     $a1,$T1
157         ror     \$`$sigma1[1]-$sigma1[0]`,$a1
158
159         xor     $a1,$T1                 # sigma1(X[(i+14)&0xf])
160
161         add     $a0,$T1
162
163         add     `$SZ*(($i+9)&0xf)`(%rsp),$T1
164
165         add     `$SZ*($i&0xf)`(%rsp),$T1
166 ___
167         &ROUND_00_15(@_);
168 }
169
170 $code=<<___;
171 .text
172
173 .globl  $func
174 .type   $func,\@function,4
175 .align  16
176 $func:
177         push    %rbx
178         push    %rbp
179         push    %r12
180         push    %r13
181         push    %r14
182         push    %r15
183         mov     %rsp,%rbp               # copy %rsp
184         shl     \$4,%rdx                # num*16
185         sub     \$$framesz,%rsp
186         lea     ($inp,%rdx,$SZ),%rdx    # inp+num*16*$SZ
187         and     \$-64,%rsp              # align stack frame
188         mov     $ctx,$_ctx              # save ctx, 1st arg
189         mov     $inp,$_inp              # save inp, 2nd arh
190         mov     %rdx,$_end              # save end pointer, "3rd" arg
191         mov     %ecx,$_ord              # save host, 4th arg
192         mov     %rbp,$_rsp              # save copy of %rsp
193
194         .picmeup $Tbl
195         lea     $TABLE-.($Tbl),$Tbl
196
197         mov     $SZ*0($ctx),$A
198         mov     $SZ*1($ctx),$B
199         mov     $SZ*2($ctx),$C
200         mov     $SZ*3($ctx),$D
201         mov     $SZ*4($ctx),$E
202         mov     $SZ*5($ctx),$F
203         mov     $SZ*6($ctx),$G
204         mov     $SZ*7($ctx),$H
205         jmp     .Lloop
206
207 .align  16
208 .Lloop:
209         xor     $round,$round
210 ___
211 if ($SZ==4) {
212 $code.=<<___;
213         cmpl    \$0,$_ord
214         je      .Ldata_order
215 .align  16
216 .Lhost_order:
217 ___
218
219         for($i=0;$i<16;$i++) {
220                 $code.="        mov     $SZ*$i($inp),$T1\n";
221                 &ROUND_00_15($i,@ROT);
222                 unshift(@ROT,pop(@ROT));
223         }
224 $code.=<<___;
225         jmp     .Lrounds_16_xx
226 .align  16
227 .Ldata_order:
228 ___
229 } # 256
230         for($i=0;$i<16;$i++) {
231                 $code.="        mov     $SZ*$i($inp),$T1\n";
232                 $code.="        bswap   $T1\n";
233                 &ROUND_00_15($i,@ROT);
234                 unshift(@ROT,pop(@ROT));
235         }
236 $code.=<<___;
237         jmp     .Lrounds_16_xx
238 .align  16
239 .Lrounds_16_xx:
240 ___
241         for(;$i<32;$i++) {
242                 &ROUND_16_XX($i,@ROT);
243                 unshift(@ROT,pop(@ROT));
244         }
245
246 $code.=<<___;
247         cmp     \$$rounds,$round
248         jb      .Lrounds_16_xx
249
250         mov     $_ctx,$ctx
251         lea     16*$SZ($inp),$inp
252
253         add     $SZ*0($ctx),$A
254         add     $SZ*1($ctx),$B
255         add     $SZ*2($ctx),$C
256         add     $SZ*3($ctx),$D
257         add     $SZ*4($ctx),$E
258         add     $SZ*5($ctx),$F
259         add     $SZ*6($ctx),$G
260         add     $SZ*7($ctx),$H
261
262         cmp     $_end,$inp
263
264         mov     $A,$SZ*0($ctx)
265         mov     $B,$SZ*1($ctx)
266         mov     $C,$SZ*2($ctx)
267         mov     $D,$SZ*3($ctx)
268         mov     $E,$SZ*4($ctx)
269         mov     $F,$SZ*5($ctx)
270         mov     $G,$SZ*6($ctx)
271         mov     $H,$SZ*7($ctx)
272         jb      .Lloop
273
274         mov     $_rsp,%rsp
275         pop     %r15
276         pop     %r14
277         pop     %r13
278         pop     %r12
279         pop     %rbp
280         pop     %rbx
281
282         ret
283 .size   $func,.-$func
284 ___
285
286 if ($SZ==4) {
287 $code.=<<___;
288 .align  64
289 .type   $TABLE,\@object
290 $TABLE:
291         .long   0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5
292         .long   0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5
293         .long   0xd807aa98,0x12835b01,0x243185be,0x550c7dc3
294         .long   0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174
295         .long   0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc
296         .long   0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da
297         .long   0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7
298         .long   0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967
299         .long   0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13
300         .long   0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85
301         .long   0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3
302         .long   0xd192e819,0xd6990624,0xf40e3585,0x106aa070
303         .long   0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5
304         .long   0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3
305         .long   0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208
306         .long   0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
307 ___
308 } else {
309 $code.=<<___;
310 .align  64
311 .type   $TABLE,\@object
312 $TABLE:
313         .quad   0x428a2f98d728ae22,0x7137449123ef65cd
314         .quad   0xb5c0fbcfec4d3b2f,0xe9b5dba58189dbbc
315         .quad   0x3956c25bf348b538,0x59f111f1b605d019
316         .quad   0x923f82a4af194f9b,0xab1c5ed5da6d8118
317         .quad   0xd807aa98a3030242,0x12835b0145706fbe
318         .quad   0x243185be4ee4b28c,0x550c7dc3d5ffb4e2
319         .quad   0x72be5d74f27b896f,0x80deb1fe3b1696b1
320         .quad   0x9bdc06a725c71235,0xc19bf174cf692694
321         .quad   0xe49b69c19ef14ad2,0xefbe4786384f25e3
322         .quad   0x0fc19dc68b8cd5b5,0x240ca1cc77ac9c65
323         .quad   0x2de92c6f592b0275,0x4a7484aa6ea6e483
324         .quad   0x5cb0a9dcbd41fbd4,0x76f988da831153b5
325         .quad   0x983e5152ee66dfab,0xa831c66d2db43210
326         .quad   0xb00327c898fb213f,0xbf597fc7beef0ee4
327         .quad   0xc6e00bf33da88fc2,0xd5a79147930aa725
328         .quad   0x06ca6351e003826f,0x142929670a0e6e70
329         .quad   0x27b70a8546d22ffc,0x2e1b21385c26c926
330         .quad   0x4d2c6dfc5ac42aed,0x53380d139d95b3df
331         .quad   0x650a73548baf63de,0x766a0abb3c77b2a8
332         .quad   0x81c2c92e47edaee6,0x92722c851482353b
333         .quad   0xa2bfe8a14cf10364,0xa81a664bbc423001
334         .quad   0xc24b8b70d0f89791,0xc76c51a30654be30
335         .quad   0xd192e819d6ef5218,0xd69906245565a910
336         .quad   0xf40e35855771202a,0x106aa07032bbd1b8
337         .quad   0x19a4c116b8d2d0c8,0x1e376c085141ab53
338         .quad   0x2748774cdf8eeb99,0x34b0bcb5e19b48a8
339         .quad   0x391c0cb3c5c95a63,0x4ed8aa4ae3418acb
340         .quad   0x5b9cca4f7763e373,0x682e6ff3d6b2b8a3
341         .quad   0x748f82ee5defb2fc,0x78a5636f43172f60
342         .quad   0x84c87814a1f0ab72,0x8cc702081a6439ec
343         .quad   0x90befffa23631e28,0xa4506cebde82bde9
344         .quad   0xbef9a3f7b2c67915,0xc67178f2e372532b
345         .quad   0xca273eceea26619c,0xd186b8c721c0c207
346         .quad   0xeada7dd6cde0eb1e,0xf57d4f7fee6ed178
347         .quad   0x06f067aa72176fba,0x0a637dc5a2c898a6
348         .quad   0x113f9804bef90dae,0x1b710b35131c471b
349         .quad   0x28db77f523047d84,0x32caab7b40c72493
350         .quad   0x3c9ebe0a15c9bebc,0x431d67c49c100d4c
351         .quad   0x4cc5d4becb3e42b6,0x597f299cfc657e2a
352         .quad   0x5fcb6fab3ad6faec,0x6c44198c4a475817
353 ___
354 }
355
356 $code =~ s/\`([^\`]*)\`/eval $1/gem;
357 print $code;
358 close STDOUT;