4 * Feature detection for x86 processors.
9 * The author dedicates this file to the public domain.
10 * You can do whatever you want with this file.
13 #include "wimlib/x86_cpu_features.h"
15 #if defined(__i386__) || defined(__x86_64__)
23 u32 _x86_cpu_features = 0;
25 /* With old GCC versions we have to manually save and restore the x86_32 PIC
26 * register (ebx). See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47602 */
27 #if defined(__i386__) && defined(__PIC__)
28 # define EBX_CONSTRAINT "=r"
30 # define EBX_CONSTRAINT "=b"
33 /* Execute the CPUID instruction. */
35 cpuid(u32 leaf, u32 subleaf, u32 *a, u32 *b, u32 *c, u32 *d)
37 __asm__(".ifnc %%ebx, %1; mov %%ebx, %1; .endif\n"
39 ".ifnc %%ebx, %1; xchg %%ebx, %1; .endif\n"
40 : "=a" (*a), EBX_CONSTRAINT (*b), "=c" (*c), "=d" (*d)
41 : "a" (leaf), "c" (subleaf));
44 /* Read an extended control register. */
50 /* Execute the "xgetbv" instruction. Old versions of binutils do not
51 * recognize this instruction, so list the raw bytes instead. */
52 __asm__ (".byte 0x0f, 0x01, 0xd0" : "=d" (edx), "=a" (eax) : "c" (index));
54 return ((u64)edx << 32) | eax;
57 #define IS_SET(reg, bit) ((reg) & ((u32)1 << (bit)))
59 /* Initialize _x86_cpu_features with bits for interesting processor features. */
61 x86_setup_cpu_features(void)
64 u32 dummy1, dummy2, dummy3, dummy4;
66 u32 features_1, features_2, features_3, features_4;
67 bool os_saves_ymm_regs = false;
69 /* Get maximum supported function */
70 cpuid(0, 0, &max_function, &dummy2, &dummy3, &dummy4);
74 /* Standard feature flags */
75 cpuid(1, 0, &dummy1, &dummy2, &features_2, &features_1);
77 if (IS_SET(features_1, 25))
78 features |= X86_CPU_FEATURE_SSE;
80 if (IS_SET(features_1, 26))
81 features |= X86_CPU_FEATURE_SSE2;
83 if (IS_SET(features_2, 0))
84 features |= X86_CPU_FEATURE_SSE3;
86 if (IS_SET(features_2, 9))
87 features |= X86_CPU_FEATURE_SSSE3;
89 if (IS_SET(features_2, 19))
90 features |= X86_CPU_FEATURE_SSE4_1;
92 if (IS_SET(features_2, 20))
93 features |= X86_CPU_FEATURE_SSE4_2;
95 if (IS_SET(features_2, 27)) /* OSXSAVE set? */
96 if ((read_xcr(0) & 0x6) == 0x6)
97 os_saves_ymm_regs = true;
99 if (os_saves_ymm_regs && IS_SET(features_2, 28))
100 features |= X86_CPU_FEATURE_AVX;
102 if (max_function < 7)
105 /* Extended feature flags */
106 cpuid(7, 0, &dummy1, &features_3, &features_4, &dummy4);
108 if (IS_SET(features_3, 3))
109 features |= X86_CPU_FEATURE_BMI;
111 if (os_saves_ymm_regs && IS_SET(features_3, 5))
112 features |= X86_CPU_FEATURE_AVX2;
114 if (IS_SET(features_3, 8))
115 features |= X86_CPU_FEATURE_BMI2;
120 printf("Detected x86 CPU features: ");
121 if (features & X86_CPU_FEATURE_SSE)
123 if (features & X86_CPU_FEATURE_SSE2)
125 if (features & X86_CPU_FEATURE_SSE3)
127 if (features & X86_CPU_FEATURE_SSSE3)
129 if (features & X86_CPU_FEATURE_SSE4_1)
131 if (features & X86_CPU_FEATURE_SSE4_2)
133 if (features & X86_CPU_FEATURE_BMI)
135 if (features & X86_CPU_FEATURE_AVX)
137 if (features & X86_CPU_FEATURE_BMI2)
139 if (features & X86_CPU_FEATURE_AVX2)
144 _x86_cpu_features = features | X86_CPU_FEATURES_KNOWN;
147 #endif /* __i386__ || __x86_64__ */