1 /* This file is part of the Vc library. {{{
2 Copyright © 2010-2015 Matthias Kretz <kretz@kde.org>
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
6 * Redistributions of source code must retain the above copyright
7 notice, this list of conditions and the following disclaimer.
8 * Redistributions in binary form must reproduce the above copyright
9 notice, this list of conditions and the following disclaimer in the
10 documentation and/or other materials provided with the distribution.
11 * Neither the names of contributing organizations nor the
12 names of its contributors may be used to endorse or promote products
13 derived from this software without specific prior written permission.
14
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
19 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26 }}}*/
27
28 #include <Vc/global.h>
29 #include <Vc/cpuid.h>
30 #include <Vc/support.h>
31
32 #ifdef Vc_MSVC
33 #include <intrin.h>
34 #endif
35
36 #if defined(Vc_GCC) && Vc_GCC >= 0x40400
37 #define Vc_TARGET_NO_SIMD __attribute__((target("no-sse2,no-avx")))
38 #else
39 #define Vc_TARGET_NO_SIMD
40 #endif
41
42 namespace Vc_VERSIONED_NAMESPACE
43 {
44
45 Vc_TARGET_NO_SIMD
xgetbvCheck(unsigned int bits)46 static inline bool xgetbvCheck(unsigned int bits)
47 {
48 #if defined(Vc_MSVC) && Vc_MSVC >= 160040219 // MSVC 2010 SP1 introduced _xgetbv
49 unsigned long long xcrFeatureMask = _xgetbv(_XCR_XFEATURE_ENABLED_MASK);
50 return (xcrFeatureMask & bits) == bits;
51 #elif defined(Vc_GNU_ASM) && !defined(Vc_NO_XGETBV)
52 unsigned int eax;
53 asm("xgetbv" : "=a"(eax) : "c"(0) : "edx");
54 return (eax & bits) == bits;
55 #else
56 // can't check, but if OSXSAVE is true let's assume it'll work
57 return bits > 0; // ignore 'warning: unused parameter'
58 #endif
59 }
60
61 Vc_TARGET_NO_SIMD
isImplementationSupported(Implementation impl)62 bool isImplementationSupported(Implementation impl)
63 {
64 CpuId::init();
65
66 switch (impl) {
67 case ScalarImpl:
68 return true;
69 case SSE2Impl:
70 return CpuId::hasSse2();
71 case SSE3Impl:
72 return CpuId::hasSse3();
73 case SSSE3Impl:
74 return CpuId::hasSsse3();
75 case SSE41Impl:
76 return CpuId::hasSse41();
77 case SSE42Impl:
78 return CpuId::hasSse42();
79 case AVXImpl:
80 return CpuId::hasOsxsave() && CpuId::hasAvx() && xgetbvCheck(0x6);
81 case AVX2Impl:
82 return CpuId::hasOsxsave() && CpuId::hasAvx2() && xgetbvCheck(0x6);
83 case MICImpl:
84 return CpuId::processorFamily() == 0xB && CpuId::processorModel() == 0x1
85 && CpuId::isIntel();
86 case ImplementationMask:
87 return false;
88 }
89 return false;
90 }
91
92 Vc_TARGET_NO_SIMD
bestImplementationSupported()93 Vc::Implementation bestImplementationSupported()
94 {
95 CpuId::init();
96
97 if (CpuId::processorFamily() == 0xB && CpuId::processorModel() == 0x1
98 && CpuId::isIntel()) {
99 return Vc::MICImpl;
100 }
101 if (!CpuId::hasSse2 ()) return Vc::ScalarImpl;
102 if (!CpuId::hasSse3 ()) return Vc::SSE2Impl;
103 if (!CpuId::hasSsse3()) return Vc::SSE3Impl;
104 if (!CpuId::hasSse41()) return Vc::SSSE3Impl;
105 if (!CpuId::hasSse42()) return Vc::SSE41Impl;
106 if (CpuId::hasAvx() && CpuId::hasOsxsave() && xgetbvCheck(0x6)) {
107 if (!CpuId::hasAvx2()) return Vc::AVXImpl;
108 return Vc::AVX2Impl;
109 }
110 return Vc::SSE42Impl;
111 }
112
113 Vc_TARGET_NO_SIMD
extraInstructionsSupported()114 unsigned int extraInstructionsSupported()
115 {
116 unsigned int flags = 0;
117 if (CpuId::hasF16c()) flags |= Vc::Float16cInstructions;
118 if (CpuId::hasFma4()) flags |= Vc::Fma4Instructions;
119 if (CpuId::hasXop ()) flags |= Vc::XopInstructions;
120 if (CpuId::hasPopcnt()) flags |= Vc::PopcntInstructions;
121 if (CpuId::hasSse4a()) flags |= Vc::Sse4aInstructions;
122 if (CpuId::hasFma ()) flags |= Vc::FmaInstructions;
123 if (CpuId::hasBmi2()) flags |= Vc::Bmi2Instructions;
124 if (CpuId::hasOsxsave() && CpuId::hasAvx() && xgetbvCheck(0x6)) flags |= Vc::VexInstructions;
125 //if (CpuId::hasPclmulqdq()) flags |= Vc::PclmulqdqInstructions;
126 //if (CpuId::hasAes()) flags |= Vc::AesInstructions;
127 //if (CpuId::hasRdrand()) flags |= Vc::RdrandInstructions;
128 return flags;
129 }
130
131 }
132
133 #undef Vc_TARGET_NO_SIMD
134
135 // vim: sw=4 sts=4 et tw=100
136