Skip to content

Commit 73938f2

Browse files
committed
[x86] expose cpuid, xgetbv, pushfd, popfd
Expose the `cpuid` and `xgetby` `x86`/`x86_64` intrinsics. The `cpuid` intrinsic is not available on all `x86` CPUs. The `has_cpuid() -> bool` intrinsic detect this on non `x86_64` hosts. For convenience, this is exposed on `x86_64` as well but there it always returns `true`. The `pushfd` and `popfd` intrinsics, which read/write the `EFLAGS` register and are required to implement `has_cpuid`, are exposed as well. GCC exposes them too. When doing run-time feature detection for `x86`/`x86_64` we now properly check whether the `cpuid` instruction is available before using it. If it is not available, are features are exposes as "not available". One TODO: - The `xgetbv` intrinsic requires the `xsave` target feature but this is not currently exposed by rustc, see #167 .
1 parent 4c244fb commit 73938f2

File tree

4 files changed

+190
-35
lines changed

4 files changed

+190
-35
lines changed

src/x86/misc.rs

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
//! Miscelaneous x86 intrinsics available on all hosts.
2+
//!
3+
//! TODO: figure out a more discoverable name.
4+
5+
#[cfg(test)]
6+
use stdsimd_test::assert_instr;
7+
8+
/// Result of the `cpuid` instruction.
9+
pub struct CpuidResult {
10+
/// EAX register.
11+
pub eax: u32,
12+
/// EBX register.
13+
pub ebx: u32,
14+
/// ECX register.
15+
pub ecx: u32,
16+
/// EDX register.
17+
pub edx: u32,
18+
}
19+
20+
/// `cpuid` instruction.
21+
///
22+
/// The [CPUID Wikipedia page][wiki_cpuid] contains how to query which
23+
/// information using the `eax` and `ecx` registers, and the format in
24+
/// which this information is returned in `eax...edx`.
25+
///
26+
/// The `has_cpuid()` intrinsics can be used to query whether the `cpuid`
27+
/// instruction is available.
28+
///
29+
/// The definitive references are:
30+
/// - [Intel 64 and IA-32 Architectures Software Developer's Manual Volume 2:
31+
/// Instruction Set Reference, A-Z][intel64_ref].
32+
/// - [AMD64 Architecture Programmer's Manual, Volume 3: General-Purpose and
33+
/// System Instructions][amd64_ref].
34+
///
35+
/// [wiki_cpuid]: https://en.wikipedia.org/wiki/CPUID
36+
/// [intel64_ref]: http://www.intel.de/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf
37+
/// [amd64_ref]: http://support.amd.com/TechDocs/24594.pdf
38+
#[inline(always)]
39+
#[cfg_attr(test, assert_instr(cpuid))]
40+
pub unsafe fn cpuid(eax: u32, ecx: u32) -> CpuidResult {
41+
let mut r = ::std::mem::uninitialized::<CpuidResult>();
42+
asm!("cpuid"
43+
: "={eax}"(r.eax), "={ebx}"(r.ebx), "={ecx}"(r.ecx), "={edx}"(r.edx)
44+
: "{eax}"(eax), "{ecx}"(ecx)
45+
: :);
46+
r
47+
}
48+
49+
/// Reads EFLAGS.
50+
#[cfg(target_arch = "x86")]
51+
#[inline(always)]
52+
pub unsafe fn pushfd() -> u32 {
53+
let eflags: u32;
54+
asm!("pushfd; popl $0" : "=r"(eflags) : : : "volatile");
55+
eflags
56+
}
57+
58+
/// Write EFLAGS.
59+
#[cfg(target_arch = "x86")]
60+
#[inline(always)]
61+
pub unsafe fn popfd(eflags: u32) {
62+
asm!("pushl $0; popfd" : : "r"(eflags) : "cc", "flags" : "volatile");
63+
}
64+
65+
/// Does the host support the `cpuid` instruction?
66+
#[inline(always)]
67+
pub fn has_cpuid() -> bool {
68+
#[cfg(target_arch = "x86_64")]
69+
{
70+
true
71+
}
72+
#[cfg(target_arch = "x86")]
73+
{
74+
// On `x86` the `cpuid` instruction is not always available.
75+
// This follows the approach indicated in:
76+
// http://wiki.osdev.org/CPUID#Checking_CPUID_availability
77+
unsafe {
78+
// Read EFLAGS:
79+
let eflags: u32 = pushfd();
80+
81+
// Invert the ID bit in EFLAGS:
82+
let eflags_mod: u32 = eflags | 0x0020_0000;
83+
84+
// Store the modified EFLAGS (ID bit may or may not be inverted)
85+
popfd(eflags_mod);
86+
87+
// Read EFLAGS again:
88+
let eflags_after: u32 = pushfd();
89+
90+
// Check if the ID bit changed:
91+
eflags_after != eflags
92+
}
93+
}
94+
}
95+
96+
/// Reads the contents of the extended control register `XCR`
97+
/// specified in `xcr_no`.
98+
#[inline(always)]
99+
// FIXME: see
100+
// https://github.com/rust-lang-nursery/stdsimd/issues/167
101+
// #[target_feature = "+xsave"]
102+
#[cfg_attr(test, assert_instr(xgetbv))]
103+
pub unsafe fn xgetbv(xcr_no: u32) -> u64 {
104+
let eax: u32;
105+
let edx: u32;
106+
107+
asm!("xgetbv"
108+
: "={eax}"(eax), "={edx}"(edx)
109+
: "{ecx}"(xcr_no)
110+
: :);
111+
112+
((edx as u64) << 32) | (eax as u64)
113+
}
114+
115+
#[cfg(test)]
116+
mod tests {
117+
use super::*;
118+
119+
#[test]
120+
fn test_always_has_cpuid() {
121+
// all currently-tested targets have the instruction
122+
// FIXME: add targets without `cpuid` to CI
123+
assert!(has_cpuid());
124+
}
125+
126+
#[cfg(target_arch = "x86")]
127+
#[test]
128+
fn test_has_cpuid() {
129+
unsafe {
130+
let before = pushfd();
131+
132+
if has_cpuid() {
133+
assert!(before != pushfd());
134+
} else {
135+
assert!(before == pushfd());
136+
}
137+
}
138+
}
139+
140+
#[cfg(target_arch = "x86")]
141+
#[test]
142+
fn test_eflags() {
143+
unsafe {
144+
// reads eflags, writes them back, reads them again,
145+
// and compare for equality:
146+
let v = pushfd();
147+
popfd(v);
148+
let u = pushfd();
149+
assert_eq!(v, u);
150+
}
151+
}
152+
}

src/x86/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! `x86` and `x86_64` intrinsics.
22
3+
pub use self::misc::*;
4+
35
pub use self::sse::*;
46
pub use self::sse2::*;
57
pub use self::sse3::*;
@@ -28,6 +30,8 @@ mod macros;
2830
#[macro_use]
2931
mod runtime;
3032

33+
mod misc;
34+
3135
mod sse;
3236
mod sse2;
3337
mod sse3;

src/x86/runtime.rs

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -159,31 +159,37 @@ fn test_bit(x: usize, bit: u32) -> bool {
159159
/// [intel64_ref]: http://www.intel.de/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf
160160
/// [amd64_ref]: http://support.amd.com/TechDocs/24594.pdf
161161
fn detect_features() -> usize {
162-
let extended_features_ebx;
163-
let proc_info_ecx;
164-
let proc_info_edx;
162+
use super::misc::{cpuid, has_cpuid, CpuidResult};
163+
let mut value: usize = 0;
165164

166-
unsafe {
167-
/// To obtain all feature flags we need two CPUID queries:
165+
// If the x86 CPU does not support the CPUID instruction then it is too
166+
// old to support any of the currently-detectable features.
167+
if !has_cpuid() {
168+
return value;
169+
}
168170

169-
/// 1. EAX=1, ECX=0: Queries "Processor Info and Feature Bits"
170-
/// This gives us most of the CPU features in ECX and EDX (see
171-
/// below).
172-
asm!("cpuid"
173-
: "={ecx}"(proc_info_ecx), "={edx}"(proc_info_edx)
174-
: "{eax}"(0x0000_0001_u32), "{ecx}"(0 as u32)
175-
: :);
171+
// Calling `cpuid` from here on is safe because the CPU has the `cpuid`
172+
// instruction.
176173

177-
/// 2. EAX=7, ECX=0: Queries "Extended Features"
178-
/// This gives us information about bmi,bmi2, and avx2 support
179-
/// (see below); the result in ECX is not currently needed.
180-
asm!("cpuid"
181-
: "={ebx}"(extended_features_ebx)
182-
: "{eax}"(0x0000_0007_u32), "{ecx}"(0 as u32)
183-
: :);
184-
}
174+
// 1. EAX=1, ECX=0: Queries "Processor Info and Feature Bits";
175+
// Contains information about most x86 features.
176+
let CpuidResult {
177+
ecx: proc_info_ecx,
178+
edx: proc_info_edx,
179+
..
180+
} = unsafe { cpuid(0x0000_0001_u32, 0) };
185181

186-
let mut value: usize = 0;
182+
// 2. EAX=7, ECX=0: Queries "Extended Features";
183+
// Contains information about bmi,bmi2, and avx2 support.
184+
let CpuidResult {
185+
ebx: extended_features_ebx,
186+
..
187+
} = unsafe { cpuid(0x0000_0007_u32, 0) };
188+
189+
let proc_info_ecx = proc_info_ecx as usize;
190+
let proc_info_edx = proc_info_edx as usize;
191+
192+
let extended_features_ebx = extended_features_ebx as usize;
187193

188194
if test_bit(extended_features_ebx, 3) {
189195
value = set_bit(value, __Feature::bmi as u32);
@@ -233,18 +239,7 @@ fn detect_features() -> usize {
233239
// org/mozilla-central/file/64bab5cbb9b6/mozglue/build/SSE.cpp#l190
234240
//
235241
if test_bit(proc_info_ecx, 26) && test_bit(proc_info_ecx, 27) {
236-
/// XGETBV: reads the contents of the extended control
237-
/// register (XCR).
238-
unsafe fn xgetbv(xcr_no: u32) -> u64 {
239-
let eax: u32;
240-
let edx: u32;
241-
// xgetbv
242-
asm!("xgetbv"
243-
: "={eax}"(eax), "={edx}"(edx)
244-
: "{ecx}"(xcr_no)
245-
: :);
246-
((edx as u64) << 32) | (eax as u64)
247-
}
242+
use super::misc::xgetbv;
248243

249244
// This is safe because on x86 `xgetbv` is always available.
250245
if unsafe { xgetbv(0) } & 6 == 6 {

src/x86/sse2.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1792,7 +1792,9 @@ pub unsafe fn _mm_cvtsd_si64(a: f64x2) -> i64 {
17921792
#[inline(always)]
17931793
#[target_feature = "+sse2"]
17941794
#[cfg_attr(test, assert_instr(cvtsd2si))]
1795-
pub unsafe fn _mm_cvtsd_si64x(a: f64x2) -> i64 { _mm_cvtsd_si64(a) }
1795+
pub unsafe fn _mm_cvtsd_si64x(a: f64x2) -> i64 {
1796+
_mm_cvtsd_si64(a)
1797+
}
17961798

17971799
/// Convert the lower double-precision (64-bit) floating-point element in `b`
17981800
/// to a single-precision (32-bit) floating-point element, store the result in
@@ -1857,7 +1859,9 @@ pub unsafe fn _mm_cvttsd_si64(a: f64x2) -> i64 {
18571859
#[inline(always)]
18581860
#[target_feature = "+sse2"]
18591861
#[cfg_attr(test, assert_instr(cvttsd2si))]
1860-
pub unsafe fn _mm_cvttsd_si64x(a: f64x2) -> i64 { _mm_cvttsd_si64(a) }
1862+
pub unsafe fn _mm_cvttsd_si64x(a: f64x2) -> i64 {
1863+
_mm_cvttsd_si64(a)
1864+
}
18611865

18621866
/// Convert packed single-precision (32-bit) floating-point elements in `a` to
18631867
/// packed 32-bit integers with truncation.

0 commit comments

Comments
 (0)