Fix source where indent will not be able to cope
[openssl.git] / crypto / ppccap.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <setjmp.h>
5 #include <signal.h>
6 #include <unistd.h>
7 #include <crypto.h>
8 #include <openssl/bn.h>
9
10 #define PPC_FPU64       (1<<0)
11 #define PPC_ALTIVEC     (1<<1)
12
13 static int OPENSSL_ppccap_P = 0;
14
15 static sigset_t all_masked;
16
17 #ifdef OPENSSL_BN_ASM_MONT
18 int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0, int num)
19         {
20         int bn_mul_mont_fpu64(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0, int num);
21         int bn_mul_mont_int(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, const BN_ULONG *np, const BN_ULONG *n0, int num);
22
23         if (sizeof(size_t)==4)
24                 {
25 #if (defined(__APPLE__) && defined(__MACH__))
26                 if (num>=8 && (num&3)==0 && (OPENSSL_ppccap_P&PPC_FPU64))
27                         return bn_mul_mont_fpu64(rp,ap,bp,np,n0,num);
28 #else
29                 /* boundary of 32 was experimentally determined on
30                    Linux 2.6.22, might have to be adjusted on AIX... */
31                 if (num>=32 && (num&3)==0 && (OPENSSL_ppccap_P&PPC_FPU64))
32                         {
33                         sigset_t oset;
34                         int ret;
35
36                         sigprocmask(SIG_SETMASK,&all_masked,&oset);
37                         ret=bn_mul_mont_fpu64(rp,ap,bp,np,n0,num);
38                         sigprocmask(SIG_SETMASK,&oset,NULL);
39
40                         return ret;
41                         }
42 #endif
43                 }
44         else if ((OPENSSL_ppccap_P&PPC_FPU64))
45                 /* this is a "must" on POWER6, but run-time detection
46                  * is not implemented yet... */
47                 return bn_mul_mont_fpu64(rp,ap,bp,np,n0,num);
48
49         return bn_mul_mont_int(rp,ap,bp,np,n0,num);
50         }
51 #endif
52
53 static sigjmp_buf ill_jmp;
54 static void ill_handler (int sig) { siglongjmp(ill_jmp,sig); }
55
56 void OPENSSL_ppc64_probe(void);
57 void OPENSSL_altivec_probe(void);
58
59 void OPENSSL_cpuid_setup(void)
60         {
61         char *e;
62         struct sigaction        ill_oact,ill_act;
63         sigset_t                oset;
64         static int trigger=0;
65
66         if (trigger) return;
67         trigger=1;
68  
69         sigfillset(&all_masked);
70         sigdelset(&all_masked,SIGILL);
71         sigdelset(&all_masked,SIGTRAP);
72 #ifdef SIGEMT
73         sigdelset(&all_masked,SIGEMT);
74 #endif
75         sigdelset(&all_masked,SIGFPE);
76         sigdelset(&all_masked,SIGBUS);
77         sigdelset(&all_masked,SIGSEGV);
78
79         if ((e=getenv("OPENSSL_ppccap")))
80                 {
81                 OPENSSL_ppccap_P=strtoul(e,NULL,0);
82                 return;
83                 }
84
85         OPENSSL_ppccap_P = 0;
86
87 #if defined(_AIX)
88         if (sizeof(size_t)==4
89 # if defined(_SC_AIX_KERNEL_BITMODE)
90             && sysconf(_SC_AIX_KERNEL_BITMODE)!=64
91 # endif
92            )
93                 return;
94 #endif
95
96         memset(&ill_act,0,sizeof(ill_act));
97         ill_act.sa_handler = ill_handler;
98         ill_act.sa_mask    = all_masked;
99
100         sigprocmask(SIG_SETMASK,&ill_act.sa_mask,&oset);
101         sigaction(SIGILL,&ill_act,&ill_oact);
102
103         if (sizeof(size_t)==4)
104                 {
105                 if (sigsetjmp(ill_jmp,1) == 0)
106                         {
107                         OPENSSL_ppc64_probe();
108                         OPENSSL_ppccap_P |= PPC_FPU64;
109                         }
110                 }
111         else
112                 {
113                 /*
114                  * Wanted code detecting POWER6 CPU and setting PPC_FPU64
115                  */
116                 }
117
118         if (sigsetjmp(ill_jmp,1) == 0)
119                 {
120                 OPENSSL_altivec_probe();
121                 OPENSSL_ppccap_P |= PPC_ALTIVEC;
122                 }
123
124         sigaction (SIGILL,&ill_oact,NULL);
125         sigprocmask(SIG_SETMASK,&oset,NULL);
126         }