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 #ifdef FFTW_SINGLE
25 #  define DS(d,s) s /* single-precision option */
26 #else
27 #  define DS(d,s) d /* double-precision option */
28 #endif
29 
30 #if HAVE_SSE2
31 
32 # if defined(__x86_64__) || defined(_M_X64) || defined(_M_AMD64)
33 
X(have_simd_sse2)34   int X(have_simd_sse2)(void)
35   {
36        return 1;
37   }
38 
39 # else /* !x86_64 */
40 
41 # include <signal.h>
42 # include <setjmp.h>
43 # include "x86-cpuid.h"
44 
45   static jmp_buf jb;
46 
sighandler(int x)47   static void sighandler(int x)
48   {
49        UNUSED(x);
50        longjmp(jb, 1);
51   }
52 
sse2_works(void)53   static int sse2_works(void)
54   {
55        void (*oldsig)(int);
56        oldsig = signal(SIGILL, sighandler);
57        if (setjmp(jb)) {
58 	    signal(SIGILL, oldsig);
59 	    return 0;
60        } else {
61 #         ifdef _MSC_VER
62 	    _asm { DS(xorpd,xorps) xmm0,xmm0 }
63 #         else
64 	    /* asm volatile ("xorpd/s %xmm0, %xmm0"); */
65 	    asm volatile(DS(".byte 0x66; .byte 0x0f; .byte 0x57; .byte 0xc0",
66 			                ".byte 0x0f; .byte 0x57; .byte 0xc0"));
67 #         endif
68 	    signal(SIGILL, oldsig);
69 	    return 1;
70        }
71   }
72 
X(have_simd_sse2)73   int X(have_simd_sse2)(void)
74   {
75        static int init = 0, res;
76 
77        if (!init) {
78 	    res =   !is_386()
79 		 && has_cpuid()
80 		 && (cpuid_edx(1) & (1 << DS(26,25)))
81 		 && sse2_works();
82 	    init = 1;
83        }
84        return res;
85   }
86 
87 # endif
88 
89 #endif
90