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 __asm__ ("xgetbv" : "=d" (edx), "=a" (eax) : "c" (index));
52 return ((u64)edx << 32) | eax;
55 #define IS_SET(reg, bit) ((reg) & ((u32)1 << (bit)))
57 /* Initialize _x86_cpu_features with bits for interesting processor features. */
59 x86_setup_cpu_features(void)
62 u32 dummy1, dummy2, dummy3, dummy4;
64 u32 features_1, features_2, features_3, features_4;
65 bool os_saves_ymm_regs = false;
67 /* Get maximum supported function */
68 cpuid(0, 0, &max_function, &dummy2, &dummy3, &dummy4);
72 /* Standard feature flags */
73 cpuid(1, 0, &dummy1, &dummy2, &features_2, &features_1);
75 if (IS_SET(features_1, 25))
76 features |= X86_CPU_FEATURE_SSE;
78 if (IS_SET(features_1, 26))
79 features |= X86_CPU_FEATURE_SSE2;
81 if (IS_SET(features_2, 0))
82 features |= X86_CPU_FEATURE_SSE3;
84 if (IS_SET(features_2, 9))
85 features |= X86_CPU_FEATURE_SSSE3;
87 if (IS_SET(features_2, 19))
88 features |= X86_CPU_FEATURE_SSE4_1;
90 if (IS_SET(features_2, 20))
91 features |= X86_CPU_FEATURE_SSE4_2;
93 if (IS_SET(features_2, 27)) /* OSXSAVE set? */
94 if ((read_xcr(0) & 0x6) == 0x6)
95 os_saves_ymm_regs = true;
97 if (os_saves_ymm_regs && IS_SET(features_2, 28))
98 features |= X86_CPU_FEATURE_AVX;
100 if (max_function < 7)
103 /* Extended feature flags */
104 cpuid(7, 0, &dummy1, &features_3, &features_4, &dummy4);
106 if (IS_SET(features_3, 3))
107 features |= X86_CPU_FEATURE_BMI;
109 if (os_saves_ymm_regs && IS_SET(features_3, 5))
110 features |= X86_CPU_FEATURE_AVX2;
112 if (IS_SET(features_3, 8))
113 features |= X86_CPU_FEATURE_BMI2;
118 printf("Detected x86 CPU features: ");
119 if (features & X86_CPU_FEATURE_SSE)
121 if (features & X86_CPU_FEATURE_SSE2)
123 if (features & X86_CPU_FEATURE_SSE3)
125 if (features & X86_CPU_FEATURE_SSSE3)
127 if (features & X86_CPU_FEATURE_SSE4_1)
129 if (features & X86_CPU_FEATURE_SSE4_2)
131 if (features & X86_CPU_FEATURE_BMI)
133 if (features & X86_CPU_FEATURE_AVX)
135 if (features & X86_CPU_FEATURE_BMI2)
137 if (features & X86_CPU_FEATURE_AVX2)
142 _x86_cpu_features = features | X86_CPU_FEATURES_KNOWN;
145 #endif /* __i386__ || __x86_64__ */