6fac797ff52dd283c3aafae686bebf7b6ebb4404
[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 <openssl/bn.h>
7
8 #define PPC_FPU64       (1<<0)
9
10 static int OPENSSL_ppccap_P = 0;
11
12 static sigset_t all_masked;
13
14 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)
15         {
16         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);
17         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);
18
19         if (sizeof(size_t)==4)
20                 {
21 #if (defined(__APPLE__) && defined(__MACH__))
22                 if ((OPENSSL_ppccap_P&PPC_FPU64))
23                         return bn_mul_mont_fpu64(rp,ap,bp,np,n0,num);
24 #else
25                 /* boundary of 32 was experimentally determined on
26                    Linux 2.6.22, might have to be adjusted on AIX... */
27                 if ((num>=32) && (OPENSSL_ppccap_P&PPC_FPU64))
28                         {
29                         sigset_t oset;
30                         int ret;
31
32                         sigprocmask(SIG_SETMASK,&all_masked,&oset);
33                         ret=bn_mul_mont_fpu64(rp,ap,bp,np,n0,num);
34                         sigprocmask(SIG_SETMASK,&oset,NULL);
35
36                         return ret;
37                         }
38 #endif
39                 }
40         else if ((OPENSSL_ppccap_P&PPC_FPU64))
41                 /* this is a "must" on Power 6, but run-time detection
42                  * is not implemented yet... */
43                 return bn_mul_mont_fpu64(rp,ap,bp,np,n0,num);
44
45         return bn_mul_mont_int(rp,ap,bp,np,n0,num);
46         }
47
48 static sigjmp_buf ill_jmp;
49 static void ill_handler (int sig) { siglongjmp(ill_jmp,sig); }
50
51 void OPENSSL_cpuid_setup(void)
52         {
53         char *e;
54
55         sigfillset(&all_masked);
56         sigdelset(&all_masked,SIGSEGV);
57         sigdelset(&all_masked,SIGILL);
58
59         if ((e=getenv("OPENSSL_ppccap")))
60                 {
61                 OPENSSL_ppccap_P=strtoul(e,NULL,0);
62                 return;
63                 }
64
65         if (sizeof(size_t)==4)
66                 {
67                 struct sigaction        ill_oact,ill_act;
68                 sigset_t                oset;
69
70                 memset(&ill_act,0,sizeof(ill_act));
71                 ill_act.sa_handler = ill_handler;
72                 sigfillset(&ill_act.sa_mask);
73                 sigdelset(&ill_act.sa_mask,SIGILL);
74                 sigprocmask(SIG_SETMASK,&ill_act.sa_mask,&oset);
75                 sigaction (SIGILL,&ill_act,&ill_oact);
76                 if (sigsetjmp(ill_jmp,0) == 0)
77                         {
78                         OPENSSL_ppc64_probe();
79                         OPENSSL_ppccap_P |= PPC_FPU64;
80                         }
81                 else
82                         {
83                         OPENSSL_ppccap_P &= ~PPC_FPU64;
84                         }
85                 sigaction (SIGILL,&ill_oact,NULL);
86                 sigprocmask(SIG_SETMASK,&oset,NULL);
87                 }
88         }