Josh Coalson | edade2c | 2001-05-23 20:53:31 +0000 | [diff] [blame] | 1 | /* libFLAC - Free Lossless Audio Codec library |
Erik de Castro Lopo | b1982fb | 2013-05-25 17:11:19 +1000 | [diff] [blame] | 2 | * Copyright (C) 2001-2009 Josh Coalson |
Erik de Castro Lopo | 1437391 | 2014-11-24 22:07:15 +1100 | [diff] [blame] | 3 | * Copyright (C) 2011-2014 Xiph.Org Foundation |
Josh Coalson | edade2c | 2001-05-23 20:53:31 +0000 | [diff] [blame] | 4 | * |
Josh Coalson | afd8107 | 2003-01-31 23:34:56 +0000 | [diff] [blame] | 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
Josh Coalson | edade2c | 2001-05-23 20:53:31 +0000 | [diff] [blame] | 8 | * |
Josh Coalson | afd8107 | 2003-01-31 23:34:56 +0000 | [diff] [blame] | 9 | * - Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
Josh Coalson | edade2c | 2001-05-23 20:53:31 +0000 | [diff] [blame] | 11 | * |
Josh Coalson | afd8107 | 2003-01-31 23:34:56 +0000 | [diff] [blame] | 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. |
Josh Coalson | edade2c | 2001-05-23 20:53:31 +0000 | [diff] [blame] | 31 | */ |
| 32 | |
Erik de Castro Lopo | 006b835 | 2014-03-23 21:59:46 +1100 | [diff] [blame] | 33 | #ifdef HAVE_CONFIG_H |
Josh Coalson | b1ec796 | 2006-05-24 04:41:36 +0000 | [diff] [blame] | 34 | # include <config.h> |
| 35 | #endif |
| 36 | |
Josh Coalson | edade2c | 2001-05-23 20:53:31 +0000 | [diff] [blame] | 37 | #include "private/cpu.h" |
Erik de Castro Lopo | a08e90c | 2016-06-26 21:09:08 +1000 | [diff] [blame] | 38 | #include "share/compat.h" |
Josh Coalson | 663c599 | 2005-01-21 01:53:02 +0000 | [diff] [blame] | 39 | #include <stdlib.h> |
Erik de Castro Lopo | 35c14b1 | 2014-10-04 09:05:08 +1000 | [diff] [blame] | 40 | #include <memory.h> |
Erik de Castro Lopo | 23778a3 | 2016-06-25 17:02:06 +1000 | [diff] [blame] | 41 | |
Erik de Castro Lopo | 23778a3 | 2016-06-25 17:02:06 +1000 | [diff] [blame] | 42 | #if defined (__NetBSD__) || defined(__OpenBSD__) |
| 43 | # include <sys/param.h> |
| 44 | # include <sys/sysctl.h> |
| 45 | # include <machine/cpu.h> |
| 46 | #endif |
| 47 | |
| 48 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__APPLE__) |
| 49 | # include <sys/types.h> |
| 50 | # include <sys/sysctl.h> |
| 51 | #endif |
| 52 | |
| 53 | #if defined(__linux__) && defined(FLAC__CPU_IA32) && !defined FLAC__NO_ASM && (defined FLAC__HAS_NASM || FLAC__HAS_X86INTRIN) && !FLAC__SSE_OS |
| 54 | # include <sys/ucontext.h> |
| 55 | #endif |
| 56 | |
| 57 | #if defined(_MSC_VER) |
| 58 | # include <windows.h> |
| 59 | # include <intrin.h> /* for __cpuid() and _xgetbv() */ |
| 60 | #endif |
| 61 | |
| 62 | #if defined __GNUC__ && defined HAVE_CPUID_H |
| 63 | # include <cpuid.h> /* for __get_cpuid() and __get_cpuid_max() */ |
Erik de Castro Lopo | 35c14b1 | 2014-10-04 09:05:08 +1000 | [diff] [blame] | 64 | #endif |
Josh Coalson | edade2c | 2001-05-23 20:53:31 +0000 | [diff] [blame] | 65 | |
Erik de Castro Lopo | 7a8f359 | 2016-06-26 12:59:11 +1000 | [diff] [blame] | 66 | #ifdef DEBUG |
Julian Calaby | 828e33b | 2016-06-27 22:10:47 +1000 | [diff] [blame] | 67 | #include <stdio.h> |
| 68 | |
Julian Calaby | 601c051 | 2016-06-27 22:08:51 +1000 | [diff] [blame] | 69 | #define dfprintf fprintf |
Erik de Castro Lopo | 7a8f359 | 2016-06-26 12:59:11 +1000 | [diff] [blame] | 70 | #else |
Julian Calaby | 601c051 | 2016-06-27 22:08:51 +1000 | [diff] [blame] | 71 | /* This is bad practice, it should be a static void empty function */ |
Erik de Castro Lopo | 51fc948 | 2016-07-01 06:41:18 +1000 | [diff] [blame] | 72 | #define dfprintf(file, format, ...) |
Erik de Castro Lopo | 7a8f359 | 2016-06-26 12:59:11 +1000 | [diff] [blame] | 73 | #endif |
| 74 | |
| 75 | |
Josh Coalson | f2f328b | 2007-03-13 06:35:55 +0000 | [diff] [blame] | 76 | /* these are flags in EDX of CPUID AX=00000001 */ |
Erik de Castro Lopo | 23f6a35 | 2016-09-08 20:39:16 +1000 | [diff] [blame^] | 77 | #if defined FLAC__CPU_IA32 |
Josh Coalson | f2f328b | 2007-03-13 06:35:55 +0000 | [diff] [blame] | 78 | static const unsigned FLAC__CPUINFO_IA32_CPUID_CMOV = 0x00008000; |
| 79 | static const unsigned FLAC__CPUINFO_IA32_CPUID_MMX = 0x00800000; |
| 80 | static const unsigned FLAC__CPUINFO_IA32_CPUID_FXSR = 0x01000000; |
| 81 | static const unsigned FLAC__CPUINFO_IA32_CPUID_SSE = 0x02000000; |
| 82 | static const unsigned FLAC__CPUINFO_IA32_CPUID_SSE2 = 0x04000000; |
Erik de Castro Lopo | 23f6a35 | 2016-09-08 20:39:16 +1000 | [diff] [blame^] | 83 | #endif |
Erik de Castro Lopo | 35c14b1 | 2014-10-04 09:05:08 +1000 | [diff] [blame] | 84 | |
Josh Coalson | f2f328b | 2007-03-13 06:35:55 +0000 | [diff] [blame] | 85 | /* these are flags in ECX of CPUID AX=00000001 */ |
| 86 | static const unsigned FLAC__CPUINFO_IA32_CPUID_SSE3 = 0x00000001; |
| 87 | static const unsigned FLAC__CPUINFO_IA32_CPUID_SSSE3 = 0x00000200; |
Erik de Castro Lopo | 8fe2c23 | 2013-09-25 23:05:13 +1000 | [diff] [blame] | 88 | static const unsigned FLAC__CPUINFO_IA32_CPUID_SSE41 = 0x00080000; |
| 89 | static const unsigned FLAC__CPUINFO_IA32_CPUID_SSE42 = 0x00100000; |
Josh Coalson | edade2c | 2001-05-23 20:53:31 +0000 | [diff] [blame] | 90 | |
Erik de Castro Lopo | a75b870 | 2014-10-04 09:14:18 +1000 | [diff] [blame] | 91 | /* these are flags in ECX of CPUID AX=00000001 */ |
| 92 | static const unsigned FLAC__CPUINFO_IA32_CPUID_OSXSAVE = 0x08000000; |
| 93 | static const unsigned FLAC__CPUINFO_IA32_CPUID_AVX = 0x10000000; |
| 94 | static const unsigned FLAC__CPUINFO_IA32_CPUID_FMA = 0x00001000; |
| 95 | /* these are flags in EBX of CPUID AX=00000007 */ |
| 96 | static const unsigned FLAC__CPUINFO_IA32_CPUID_AVX2 = 0x00000020; |
Erik de Castro Lopo | a75b870 | 2014-10-04 09:14:18 +1000 | [diff] [blame] | 97 | |
Josh Coalson | 83f42e3 | 2007-03-03 01:52:19 +0000 | [diff] [blame] | 98 | /* |
| 99 | * Extra stuff needed for detection of OS support for SSE on IA-32 |
| 100 | */ |
Erik de Castro Lopo | 23778a3 | 2016-06-25 17:02:06 +1000 | [diff] [blame] | 101 | #if defined(__linux__) && defined(FLAC__CPU_IA32) && !defined FLAC__NO_ASM && (defined FLAC__HAS_NASM || FLAC__HAS_X86INTRIN) && !FLAC__SSE_OS |
Josh Coalson | 83f42e3 | 2007-03-03 01:52:19 +0000 | [diff] [blame] | 102 | /* |
| 103 | * If the OS doesn't support SSE, we will get here with a SIGILL. We |
| 104 | * modify the return address to jump over the offending SSE instruction |
| 105 | * and also the operation following it that indicates the instruction |
| 106 | * executed successfully. In this way we use no global variables and |
| 107 | * stay thread-safe. |
| 108 | * |
| 109 | * 3 + 3 + 6: |
| 110 | * 3 bytes for "xorps xmm0,xmm0" |
| 111 | * 3 bytes for estimate of how long the follwing "inc var" instruction is |
| 112 | * 6 bytes extra in case our estimate is wrong |
| 113 | * 12 bytes puts us in the NOP "landing zone" |
| 114 | */ |
Erik de Castro Lopo | 23778a3 | 2016-06-25 17:02:06 +1000 | [diff] [blame] | 115 | static void sigill_handler_sse_os(int signal, siginfo_t *si, void *uc) |
| 116 | { |
| 117 | (void)signal, (void)si; |
| 118 | ((ucontext_t*)uc)->uc_mcontext.gregs[14/*REG_EIP*/] += 3 + 3 + 6; |
| 119 | } |
Josh Coalson | 83f42e3 | 2007-03-03 01:52:19 +0000 | [diff] [blame] | 120 | #endif |
| 121 | |
Erik de Castro Lopo | 85c902f | 2016-06-27 05:09:07 +1000 | [diff] [blame] | 122 | #if defined FLAC__CPU_IA32 |
Erik de Castro Lopo | 162a449 | 2016-06-26 17:28:27 +1000 | [diff] [blame] | 123 | static void |
| 124 | ia32_disable_sse(FLAC__CPUInfo *info) |
| 125 | { |
| 126 | info->ia32.sse = false; |
| 127 | info->ia32.sse2 = false; |
| 128 | info->ia32.sse3 = false; |
| 129 | info->ia32.ssse3 = false; |
| 130 | info->ia32.sse41 = false; |
| 131 | info->ia32.sse42 = false; |
| 132 | } |
Erik de Castro Lopo | 85c902f | 2016-06-27 05:09:07 +1000 | [diff] [blame] | 133 | #endif |
Erik de Castro Lopo | 162a449 | 2016-06-26 17:28:27 +1000 | [diff] [blame] | 134 | |
Erik de Castro Lopo | 85c902f | 2016-06-27 05:09:07 +1000 | [diff] [blame] | 135 | #if defined FLAC__CPU_IA32 || defined FLAC__CPU_X86_64 |
Erik de Castro Lopo | 162a449 | 2016-06-26 17:28:27 +1000 | [diff] [blame] | 136 | static uint32_t |
| 137 | cpu_xgetbv_x86(void) |
Erik de Castro Lopo | fa24613 | 2016-06-26 10:39:18 +1000 | [diff] [blame] | 138 | { |
Erik de Castro Lopo | 7a8f359 | 2016-06-26 12:59:11 +1000 | [diff] [blame] | 139 | #if (defined _MSC_VER || defined __INTEL_COMPILER) && FLAC__HAS_X86INTRIN && FLAC__AVX_SUPPORTED |
Erik de Castro Lopo | fa24613 | 2016-06-26 10:39:18 +1000 | [diff] [blame] | 140 | return (uint32_t)_xgetbv(0); |
| 141 | #elif defined __GNUC__ |
| 142 | uint32_t lo, hi; |
| 143 | asm volatile (".byte 0x0f, 0x01, 0xd0" : "=a"(lo), "=d"(hi) : "c" (0)); |
| 144 | return lo; |
| 145 | #else |
| 146 | return 0; |
| 147 | #endif |
| 148 | } |
Erik de Castro Lopo | 162a449 | 2016-06-26 17:28:27 +1000 | [diff] [blame] | 149 | #endif |
Josh Coalson | 83f42e3 | 2007-03-03 01:52:19 +0000 | [diff] [blame] | 150 | |
Erik de Castro Lopo | 7a8f359 | 2016-06-26 12:59:11 +1000 | [diff] [blame] | 151 | static void |
| 152 | ia32_cpu_info (FLAC__CPUInfo *info) |
Josh Coalson | edade2c | 2001-05-23 20:53:31 +0000 | [diff] [blame] | 153 | { |
Erik de Castro Lopo | 85c902f | 2016-06-27 05:09:07 +1000 | [diff] [blame] | 154 | #if !defined FLAC__CPU_IA32 |
Erik de Castro Lopo | 162a449 | 2016-06-26 17:28:27 +1000 | [diff] [blame] | 155 | (void) info; |
Julian Calaby | 078c631 | 2016-06-27 22:13:30 +1000 | [diff] [blame] | 156 | #elif defined(__ANDROID__) || defined(ANDROID) |
| 157 | /* no need to check OS SSE support */ |
| 158 | info->use_asm = true; |
Erik de Castro Lopo | 162a449 | 2016-06-26 17:28:27 +1000 | [diff] [blame] | 159 | #else |
Erik de Castro Lopo | 35c14b1 | 2014-10-04 09:05:08 +1000 | [diff] [blame] | 160 | FLAC__bool ia32_fxsr = false; |
Erik de Castro Lopo | a75b870 | 2014-10-04 09:14:18 +1000 | [diff] [blame] | 161 | FLAC__bool ia32_osxsave = false; |
Erik de Castro Lopo | 7a8f359 | 2016-06-26 12:59:11 +1000 | [diff] [blame] | 162 | FLAC__uint32 flags_eax, flags_ebx, flags_ecx, flags_edx; |
| 163 | |
Erik de Castro Lopo | 36a0ab1 | 2016-06-20 20:29:59 +1000 | [diff] [blame] | 164 | #if !defined FLAC__NO_ASM && (defined FLAC__HAS_NASM || FLAC__HAS_X86INTRIN) |
Josh Coalson | f2f328b | 2007-03-13 06:35:55 +0000 | [diff] [blame] | 165 | info->use_asm = true; /* we assume a minimum of 80386 with FLAC__CPU_IA32 */ |
Erik de Castro Lopo | fa24613 | 2016-06-26 10:39:18 +1000 | [diff] [blame] | 166 | #if !FLAC__HAS_X86INTRIN |
Erik de Castro Lopo | 35c14b1 | 2014-10-04 09:05:08 +1000 | [diff] [blame] | 167 | if(!FLAC__cpu_have_cpuid_asm_ia32()) |
| 168 | return; |
| 169 | #endif |
Erik de Castro Lopo | 7a8f359 | 2016-06-26 12:59:11 +1000 | [diff] [blame] | 170 | /* http://www.sandpile.org/x86/cpuid.htm */ |
| 171 | if (FLAC__HAS_X86INTRIN) { |
| 172 | FLAC__cpu_info_x86(0, &flags_eax, &flags_ebx, &flags_ecx, &flags_edx); |
| 173 | info->ia32.intel = (flags_ebx == 0x756E6547 && flags_edx == 0x49656E69 && flags_ecx == 0x6C65746E) ? true : false; /* GenuineIntel */ |
| 174 | FLAC__cpu_info_x86(1, &flags_eax, &flags_ebx, &flags_ecx, &flags_edx); |
| 175 | } |
| 176 | else { |
| 177 | FLAC__cpu_info_asm_ia32(&flags_edx, &flags_ecx); |
Erik de Castro Lopo | cf732a8 | 2014-09-24 06:41:56 +1000 | [diff] [blame] | 178 | } |
Erik de Castro Lopo | 35c14b1 | 2014-10-04 09:05:08 +1000 | [diff] [blame] | 179 | |
Erik de Castro Lopo | 7a8f359 | 2016-06-26 12:59:11 +1000 | [diff] [blame] | 180 | info->ia32.cmov = (flags_edx & FLAC__CPUINFO_IA32_CPUID_CMOV ) ? true : false; |
| 181 | info->ia32.mmx = (flags_edx & FLAC__CPUINFO_IA32_CPUID_MMX ) ? true : false; |
| 182 | ia32_fxsr = (flags_edx & FLAC__CPUINFO_IA32_CPUID_FXSR ) ? true : false; |
| 183 | info->ia32.sse = (flags_edx & FLAC__CPUINFO_IA32_CPUID_SSE ) ? true : false; |
| 184 | info->ia32.sse2 = (flags_edx & FLAC__CPUINFO_IA32_CPUID_SSE2 ) ? true : false; |
| 185 | info->ia32.sse3 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSE3 ) ? true : false; |
| 186 | info->ia32.ssse3 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSSE3) ? true : false; |
| 187 | info->ia32.sse41 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSE41) ? true : false; |
| 188 | info->ia32.sse42 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSE42) ? true : false; |
| 189 | |
Erik de Castro Lopo | 936e968 | 2016-06-26 11:32:06 +1000 | [diff] [blame] | 190 | if (FLAC__HAS_X86INTRIN && FLAC__AVX_SUPPORTED) { |
Erik de Castro Lopo | 7a8f359 | 2016-06-26 12:59:11 +1000 | [diff] [blame] | 191 | ia32_osxsave = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_OSXSAVE) ? true : false; |
| 192 | info->ia32.avx = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_AVX ) ? true : false; |
| 193 | info->ia32.fma = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_FMA ) ? true : false; |
| 194 | FLAC__cpu_info_x86(7, &flags_eax, &flags_ebx, &flags_ecx, &flags_edx); |
| 195 | info->ia32.avx2 = (flags_ebx & FLAC__CPUINFO_IA32_CPUID_AVX2 ) ? true : false; |
Erik de Castro Lopo | 936e968 | 2016-06-26 11:32:06 +1000 | [diff] [blame] | 196 | } |
Erik de Castro Lopo | 7a8f359 | 2016-06-26 12:59:11 +1000 | [diff] [blame] | 197 | |
Julian Calaby | 601c051 | 2016-06-27 22:08:51 +1000 | [diff] [blame] | 198 | dfprintf(stderr, "CPU info (IA-32):\n"); |
| 199 | dfprintf(stderr, " CMOV ....... %c\n", info->ia32.cmov ? 'Y' : 'n'); |
| 200 | dfprintf(stderr, " MMX ........ %c\n", info->ia32.mmx ? 'Y' : 'n'); |
| 201 | dfprintf(stderr, " SSE ........ %c\n", info->ia32.sse ? 'Y' : 'n'); |
| 202 | dfprintf(stderr, " SSE2 ....... %c\n", info->ia32.sse2 ? 'Y' : 'n'); |
| 203 | dfprintf(stderr, " SSE3 ....... %c\n", info->ia32.sse3 ? 'Y' : 'n'); |
| 204 | dfprintf(stderr, " SSSE3 ...... %c\n", info->ia32.ssse3 ? 'Y' : 'n'); |
| 205 | dfprintf(stderr, " SSE41 ...... %c\n", info->ia32.sse41 ? 'Y' : 'n'); |
| 206 | dfprintf(stderr, " SSE42 ...... %c\n", info->ia32.sse42 ? 'Y' : 'n'); |
Erik de Castro Lopo | 7a8f359 | 2016-06-26 12:59:11 +1000 | [diff] [blame] | 207 | |
Julian Calaby | 601c051 | 2016-06-27 22:08:51 +1000 | [diff] [blame] | 208 | if (FLAC__HAS_X86INTRIN && FLAC__AVX_SUPPORTED) { |
| 209 | dfprintf(stderr, " AVX ........ %c\n", info->ia32.avx ? 'Y' : 'n'); |
| 210 | dfprintf(stderr, " FMA ........ %c\n", info->ia32.fma ? 'Y' : 'n'); |
| 211 | dfprintf(stderr, " AVX2 ....... %c\n", info->ia32.avx2 ? 'Y' : 'n'); |
Erik de Castro Lopo | 7a8f359 | 2016-06-26 12:59:11 +1000 | [diff] [blame] | 212 | } |
Josh Coalson | f2f328b | 2007-03-13 06:35:55 +0000 | [diff] [blame] | 213 | |
Erik de Castro Lopo | cf732a8 | 2014-09-24 06:41:56 +1000 | [diff] [blame] | 214 | /* |
| 215 | * now have to check for OS support of SSE instructions |
| 216 | */ |
| 217 | if(info->ia32.sse) { |
Erik de Castro Lopo | 23778a3 | 2016-06-25 17:02:06 +1000 | [diff] [blame] | 218 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) || defined(__APPLE__) |
Erik de Castro Lopo | cf732a8 | 2014-09-24 06:41:56 +1000 | [diff] [blame] | 219 | int sse = 0; |
Erik de Castro Lopo | 7a8f359 | 2016-06-26 12:59:11 +1000 | [diff] [blame] | 220 | size_t len = sizeof(sse); |
Erik de Castro Lopo | cf732a8 | 2014-09-24 06:41:56 +1000 | [diff] [blame] | 221 | /* at least one of these must work: */ |
Erik de Castro Lopo | 7a8f359 | 2016-06-26 12:59:11 +1000 | [diff] [blame] | 222 | sse = sse || (sysctlbyname("hw.instruction_sse", &sse, &len, NULL, 0) == 0 && sse); |
| 223 | sse = sse || (sysctlbyname("hw.optional.sse" , &sse, &len, NULL, 0) == 0 && sse); /* __APPLE__ ? */ |
Erik de Castro Lopo | cf732a8 | 2014-09-24 06:41:56 +1000 | [diff] [blame] | 224 | if(!sse) |
Erik de Castro Lopo | 7a8f359 | 2016-06-26 12:59:11 +1000 | [diff] [blame] | 225 | ia32_disable_sse(info); |
Josh Coalson | 83f42e3 | 2007-03-03 01:52:19 +0000 | [diff] [blame] | 226 | #elif defined(__NetBSD__) || defined (__OpenBSD__) |
Erik de Castro Lopo | cf732a8 | 2014-09-24 06:41:56 +1000 | [diff] [blame] | 227 | int val = 0, mib[2] = { CTL_MACHDEP, CPU_SSE }; |
| 228 | size_t len = sizeof(val); |
| 229 | if(sysctl(mib, 2, &val, &len, NULL, 0) < 0 || !val) |
Erik de Castro Lopo | 7a8f359 | 2016-06-26 12:59:11 +1000 | [diff] [blame] | 230 | ia32_disable_sse(info); |
Erik de Castro Lopo | cf732a8 | 2014-09-24 06:41:56 +1000 | [diff] [blame] | 231 | else { /* double-check SSE2 */ |
| 232 | mib[1] = CPU_SSE2; |
| 233 | len = sizeof(val); |
| 234 | if(sysctl(mib, 2, &val, &len, NULL, 0) < 0 || !val) { |
Erik de Castro Lopo | 7a8f359 | 2016-06-26 12:59:11 +1000 | [diff] [blame] | 235 | ia32_disable_sse(info); |
Erik de Castro Lopo | 35c14b1 | 2014-10-04 09:05:08 +1000 | [diff] [blame] | 236 | info->ia32.sse = true; |
Erik de Castro Lopo | cf732a8 | 2014-09-24 06:41:56 +1000 | [diff] [blame] | 237 | } |
| 238 | } |
Erik de Castro Lopo | e120037 | 2016-06-19 21:51:31 +1000 | [diff] [blame] | 239 | #elif defined(__linux__) && !FLAC__SSE_OS |
Erik de Castro Lopo | cf732a8 | 2014-09-24 06:41:56 +1000 | [diff] [blame] | 240 | int sse = 0; |
| 241 | struct sigaction sigill_save; |
| 242 | struct sigaction sigill_sse; |
| 243 | sigill_sse.sa_sigaction = sigill_handler_sse_os; |
Erik de Castro Lopo | a9f8442 | 2016-03-14 18:14:31 +1100 | [diff] [blame] | 244 | sigemptyset(&sigill_sse.sa_mask); |
Erik de Castro Lopo | cf732a8 | 2014-09-24 06:41:56 +1000 | [diff] [blame] | 245 | sigill_sse.sa_flags = SA_SIGINFO | SA_RESETHAND; /* SA_RESETHAND just in case our SIGILL return jump breaks, so we don't get stuck in a loop */ |
| 246 | if(0 == sigaction(SIGILL, &sigill_sse, &sigill_save)) |
| 247 | { |
| 248 | /* http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html */ |
| 249 | /* see sigill_handler_sse_os() for an explanation of the following: */ |
| 250 | asm volatile ( |
| 251 | "xorps %%xmm0,%%xmm0\n\t" /* will cause SIGILL if unsupported by OS */ |
| 252 | "incl %0\n\t" /* SIGILL handler will jump over this */ |
| 253 | /* landing zone */ |
| 254 | "nop\n\t" /* SIGILL jump lands here if "inc" is 9 bytes */ |
| 255 | "nop\n\t" |
| 256 | "nop\n\t" |
| 257 | "nop\n\t" |
| 258 | "nop\n\t" |
| 259 | "nop\n\t" |
| 260 | "nop\n\t" /* SIGILL jump lands here if "inc" is 3 bytes (expected) */ |
| 261 | "nop\n\t" |
| 262 | "nop" /* SIGILL jump lands here if "inc" is 1 byte */ |
| 263 | : "=r"(sse) |
| 264 | : "0"(sse) |
| 265 | ); |
Josh Coalson | 83f42e3 | 2007-03-03 01:52:19 +0000 | [diff] [blame] | 266 | |
Erik de Castro Lopo | cf732a8 | 2014-09-24 06:41:56 +1000 | [diff] [blame] | 267 | sigaction(SIGILL, &sigill_save, NULL); |
| 268 | } |
Josh Coalson | 83f42e3 | 2007-03-03 01:52:19 +0000 | [diff] [blame] | 269 | |
Erik de Castro Lopo | cf732a8 | 2014-09-24 06:41:56 +1000 | [diff] [blame] | 270 | if(!sse) |
Erik de Castro Lopo | 7a8f359 | 2016-06-26 12:59:11 +1000 | [diff] [blame] | 271 | ia32_disable_sse(info); |
Erik de Castro Lopo | cf732a8 | 2014-09-24 06:41:56 +1000 | [diff] [blame] | 272 | #elif defined(_MSC_VER) |
| 273 | __try { |
| 274 | __asm { |
| 275 | xorps xmm0,xmm0 |
| 276 | } |
| 277 | } |
| 278 | __except(EXCEPTION_EXECUTE_HANDLER) { |
| 279 | if (_exception_code() == STATUS_ILLEGAL_INSTRUCTION) |
Erik de Castro Lopo | 7a8f359 | 2016-06-26 12:59:11 +1000 | [diff] [blame] | 280 | ia32_disable_sse(info); |
Erik de Castro Lopo | cf732a8 | 2014-09-24 06:41:56 +1000 | [diff] [blame] | 281 | } |
| 282 | #elif defined(__GNUC__) /* MinGW goes here */ |
| 283 | int sse = 0; |
| 284 | /* Based on the idea described in Agner Fog's manual "Optimizing subroutines in assembly language" */ |
| 285 | /* In theory, not guaranteed to detect lack of OS SSE support on some future Intel CPUs, but in practice works (see the aforementioned manual) */ |
Erik de Castro Lopo | 35c14b1 | 2014-10-04 09:05:08 +1000 | [diff] [blame] | 286 | if (ia32_fxsr) { |
Erik de Castro Lopo | cf732a8 | 2014-09-24 06:41:56 +1000 | [diff] [blame] | 287 | struct { |
| 288 | FLAC__uint32 buff[128]; |
| 289 | } __attribute__((aligned(16))) fxsr; |
| 290 | FLAC__uint32 old_val, new_val; |
| 291 | |
Erik de Castro Lopo | 8d43282 | 2015-10-13 20:20:29 +1100 | [diff] [blame] | 292 | memset(fxsr.buff, 0, sizeof (fxsr.buff)); |
| 293 | |
Erik de Castro Lopo | cf732a8 | 2014-09-24 06:41:56 +1000 | [diff] [blame] | 294 | asm volatile ("fxsave %0" : "=m" (fxsr) : "m" (fxsr)); |
| 295 | old_val = fxsr.buff[50]; |
| 296 | fxsr.buff[50] ^= 0x0013c0de; /* change value in the buffer */ |
| 297 | asm volatile ("fxrstor %0" : "=m" (fxsr) : "m" (fxsr)); /* try to change SSE register */ |
| 298 | fxsr.buff[50] = old_val; /* restore old value in the buffer */ |
Erik de Castro Lopo | 6ec64d3 | 2015-11-18 18:59:26 +1100 | [diff] [blame] | 299 | asm volatile ("fxsave %0" : "=m" (fxsr) : "m" (fxsr)); /* old value will be overwritten if SSE register was changed */ |
Erik de Castro Lopo | cf732a8 | 2014-09-24 06:41:56 +1000 | [diff] [blame] | 300 | new_val = fxsr.buff[50]; /* == old_val if FXRSTOR didn't change SSE register and (old_val ^ 0x0013c0de) otherwise */ |
| 301 | fxsr.buff[50] = old_val; /* again restore old value in the buffer */ |
| 302 | asm volatile ("fxrstor %0" : "=m" (fxsr) : "m" (fxsr)); /* restore old values of registers */ |
| 303 | |
| 304 | if ((old_val^new_val) == 0x0013c0de) |
| 305 | sse = 1; |
| 306 | } |
| 307 | if(!sse) |
Erik de Castro Lopo | 7a8f359 | 2016-06-26 12:59:11 +1000 | [diff] [blame] | 308 | ia32_disable_sse(info); |
Erik de Castro Lopo | cf732a8 | 2014-09-24 06:41:56 +1000 | [diff] [blame] | 309 | #else |
| 310 | /* no way to test, disable to be safe */ |
Erik de Castro Lopo | 7a8f359 | 2016-06-26 12:59:11 +1000 | [diff] [blame] | 311 | ia32_disable_sse(info); |
Josh Coalson | 83f42e3 | 2007-03-03 01:52:19 +0000 | [diff] [blame] | 312 | #endif |
Julian Calaby | 601c051 | 2016-06-27 22:08:51 +1000 | [diff] [blame] | 313 | dfprintf(stderr, " SSE OS sup . %c\n", info->ia32.sse ? 'Y' : 'n'); |
Josh Coalson | edade2c | 2001-05-23 20:53:31 +0000 | [diff] [blame] | 314 | } |
Erik de Castro Lopo | cf732a8 | 2014-09-24 06:41:56 +1000 | [diff] [blame] | 315 | else /* info->ia32.sse == false */ |
Erik de Castro Lopo | 7a8f359 | 2016-06-26 12:59:11 +1000 | [diff] [blame] | 316 | ia32_disable_sse(info); |
Erik de Castro Lopo | cf732a8 | 2014-09-24 06:41:56 +1000 | [diff] [blame] | 317 | |
Erik de Castro Lopo | a75b870 | 2014-10-04 09:14:18 +1000 | [diff] [blame] | 318 | /* |
| 319 | * now have to check for OS support of AVX instructions |
| 320 | */ |
Julian Calaby | 42c5de6 | 2016-06-27 23:22:20 +1000 | [diff] [blame] | 321 | if (!FLAC__HAS_X86INTRIN || !info->ia32.avx || !ia32_osxsave || (cpu_xgetbv_x86() & 0x6) != 0x6) { |
| 322 | /* no OS AVX support */ |
| 323 | info->ia32.avx = false; |
| 324 | info->ia32.avx2 = false; |
| 325 | info->ia32.fma = false; |
Erik de Castro Lopo | a75b870 | 2014-10-04 09:14:18 +1000 | [diff] [blame] | 326 | } |
Erik de Castro Lopo | 7a8f359 | 2016-06-26 12:59:11 +1000 | [diff] [blame] | 327 | |
Erik de Castro Lopo | bba26a5 | 2016-06-28 07:12:42 +1000 | [diff] [blame] | 328 | if (FLAC__HAS_X86INTRIN && FLAC__AVX_SUPPORTED) { |
Julian Calaby | 42c5de6 | 2016-06-27 23:22:20 +1000 | [diff] [blame] | 329 | dfprintf(stderr, " AVX OS sup . %c\n", info->ia32.avx ? 'Y' : 'n'); |
Erik de Castro Lopo | bba26a5 | 2016-06-28 07:12:42 +1000 | [diff] [blame] | 330 | } |
Erik de Castro Lopo | 826b64f | 2015-11-07 07:06:23 +1100 | [diff] [blame] | 331 | #else |
Josh Coalson | edade2c | 2001-05-23 20:53:31 +0000 | [diff] [blame] | 332 | info->use_asm = false; |
| 333 | #endif |
Erik de Castro Lopo | 162a449 | 2016-06-26 17:28:27 +1000 | [diff] [blame] | 334 | #endif |
Erik de Castro Lopo | 7a8f359 | 2016-06-26 12:59:11 +1000 | [diff] [blame] | 335 | } |
Josh Coalson | 83f42e3 | 2007-03-03 01:52:19 +0000 | [diff] [blame] | 336 | |
Erik de Castro Lopo | 7a8f359 | 2016-06-26 12:59:11 +1000 | [diff] [blame] | 337 | static void |
| 338 | x86_64_cpu_info (FLAC__CPUInfo *info) |
| 339 | { |
Erik de Castro Lopo | 85c902f | 2016-06-27 05:09:07 +1000 | [diff] [blame] | 340 | #if !defined FLAC__CPU_X86_64 |
Erik de Castro Lopo | 162a449 | 2016-06-26 17:28:27 +1000 | [diff] [blame] | 341 | (void) info; |
Julian Calaby | 078c631 | 2016-06-27 22:13:30 +1000 | [diff] [blame] | 342 | #elif defined(__ANDROID__) || defined(ANDROID) |
| 343 | /* no need to check OS SSE support */ |
| 344 | info->use_asm = true; |
Julian Calaby | e58a6e7 | 2016-06-27 22:17:03 +1000 | [diff] [blame] | 345 | #elif !defined FLAC__NO_ASM && FLAC__HAS_X86INTRIN |
Erik de Castro Lopo | a75b870 | 2014-10-04 09:14:18 +1000 | [diff] [blame] | 346 | FLAC__bool x86_osxsave = false; |
Erik de Castro Lopo | 7a8f359 | 2016-06-26 12:59:11 +1000 | [diff] [blame] | 347 | FLAC__uint32 flags_eax, flags_ebx, flags_ecx, flags_edx; |
| 348 | |
Erik de Castro Lopo | 84c3e3d | 2013-09-15 09:46:17 +1000 | [diff] [blame] | 349 | info->use_asm = true; |
Erik de Castro Lopo | 7a8f359 | 2016-06-26 12:59:11 +1000 | [diff] [blame] | 350 | |
| 351 | /* http://www.sandpile.org/x86/cpuid.htm */ |
| 352 | FLAC__cpu_info_x86(0, &flags_eax, &flags_ebx, &flags_ecx, &flags_edx); |
| 353 | info->x86.intel = (flags_ebx == 0x756E6547 && flags_edx == 0x49656E69 && flags_ecx == 0x6C65746E) ? true : false; /* GenuineIntel */ |
| 354 | FLAC__cpu_info_x86(1, &flags_eax, &flags_ebx, &flags_ecx, &flags_edx); |
| 355 | info->x86.sse3 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSE3 ) ? true : false; |
| 356 | info->x86.ssse3 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSSE3) ? true : false; |
| 357 | info->x86.sse41 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSE41) ? true : false; |
| 358 | info->x86.sse42 = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_SSE42) ? true : false; |
| 359 | |
| 360 | if (FLAC__AVX_SUPPORTED) { |
| 361 | x86_osxsave = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_OSXSAVE) ? true : false; |
| 362 | info->x86.avx = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_AVX ) ? true : false; |
| 363 | info->x86.fma = (flags_ecx & FLAC__CPUINFO_IA32_CPUID_FMA ) ? true : false; |
| 364 | FLAC__cpu_info_x86(7, &flags_eax, &flags_ebx, &flags_ecx, &flags_edx); |
| 365 | info->x86.avx2 = (flags_ebx & FLAC__CPUINFO_IA32_CPUID_AVX2 ) ? true : false; |
| 366 | } |
| 367 | |
Julian Calaby | 601c051 | 2016-06-27 22:08:51 +1000 | [diff] [blame] | 368 | dfprintf(stderr, "CPU info (x86-64):\n"); |
| 369 | dfprintf(stderr, " SSE3 ....... %c\n", info->x86.sse3 ? 'Y' : 'n'); |
| 370 | dfprintf(stderr, " SSSE3 ...... %c\n", info->x86.ssse3 ? 'Y' : 'n'); |
| 371 | dfprintf(stderr, " SSE41 ...... %c\n", info->x86.sse41 ? 'Y' : 'n'); |
| 372 | dfprintf(stderr, " SSE42 ...... %c\n", info->x86.sse42 ? 'Y' : 'n'); |
Erik de Castro Lopo | 7a8f359 | 2016-06-26 12:59:11 +1000 | [diff] [blame] | 373 | |
Julian Calaby | 601c051 | 2016-06-27 22:08:51 +1000 | [diff] [blame] | 374 | if (FLAC__AVX_SUPPORTED) { |
| 375 | dfprintf(stderr, " AVX ........ %c\n", info->x86.avx ? 'Y' : 'n'); |
| 376 | dfprintf(stderr, " FMA ........ %c\n", info->x86.fma ? 'Y' : 'n'); |
| 377 | dfprintf(stderr, " AVX2 ....... %c\n", info->x86.avx2 ? 'Y' : 'n'); |
Erik de Castro Lopo | 84c3e3d | 2013-09-15 09:46:17 +1000 | [diff] [blame] | 378 | } |
Erik de Castro Lopo | 84c3e3d | 2013-09-15 09:46:17 +1000 | [diff] [blame] | 379 | |
Erik de Castro Lopo | a75b870 | 2014-10-04 09:14:18 +1000 | [diff] [blame] | 380 | /* |
| 381 | * now have to check for OS support of AVX instructions |
| 382 | */ |
Julian Calaby | 42c5de6 | 2016-06-27 23:22:20 +1000 | [diff] [blame] | 383 | if (!info->x86.avx || !x86_osxsave || (cpu_xgetbv_x86() & 0x6) != 0x6) { |
| 384 | /* no OS AVX support */ |
| 385 | info->x86.avx = false; |
| 386 | info->x86.avx2 = false; |
| 387 | info->x86.fma = false; |
Erik de Castro Lopo | a75b870 | 2014-10-04 09:14:18 +1000 | [diff] [blame] | 388 | } |
Julian Calaby | 42c5de6 | 2016-06-27 23:22:20 +1000 | [diff] [blame] | 389 | |
Erik de Castro Lopo | bba26a5 | 2016-06-28 07:12:42 +1000 | [diff] [blame] | 390 | if (FLAC__AVX_SUPPORTED) { |
Julian Calaby | 42c5de6 | 2016-06-27 23:22:20 +1000 | [diff] [blame] | 391 | dfprintf(stderr, " AVX OS sup . %c\n", info->x86.avx ? 'Y' : 'n'); |
Erik de Castro Lopo | bba26a5 | 2016-06-28 07:12:42 +1000 | [diff] [blame] | 392 | } |
Erik de Castro Lopo | 84c3e3d | 2013-09-15 09:46:17 +1000 | [diff] [blame] | 393 | #endif |
Erik de Castro Lopo | 7a8f359 | 2016-06-26 12:59:11 +1000 | [diff] [blame] | 394 | } |
Erik de Castro Lopo | 84c3e3d | 2013-09-15 09:46:17 +1000 | [diff] [blame] | 395 | |
Erik de Castro Lopo | 7a8f359 | 2016-06-26 12:59:11 +1000 | [diff] [blame] | 396 | void FLAC__cpu_info (FLAC__CPUInfo *info) |
| 397 | { |
| 398 | memset(info, 0, sizeof(*info)); |
| 399 | |
| 400 | #ifdef FLAC__CPU_IA32 |
| 401 | info->type = FLAC__CPUINFO_TYPE_IA32; |
| 402 | #elif defined FLAC__CPU_X86_64 |
| 403 | info->type = FLAC__CPUINFO_TYPE_X86_64; |
Josh Coalson | edade2c | 2001-05-23 20:53:31 +0000 | [diff] [blame] | 404 | #else |
Josh Coalson | edade2c | 2001-05-23 20:53:31 +0000 | [diff] [blame] | 405 | info->type = FLAC__CPUINFO_TYPE_UNKNOWN; |
| 406 | info->use_asm = false; |
| 407 | #endif |
Erik de Castro Lopo | 7a8f359 | 2016-06-26 12:59:11 +1000 | [diff] [blame] | 408 | |
| 409 | switch (info->type) { |
| 410 | case FLAC__CPUINFO_TYPE_IA32: |
| 411 | ia32_cpu_info (info); |
| 412 | break; |
| 413 | case FLAC__CPUINFO_TYPE_X86_64: |
| 414 | x86_64_cpu_info (info); |
| 415 | break; |
| 416 | default: |
| 417 | info->use_asm = false; |
| 418 | break; |
| 419 | } |
Josh Coalson | edade2c | 2001-05-23 20:53:31 +0000 | [diff] [blame] | 420 | } |
Erik de Castro Lopo | 84c3e3d | 2013-09-15 09:46:17 +1000 | [diff] [blame] | 421 | |
Erik de Castro Lopo | 36a0ab1 | 2016-06-20 20:29:59 +1000 | [diff] [blame] | 422 | #if (defined FLAC__CPU_IA32 || defined FLAC__CPU_X86_64) && FLAC__HAS_X86INTRIN |
Erik de Castro Lopo | 84c3e3d | 2013-09-15 09:46:17 +1000 | [diff] [blame] | 423 | |
Erik de Castro Lopo | a75b870 | 2014-10-04 09:14:18 +1000 | [diff] [blame] | 424 | void FLAC__cpu_info_x86(FLAC__uint32 level, FLAC__uint32 *eax, FLAC__uint32 *ebx, FLAC__uint32 *ecx, FLAC__uint32 *edx) |
Erik de Castro Lopo | 84c3e3d | 2013-09-15 09:46:17 +1000 | [diff] [blame] | 425 | { |
Erik de Castro Lopo | 57297ee | 2014-01-30 21:53:37 +1100 | [diff] [blame] | 426 | #if defined _MSC_VER || defined __INTEL_COMPILER |
Erik de Castro Lopo | 8fe2c23 | 2013-09-25 23:05:13 +1000 | [diff] [blame] | 427 | int cpuinfo[4]; |
Erik de Castro Lopo | a75b870 | 2014-10-04 09:14:18 +1000 | [diff] [blame] | 428 | int ext = level & 0x80000000; |
| 429 | __cpuid(cpuinfo, ext); |
Julian Calaby | 79ca23e | 2016-06-27 22:37:45 +1000 | [diff] [blame] | 430 | if((unsigned)cpuinfo[0] >= level) { |
Julian Calaby | eae338e | 2016-06-27 22:41:13 +1000 | [diff] [blame] | 431 | #if FLAC__AVX_SUPPORTED |
| 432 | __cpuidex(cpuinfo, ext, 0); /* for AVX2 detection */ |
| 433 | #else |
| 434 | __cpuid(cpuinfo, ext); /* some old compilers don't support __cpuidex */ |
| 435 | #endif |
Julian Calaby | 79ca23e | 2016-06-27 22:37:45 +1000 | [diff] [blame] | 436 | |
| 437 | *eax = cpuinfo[0]; *ebx = cpuinfo[1]; *ecx = cpuinfo[2]; *edx = cpuinfo[3]; |
| 438 | |
Erik de Castro Lopo | 2c15052 | 2014-06-28 21:50:09 +1000 | [diff] [blame] | 439 | return; |
| 440 | } |
Erik de Castro Lopo | a75b870 | 2014-10-04 09:14:18 +1000 | [diff] [blame] | 441 | #elif defined __GNUC__ && defined HAVE_CPUID_H |
| 442 | FLAC__uint32 ext = level & 0x80000000; |
| 443 | __cpuid(ext, *eax, *ebx, *ecx, *edx); |
Julian Calaby | 79ca23e | 2016-06-27 22:37:45 +1000 | [diff] [blame] | 444 | if (*eax >= level) { |
| 445 | __cpuid_count(level, 0, *eax, *ebx, *ecx, *edx); |
| 446 | |
Erik de Castro Lopo | a75b870 | 2014-10-04 09:14:18 +1000 | [diff] [blame] | 447 | return; |
| 448 | } |
Erik de Castro Lopo | a75b870 | 2014-10-04 09:14:18 +1000 | [diff] [blame] | 449 | #endif |
Julian Calaby | 79ca23e | 2016-06-27 22:37:45 +1000 | [diff] [blame] | 450 | *eax = *ebx = *ecx = *edx = 0; |
Erik de Castro Lopo | a75b870 | 2014-10-04 09:14:18 +1000 | [diff] [blame] | 451 | } |
| 452 | |
Erik de Castro Lopo | b8d58e3 | 2014-06-15 20:29:34 +1000 | [diff] [blame] | 453 | #endif /* (FLAC__CPU_IA32 || FLAC__CPU_X86_64) && FLAC__HAS_X86INTRIN */ |