Move & split opensslconf.h.in
[openssl.git] / crypto / ppccpuid.pl
1 #!/usr/bin/env perl
2
3 $flavour = shift;
4
5 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
6 ( $xlate="${dir}ppc-xlate.pl" and -f $xlate ) or
7 ( $xlate="${dir}perlasm/ppc-xlate.pl" and -f $xlate) or
8 die "can't locate ppc-xlate.pl";
9
10 open STDOUT,"| $^X $xlate $flavour ".shift || die "can't call $xlate: $!";
11
12 if ($flavour=~/64/) {
13     $CMPLI="cmpldi";
14     $SHRLI="srdi";
15     $SIGNX="extsw";
16 } else {
17     $CMPLI="cmplwi";
18     $SHRLI="srwi";
19     $SIGNX="mr";
20 }
21
22 $code=<<___;
23 .machine        "any"
24 .text
25
26 .globl  .OPENSSL_fpu_probe
27 .align  4
28 .OPENSSL_fpu_probe:
29         fmr     f0,f0
30         blr
31         .long   0
32         .byte   0,12,0x14,0,0,0,0,0
33 .size   .OPENSSL_fpu_probe,.-.OPENSSL_fpu_probe
34 .globl  .OPENSSL_ppc64_probe
35 .align  4
36 .OPENSSL_ppc64_probe:
37         fcfid   f1,f1
38         extrdi  r0,r0,32,0
39         blr
40         .long   0
41         .byte   0,12,0x14,0,0,0,0,0
42 .size   .OPENSSL_ppc64_probe,.-.OPENSSL_ppc64_probe
43
44 .globl  .OPENSSL_altivec_probe
45 .align  4
46 .OPENSSL_altivec_probe:
47         .long   0x10000484      # vor   v0,v0,v0
48         blr
49         .long   0
50         .byte   0,12,0x14,0,0,0,0,0
51 .size   .OPENSSL_altivec_probe,.-..OPENSSL_altivec_probe
52
53 .globl  .OPENSSL_crypto207_probe
54 .align  4
55 .OPENSSL_crypto207_probe:
56         lvx_u   v0,0,r1
57         vcipher v0,v0,v0
58         blr
59         .long   0
60         .byte   0,12,0x14,0,0,0,0,0
61 .size   .OPENSSL_crypto207_probe,.-.OPENSSL_crypto207_probe
62
63 .globl  .OPENSSL_wipe_cpu
64 .align  4
65 .OPENSSL_wipe_cpu:
66         xor     r0,r0,r0
67         fmr     f0,f31
68         fmr     f1,f31
69         fmr     f2,f31
70         mr      r3,r1
71         fmr     f3,f31
72         xor     r4,r4,r4
73         fmr     f4,f31
74         xor     r5,r5,r5
75         fmr     f5,f31
76         xor     r6,r6,r6
77         fmr     f6,f31
78         xor     r7,r7,r7
79         fmr     f7,f31
80         xor     r8,r8,r8
81         fmr     f8,f31
82         xor     r9,r9,r9
83         fmr     f9,f31
84         xor     r10,r10,r10
85         fmr     f10,f31
86         xor     r11,r11,r11
87         fmr     f11,f31
88         xor     r12,r12,r12
89         fmr     f12,f31
90         fmr     f13,f31
91         blr
92         .long   0
93         .byte   0,12,0x14,0,0,0,0,0
94 .size   .OPENSSL_wipe_cpu,.-.OPENSSL_wipe_cpu
95
96 .globl  .OPENSSL_atomic_add
97 .align  4
98 .OPENSSL_atomic_add:
99 Ladd:   lwarx   r5,0,r3
100         add     r0,r4,r5
101         stwcx.  r0,0,r3
102         bne-    Ladd
103         $SIGNX  r3,r0
104         blr
105         .long   0
106         .byte   0,12,0x14,0,0,0,2,0
107         .long   0
108 .size   .OPENSSL_atomic_add,.-.OPENSSL_atomic_add
109
110 .globl  .OPENSSL_rdtsc
111 .align  4
112 .OPENSSL_rdtsc:
113 ___
114 $code.=<<___    if ($flavour =~ /64/);
115         mftb    r3
116 ___
117 $code.=<<___    if ($flavour !~ /64/);
118 Loop_rdtsc:
119         mftbu   r5
120         mftb    r3
121         mftbu   r4
122         cmplw   r4,r5
123         bne     Loop_rdtsc
124 ___
125 $code.=<<___;
126         blr
127         .long   0
128         .byte   0,12,0x14,0,0,0,0,0
129 .size   .OPENSSL_rdtsc,.-.OPENSSL_rdtsc
130
131 .globl  .OPENSSL_cleanse
132 .align  4
133 .OPENSSL_cleanse:
134         $CMPLI  r4,7
135         li      r0,0
136         bge     Lot
137         $CMPLI  r4,0
138         beqlr-
139 Little: mtctr   r4
140         stb     r0,0(r3)
141         addi    r3,r3,1
142         bdnz    \$-8
143         blr
144 Lot:    andi.   r5,r3,3
145         beq     Laligned
146         stb     r0,0(r3)
147         subi    r4,r4,1
148         addi    r3,r3,1
149         b       Lot
150 Laligned:
151         $SHRLI  r5,r4,2
152         mtctr   r5
153         stw     r0,0(r3)
154         addi    r3,r3,4
155         bdnz    \$-8
156         andi.   r4,r4,3
157         bne     Little
158         blr
159         .long   0
160         .byte   0,12,0x14,0,0,0,2,0
161         .long   0
162 .size   .OPENSSL_cleanse,.-.OPENSSL_cleanse
163 ___
164 {
165 my ($out,$cnt,$max)=("r3","r4","r5");
166 my ($tick,$lasttick)=("r6","r7");
167 my ($diff,$lastdiff)=("r8","r9");
168
169 $code.=<<___;
170 .globl  .OPENSSL_instrument_bus
171 .align  4
172 .OPENSSL_instrument_bus:
173         mtctr   $cnt
174
175         mftb    $lasttick               # collect 1st tick
176         li      $diff,0
177
178         dcbf    0,$out                  # flush cache line
179         lwarx   $tick,0,$out            # load and lock
180         add     $tick,$tick,$diff
181         stwcx.  $tick,0,$out
182         stwx    $tick,0,$out
183
184 Loop:   mftb    $tick
185         sub     $diff,$tick,$lasttick
186         mr      $lasttick,$tick
187         dcbf    0,$out                  # flush cache line
188         lwarx   $tick,0,$out            # load and lock
189         add     $tick,$tick,$diff
190         stwcx.  $tick,0,$out
191         stwx    $tick,0,$out
192         addi    $out,$out,4             # ++$out
193         bdnz    Loop
194
195         mr      r3,$cnt
196         blr
197         .long   0
198         .byte   0,12,0x14,0,0,0,2,0
199         .long   0
200 .size   .OPENSSL_instrument_bus,.-.OPENSSL_instrument_bus
201
202 .globl  .OPENSSL_instrument_bus2
203 .align  4
204 .OPENSSL_instrument_bus2:
205         mr      r0,$cnt
206         slwi    $cnt,$cnt,2
207
208         mftb    $lasttick               # collect 1st tick
209         li      $diff,0
210
211         dcbf    0,$out                  # flush cache line
212         lwarx   $tick,0,$out            # load and lock
213         add     $tick,$tick,$diff
214         stwcx.  $tick,0,$out
215         stwx    $tick,0,$out
216
217         mftb    $tick                   # collect 1st diff
218         sub     $diff,$tick,$lasttick
219         mr      $lasttick,$tick
220         mr      $lastdiff,$diff
221 Loop2:
222         dcbf    0,$out                  # flush cache line
223         lwarx   $tick,0,$out            # load and lock
224         add     $tick,$tick,$diff
225         stwcx.  $tick,0,$out
226         stwx    $tick,0,$out
227
228         addic.  $max,$max,-1
229         beq     Ldone2
230
231         mftb    $tick
232         sub     $diff,$tick,$lasttick
233         mr      $lasttick,$tick
234         cmplw   7,$diff,$lastdiff
235         mr      $lastdiff,$diff
236
237         mfcr    $tick                   # pull cr
238         not     $tick,$tick             # flip bits
239         rlwinm  $tick,$tick,1,29,29     # isolate flipped eq bit and scale
240
241         sub.    $cnt,$cnt,$tick         # conditional --$cnt
242         add     $out,$out,$tick         # conditional ++$out
243         bne     Loop2
244
245 Ldone2:
246         srwi    $cnt,$cnt,2
247         sub     r3,r0,$cnt
248         blr
249         .long   0
250         .byte   0,12,0x14,0,0,0,3,0
251         .long   0
252 .size   .OPENSSL_instrument_bus2,.-.OPENSSL_instrument_bus2
253 ___
254 }
255
256 $code =~ s/\`([^\`]*)\`/eval $1/gem;
257 print $code;
258 close STDOUT;