1 /*
2  * Copyright (c) 2003, 2007-14 Matteo Frigo
3  * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  */
20 
21 
22 #include "kernel/ifftw.h"
23 
24 #if HAVE_ALTIVEC
25 
26 #if HAVE_SYS_SYSCTL_H
27 #  include <sys/sysctl.h>
28 #endif
29 
30 #if HAVE_SYS_SYSCTL_H && HAVE_SYSCTL && defined(CTL_HW) && defined(HW_VECTORUNIT)
31 /* code for darwin */
really_have_altivec(void)32 static int really_have_altivec(void)
33 {
34      int mib[2], altivecp;
35      size_t len;
36      mib[0] = CTL_HW;
37      mib[1] = HW_VECTORUNIT;
38      len = sizeof(altivecp);
39      sysctl(mib, 2, &altivecp, &len, NULL, 0);
40      return altivecp;
41 }
42 #else /* GNU/Linux and other non-Darwin systems (!HAVE_SYS_SYSCTL_H etc.) */
43 
44 #include <signal.h>
45 #include <setjmp.h>
46 
47 static jmp_buf jb;
48 
sighandler(int x)49 static void sighandler(int x)
50 {
51      longjmp(jb, 1);
52 }
53 
really_have_altivec(void)54 static int really_have_altivec(void)
55 {
56      void (*oldsig)(int);
57      oldsig = signal(SIGILL, sighandler);
58      if (setjmp(jb)) {
59 	  signal(SIGILL, oldsig);
60 	  return 0;
61      } else {
62 	  __asm__ __volatile__ (".long 0x10000484"); /* vor 0,0,0 */
63 	  signal(SIGILL, oldsig);
64 	  return 1;
65      }
66      return 0;
67 }
68 #endif
69 
X(have_simd_altivec)70 int X(have_simd_altivec)(void)
71 {
72      static int init = 0, res;
73      if (!init) {
74 	  res = really_have_altivec();
75 	  init = 1;
76      }
77      return res;
78 }
79 
80 #endif
81