| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * |
| 9 | * - Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * |
| 12 | * - Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * |
| 16 | * - Neither the name of the Xiph.org Foundation nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 24 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 26 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 27 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 28 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 29 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | */ |
| 32 |
| 33 | #ifdef HAVE_CONFIG_H |
| 34 | # include <config.h> |
| 35 | #endif |
| 36 |
| 37 | #include "private/cpu.h" |
| 38 | #include "share/compat.h" |
| 39 | #include <stdlib.h> |
| 40 | #include <string.h> |
| 41 |
| 42 | #if defined _MSC_VER |
| 43 | #include <intrin.h> /* for __cpuid() and _xgetbv() */ |
| 44 | #elif defined __GNUC__ && defined HAVE_CPUID_H |
| 45 | #include <cpuid.h> /* for __get_cpuid() and __get_cpuid_max() */ |
| 46 | #endif |
| 47 |
| 48 | #ifndef NDEBUG |
| 49 | #include <stdio.h> |
| 50 | #define dfprintf fprintf |
| 51 | #else |
| 52 | /* This is bad practice, it should be a static void empty function */ |
| 53 | #define dfprintf(file, format, ...) |
| 54 | #endif |
| 55 |
| 56 |
| 57 | #if (defined FLAC__CPU_IA32 || defined FLAC__CPU_X86_64) && (defined FLAC__HAS_NASM || FLAC__HAS_X86INTRIN) && !defined FLAC__NO_ASM |
| 58 |
| 59 | /* these are flags in EDX of CPUID AX=00000001 */ |
| 60 | static const uint32_t FLAC__CPUINFO_X86_CPUID_CMOV = 0x00008000; |
| 61 | static const uint32_t FLAC__CPUINFO_X86_CPUID_MMX = 0x00800000; |
| 62 | static const uint32_t FLAC__CPUINFO_X86_CPUID_SSE = 0x02000000; |
| 63 | static const uint32_t FLAC__CPUINFO_X86_CPUID_SSE2 = 0x04000000; |
| 64 |
| 65 | /* these are flags in ECX of CPUID AX=00000001 */ |
| 66 | static const uint32_t FLAC__CPUINFO_X86_CPUID_SSE3 = 0x00000001; |
| 67 | static const uint32_t FLAC__CPUINFO_X86_CPUID_SSSE3 = 0x00000200; |
| 68 | static const uint32_t FLAC__CPUINFO_X86_CPUID_SSE41 = 0x00080000; |
| 69 | static const uint32_t FLAC__CPUINFO_X86_CPUID_SSE42 = 0x00100000; |
| 70 | static const uint32_t FLAC__CPUINFO_X86_CPUID_OSXSAVE = 0x08000000; |
| 71 | static const uint32_t FLAC__CPUINFO_X86_CPUID_AVX = 0x10000000; |
| 72 | static const uint32_t FLAC__CPUINFO_X86_CPUID_FMA = 0x00001000; |
| 73 |
| 74 | /* these are flags in EBX of CPUID AX=00000007 */ |
| 75 | static const uint32_t FLAC__CPUINFO_X86_CPUID_AVX2 = 0x00000020; |
| 76 |
| 77 | static uint32_t |
| 78 | cpu_xgetbv_x86(void) |
| 79 | { |
| 80 | #if (defined _MSC_VER || defined __INTEL_COMPILER) && FLAC__AVX_SUPPORTED |
| 81 | return (uint32_t)_xgetbv(0); |
| 82 | #elif defined __GNUC__ |
| 83 | uint32_t lo, hi; |
| 84 | __asm__ volatile (".byte 0x0f, 0x01, 0xd0" : "=a"(lo), "=d"(hi) : "c" (0)); |
| 85 | return lo; |
| 86 | #else |
| 87 | return 0; |
| 88 | #endif |
| 89 | } |
| 90 |
| 91 | static uint32_t |
| 92 | cpu_have_cpuid(void) |
| 93 | { |
| 94 | #if defined FLAC__CPU_X86_64 || defined __i686__ || defined __SSE__ || (defined _M_IX86_FP && _M_IX86_FP > 0) |
| 95 | /* target CPU does have CPUID instruction */ |
| 96 | return 1; |
| 97 | #elif defined FLAC__HAS_NASM |
| 98 | return FLAC__cpu_have_cpuid_asm_ia32(); |
| 99 | #elif defined __GNUC__ && defined HAVE_CPUID_H |
| 100 | if (__get_cpuid_max(0, 0) != 0) |
| 101 | return 1; |
| 102 | else |
| 103 | return 0; |
| 104 | #elif defined _MSC_VER |
| 105 | FLAC__uint32 flags1, flags2; |
| 106 | __asm { |
| 107 | pushfd |
| 108 | pushfd |
| 109 | pop eax |
| 110 | mov flags1, eax |
| 111 | xor eax, 0x200000 |
| 112 | push eax |
| 113 | popfd |
| 114 | pushfd |
| 115 | pop eax |
| 116 | mov flags2, eax |
| 117 | popfd |
| 118 | } |
| 119 | if (((flags1^flags2) & 0x200000) != 0) |
| 120 | return 1; |
| 121 | else |
| 122 | return 0; |
| 123 | #else |
| 124 | return 0; |
| 125 | #endif |
| 126 | } |
| 127 |
| 128 | static void |
| 129 | cpuinfo_x86(FLAC__uint32 level, FLAC__uint32 *eax, FLAC__uint32 *ebx, FLAC__uint32 *ecx, FLAC__uint32 *edx) |
| 130 | { |
| 131 | #if defined _MSC_VER |
| 132 | int cpuinfo[4]; |
| 133 | int ext = level & 0x80000000; |
| 134 | __cpuid(cpuinfo, ext); |
| 135 | if ((uint32_t)cpuinfo[0] >= level) { |
| 136 | #if FLAC__AVX_SUPPORTED |
| 137 | __cpuidex(cpuinfo, level, 0); /* for AVX2 detection */ |
| 138 | #else |
| 139 | __cpuid(cpuinfo, level); /* some old compilers don't support __cpuidex */ |
| 140 | #endif |
| 141 | *eax = cpuinfo[0]; *ebx = cpuinfo[1]; *ecx = cpuinfo[2]; *edx = cpuinfo[3]; |
| 142 | return; |
| 143 | } |
| 144 | #elif defined __GNUC__ && defined HAVE_CPUID_H |
| 145 | FLAC__uint32 ext = level & 0x80000000; |
| 146 | __cpuid(ext, *eax, *ebx, *ecx, *edx); |
| 147 | if (*eax >= level) { |
| 148 | __cpuid_count(level, 0, *eax, *ebx, *ecx, *edx); |
| 149 | return; |
| 150 | } |
| 151 | #elif defined FLAC__HAS_NASM && defined FLAC__CPU_IA32 |
| 152 | FLAC__cpu_info_asm_ia32(level, eax, ebx, ecx, edx); |
| 153 | return; |
| 154 | #endif |
| 155 | *eax = *ebx = *ecx = *edx = 0; |
| 156 | } |
| 157 |
| 158 | #endif |
| 159 |
| 160 | static void |
| 161 | x86_cpu_info (FLAC__CPUInfo *info) |
| 162 | { |
| 163 | #if (defined FLAC__CPU_IA32 || defined FLAC__CPU_X86_64) && (defined FLAC__HAS_NASM || FLAC__HAS_X86INTRIN) && !defined FLAC__NO_ASM |
| 164 | FLAC__bool x86_osxsave = false; |
| 165 | FLAC__bool os_avx = false; |
| 166 | FLAC__uint32 flags_eax, flags_ebx, flags_ecx, flags_edx; |
| 167 |
| 168 | info->use_asm = true; /* we assume a minimum of 80386 */ |
| 169 | if (!cpu_have_cpuid()) |
| 170 | return; |
| 171 |
| 172 | cpuinfo_x86(0, &flags_eax, &flags_ebx, &flags_ecx, &flags_edx); |
| 173 | info->x86.intel = (flags_ebx == 0x756E6547 && flags_edx == 0x49656E69 && flags_ecx == 0x6C65746E) ? true : false; /* GenuineIntel */ |
| 174 | cpuinfo_x86(1, &flags_eax, &flags_ebx, &flags_ecx, &flags_edx); |
| 175 |
| 176 | info->x86.cmov = (flags_edx & FLAC__CPUINFO_X86_CPUID_CMOV ) ? true : false; |
| 177 | info->x86.mmx = (flags_edx & FLAC__CPUINFO_X86_CPUID_MMX ) ? true : false; |
| 178 | info->x86.sse = (flags_edx & FLAC__CPUINFO_X86_CPUID_SSE ) ? true : false; |
| 179 | info->x86.sse2 = (flags_edx & FLAC__CPUINFO_X86_CPUID_SSE2 ) ? true : false; |
| 180 | info->x86.sse3 = (flags_ecx & FLAC__CPUINFO_X86_CPUID_SSE3 ) ? true : false; |
| 181 | info->x86.ssse3 = (flags_ecx & FLAC__CPUINFO_X86_CPUID_SSSE3) ? true : false; |
| 182 | info->x86.sse41 = (flags_ecx & FLAC__CPUINFO_X86_CPUID_SSE41) ? true : false; |
| 183 | info->x86.sse42 = (flags_ecx & FLAC__CPUINFO_X86_CPUID_SSE42) ? true : false; |
| 184 |
| 185 | if (FLAC__AVX_SUPPORTED) { |
| 186 | x86_osxsave = (flags_ecx & FLAC__CPUINFO_X86_CPUID_OSXSAVE) ? true : false; |
| 187 | info->x86.avx = (flags_ecx & FLAC__CPUINFO_X86_CPUID_AVX ) ? true : false; |
| 188 | info->x86.fma = (flags_ecx & FLAC__CPUINFO_X86_CPUID_FMA ) ? true : false; |
| 189 | cpuinfo_x86(7, &flags_eax, &flags_ebx, &flags_ecx, &flags_edx); |
| 190 | info->x86.avx2 = (flags_ebx & FLAC__CPUINFO_X86_CPUID_AVX2 ) ? true : false; |
| 191 | } |
| 192 |
| 193 | #if defined FLAC__CPU_IA32 |
| 194 | dfprintf(stderr, "CPU info (IA-32):\n"); |
| 195 | #else |
| 196 | dfprintf(stderr, "CPU info (x86-64):\n"); |
| 197 | #endif |
| 198 | dfprintf(stderr, " CMOV ....... %c\n", info->x86.cmov ? 'Y' : 'n'); |
| 199 | dfprintf(stderr, " MMX ........ %c\n", info->x86.mmx ? 'Y' : 'n'); |
| 200 | dfprintf(stderr, " SSE ........ %c\n", info->x86.sse ? 'Y' : 'n'); |
| 201 | dfprintf(stderr, " SSE2 ....... %c\n", info->x86.sse2 ? 'Y' : 'n'); |
| 202 | dfprintf(stderr, " SSE3 ....... %c\n", info->x86.sse3 ? 'Y' : 'n'); |
| 203 | dfprintf(stderr, " SSSE3 ...... %c\n", info->x86.ssse3 ? 'Y' : 'n'); |
| 204 | dfprintf(stderr, " SSE41 ...... %c\n", info->x86.sse41 ? 'Y' : 'n'); |
| 205 | dfprintf(stderr, " SSE42 ...... %c\n", info->x86.sse42 ? 'Y' : 'n'); |
| 206 |
| 207 | if (FLAC__AVX_SUPPORTED) { |
| 208 | dfprintf(stderr, " AVX ........ %c\n", info->x86.avx ? 'Y' : 'n'); |
| 209 | dfprintf(stderr, " FMA ........ %c\n", info->x86.fma ? 'Y' : 'n'); |
| 210 | dfprintf(stderr, " AVX2 ....... %c\n", info->x86.avx2 ? 'Y' : 'n'); |
| 211 | } |
| 212 |
| 213 | /* |
| 214 | * now have to check for OS support of AVX instructions |
| 215 | */ |
| 216 | if (FLAC__AVX_SUPPORTED && info->x86.avx && x86_osxsave && (cpu_xgetbv_x86() & 0x6) == 0x6) { |
| 217 | os_avx = true; |
| 218 | } |
| 219 | if (os_avx) { |
| 220 | dfprintf(stderr, " AVX OS sup . %c\n", info->x86.avx ? 'Y' : 'n'); |
| 221 | } |
| 222 | if (!os_avx) { |
| 223 | /* no OS AVX support */ |
| 224 | info->x86.avx = false; |
| 225 | info->x86.avx2 = false; |
| 226 | info->x86.fma = false; |
| 227 | } |
| 228 | #else |
| 229 | info->use_asm = false; |
| 230 | #endif |
| 231 | } |
| 232 |
| 233 | void FLAC__cpu_info (FLAC__CPUInfo *info) |
| 234 | { |
| 235 | memset(info, 0, sizeof(*info)); |
| 236 |
| 237 | #ifdef FLAC__CPU_IA32 |
| 238 | info->type = FLAC__CPUINFO_TYPE_IA32; |
| 239 | #elif defined FLAC__CPU_X86_64 |
| 240 | info->type = FLAC__CPUINFO_TYPE_X86_64; |
| 241 | #else |
| 242 | info->type = FLAC__CPUINFO_TYPE_UNKNOWN; |
| 243 | #endif |
| 244 |
| 245 | switch (info->type) { |
| 246 | case FLAC__CPUINFO_TYPE_IA32: /* fallthrough */ |
| 247 | case FLAC__CPUINFO_TYPE_X86_64: |
| 248 | x86_cpu_info (info); |
| 249 | break; |
| 250 | default: |
| 251 | info->use_asm = false; |
| 252 | break; |
| 253 | } |
| 254 | } |
| 255 |