1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#![cfg_attr(feature = "cargo-clippy", allow(stutter))]
use mem;
#[cfg(test)]
use stdsimd_test::assert_instr;
#[derive(Copy, Clone, Eq, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "cargo-clippy", allow(stutter))]
#[stable(feature = "simd_x86", since = "1.27.0")]
pub struct CpuidResult {
#[stable(feature = "simd_x86", since = "1.27.0")]
pub eax: u32,
#[stable(feature = "simd_x86", since = "1.27.0")]
pub ebx: u32,
#[stable(feature = "simd_x86", since = "1.27.0")]
pub ecx: u32,
#[stable(feature = "simd_x86", since = "1.27.0")]
pub edx: u32,
}
#[inline]
#[cfg_attr(test, assert_instr(cpuid))]
#[stable(feature = "simd_x86", since = "1.27.0")]
pub unsafe fn __cpuid_count(leaf: u32, sub_leaf: u32) -> CpuidResult {
let mut r = mem::uninitialized::<CpuidResult>();
if cfg!(target_arch = "x86") {
asm!("cpuid"
: "={eax}"(r.eax), "={ebx}"(r.ebx), "={ecx}"(r.ecx), "={edx}"(r.edx)
: "{eax}"(leaf), "{ecx}"(sub_leaf)
: :);
} else {
asm!("cpuid\n"
: "={eax}"(r.eax), "={ebx}"(r.ebx), "={ecx}"(r.ecx), "={edx}"(r.edx)
: "{eax}"(leaf), "{ecx}"(sub_leaf)
: "rbx" :);
}
r
}
#[inline]
#[cfg_attr(test, assert_instr(cpuid))]
#[stable(feature = "simd_x86", since = "1.27.0")]
pub unsafe fn __cpuid(leaf: u32) -> CpuidResult {
__cpuid_count(leaf, 0)
}
#[inline]
pub fn has_cpuid() -> bool {
#[cfg(target_arch = "x86_64")]
{
true
}
#[cfg(target_arch = "x86")]
{
use coresimd::x86::{__readeflags, __writeeflags};
unsafe {
let eflags: u32 = __readeflags();
let eflags_mod: u32 = eflags | 0x0020_0000;
__writeeflags(eflags_mod);
let eflags_after: u32 = __readeflags();
eflags_after != eflags
}
}
}
#[inline]
#[stable(feature = "simd_x86", since = "1.27.0")]
pub unsafe fn __get_cpuid_max(leaf: u32) -> (u32, u32) {
let CpuidResult { eax, ebx, .. } = __cpuid(leaf);
(eax, ebx)
}
#[cfg(test)]
mod tests {
use coresimd::x86::*;
#[test]
fn test_always_has_cpuid() {
assert!(cpuid::has_cpuid());
}
#[cfg(target_arch = "x86")]
#[test]
fn test_has_cpuid() {
unsafe {
let before = __readeflags();
if cpuid::has_cpuid() {
assert!(before != __readeflags());
} else {
assert!(before == __readeflags());
}
}
}
}