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