2 * mono-hwcap-x86.c: x86 hardware feature detection
5 * Alex Rønne Petersen (alexrp@xamarin.com)
6 * Elijah Taylor (elijahtaylor@google.com)
7 * Miguel de Icaza (miguel@xamarin.com)
8 * Neale Ferguson (Neale.Ferguson@SoftwareAG-usa.com)
9 * Paolo Molaro (lupus@xamarin.com)
10 * Rodrigo Kumpera (kumpera@gmail.com)
11 * Sebastien Pouliot (sebastien@xamarin.com)
12 * Zoltan Varga (vargaz@xamarin.com)
14 * Copyright 2003 Ximian, Inc.
15 * Copyright 2003-2011 Novell, Inc
16 * Copyright 2006 Broadcom
17 * Copyright 2007-2008 Andreas Faerber
18 * Copyright 2011-2013 Xamarin Inc
21 #include "mono/utils/mono-hwcap-x86.h"
23 #if defined(HAVE_UNISTD_H)
31 gboolean mono_hwcap_x86_is_xen = FALSE;
32 gboolean mono_hwcap_x86_has_cmov = FALSE;
33 gboolean mono_hwcap_x86_has_fcmov = FALSE;
34 gboolean mono_hwcap_x86_has_sse1 = FALSE;
35 gboolean mono_hwcap_x86_has_sse2 = FALSE;
36 gboolean mono_hwcap_x86_has_sse3 = FALSE;
37 gboolean mono_hwcap_x86_has_ssse3 = FALSE;
38 gboolean mono_hwcap_x86_has_sse41 = FALSE;
39 gboolean mono_hwcap_x86_has_sse42 = FALSE;
40 gboolean mono_hwcap_x86_has_sse4a = FALSE;
43 cpuid (int id, int *p_eax, int *p_ebx, int *p_ecx, int *p_edx)
45 /* First, make sure we can use cpuid if we're on 32-bit. */
46 #if defined(TARGET_X86)
47 gboolean have_cpuid = FALSE;
64 __asm__ __volatile__ (
67 "movl\t%%eax, %%edx\n\t"
68 "xorl\t$0x200000, %%eax\n\t"
73 "xorl\t%%edx, %%eax\n\t"
74 "andl\t$0x200000, %%eax\n\t"
86 /* Now issue the actual cpuid instruction. We can use
87 MSVC's __cpuid on both 32-bit and 64-bit. */
95 #elif defined(TARGET_X86)
96 /* This complicated stuff is necessary because EBX
97 may be used by the compiler in PIC mode. */
98 __asm__ __volatile__ (
99 "xchgl\t%%ebx, %k1\n\t"
101 "xchgl\t%%ebx, %k1\n\t"
102 : "=a" (*p_eax), "=&r" (*p_ebx), "=c" (*p_ecx), "=d" (*p_edx)
106 __asm__ __volatile__ (
108 : "=a" (*p_eax), "=b" (*p_ebx), "=c" (*p_ecx), "=d" (*p_edx)
117 mono_hwcap_arch_init (void)
119 int eax, ebx, ecx, edx;
121 if (cpuid (1, &eax, &ebx, &ecx, &edx)) {
122 if (edx & (1 << 15)) {
123 mono_hwcap_x86_has_cmov = TRUE;
126 mono_hwcap_x86_has_fcmov = TRUE;
130 mono_hwcap_x86_has_sse1 = TRUE;
133 mono_hwcap_x86_has_sse2 = TRUE;
136 mono_hwcap_x86_has_sse3 = TRUE;
139 mono_hwcap_x86_has_ssse3 = TRUE;
142 mono_hwcap_x86_has_sse41 = TRUE;
145 mono_hwcap_x86_has_sse42 = TRUE;
148 if (cpuid (0x80000000, &eax, &ebx, &ecx, &edx)) {
149 if ((unsigned int) eax >= 0x80000001 && ebx == 0x68747541 && ecx == 0x444D4163 && edx == 0x69746E65) {
150 if (cpuid (0x80000001, &eax, &ebx, &ecx, &edx)) {
152 mono_hwcap_x86_has_sse4a = TRUE;
157 #if defined(HAVE_UNISTD_H)
158 mono_hwcap_x86_is_xen = !access ("/proc/xen", F_OK);
163 mono_hwcap_print (FILE *f)
165 g_fprintf (f, "mono_hwcap_x86_is_xen = %i\n", mono_hwcap_x86_is_xen);
166 g_fprintf (f, "mono_hwcap_x86_has_cmov = %i\n", mono_hwcap_x86_has_cmov);
167 g_fprintf (f, "mono_hwcap_x86_has_fcmov = %i\n", mono_hwcap_x86_has_fcmov);
168 g_fprintf (f, "mono_hwcap_x86_has_sse1 = %i\n", mono_hwcap_x86_has_sse1);
169 g_fprintf (f, "mono_hwcap_x86_has_sse2 = %i\n", mono_hwcap_x86_has_sse2);
170 g_fprintf (f, "mono_hwcap_x86_has_sse3 = %i\n", mono_hwcap_x86_has_sse3);
171 g_fprintf (f, "mono_hwcap_x86_has_ssse3 = %i\n", mono_hwcap_x86_has_ssse3);
172 g_fprintf (f, "mono_hwcap_x86_has_sse41 = %i\n", mono_hwcap_x86_has_sse41);
173 g_fprintf (f, "mono_hwcap_x86_has_sse42 = %i\n", mono_hwcap_x86_has_sse42);
174 g_fprintf (f, "mono_hwcap_x86_has_sse4a = %i\n", mono_hwcap_x86_has_sse4a);