Fix some assembler generating scripts for better unification
[openssl.git] / crypto / bn / asm / sparcv9-gf2m.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 # October 2012
11 #
12 # The module implements bn_GF2m_mul_2x2 polynomial multiplication used
13 # in bn_gf2m.c. It's kind of low-hanging mechanical port from C for
14 # the time being... Except that it has two code paths: one suitable
15 # for all SPARCv9 processors and one for VIS3-capable ones. Former
16 # delivers ~25-45% more, more for longer keys, heaviest DH and DSA
17 # verify operations on venerable UltraSPARC II. On T4 VIS3 code is
18 # ~100-230% faster than gcc-generated code and ~35-90% faster than
19 # the pure SPARCv9 code path.
20
21 $output = pop;
22 open STDOUT,">$output";
23
24 $locals=16*8;
25
26 $tab="%l0";
27
28 @T=("%g2","%g3");
29 @i=("%g4","%g5");
30
31 ($a1,$a2,$a4,$a8,$a12,$a48)=map("%o$_",(0..5));
32 ($lo,$hi,$b)=("%g1",$a8,"%o7"); $a=$lo;
33
34 $code.=<<___;
35 #include <sparc_arch.h>
36
37 #ifdef __arch64__
38 .register       %g2,#scratch
39 .register       %g3,#scratch
40 #endif
41
42 #ifdef __PIC__
43 SPARC_PIC_THUNK(%g1)
44 #endif
45
46 .globl  bn_GF2m_mul_2x2
47 .align  16
48 bn_GF2m_mul_2x2:
49         SPARC_LOAD_ADDRESS_LEAF(OPENSSL_sparcv9cap_P,%g1,%g5)
50         ld      [%g1+0],%g1                     ! OPENSSL_sparcv9cap_P[0]
51
52         andcc   %g1, SPARCV9_VIS3, %g0
53         bz,pn   %icc,.Lsoftware
54         nop
55
56         sllx    %o1, 32, %o1
57         sllx    %o3, 32, %o3
58         or      %o2, %o1, %o1
59         or      %o4, %o3, %o3
60         .word   0x95b262ab                      ! xmulx   %o1, %o3, %o2
61         .word   0x99b262cb                      ! xmulxhi %o1, %o3, %o4
62         srlx    %o2, 32, %o1                    ! 13 cycles later
63         st      %o2, [%o0+0]
64         st      %o1, [%o0+4]
65         srlx    %o4, 32, %o3
66         st      %o4, [%o0+8]
67         retl
68         st      %o3, [%o0+12]
69
70 .align  16
71 .Lsoftware:
72         save    %sp,-STACK_FRAME-$locals,%sp
73
74         sllx    %i1,32,$a
75         mov     -1,$a12
76         sllx    %i3,32,$b
77         or      %i2,$a,$a
78         srlx    $a12,1,$a48                     ! 0x7fff...
79         or      %i4,$b,$b
80         srlx    $a12,2,$a12                     ! 0x3fff...
81         add     %sp,STACK_BIAS+STACK_FRAME,$tab
82
83         sllx    $a,2,$a4
84         mov     $a,$a1
85         sllx    $a,1,$a2
86
87         srax    $a4,63,@i[1]                    ! broadcast 61st bit
88         and     $a48,$a4,$a4                    ! (a<<2)&0x7fff...
89         srlx    $a48,2,$a48
90         srax    $a2,63,@i[0]                    ! broadcast 62nd bit
91         and     $a12,$a2,$a2                    ! (a<<1)&0x3fff...
92         srax    $a1,63,$lo                      ! broadcast 63rd bit
93         and     $a48,$a1,$a1                    ! (a<<0)&0x1fff...
94
95         sllx    $a1,3,$a8
96         and     $b,$lo,$lo
97         and     $b,@i[0],@i[0]
98         and     $b,@i[1],@i[1]
99
100         stx     %g0,[$tab+0*8]                  ! tab[0]=0
101         xor     $a1,$a2,$a12
102         stx     $a1,[$tab+1*8]                  ! tab[1]=a1
103         stx     $a2,[$tab+2*8]                  ! tab[2]=a2
104          xor    $a4,$a8,$a48
105         stx     $a12,[$tab+3*8]                 ! tab[3]=a1^a2
106          xor    $a4,$a1,$a1
107
108         stx     $a4,[$tab+4*8]                  ! tab[4]=a4
109         xor     $a4,$a2,$a2
110         stx     $a1,[$tab+5*8]                  ! tab[5]=a1^a4
111         xor     $a4,$a12,$a12
112         stx     $a2,[$tab+6*8]                  ! tab[6]=a2^a4
113          xor    $a48,$a1,$a1
114         stx     $a12,[$tab+7*8]                 ! tab[7]=a1^a2^a4
115          xor    $a48,$a2,$a2
116
117         stx     $a8,[$tab+8*8]                  ! tab[8]=a8
118         xor     $a48,$a12,$a12
119         stx     $a1,[$tab+9*8]                  ! tab[9]=a1^a8
120          xor    $a4,$a1,$a1
121         stx     $a2,[$tab+10*8]                 ! tab[10]=a2^a8
122          xor    $a4,$a2,$a2
123         stx     $a12,[$tab+11*8]                ! tab[11]=a1^a2^a8
124
125         xor     $a4,$a12,$a12
126         stx     $a48,[$tab+12*8]                ! tab[12]=a4^a8
127          srlx   $lo,1,$hi
128         stx     $a1,[$tab+13*8]                 ! tab[13]=a1^a4^a8
129          sllx   $lo,63,$lo
130         stx     $a2,[$tab+14*8]                 ! tab[14]=a2^a4^a8
131          srlx   @i[0],2,@T[0]
132         stx     $a12,[$tab+15*8]                ! tab[15]=a1^a2^a4^a8
133
134         sllx    @i[0],62,$a1
135          sllx   $b,3,@i[0]
136         srlx    @i[1],3,@T[1]
137          and    @i[0],`0xf<<3`,@i[0]
138         sllx    @i[1],61,$a2
139          ldx    [$tab+@i[0]],@i[0]
140          srlx   $b,4-3,@i[1]
141         xor     @T[0],$hi,$hi
142          and    @i[1],`0xf<<3`,@i[1]
143         xor     $a1,$lo,$lo
144          ldx    [$tab+@i[1]],@i[1]
145         xor     @T[1],$hi,$hi
146
147         xor     @i[0],$lo,$lo
148         srlx    $b,8-3,@i[0]
149          xor    $a2,$lo,$lo
150         and     @i[0],`0xf<<3`,@i[0]
151 ___
152 for($n=1;$n<14;$n++) {
153 $code.=<<___;
154         sllx    @i[1],`$n*4`,@T[0]
155         ldx     [$tab+@i[0]],@i[0]
156         srlx    @i[1],`64-$n*4`,@T[1]
157         xor     @T[0],$lo,$lo
158         srlx    $b,`($n+2)*4`-3,@i[1]
159         xor     @T[1],$hi,$hi
160         and     @i[1],`0xf<<3`,@i[1]
161 ___
162         push(@i,shift(@i)); push(@T,shift(@T));
163 }
164 $code.=<<___;
165         sllx    @i[1],`$n*4`,@T[0]
166         ldx     [$tab+@i[0]],@i[0]
167         srlx    @i[1],`64-$n*4`,@T[1]
168         xor     @T[0],$lo,$lo
169
170         sllx    @i[0],`($n+1)*4`,@T[0]
171          xor    @T[1],$hi,$hi
172         srlx    @i[0],`64-($n+1)*4`,@T[1]
173         xor     @T[0],$lo,$lo
174         xor     @T[1],$hi,$hi
175
176         srlx    $lo,32,%i1
177         st      $lo,[%i0+0]
178         st      %i1,[%i0+4]
179         srlx    $hi,32,%i2
180         st      $hi,[%i0+8]
181         st      %i2,[%i0+12]
182
183         ret
184         restore
185 .type   bn_GF2m_mul_2x2,#function
186 .size   bn_GF2m_mul_2x2,.-bn_GF2m_mul_2x2
187 .asciz  "GF(2^m) Multiplication for SPARCv9, CRYPTOGAMS by <appro\@openssl.org>"
188 .align  4
189 ___
190
191 $code =~ s/\`([^\`]*)\`/eval($1)/gem;
192 print $code;
193 close STDOUT;