perlasm/sparcv9_modes.pl: addendum to commit#22966.
[openssl.git] / crypto / perlasm / sparcv9_modes.pl
1 #!/usr/bin/env perl
2
3 # Specific modes implementations for SPARC Architecture 2011. There
4 # is T4 dependency though, an ASI value that is not specified in the
5 # Architecture Manual. But as SPARC universe is rather monocultural,
6 # we imply that processor capable of executing crypto instructions
7 # can handle the ASI in question as well. This means that we ought to
8 # keep eyes open when new processors emerge...
9 #
10 # As for above mentioned ASI. It's so called "block initializing
11 # store" which cancels "read" in "read-update-write" on cache lines.
12 # This is "cooperative" optimization, as it reduces overall pressure
13 # on memory interface. Benefits can't be observed/quantified with
14 # usual benchmarks, on the contrary you can notice that single-thread
15 # performance for parallelizable modes is ~1.5% worse for largest
16 # block sizes [though few percent better for not so long ones]. All
17 # this based on suggestions from David Miller.
18
19 sub asm_init {          # to be called with @ARGV as argument
20     for (@_)            { $::abibits=64 if (/\-m64/ || /\-xarch\=v9/); }
21     if ($::abibits==64) { $::bias=2047; $::frame=192; $::size_t_cc="%xcc"; }
22     else                { $::bias=0;    $::frame=112; $::size_t_cc="%icc"; }
23 }
24
25 # unified interface
26 my ($inp,$out,$len,$key,$ivec)=map("%i$_",(0..5));
27 # local variables
28 my ($ileft,$iright,$ooff,$omask,$ivoff,$blk_init)=map("%l$_",(0..7));
29
30 sub alg_cbc_encrypt_implement {
31 my ($alg,$bits) = @_;
32
33 $::code.=<<___;
34 .globl  ${alg}${bits}_t4_cbc_encrypt
35 .align  32
36 ${alg}${bits}_t4_cbc_encrypt:
37         save            %sp, -$::frame, %sp
38         sub             $inp, $out, $blk_init   ! $inp!=$out
39 ___
40 $::code.=<<___ if (!$::evp);
41         andcc           $ivec, 7, $ivoff
42         alignaddr       $ivec, %g0, $ivec
43
44         ldd             [$ivec + 0], %f0        ! load ivec
45         bz,pt           %icc, 1f
46         ldd             [$ivec + 8], %f2
47         ldd             [$ivec + 16], %f4
48         faligndata      %f0, %f2, %f0
49         faligndata      %f2, %f4, %f2
50 1:
51 ___
52 $::code.=<<___ if ($::evp);
53         ld              [$ivec + 0], %f0
54         ld              [$ivec + 4], %f1
55         ld              [$ivec + 8], %f2
56         ld              [$ivec + 12], %f3
57 ___
58 $::code.=<<___;
59         prefetch        [$inp], 20
60         prefetch        [$inp + 63], 20
61         call            _${alg}${bits}_load_enckey
62         and             $inp, 7, $ileft
63         andn            $inp, 7, $inp
64         sll             $ileft, 3, $ileft
65         mov             64, $iright
66         mov             0xff, $omask
67         sub             $iright, $ileft, $iright
68         and             $out, 7, $ooff
69         cmp             $len, 127
70         movrnz          $ooff, 0, $blk_init             ! if (  $out&7 ||
71         movleu          $::size_t_cc, 0, $blk_init      !       $len<128 ||
72         brnz,pn         $blk_init, .L${bits}cbc_enc_blk !       $inp==$out)
73         srl             $omask, $ooff, $omask
74
75         alignaddrl      $out, %g0, $out
76         srlx            $len, 4, $len
77         prefetch        [$out], 22
78
79 .L${bits}_cbc_enc_loop:
80         ldx             [$inp + 0], %o0
81         brz,pt          $ileft, 4f
82         ldx             [$inp + 8], %o1
83
84         ldx             [$inp + 16], %o2
85         sllx            %o0, $ileft, %o0
86         srlx            %o1, $iright, %g1
87         sllx            %o1, $ileft, %o1
88         or              %g1, %o0, %o0
89         srlx            %o2, $iright, %o2
90         or              %o2, %o1, %o1
91 4:
92         xor             %g4, %o0, %o0           ! ^= rk[0]
93         xor             %g5, %o1, %o1
94         movxtod         %o0, %f12
95         movxtod         %o1, %f14
96
97         fxor            %f12, %f0, %f0          ! ^= ivec
98         fxor            %f14, %f2, %f2
99         prefetch        [$out + 63], 22
100         prefetch        [$inp + 16+63], 20
101         call            _${alg}${bits}_encrypt_1x
102         add             $inp, 16, $inp
103
104         brnz,pn         $ooff, 2f
105         sub             $len, 1, $len
106                 
107         std             %f0, [$out + 0]
108         std             %f2, [$out + 8]
109         brnz,pt         $len, .L${bits}_cbc_enc_loop
110         add             $out, 16, $out
111 ___
112 $::code.=<<___ if ($::evp);
113         st              %f0, [$ivec + 0]
114         st              %f1, [$ivec + 4]
115         st              %f2, [$ivec + 8]
116         st              %f3, [$ivec + 12]
117 ___
118 $::code.=<<___ if (!$::evp);
119         brnz,pn         $ivoff, 3f
120         nop
121
122         std             %f0, [$ivec + 0]        ! write out ivec
123         std             %f2, [$ivec + 8]
124 ___
125 $::code.=<<___;
126         ret
127         restore
128
129 .align  16
130 2:      ldxa            [$inp]0x82, %o0         ! avoid read-after-write hazard
131                                                 ! and ~3x deterioration
132                                                 ! in inp==out case
133         faligndata      %f0, %f0, %f4           ! handle unaligned output
134         faligndata      %f0, %f2, %f6
135         faligndata      %f2, %f2, %f8
136
137         stda            %f4, [$out + $omask]0xc0        ! partial store
138         std             %f6, [$out + 8]
139         add             $out, 16, $out
140         orn             %g0, $omask, $omask
141         stda            %f8, [$out + $omask]0xc0        ! partial store
142
143         brnz,pt         $len, .L${bits}_cbc_enc_loop+4
144         orn             %g0, $omask, $omask
145 ___
146 $::code.=<<___ if ($::evp);
147         st              %f0, [$ivec + 0]
148         st              %f1, [$ivec + 4]
149         st              %f2, [$ivec + 8]
150         st              %f3, [$ivec + 12]
151 ___
152 $::code.=<<___ if (!$::evp);
153         brnz,pn         $ivoff, 3f
154         nop
155
156         std             %f0, [$ivec + 0]        ! write out ivec
157         std             %f2, [$ivec + 8]
158         ret
159         restore
160
161 .align  16
162 3:      alignaddrl      $ivec, $ivoff, %g0      ! handle unaligned ivec
163         mov             0xff, $omask
164         srl             $omask, $ivoff, $omask
165         faligndata      %f0, %f0, %f4
166         faligndata      %f0, %f2, %f6
167         faligndata      %f2, %f2, %f8
168         stda            %f4, [$ivec + $omask]0xc0
169         std             %f6, [$ivec + 8]
170         add             $ivec, 16, $ivec
171         orn             %g0, $omask, $omask
172         stda            %f8, [$ivec + $omask]0xc0
173 ___
174 $::code.=<<___;
175         ret
176         restore
177
178 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
179 .align  32
180 .L${bits}cbc_enc_blk:
181         add     $out, $len, $blk_init
182         and     $blk_init, 63, $blk_init        ! tail
183         sub     $len, $blk_init, $len
184         add     $blk_init, 15, $blk_init        ! round up to 16n
185         srlx    $len, 4, $len
186         srl     $blk_init, 4, $blk_init
187
188 .L${bits}_cbc_enc_blk_loop:
189         ldx             [$inp + 0], %o0
190         brz,pt          $ileft, 5f
191         ldx             [$inp + 8], %o1
192
193         ldx             [$inp + 16], %o2
194         sllx            %o0, $ileft, %o0
195         srlx            %o1, $iright, %g1
196         sllx            %o1, $ileft, %o1
197         or              %g1, %o0, %o0
198         srlx            %o2, $iright, %o2
199         or              %o2, %o1, %o1
200 5:
201         xor             %g4, %o0, %o0           ! ^= rk[0]
202         xor             %g5, %o1, %o1
203         movxtod         %o0, %f12
204         movxtod         %o1, %f14
205
206         fxor            %f12, %f0, %f0          ! ^= ivec
207         fxor            %f14, %f2, %f2
208         prefetch        [$inp + 16+63], 20
209         call            _${alg}${bits}_encrypt_1x
210         add             $inp, 16, $inp
211         sub             $len, 1, $len
212                 
213         stda            %f0, [$out]0xe2         ! ASI_BLK_INIT, T4-specific
214         add             $out, 8, $out
215         stda            %f2, [$out]0xe2         ! ASI_BLK_INIT, T4-specific
216         brnz,pt         $len, .L${bits}_cbc_enc_blk_loop
217         add             $out, 8, $out
218
219         membar          #StoreLoad|#StoreStore
220         brnz,pt         $blk_init, .L${bits}_cbc_enc_loop
221         mov             $blk_init, $len
222 ___
223 $::code.=<<___ if ($::evp);
224         st              %f0, [$ivec + 0]
225         st              %f1, [$ivec + 4]
226         st              %f2, [$ivec + 8]
227         st              %f3, [$ivec + 12]
228 ___
229 $::code.=<<___ if (!$::evp);
230         brnz,pn         $ivoff, 3b
231         nop
232
233         std             %f0, [$ivec + 0]        ! write out ivec
234         std             %f2, [$ivec + 8]
235 ___
236 $::code.=<<___;
237         ret
238         restore
239 .type   ${alg}${bits}_t4_cbc_encrypt,#function
240 .size   ${alg}${bits}_t4_cbc_encrypt,.-${alg}${bits}_t4_cbc_encrypt
241 ___
242 }
243
244 sub alg_cbc_decrypt_implement {
245 my ($alg,$bits) = @_;
246
247 $::code.=<<___;
248 .globl  ${alg}${bits}_t4_cbc_decrypt
249 .align  32
250 ${alg}${bits}_t4_cbc_decrypt:
251         save            %sp, -$::frame, %sp
252         sub             $inp, $out, $blk_init   ! $inp!=$out
253 ___
254 $::code.=<<___ if (!$::evp);
255         andcc           $ivec, 7, $ivoff
256         alignaddr       $ivec, %g0, $ivec
257
258         ldd             [$ivec + 0], %f12       ! load ivec
259         bz,pt           %icc, 1f
260         ldd             [$ivec + 8], %f14
261         ldd             [$ivec + 16], %f0
262         faligndata      %f12, %f14, %f12
263         faligndata      %f14, %f0, %f14
264 1:
265 ___
266 $::code.=<<___ if ($::evp);
267         ld              [$ivec + 0], %f12       ! load ivec
268         ld              [$ivec + 4], %f13
269         ld              [$ivec + 8], %f14
270         ld              [$ivec + 12], %f15
271 ___
272 $::code.=<<___;
273         prefetch        [$inp], 20
274         prefetch        [$inp + 63], 20
275         call            _${alg}${bits}_load_deckey
276         and             $inp, 7, $ileft
277         andn            $inp, 7, $inp
278         sll             $ileft, 3, $ileft
279         mov             64, $iright
280         mov             0xff, $omask
281         sub             $iright, $ileft, $iright
282         and             $out, 7, $ooff
283         cmp             $len, 255
284         movrnz          $ooff, 0, $blk_init             ! if (  $out&7 ||
285         movleu          $::size_t_cc, 0, $blk_init      !       $len<256 ||
286         brnz,pn         $blk_init, .L${bits}cbc_dec_blk !       $inp==$out)
287         srl             $omask, $ooff, $omask
288
289         andcc           $len, 16, %g0           ! is number of blocks even?
290         srlx            $len, 4, $len
291         alignaddrl      $out, %g0, $out
292         bz              %icc, .L${bits}_cbc_dec_loop2x
293         prefetch        [$out], 22
294 .L${bits}_cbc_dec_loop:
295         ldx             [$inp + 0], %o0
296         brz,pt          $ileft, 4f
297         ldx             [$inp + 8], %o1
298
299         ldx             [$inp + 16], %o2
300         sllx            %o0, $ileft, %o0
301         srlx            %o1, $iright, %g1
302         sllx            %o1, $ileft, %o1
303         or              %g1, %o0, %o0
304         srlx            %o2, $iright, %o2
305         or              %o2, %o1, %o1
306 4:
307         xor             %g4, %o0, %o2           ! ^= rk[0]
308         xor             %g5, %o1, %o3
309         movxtod         %o2, %f0
310         movxtod         %o3, %f2
311
312         prefetch        [$out + 63], 22
313         prefetch        [$inp + 16+63], 20
314         call            _${alg}${bits}_decrypt_1x
315         add             $inp, 16, $inp
316
317         fxor            %f12, %f0, %f0          ! ^= ivec
318         fxor            %f14, %f2, %f2
319         movxtod         %o0, %f12
320         movxtod         %o1, %f14
321
322         brnz,pn         $ooff, 2f
323         sub             $len, 1, $len
324                 
325         std             %f0, [$out + 0]
326         std             %f2, [$out + 8]
327         brnz,pt         $len, .L${bits}_cbc_dec_loop2x
328         add             $out, 16, $out
329 ___
330 $::code.=<<___ if ($::evp);
331         st              %f12, [$ivec + 0]
332         st              %f13, [$ivec + 4]
333         st              %f14, [$ivec + 8]
334         st              %f15, [$ivec + 12]
335 ___
336 $::code.=<<___ if (!$::evp);
337         brnz,pn         $ivoff, .L${bits}_cbc_dec_unaligned_ivec
338         nop
339
340         std             %f12, [$ivec + 0]       ! write out ivec
341         std             %f14, [$ivec + 8]
342 ___
343 $::code.=<<___;
344         ret
345         restore
346
347 .align  16
348 2:      ldxa            [$inp]0x82, %o0         ! avoid read-after-write hazard
349                                                 ! and ~3x deterioration
350                                                 ! in inp==out case
351         faligndata      %f0, %f0, %f4           ! handle unaligned output
352         faligndata      %f0, %f2, %f6
353         faligndata      %f2, %f2, %f8
354
355         stda            %f4, [$out + $omask]0xc0        ! partial store
356         std             %f6, [$out + 8]
357         add             $out, 16, $out
358         orn             %g0, $omask, $omask
359         stda            %f8, [$out + $omask]0xc0        ! partial store
360
361         brnz,pt         $len, .L${bits}_cbc_dec_loop2x+4
362         orn             %g0, $omask, $omask
363 ___
364 $::code.=<<___ if ($::evp);
365         st              %f12, [$ivec + 0]
366         st              %f13, [$ivec + 4]
367         st              %f14, [$ivec + 8]
368         st              %f15, [$ivec + 12]
369 ___
370 $::code.=<<___ if (!$::evp);
371         brnz,pn         $ivoff, .L${bits}_cbc_dec_unaligned_ivec
372         nop
373
374         std             %f12, [$ivec + 0]       ! write out ivec
375         std             %f14, [$ivec + 8]
376 ___
377 $::code.=<<___;
378         ret
379         restore
380
381 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
382 .align  32
383 .L${bits}_cbc_dec_loop2x:
384         ldx             [$inp + 0], %o0
385         ldx             [$inp + 8], %o1
386         ldx             [$inp + 16], %o2
387         brz,pt          $ileft, 4f
388         ldx             [$inp + 24], %o3
389
390         ldx             [$inp + 32], %o4
391         sllx            %o0, $ileft, %o0
392         srlx            %o1, $iright, %g1
393         or              %g1, %o0, %o0
394         sllx            %o1, $ileft, %o1
395         srlx            %o2, $iright, %g1
396         or              %g1, %o1, %o1
397         sllx            %o2, $ileft, %o2
398         srlx            %o3, $iright, %g1
399         or              %g1, %o2, %o2
400         sllx            %o3, $ileft, %o3
401         srlx            %o4, $iright, %o4
402         or              %o4, %o3, %o3
403 4:
404         xor             %g4, %o0, %o4           ! ^= rk[0]
405         xor             %g5, %o1, %o5
406         movxtod         %o4, %f0
407         movxtod         %o5, %f2
408         xor             %g4, %o2, %o4
409         xor             %g5, %o3, %o5
410         movxtod         %o4, %f4
411         movxtod         %o5, %f6
412
413         prefetch        [$out + 63], 22
414         prefetch        [$inp + 32+63], 20
415         call            _${alg}${bits}_decrypt_2x
416         add             $inp, 32, $inp
417
418         movxtod         %o0, %f8
419         movxtod         %o1, %f10
420         fxor            %f12, %f0, %f0          ! ^= ivec
421         fxor            %f14, %f2, %f2
422         movxtod         %o2, %f12
423         movxtod         %o3, %f14
424         fxor            %f8, %f4, %f4
425         fxor            %f10, %f6, %f6
426
427         brnz,pn         $ooff, 2f
428         sub             $len, 2, $len
429                 
430         std             %f0, [$out + 0]
431         std             %f2, [$out + 8]
432         std             %f4, [$out + 16]
433         std             %f6, [$out + 24]
434         brnz,pt         $len, .L${bits}_cbc_dec_loop2x
435         add             $out, 32, $out
436 ___
437 $::code.=<<___ if ($::evp);
438         st              %f12, [$ivec + 0]
439         st              %f13, [$ivec + 4]
440         st              %f14, [$ivec + 8]
441         st              %f15, [$ivec + 12]
442 ___
443 $::code.=<<___ if (!$::evp);
444         brnz,pn         $ivoff, .L${bits}_cbc_dec_unaligned_ivec
445         nop
446
447         std             %f12, [$ivec + 0]       ! write out ivec
448         std             %f14, [$ivec + 8]
449 ___
450 $::code.=<<___;
451         ret
452         restore
453
454 .align  16
455 2:      ldxa            [$inp]0x82, %o0         ! avoid read-after-write hazard
456                                                 ! and ~3x deterioration
457                                                 ! in inp==out case
458         faligndata      %f0, %f0, %f8           ! handle unaligned output
459         faligndata      %f0, %f2, %f0
460         faligndata      %f2, %f4, %f2
461         faligndata      %f4, %f6, %f4
462         faligndata      %f6, %f6, %f6
463         stda            %f8, [$out + $omask]0xc0        ! partial store
464         std             %f0, [$out + 8]
465         std             %f2, [$out + 16]
466         std             %f4, [$out + 24]
467         add             $out, 32, $out
468         orn             %g0, $omask, $omask
469         stda            %f6, [$out + $omask]0xc0        ! partial store
470
471         brnz,pt         $len, .L${bits}_cbc_dec_loop2x+4
472         orn             %g0, $omask, $omask
473 ___
474 $::code.=<<___ if ($::evp);
475         st              %f12, [$ivec + 0]
476         st              %f13, [$ivec + 4]
477         st              %f14, [$ivec + 8]
478         st              %f15, [$ivec + 12]
479 ___
480 $::code.=<<___ if (!$::evp);
481         brnz,pn         $ivoff, .L${bits}_cbc_dec_unaligned_ivec
482         nop
483
484         std             %f12, [$ivec + 0]       ! write out ivec
485         std             %f14, [$ivec + 8]
486         ret
487         restore
488
489 .align  16
490 .L${bits}_cbc_dec_unaligned_ivec:
491         alignaddrl      $ivec, $ivoff, %g0      ! handle unaligned ivec
492         mov             0xff, $omask
493         srl             $omask, $ivoff, $omask
494         faligndata      %f12, %f12, %f0
495         faligndata      %f12, %f14, %f2
496         faligndata      %f14, %f14, %f4
497         stda            %f0, [$ivec + $omask]0xc0
498         std             %f2, [$ivec + 8]
499         add             $ivec, 16, $ivec
500         orn             %g0, $omask, $omask
501         stda            %f4, [$ivec + $omask]0xc0
502 ___
503 $::code.=<<___;
504         ret
505         restore
506
507 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
508 .align  32
509 .L${bits}cbc_dec_blk:
510         add     $out, $len, $blk_init
511         and     $blk_init, 63, $blk_init        ! tail
512         sub     $len, $blk_init, $len
513         add     $blk_init, 15, $blk_init        ! round up to 16n
514         srlx    $len, 4, $len
515         srl     $blk_init, 4, $blk_init
516         sub     $len, 1, $len
517         add     $blk_init, 1, $blk_init
518
519 .L${bits}_cbc_dec_blk_loop2x:
520         ldx             [$inp + 0], %o0
521         ldx             [$inp + 8], %o1
522         ldx             [$inp + 16], %o2
523         brz,pt          $ileft, 5f
524         ldx             [$inp + 24], %o3
525
526         ldx             [$inp + 32], %o4
527         sllx            %o0, $ileft, %o0
528         srlx            %o1, $iright, %g1
529         or              %g1, %o0, %o0
530         sllx            %o1, $ileft, %o1
531         srlx            %o2, $iright, %g1
532         or              %g1, %o1, %o1
533         sllx            %o2, $ileft, %o2
534         srlx            %o3, $iright, %g1
535         or              %g1, %o2, %o2
536         sllx            %o3, $ileft, %o3
537         srlx            %o4, $iright, %o4
538         or              %o4, %o3, %o3
539 5:
540         xor             %g4, %o0, %o4           ! ^= rk[0]
541         xor             %g5, %o1, %o5
542         movxtod         %o4, %f0
543         movxtod         %o5, %f2
544         xor             %g4, %o2, %o4
545         xor             %g5, %o3, %o5
546         movxtod         %o4, %f4
547         movxtod         %o5, %f6
548
549         prefetch        [$inp + 32+63], 20
550         call            _${alg}${bits}_decrypt_2x
551         add             $inp, 32, $inp
552         subcc           $len, 2, $len
553
554         movxtod         %o0, %f8
555         movxtod         %o1, %f10
556         fxor            %f12, %f0, %f0          ! ^= ivec
557         fxor            %f14, %f2, %f2
558         movxtod         %o2, %f12
559         movxtod         %o3, %f14
560         fxor            %f8, %f4, %f4
561         fxor            %f10, %f6, %f6
562
563         stda            %f0, [$out]0xe2         ! ASI_BLK_INIT, T4-specific
564         add             $out, 8, $out
565         stda            %f2, [$out]0xe2         ! ASI_BLK_INIT, T4-specific
566         add             $out, 8, $out
567         stda            %f4, [$out]0xe2         ! ASI_BLK_INIT, T4-specific
568         add             $out, 8, $out
569         stda            %f6, [$out]0xe2         ! ASI_BLK_INIT, T4-specific
570         bgu,pt          $::size_t_cc, .L${bits}_cbc_dec_blk_loop2x
571         add             $out, 8, $out
572
573         add             $blk_init, $len, $len
574         andcc           $len, 1, %g0            ! is number of blocks even?
575         membar          #StoreLoad|#StoreStore
576         bnz,pt          %icc, .L${bits}_cbc_dec_loop
577         srl             $len, 0, $len
578         brnz,pn         $len, .L${bits}_cbc_dec_loop2x
579         nop
580 ___
581 $::code.=<<___ if ($::evp);
582         st              %f12, [$ivec + 0]       ! write out ivec
583         st              %f13, [$ivec + 4]
584         st              %f14, [$ivec + 8]
585         st              %f15, [$ivec + 12]
586 ___
587 $::code.=<<___ if (!$::evp);
588         brnz,pn         $ivoff, 3b
589         nop
590
591         std             %f12, [$ivec + 0]       ! write out ivec
592         std             %f14, [$ivec + 8]
593 ___
594 $::code.=<<___;
595         ret
596         restore
597 .type   ${alg}${bits}_t4_cbc_decrypt,#function
598 .size   ${alg}${bits}_t4_cbc_decrypt,.-${alg}${bits}_t4_cbc_decrypt
599 ___
600 }
601
602 sub alg_ctr32_implement {
603 my ($alg,$bits) = @_;
604
605 $::code.=<<___;
606 .globl  ${alg}${bits}_t4_ctr32_encrypt
607 .align  32
608 ${alg}${bits}_t4_ctr32_encrypt:
609         save            %sp, -$::frame, %sp
610
611         prefetch        [$inp], 20
612         prefetch        [$inp + 63], 20
613         call            _${alg}${bits}_load_enckey
614         sllx            $len, 4, $len
615
616         ld              [$ivec + 0], %l4        ! counter
617         ld              [$ivec + 4], %l5
618         ld              [$ivec + 8], %l6
619         ld              [$ivec + 12], %l7
620
621         sllx            %l4, 32, %o5
622         or              %l5, %o5, %o5
623         sllx            %l6, 32, %g1
624         xor             %o5, %g4, %g4           ! ^= rk[0]
625         xor             %g1, %g5, %g5
626         movxtod         %g4, %f14               ! most significant 64 bits
627
628         sub             $inp, $out, $blk_init   ! $inp!=$out
629         and             $inp, 7, $ileft
630         andn            $inp, 7, $inp
631         sll             $ileft, 3, $ileft
632         mov             64, $iright
633         mov             0xff, $omask
634         sub             $iright, $ileft, $iright
635         and             $out, 7, $ooff
636         cmp             $len, 255
637         movrnz          $ooff, 0, $blk_init             ! if (  $out&7 ||
638         movleu          $::size_t_cc, 0, $blk_init      !       $len<256 ||
639         brnz,pn         $blk_init, .L${bits}_ctr32_blk  !       $inp==$out)
640         srl             $omask, $ooff, $omask
641
642         andcc           $len, 16, %g0           ! is number of blocks even?
643         alignaddrl      $out, %g0, $out
644         bz              %icc, .L${bits}_ctr32_loop2x
645         srlx            $len, 4, $len
646 .L${bits}_ctr32_loop:
647         ldx             [$inp + 0], %o0
648         brz,pt          $ileft, 4f
649         ldx             [$inp + 8], %o1
650
651         ldx             [$inp + 16], %o2
652         sllx            %o0, $ileft, %o0
653         srlx            %o1, $iright, %g1
654         sllx            %o1, $ileft, %o1
655         or              %g1, %o0, %o0
656         srlx            %o2, $iright, %o2
657         or              %o2, %o1, %o1
658 4:
659         xor             %g5, %l7, %g1           ! ^= rk[0]
660         add             %l7, 1, %l7
661         movxtod         %g1, %f2
662         srl             %l7, 0, %l7             ! clruw
663         prefetch        [$out + 63], 22
664         prefetch        [$inp + 16+63], 20
665 ___
666 $::code.=<<___ if ($alg eq "aes");
667         aes_eround01    %f16, %f14, %f2, %f4
668         aes_eround23    %f18, %f14, %f2, %f2
669 ___
670 $::code.=<<___ if ($alg eq "cmll");
671         camellia_f      %f16, %f2, %f14, %f2
672         camellia_f      %f18, %f14, %f2, %f0
673 ___
674 $::code.=<<___;
675         call            _${alg}${bits}_encrypt_1x+8
676         add             $inp, 16, $inp
677
678         movxtod         %o0, %f10
679         movxtod         %o1, %f12
680         fxor            %f10, %f0, %f0          ! ^= inp
681         fxor            %f12, %f2, %f2
682
683         brnz,pn         $ooff, 2f
684         sub             $len, 1, $len
685                 
686         std             %f0, [$out + 0]
687         std             %f2, [$out + 8]
688         brnz,pt         $len, .L${bits}_ctr32_loop2x
689         add             $out, 16, $out
690
691         ret
692         restore
693
694 .align  16
695 2:      ldxa            [$inp]0x82, %o0         ! avoid read-after-write hazard
696                                                 ! and ~3x deterioration
697                                                 ! in inp==out case
698         faligndata      %f0, %f0, %f4           ! handle unaligned output
699         faligndata      %f0, %f2, %f6
700         faligndata      %f2, %f2, %f8
701         stda            %f4, [$out + $omask]0xc0        ! partial store
702         std             %f6, [$out + 8]
703         add             $out, 16, $out
704         orn             %g0, $omask, $omask
705         stda            %f8, [$out + $omask]0xc0        ! partial store
706
707         brnz,pt         $len, .L${bits}_ctr32_loop2x+4
708         orn             %g0, $omask, $omask
709
710         ret
711         restore
712
713 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
714 .align  32
715 .L${bits}_ctr32_loop2x:
716         ldx             [$inp + 0], %o0
717         ldx             [$inp + 8], %o1
718         ldx             [$inp + 16], %o2
719         brz,pt          $ileft, 4f
720         ldx             [$inp + 24], %o3
721
722         ldx             [$inp + 32], %o4
723         sllx            %o0, $ileft, %o0
724         srlx            %o1, $iright, %g1
725         or              %g1, %o0, %o0
726         sllx            %o1, $ileft, %o1
727         srlx            %o2, $iright, %g1
728         or              %g1, %o1, %o1
729         sllx            %o2, $ileft, %o2
730         srlx            %o3, $iright, %g1
731         or              %g1, %o2, %o2
732         sllx            %o3, $ileft, %o3
733         srlx            %o4, $iright, %o4
734         or              %o4, %o3, %o3
735 4:
736         xor             %g5, %l7, %g1           ! ^= rk[0]
737         add             %l7, 1, %l7
738         movxtod         %g1, %f2
739         srl             %l7, 0, %l7             ! clruw
740         xor             %g5, %l7, %g1
741         add             %l7, 1, %l7
742         movxtod         %g1, %f6
743         srl             %l7, 0, %l7             ! clruw
744         prefetch        [$out + 63], 22
745         prefetch        [$inp + 32+63], 20
746 ___
747 $::code.=<<___ if ($alg eq "aes");
748         aes_eround01    %f16, %f14, %f2, %f8
749         aes_eround23    %f18, %f14, %f2, %f2
750         aes_eround01    %f16, %f14, %f6, %f10
751         aes_eround23    %f18, %f14, %f6, %f6
752 ___
753 $::code.=<<___ if ($alg eq "cmll");
754         camellia_f      %f16, %f2, %f14, %f2
755         camellia_f      %f16, %f6, %f14, %f6
756         camellia_f      %f18, %f14, %f2, %f0
757         camellia_f      %f18, %f14, %f6, %f4
758 ___
759 $::code.=<<___;
760         call            _${alg}${bits}_encrypt_2x+16
761         add             $inp, 32, $inp
762
763         movxtod         %o0, %f8
764         movxtod         %o1, %f10
765         movxtod         %o2, %f12
766         fxor            %f8, %f0, %f0           ! ^= inp
767         movxtod         %o3, %f8
768         fxor            %f10, %f2, %f2
769         fxor            %f12, %f4, %f4
770         fxor            %f8, %f6, %f6
771
772         brnz,pn         $ooff, 2f
773         sub             $len, 2, $len
774                 
775         std             %f0, [$out + 0]
776         std             %f2, [$out + 8]
777         std             %f4, [$out + 16]
778         std             %f6, [$out + 24]
779         brnz,pt         $len, .L${bits}_ctr32_loop2x
780         add             $out, 32, $out
781
782         ret
783         restore
784
785 .align  16
786 2:      ldxa            [$inp]0x82, %o0         ! avoid read-after-write hazard
787                                                 ! and ~3x deterioration
788                                                 ! in inp==out case
789         faligndata      %f0, %f0, %f8           ! handle unaligned output
790         faligndata      %f0, %f2, %f0
791         faligndata      %f2, %f4, %f2
792         faligndata      %f4, %f6, %f4
793         faligndata      %f6, %f6, %f6
794
795         stda            %f8, [$out + $omask]0xc0        ! partial store
796         std             %f0, [$out + 8]
797         std             %f2, [$out + 16]
798         std             %f4, [$out + 24]
799         add             $out, 32, $out
800         orn             %g0, $omask, $omask
801         stda            %f6, [$out + $omask]0xc0        ! partial store
802
803         brnz,pt         $len, .L${bits}_ctr32_loop2x+4
804         orn             %g0, $omask, $omask
805
806         ret
807         restore
808
809 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
810 .align  32
811 .L${bits}_ctr32_blk:
812         add     $out, $len, $blk_init
813         and     $blk_init, 63, $blk_init        ! tail
814         sub     $len, $blk_init, $len
815         add     $blk_init, 15, $blk_init        ! round up to 16n
816         srlx    $len, 4, $len
817         srl     $blk_init, 4, $blk_init
818         sub     $len, 1, $len
819         add     $blk_init, 1, $blk_init
820
821 .L${bits}_ctr32_blk_loop2x:
822         ldx             [$inp + 0], %o0
823         ldx             [$inp + 8], %o1
824         ldx             [$inp + 16], %o2
825         brz,pt          $ileft, 5f
826         ldx             [$inp + 24], %o3
827
828         ldx             [$inp + 32], %o4
829         sllx            %o0, $ileft, %o0
830         srlx            %o1, $iright, %g1
831         or              %g1, %o0, %o0
832         sllx            %o1, $ileft, %o1
833         srlx            %o2, $iright, %g1
834         or              %g1, %o1, %o1
835         sllx            %o2, $ileft, %o2
836         srlx            %o3, $iright, %g1
837         or              %g1, %o2, %o2
838         sllx            %o3, $ileft, %o3
839         srlx            %o4, $iright, %o4
840         or              %o4, %o3, %o3
841 5:
842         xor             %g5, %l7, %g1           ! ^= rk[0]
843         add             %l7, 1, %l7
844         movxtod         %g1, %f2
845         srl             %l7, 0, %l7             ! clruw
846         xor             %g5, %l7, %g1
847         add             %l7, 1, %l7
848         movxtod         %g1, %f6
849         srl             %l7, 0, %l7             ! clruw
850         prefetch        [$inp + 32+63], 20
851 ___
852 $::code.=<<___ if ($alg eq "aes");
853         aes_eround01    %f16, %f14, %f2, %f8
854         aes_eround23    %f18, %f14, %f2, %f2
855         aes_eround01    %f16, %f14, %f6, %f10
856         aes_eround23    %f18, %f14, %f6, %f6
857 ___
858 $::code.=<<___ if ($alg eq "cmll");
859         camellia_f      %f16, %f2, %f14, %f2
860         camellia_f      %f16, %f6, %f14, %f6
861         camellia_f      %f18, %f14, %f2, %f0
862         camellia_f      %f18, %f14, %f6, %f4
863 ___
864 $::code.=<<___;
865         call            _${alg}${bits}_encrypt_2x+16
866         add             $inp, 32, $inp
867         subcc           $len, 2, $len
868
869         movxtod         %o0, %f8
870         movxtod         %o1, %f10
871         movxtod         %o2, %f12
872         fxor            %f8, %f0, %f0           ! ^= inp
873         movxtod         %o3, %f8
874         fxor            %f10, %f2, %f2
875         fxor            %f12, %f4, %f4
876         fxor            %f8, %f6, %f6
877
878         stda            %f0, [$out]0xe2         ! ASI_BLK_INIT, T4-specific
879         add             $out, 8, $out
880         stda            %f2, [$out]0xe2         ! ASI_BLK_INIT, T4-specific
881         add             $out, 8, $out
882         stda            %f4, [$out]0xe2         ! ASI_BLK_INIT, T4-specific
883         add             $out, 8, $out
884         stda            %f6, [$out]0xe2         ! ASI_BLK_INIT, T4-specific
885         bgu,pt          $::size_t_cc, .L${bits}_ctr32_blk_loop2x
886         add             $out, 8, $out
887
888         add             $blk_init, $len, $len
889         andcc           $len, 1, %g0            ! is number of blocks even?
890         membar          #StoreLoad|#StoreStore
891         bnz,pt          %icc, .L${bits}_ctr32_loop
892         srl             $len, 0, $len
893         brnz,pn         $len, .L${bits}_ctr32_loop2x
894         nop
895
896         ret
897         restore
898 .type   ${alg}${bits}_t4_ctr32_encrypt,#function
899 .size   ${alg}${bits}_t4_ctr32_encrypt,.-${alg}${bits}_t4_ctr32_encrypt
900 ___
901 }
902
903 # Purpose of these subroutines is to explicitly encode VIS instructions,
904 # so that one can compile the module without having to specify VIS
905 # extentions on compiler command line, e.g. -xarch=v9 vs. -xarch=v9a.
906 # Idea is to reserve for option to produce "universal" binary and let
907 # programmer detect if current CPU is VIS capable at run-time.
908 sub unvis {
909 my ($mnemonic,$rs1,$rs2,$rd)=@_;
910 my ($ref,$opf);
911 my %visopf = (  "faligndata"    => 0x048,
912                 "fnot2"         => 0x066,
913                 "fxor"          => 0x06c,
914                 "fsrc2"         => 0x078        );
915
916     $ref = "$mnemonic\t$rs1,$rs2,$rd";
917
918     if ($opf=$visopf{$mnemonic}) {
919         foreach ($rs1,$rs2,$rd) {
920             return $ref if (!/%f([0-9]{1,2})/);
921             $_=$1;
922             if ($1>=32) {
923                 return $ref if ($1&1);
924                 # re-encode for upper double register addressing
925                 $_=($1|$1>>5)&31;
926             }
927         }
928
929         return  sprintf ".word\t0x%08x !%s",
930                         0x81b00000|$rd<<25|$rs1<<14|$opf<<5|$rs2,
931                         $ref;
932     } else {
933         return $ref;
934     }
935 }
936 sub unvis3 {
937 my ($mnemonic,$rs1,$rs2,$rd)=@_;
938 my %bias = ( "g" => 0, "o" => 8, "l" => 16, "i" => 24 );
939 my ($ref,$opf);
940 my %visopf = (  "addxc"         => 0x011,
941                 "addxccc"       => 0x013,
942                 "umulxhi"       => 0x016        );
943
944     $ref = "$mnemonic\t$rs1,$rs2,$rd";
945
946     if ($opf=$visopf{$mnemonic}) {
947         foreach ($rs1,$rs2,$rd) {
948             return $ref if (!/%([goli])([0-9])/);
949             $_=$bias{$1}+$2;
950         }
951
952         return  sprintf ".word\t0x%08x !%s",
953                         0x81b00000|$rd<<25|$rs1<<14|$opf<<5|$rs2,
954                         $ref;
955     } else {
956         return $ref;
957     }
958 }
959 sub unalignaddr {
960 my ($mnemonic,$rs1,$rs2,$rd)=@_;
961 my %bias = ( "g" => 0, "o" => 8, "l" => 16, "i" => 24 );
962 my $ref = "$mnemonic\t$rs1,$rs2,$rd";
963 my $opf = $mnemonic =~ /l$/ ? 0x01a :0x18;
964
965     foreach ($rs1,$rs2,$rd) {
966         if (/%([goli])([0-7])/) { $_=$bias{$1}+$2; }
967         else                    { return $ref; }
968     }
969     return  sprintf ".word\t0x%08x !%s",
970                     0x81b00000|$rd<<25|$rs1<<14|$opf<<5|$rs2,
971                     $ref;
972 }
973
974 sub unaes_round {       # 4-argument instructions
975 my ($mnemonic,$rs1,$rs2,$rs3,$rd)=@_;
976 my ($ref,$opf);
977 my %aesopf = (  "aes_eround01"  => 0,
978                 "aes_eround23"  => 1,
979                 "aes_dround01"  => 2,
980                 "aes_dround23"  => 3,
981                 "aes_eround01_l"=> 4,
982                 "aes_eround23_l"=> 5,
983                 "aes_dround01_l"=> 6,
984                 "aes_dround23_l"=> 7,
985                 "aes_kexpand1"  => 8    );
986
987     $ref = "$mnemonic\t$rs1,$rs2,$rs3,$rd";
988
989     if (defined($opf=$aesopf{$mnemonic})) {
990         $rs3 = ($rs3 =~ /%f([0-6]*[02468])/) ? (($1|$1>>5)&31) : $rs3;
991         foreach ($rs1,$rs2,$rd) {
992             return $ref if (!/%f([0-9]{1,2})/);
993             $_=$1;
994             if ($1>=32) {
995                 return $ref if ($1&1);
996                 # re-encode for upper double register addressing
997                 $_=($1|$1>>5)&31;
998             }
999         }
1000
1001         return  sprintf ".word\t0x%08x !%s",
1002                         2<<30|$rd<<25|0x19<<19|$rs1<<14|$rs3<<9|$opf<<5|$rs2,
1003                         $ref;
1004     } else {
1005         return $ref;
1006     }
1007 }
1008
1009 sub unaes_kexpand {     # 3-argument instructions
1010 my ($mnemonic,$rs1,$rs2,$rd)=@_;
1011 my ($ref,$opf);
1012 my %aesopf = (  "aes_kexpand0"  => 0x130,
1013                 "aes_kexpand2"  => 0x131        );
1014
1015     $ref = "$mnemonic\t$rs1,$rs2,$rd";
1016
1017     if (defined($opf=$aesopf{$mnemonic})) {
1018         foreach ($rs1,$rs2,$rd) {
1019             return $ref if (!/%f([0-9]{1,2})/);
1020             $_=$1;
1021             if ($1>=32) {
1022                 return $ref if ($1&1);
1023                 # re-encode for upper double register addressing
1024                 $_=($1|$1>>5)&31;
1025             }
1026         }
1027
1028         return  sprintf ".word\t0x%08x !%s",
1029                         2<<30|$rd<<25|0x36<<19|$rs1<<14|$opf<<5|$rs2,
1030                         $ref;
1031     } else {
1032         return $ref;
1033     }
1034 }
1035
1036 sub uncamellia_f {      # 4-argument instructions
1037 my ($mnemonic,$rs1,$rs2,$rs3,$rd)=@_;
1038 my ($ref,$opf);
1039
1040     $ref = "$mnemonic\t$rs1,$rs2,$rs3,$rd";
1041
1042     if (1) {
1043         $rs3 = ($rs3 =~ /%f([0-6]*[02468])/) ? (($1|$1>>5)&31) : $rs3;
1044         foreach ($rs1,$rs2,$rd) {
1045             return $ref if (!/%f([0-9]{1,2})/);
1046             $_=$1;
1047             if ($1>=32) {
1048                 return $ref if ($1&1);
1049                 # re-encode for upper double register addressing
1050                 $_=($1|$1>>5)&31;
1051             }
1052         }
1053
1054         return  sprintf ".word\t0x%08x !%s",
1055                         2<<30|$rd<<25|0x19<<19|$rs1<<14|$rs3<<9|0xc<<5|$rs2,
1056                         $ref;
1057     } else {
1058         return $ref;
1059     }
1060 }
1061
1062 sub uncamellia3 {       # 3-argument instructions
1063 my ($mnemonic,$rs1,$rs2,$rd)=@_;
1064 my ($ref,$opf);
1065 my %cmllopf = ( "camellia_fl"   => 0x13c,
1066                 "camellia_fli"  => 0x13d        );
1067
1068     $ref = "$mnemonic\t$rs1,$rs2,$rd";
1069
1070     if (defined($opf=$cmllopf{$mnemonic})) {
1071         foreach ($rs1,$rs2,$rd) {
1072             return $ref if (!/%f([0-9]{1,2})/);
1073             $_=$1;
1074             if ($1>=32) {
1075                 return $ref if ($1&1);
1076                 # re-encode for upper double register addressing
1077                 $_=($1|$1>>5)&31;
1078             }
1079         }
1080
1081         return  sprintf ".word\t0x%08x !%s",
1082                         2<<30|$rd<<25|0x36<<19|$rs1<<14|$opf<<5|$rs2,
1083                         $ref;
1084     } else {
1085         return $ref;
1086     }
1087 }
1088
1089 sub unmovxtox {         # 2-argument instructions
1090 my ($mnemonic,$rs,$rd)=@_;
1091 my %bias = ( "g" => 0, "o" => 8, "l" => 16, "i" => 24, "f" => 0 );
1092 my ($ref,$opf);
1093 my %movxopf = ( "movdtox"       => 0x110,
1094                 "movstouw"      => 0x111,
1095                 "movstosw"      => 0x113,
1096                 "movxtod"       => 0x118,
1097                 "movwtos"       => 0x119        );
1098
1099     $ref = "$mnemonic\t$rs,$rd";
1100
1101     if (defined($opf=$movxopf{$mnemonic})) {
1102         foreach ($rs,$rd) {
1103             return $ref if (!/%([fgoli])([0-9]{1,2})/);
1104             $_=$bias{$1}+$2;
1105             if ($2>=32) {
1106                 return $ref if ($2&1);
1107                 # re-encode for upper double register addressing
1108                 $_=($2|$2>>5)&31;
1109             }
1110         }
1111
1112         return  sprintf ".word\t0x%08x !%s",
1113                         2<<30|$rd<<25|0x36<<19|$opf<<5|$rs,
1114                         $ref;
1115     } else {
1116         return $ref;
1117     }
1118 }
1119
1120 sub emit_assembler {
1121     foreach (split("\n",$::code)) {
1122         s/\`([^\`]*)\`/eval $1/ge;
1123
1124         s/\b(f[a-z]+2[sd]*)\s+(%f[0-9]{1,2}),\s*(%f[0-9]{1,2})\s*$/$1\t%f0,$2,$3/g;
1125
1126         s/\b(aes_[edk][^\s]*)\s+(%f[0-9]{1,2}),\s*(%f[0-9]{1,2}),\s*([%fx0-9]+),\s*(%f[0-9]{1,2})/
1127                 &unaes_round($1,$2,$3,$4,$5)
1128          /ge or
1129         s/\b(aes_kexpand[02])\s+(%f[0-9]{1,2}),\s*(%f[0-9]{1,2}),\s*(%f[0-9]{1,2})/
1130                 &unaes_kexpand($1,$2,$3,$4)
1131          /ge or
1132         s/\b(camellia_f)\s+(%f[0-9]{1,2}),\s*(%f[0-9]{1,2}),\s*([%fx0-9]+),\s*(%f[0-9]{1,2})/
1133                 &uncamellia_f($1,$2,$3,$4,$5)
1134          /ge or
1135         s/\b(camellia_[^s]+)\s+(%f[0-9]{1,2}),\s*(%f[0-9]{1,2}),\s*(%f[0-9]{1,2})/
1136                 &uncamellia3($1,$2,$3,$4)
1137          /ge or
1138         s/\b(mov[ds]to\w+)\s+(%f[0-9]{1,2}),\s*(%[goli][0-7])/
1139                 &unmovxtox($1,$2,$3)
1140          /ge or
1141         s/\b(mov[xw]to[ds])\s+(%[goli][0-7]),\s*(%f[0-9]{1,2})/
1142                 &unmovxtox($1,$2,$3)
1143          /ge or
1144         s/\b(f[^\s]*)\s+(%f[0-9]{1,2}),\s*(%f[0-9]{1,2}),\s*(%f[0-9]{1,2})/
1145                 &unvis($1,$2,$3,$4)
1146          /ge or
1147         s/\b(alignaddr[l]*)\s+(%[goli][0-7]),\s*(%[goli][0-7]),\s*(%[goli][0-7])/
1148                 &unalignaddr($1,$2,$3,$4)
1149          /ge or
1150         s/\b(umulxhi|addxc[c]{0,2})\s+(%[goli][0-7]),\s*(%[goli][0-7]),\s*(%[goli][0-7])/
1151                 &unvis3($1,$2,$3,$4)
1152          /ge;
1153
1154         print $_,"\n";
1155     }
1156 }
1157
1158 1;