gcm128.c: API modification and readability improvements,
[openssl.git] / crypto / modes / asm / ghash-alpha.pl
1 #!/usr/bin/env perl
2 #
3 # ====================================================================
4 # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
5 # project. The module is, however, dual licensed under OpenSSL and
6 # CRYPTOGAMS licenses depending on where you obtain it. For further
7 # details see http://www.openssl.org/~appro/cryptogams/.
8 # ====================================================================
9 #
10 # March 2010
11 #
12 # The module implements "4-bit" GCM GHASH function and underlying
13 # single multiplication operation in GF(2^128). "4-bit" means that it
14 # uses 256 bytes per-key table [+128 bytes shared table]. Even though
15 # loops are aggressively modulo-scheduled in respect to references to
16 # Htbl and Z.hi updates for 8 cycles per byte, measured performance is
17 # ~12 cycles per processed byte on 21264 CPU. It seems to be a dynamic
18 # scheduling "glitch," because uprofile(1) indicates uniform sample
19 # distribution, as if all instruction bundles execute in 1.5 cycles.
20 # Meaning that it could have been even faster, yet 12 cycles is ~60%
21 # better than gcc-generated code and ~80% than code generated by vendor
22 # compiler.
23
24 $cnt="v0";      # $0
25 $t0="t0";
26 $t1="t1";
27 $t2="t2";
28 $Thi0="t3";     # $4
29 $Tlo0="t4";
30 $Thi1="t5";
31 $Tlo1="t6";
32 $rem="t7";      # $8
33 #################
34 $Xi="a0";       # $16, input argument block
35 $Htbl="a1";
36 $inp="a2";
37 $len="a3";
38 $nlo="a4";      # $20
39 $nhi="a5";
40 $Zhi="t8";
41 $Zlo="t9";
42 $Xhi="t10";     # $24
43 $Xlo="t11";
44 $remp="t12";
45 $rem_4bit="AT"; # $28
46
47 { my $N;
48   sub loop() {
49
50         $N++;
51 $code.=<<___;
52 .align  4
53         extbl   $Xlo,7,$nlo
54         and     $nlo,0xf0,$nhi
55         sll     $nlo,4,$nlo
56         and     $nlo,0xf0,$nlo
57
58         addq    $nlo,$Htbl,$nlo
59         ldq     $Zlo,8($nlo)
60         addq    $nhi,$Htbl,$nhi
61         ldq     $Zhi,0($nlo)
62
63         and     $Zlo,0x0f,$remp
64         sll     $Zhi,60,$t0
65         lda     $cnt,6(zero)
66         extbl   $Xlo,6,$nlo
67
68         ldq     $Tlo1,8($nhi)
69         s8addq  $remp,$rem_4bit,$remp
70         ldq     $Thi1,0($nhi)
71         srl     $Zlo,4,$Zlo
72
73         ldq     $rem,0($remp)
74         srl     $Zhi,4,$Zhi
75         xor     $t0,$Zlo,$Zlo
76         and     $nlo,0xf0,$nhi
77
78         xor     $Tlo1,$Zlo,$Zlo
79         sll     $nlo,4,$nlo
80         xor     $Thi1,$Zhi,$Zhi
81         and     $nlo,0xf0,$nlo
82
83         addq    $nlo,$Htbl,$nlo
84         ldq     $Tlo0,8($nlo)
85         addq    $nhi,$Htbl,$nhi
86         ldq     $Thi0,0($nlo)
87
88 .Looplo$N:
89         and     $Zlo,0x0f,$remp
90         sll     $Zhi,60,$t0
91         subq    $cnt,1,$cnt
92         srl     $Zlo,4,$Zlo
93
94         ldq     $Tlo1,8($nhi)
95         xor     $rem,$Zhi,$Zhi
96         ldq     $Thi1,0($nhi)
97         s8addq  $remp,$rem_4bit,$remp
98
99         ldq     $rem,0($remp)
100         srl     $Zhi,4,$Zhi
101         xor     $t0,$Zlo,$Zlo
102         extbl   $Xlo,$cnt,$nlo
103
104         and     $nlo,0xf0,$nhi
105         xor     $Thi0,$Zhi,$Zhi
106         xor     $Tlo0,$Zlo,$Zlo
107         sll     $nlo,4,$nlo
108
109
110         and     $Zlo,0x0f,$remp
111         sll     $Zhi,60,$t0
112         and     $nlo,0xf0,$nlo
113         srl     $Zlo,4,$Zlo
114
115         s8addq  $remp,$rem_4bit,$remp
116         xor     $rem,$Zhi,$Zhi
117         addq    $nlo,$Htbl,$nlo
118         addq    $nhi,$Htbl,$nhi
119
120         ldq     $rem,0($remp)
121         srl     $Zhi,4,$Zhi
122         ldq     $Tlo0,8($nlo)
123         xor     $t0,$Zlo,$Zlo
124
125         xor     $Tlo1,$Zlo,$Zlo
126         xor     $Thi1,$Zhi,$Zhi
127         ldq     $Thi0,0($nlo)
128         bne     $cnt,.Looplo$N
129
130
131         and     $Zlo,0x0f,$remp
132         sll     $Zhi,60,$t0
133         lda     $cnt,7(zero)
134         srl     $Zlo,4,$Zlo
135
136         ldq     $Tlo1,8($nhi)
137         xor     $rem,$Zhi,$Zhi
138         ldq     $Thi1,0($nhi)
139         s8addq  $remp,$rem_4bit,$remp
140
141         ldq     $rem,0($remp)
142         srl     $Zhi,4,$Zhi
143         xor     $t0,$Zlo,$Zlo
144         extbl   $Xhi,$cnt,$nlo
145
146         and     $nlo,0xf0,$nhi
147         xor     $Thi0,$Zhi,$Zhi
148         xor     $Tlo0,$Zlo,$Zlo
149         sll     $nlo,4,$nlo
150
151         and     $Zlo,0x0f,$remp
152         sll     $Zhi,60,$t0
153         and     $nlo,0xf0,$nlo
154         srl     $Zlo,4,$Zlo
155
156         s8addq  $remp,$rem_4bit,$remp
157         xor     $rem,$Zhi,$Zhi
158         addq    $nlo,$Htbl,$nlo
159         addq    $nhi,$Htbl,$nhi
160
161         ldq     $rem,0($remp)
162         srl     $Zhi,4,$Zhi
163         ldq     $Tlo0,8($nlo)
164         xor     $t0,$Zlo,$Zlo
165
166         xor     $Tlo1,$Zlo,$Zlo
167         xor     $Thi1,$Zhi,$Zhi
168         ldq     $Thi0,0($nlo)
169         unop
170
171
172 .Loophi$N:
173         and     $Zlo,0x0f,$remp
174         sll     $Zhi,60,$t0
175         subq    $cnt,1,$cnt
176         srl     $Zlo,4,$Zlo
177
178         ldq     $Tlo1,8($nhi)
179         xor     $rem,$Zhi,$Zhi
180         ldq     $Thi1,0($nhi)
181         s8addq  $remp,$rem_4bit,$remp
182
183         ldq     $rem,0($remp)
184         srl     $Zhi,4,$Zhi
185         xor     $t0,$Zlo,$Zlo
186         extbl   $Xhi,$cnt,$nlo
187
188         and     $nlo,0xf0,$nhi
189         xor     $Thi0,$Zhi,$Zhi
190         xor     $Tlo0,$Zlo,$Zlo
191         sll     $nlo,4,$nlo
192
193
194         and     $Zlo,0x0f,$remp
195         sll     $Zhi,60,$t0
196         and     $nlo,0xf0,$nlo
197         srl     $Zlo,4,$Zlo
198
199         s8addq  $remp,$rem_4bit,$remp
200         xor     $rem,$Zhi,$Zhi
201         addq    $nlo,$Htbl,$nlo
202         addq    $nhi,$Htbl,$nhi
203
204         ldq     $rem,0($remp)
205         srl     $Zhi,4,$Zhi
206         ldq     $Tlo0,8($nlo)
207         xor     $t0,$Zlo,$Zlo
208
209         xor     $Tlo1,$Zlo,$Zlo
210         xor     $Thi1,$Zhi,$Zhi
211         ldq     $Thi0,0($nlo)
212         bne     $cnt,.Loophi$N
213
214
215         and     $Zlo,0x0f,$remp
216         sll     $Zhi,60,$t0
217         srl     $Zlo,4,$Zlo
218
219         ldq     $Tlo1,8($nhi)
220         xor     $rem,$Zhi,$Zhi
221         ldq     $Thi1,0($nhi)
222         s8addq  $remp,$rem_4bit,$remp
223
224         ldq     $rem,0($remp)
225         srl     $Zhi,4,$Zhi
226         xor     $t0,$Zlo,$Zlo
227
228         xor     $Tlo0,$Zlo,$Zlo
229         xor     $Thi0,$Zhi,$Zhi
230
231         and     $Zlo,0x0f,$remp
232         sll     $Zhi,60,$t0
233         srl     $Zlo,4,$Zlo
234
235         s8addq  $remp,$rem_4bit,$remp
236         xor     $rem,$Zhi,$Zhi
237
238         ldq     $rem,0($remp)
239         srl     $Zhi,4,$Zhi
240         xor     $Tlo1,$Zlo,$Zlo
241         xor     $Thi1,$Zhi,$Zhi
242         xor     $t0,$Zlo,$Zlo
243         xor     $rem,$Zhi,$Zhi
244 ___
245 }}
246
247 $code=<<___;
248 #include <asm.h>
249 #include <regdef.h>
250
251 .text
252
253 .set    noat
254 .set    noreorder
255 .globl  gcm_gmult_4bit
256 .align  4
257 .ent    gcm_gmult_4bit
258 gcm_gmult_4bit:
259         .frame  sp,0,ra
260         .prologue 0
261
262         ldq     $Xlo,8($Xi)
263         ldq     $Xhi,0($Xi)
264
265         br      $rem_4bit,.Lpic1
266 .Lpic1: lda     $rem_4bit,rem_4bit-.Lpic1($rem_4bit)
267 ___
268
269         &loop();
270
271 $code.=<<___;
272         srl     $Zlo,24,$t0     # byte swap
273         srl     $Zlo,8,$t1
274
275         sll     $Zlo,8,$t2
276         sll     $Zlo,24,$Zlo
277         zapnot  $t0,0x11,$t0
278         zapnot  $t1,0x22,$t1
279
280         zapnot  $Zlo,0x88,$Zlo
281         or      $t0,$t1,$t0
282         zapnot  $t2,0x44,$t2
283
284         or      $Zlo,$t0,$Zlo
285         srl     $Zhi,24,$t0
286         srl     $Zhi,8,$t1
287
288         or      $Zlo,$t2,$Zlo
289         sll     $Zhi,8,$t2
290         sll     $Zhi,24,$Zhi
291
292         srl     $Zlo,32,$Xlo
293         sll     $Zlo,32,$Zlo
294
295         zapnot  $t0,0x11,$t0
296         zapnot  $t1,0x22,$t1
297         or      $Zlo,$Xlo,$Xlo
298
299         zapnot  $Zhi,0x88,$Zhi
300         or      $t0,$t1,$t0
301         zapnot  $t2,0x44,$t2
302
303         or      $Zhi,$t0,$Zhi
304         or      $Zhi,$t2,$Zhi
305
306         srl     $Zhi,32,$Xhi
307         sll     $Zhi,32,$Zhi
308
309         or      $Zhi,$Xhi,$Xhi
310         stq     $Xlo,8($Xi)
311         stq     $Xhi,0($Xi)
312
313         ret     (ra)
314 .end    gcm_gmult_4bit
315 ___
316
317 $inhi="s0";
318 $inlo="s1";
319
320 $code.=<<___;
321 .globl  gcm_ghash_4bit
322 .align  4
323 .ent    gcm_ghash_4bit
324 gcm_ghash_4bit:
325         lda     sp,-32(sp)
326         stq     ra,0(sp)
327         stq     s0,8(sp)
328         stq     s1,16(sp)
329         .mask   0x04000600,-32
330         .frame  sp,32,ra
331         .prologue 0
332
333         ldq_u   $inhi,0($inp)
334         ldq_u   $Thi0,7($inp)
335         ldq_u   $inlo,8($inp)
336         ldq_u   $Tlo0,15($inp)
337         ldq     $Xhi,0($Xi)
338         ldq     $Xlo,8($Xi)
339
340         br      $rem_4bit,.Lpic2
341 .Lpic2: lda     $rem_4bit,rem_4bit-.Lpic2($rem_4bit)
342
343 .Louter:
344         extql   $inhi,$inp,$inhi
345         extqh   $Thi0,$inp,$Thi0
346         or      $inhi,$Thi0,$inhi
347         lda     $inp,16($inp)
348
349         extql   $inlo,$inp,$inlo
350         extqh   $Tlo0,$inp,$Tlo0
351         or      $inlo,$Tlo0,$inlo
352         subq    $len,16,$len
353
354         xor     $Xlo,$inlo,$Xlo
355         xor     $Xhi,$inhi,$Xhi
356 ___
357
358         &loop();
359
360 $code.=<<___;
361         srl     $Zlo,24,$t0     # byte swap
362         srl     $Zlo,8,$t1
363
364         sll     $Zlo,8,$t2
365         sll     $Zlo,24,$Zlo
366         zapnot  $t0,0x11,$t0
367         zapnot  $t1,0x22,$t1
368
369         zapnot  $Zlo,0x88,$Zlo
370         or      $t0,$t1,$t0
371         zapnot  $t2,0x44,$t2
372
373         or      $Zlo,$t0,$Zlo
374         srl     $Zhi,24,$t0
375         srl     $Zhi,8,$t1
376
377         or      $Zlo,$t2,$Zlo
378         sll     $Zhi,8,$t2
379         sll     $Zhi,24,$Zhi
380
381         srl     $Zlo,32,$Xlo
382         sll     $Zlo,32,$Zlo
383         beq     $len,.Ldone
384
385         zapnot  $t0,0x11,$t0
386         zapnot  $t1,0x22,$t1
387         or      $Zlo,$Xlo,$Xlo
388         ldq_u   $inhi,0($inp)
389
390         zapnot  $Zhi,0x88,$Zhi
391         or      $t0,$t1,$t0
392         zapnot  $t2,0x44,$t2
393         ldq_u   $Thi0,7($inp)
394
395         or      $Zhi,$t0,$Zhi
396         or      $Zhi,$t2,$Zhi
397         ldq_u   $inlo,8($inp)
398         ldq_u   $Tlo0,15($inp)
399
400         srl     $Zhi,32,$Xhi
401         sll     $Zhi,32,$Zhi
402
403         or      $Zhi,$Xhi,$Xhi
404         br      zero,.Louter
405
406 .Ldone:
407         zapnot  $t0,0x11,$t0
408         zapnot  $t1,0x22,$t1
409         or      $Zlo,$Xlo,$Xlo
410
411         zapnot  $Zhi,0x88,$Zhi
412         or      $t0,$t1,$t0
413         zapnot  $t2,0x44,$t2
414
415         or      $Zhi,$t0,$Zhi
416         or      $Zhi,$t2,$Zhi
417
418         srl     $Zhi,32,$Xhi
419         sll     $Zhi,32,$Zhi
420
421         or      $Zhi,$Xhi,$Xhi
422
423         stq     $Xlo,8($Xi)
424         stq     $Xhi,0($Xi)
425
426         .set    noreorder
427         /*ldq   ra,0(sp)*/
428         ldq     s0,8(sp)
429         ldq     s1,16(sp)
430         lda     sp,32(sp)
431         ret     (ra)
432 .end    gcm_ghash_4bit
433
434 .align  4
435 rem_4bit:
436         .quad   0x0000<<48, 0x1C20<<48, 0x3840<<48, 0x2460<<48
437         .quad   0x7080<<48, 0x6CA0<<48, 0x48C0<<48, 0x54E0<<48
438         .quad   0xE100<<48, 0xFD20<<48, 0xD940<<48, 0xC560<<48
439         .quad   0x9180<<48, 0x8DA0<<48, 0xA9C0<<48, 0xB5E0<<48
440 .asciiz "GHASH for Alpha, CRYPTOGAMS by <appro\@openssl.org>"
441 .align  4
442
443 ___
444 $output=shift and open STDOUT,">$output";
445 print $code;
446 close STDOUT;
447