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