libFLAC/cpu.c: Fixes for MSVC
authorErik de Castro Lopo <erikd@mega-nerd.com>
Sun, 26 Jun 2016 11:09:08 +0000 (21:09 +1000)
committerErik de Castro Lopo <erikd@mega-nerd.com>
Sun, 26 Jun 2016 11:58:54 +0000 (21:58 +1000)
MSVC is strange and perverse.

src/libFLAC/cpu.c

index e2bcc67..c288b03 100644 (file)
@@ -35,6 +35,7 @@
 #endif
 
 #include "private/cpu.h"
+#include "share/compat.h"
 #include <stdlib.h>
 #include <memory.h>
 
@@ -453,6 +454,21 @@ void FLAC__cpu_info (FLAC__CPUInfo *info)
 
 #if (defined FLAC__CPU_IA32 || defined FLAC__CPU_X86_64) && FLAC__HAS_X86INTRIN
 
+#if defined _MSC_VER || defined __INTEL_COMPILER
+static inline void
+cpu_id_ex (int *cpuinfo, int result)
+{
+// Stupid MSVC doesn't know how to optimise out:
+//     if (FLAC_AVC_SUPPORTER)
+//             __cpuidex(cpuinfo, level, 0); /* for AVX2 detection */
+#if FLAC__AVX_SUPPORTED
+       __cpuidex(cpuinfo, result, 0); /* for AVX2 detection */
+#else
+       __cpuid(cpuinfo, result); /* some old compilers don't support __cpuidex */
+#endif
+}
+#endif
+
 void FLAC__cpu_info_x86(FLAC__uint32 level, FLAC__uint32 *eax, FLAC__uint32 *ebx, FLAC__uint32 *ecx, FLAC__uint32 *edx)
 {
 #if defined _MSC_VER || defined __INTEL_COMPILER
@@ -463,10 +479,8 @@ void FLAC__cpu_info_x86(FLAC__uint32 level, FLAC__uint32 *eax, FLAC__uint32 *ebx
                *eax = *ebx = *ecx = *edx = 0;
                return;
        }
-    if (FLAC__AVX_SUPPORTED)
-               __cpuidex(cpuinfo, level, 0); /* for AVX2 detection */
-       else
-               __cpuid(cpuinfo, level); /* some old compilers don't support __cpuidex */
+
+    cpu_id_ex (cpuinfo, ext);
 
        *eax = cpuinfo[0]; *ebx = cpuinfo[1]; *ecx = cpuinfo[2]; *edx = cpuinfo[3];
 #elif defined __GNUC__ && defined HAVE_CPUID_H