Skip to content

Commit 09b358c

Browse files
committed
[x86] expose __cpuid, __cpuid_count, xgetbv, __readeflags, __writeeflags
Expose the `__cpuid` and `_xgetby` `x86`/`x86_64` intrinsics. The `__cpuid` and `__cpuid_count` intrinsics are 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`. These are exposed by Clang and GCC. The `__readeflags` and `__writeeflags` intrinsics, which read/write the `EFLAGS` register and are required to implement `has_cpuid`, are exposed as well. GCC and Clang 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 09b358c

File tree

6 files changed

+231
-36
lines changed

6 files changed

+231
-36
lines changed

src/x86/cpuid.rs

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
//! `cpuid` intrinsics
2+
3+
#[cfg(test)]
4+
use stdsimd_test::assert_instr;
5+
6+
/// Result of the `cpuid` instruction.
7+
pub struct CpuidResult {
8+
/// EAX register.
9+
pub eax: u32,
10+
/// EBX register.
11+
pub ebx: u32,
12+
/// ECX register.
13+
pub ecx: u32,
14+
/// EDX register.
15+
pub edx: u32,
16+
}
17+
18+
/// `cpuid` instruction.
19+
///
20+
/// The [CPUID Wikipedia page][wiki_cpuid] contains how to query which
21+
/// information using the `eax` and `ecx` registers, and the format in
22+
/// which this information is returned in `eax...edx`.
23+
///
24+
/// The `has_cpuid()` intrinsics can be used to query whether the `cpuid`
25+
/// instruction is available.
26+
///
27+
/// The definitive references are:
28+
/// - [Intel 64 and IA-32 Architectures Software Developer's Manual Volume 2:
29+
/// Instruction Set Reference, A-Z][intel64_ref].
30+
/// - [AMD64 Architecture Programmer's Manual, Volume 3: General-Purpose and
31+
/// System Instructions][amd64_ref].
32+
///
33+
/// [wiki_cpuid]: https://en.wikipedia.org/wiki/CPUID
34+
/// [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
35+
/// [amd64_ref]: http://support.amd.com/TechDocs/24594.pdf
36+
#[inline(always)]
37+
#[cfg_attr(test, assert_instr(cpuid))]
38+
pub unsafe fn __cpuid_count(eax: u32, ecx: u32) -> CpuidResult {
39+
let mut r = ::std::mem::uninitialized::<CpuidResult>();
40+
asm!("cpuid"
41+
: "={eax}"(r.eax), "={ebx}"(r.ebx), "={ecx}"(r.ecx), "={edx}"(r.edx)
42+
: "{eax}"(eax), "{ecx}"(ecx)
43+
: :);
44+
r
45+
}
46+
47+
/// `cpuid` instruction.
48+
///
49+
/// See `__cpuid_count`.
50+
#[inline(always)]
51+
#[cfg_attr(test, assert_instr(cpuid))]
52+
pub unsafe fn __cpuid(eax: u32) -> CpuidResult {
53+
__cpuid_count(eax, 0)
54+
}
55+
56+
/// Does the host support the `cpuid` instruction?
57+
#[inline(always)]
58+
pub fn has_cpuid() -> bool {
59+
#[cfg(target_arch = "x86_64")]
60+
{
61+
true
62+
}
63+
#[cfg(target_arch = "x86")]
64+
{
65+
use super::ia32::{__readeflags, __writeeflags};
66+
67+
// On `x86` the `cpuid` instruction is not always available.
68+
// This follows the approach indicated in:
69+
// http://wiki.osdev.org/CPUID#Checking_CPUID_availability
70+
unsafe {
71+
// Read EFLAGS:
72+
let eflags: u32 = __readeflags();
73+
74+
// Invert the ID bit in EFLAGS:
75+
let eflags_mod: u32 = eflags | 0x0020_0000;
76+
77+
// Store the modified EFLAGS (ID bit may or may not be inverted)
78+
__writeeflags(eflags_mod);
79+
80+
// Read EFLAGS again:
81+
let eflags_after: u32 = __readeflags();
82+
83+
// Check if the ID bit changed:
84+
eflags_after != eflags
85+
}
86+
}
87+
}
88+
89+
#[cfg(test)]
90+
mod tests {
91+
use super::*;
92+
93+
#[test]
94+
fn test_always_has_cpuid() {
95+
// all currently-tested targets have the instruction
96+
// FIXME: add targets without `cpuid` to CI
97+
assert!(has_cpuid());
98+
}
99+
100+
#[cfg(target_arch = "x86")]
101+
#[test]
102+
fn test_has_cpuid() {
103+
use ::vendor::__readeflags;
104+
unsafe {
105+
let before = __readeflags();
106+
107+
if has_cpuid() {
108+
assert!(before != __readeflags());
109+
} else {
110+
assert!(before == __readeflags());
111+
}
112+
}
113+
}
114+
115+
}

src/x86/ia32.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//! `i386/ia32` intrinsics
2+
3+
#[cfg(test)]
4+
use stdsimd_test::assert_instr;
5+
6+
/// Reads EFLAGS.
7+
#[cfg(target_arch = "x86")]
8+
#[inline(always)]
9+
pub unsafe fn __readeflags() -> u32 {
10+
let eflags: u32;
11+
asm!("pushfd; popl $0" : "=r"(eflags) : : : "volatile");
12+
eflags
13+
}
14+
15+
/// Reads EFLAGS.
16+
#[cfg(target_arch = "x86_64")]
17+
#[inline(always)]
18+
pub unsafe fn __readeflags() -> u64 {
19+
let eflags: u64;
20+
asm!("pushfq; popq $0" : "=r"(eflags) : : : "volatile");
21+
eflags
22+
}
23+
24+
/// Write EFLAGS.
25+
#[cfg(target_arch = "x86")]
26+
#[inline(always)]
27+
pub unsafe fn __writeeflags(eflags: u32) {
28+
asm!("pushl $0; popfd" : : "r"(eflags) : "cc", "flags" : "volatile");
29+
}
30+
31+
/// Write EFLAGS.
32+
#[cfg(target_arch = "x86_64")]
33+
#[inline(always)]
34+
pub unsafe fn __writeeflags(eflags: u64) {
35+
asm!("pushq $0; popfq" : : "r"(eflags) : "cc", "flags" : "volatile");
36+
}
37+
38+
#[cfg(test)]
39+
mod tests {
40+
use super::*;
41+
42+
#[test]
43+
fn test_eflags() {
44+
unsafe {
45+
// reads eflags, writes them back, reads them again,
46+
// and compare for equality:
47+
let v = pushfd();
48+
popfd(v);
49+
let u = pushfd();
50+
assert_eq!(v, u);
51+
}
52+
}
53+
}

src/x86/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
//! `x86` and `x86_64` intrinsics.
22
3+
pub use self::ia32::*;
4+
pub use self::cpuid::*;
5+
36
pub use self::sse::*;
47
pub use self::sse2::*;
58
pub use self::sse3::*;
@@ -28,6 +31,9 @@ mod macros;
2831
#[macro_use]
2932
mod runtime;
3033

34+
mod ia32;
35+
mod cpuid;
36+
3137
mod sse;
3238
mod sse2;
3339
mod sse3;
@@ -37,6 +43,8 @@ mod sse42;
3743
mod avx;
3844
mod avx2;
3945

46+
mod xsave;
47+
4048
mod abm;
4149
mod bmi;
4250
mod bmi2;

src/x86/runtime.rs

Lines changed: 29 additions & 34 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::cpuid::{__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) };
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) };
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,21 +239,10 @@ 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::xsave::_xgetbv;
248243

249244
// This is safe because on x86 `xgetbv` is always available.
250-
if unsafe { xgetbv(0) } & 6 == 6 {
245+
if unsafe { _xgetbv(0) } & 6 == 6 {
251246
if test_bit(proc_info_ecx, 28) {
252247
value = set_bit(value, __Feature::avx as u32);
253248
}

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.

src/x86/xsave.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//! `xsave` target feature intrinsics
2+
3+
4+
/// Reads the contents of the extended control register `XCR`
5+
/// specified in `xcr_no`.
6+
#[inline(always)]
7+
// #[target_feature = "+xsave"] // FIXME: see
8+
// https://github.com/rust-lang-nursery/stdsimd/issues/167
9+
#[cfg_attr(test, assert_instr(xgetbv))]
10+
pub unsafe fn _xgetbv(xcr_no: u32) -> u64 {
11+
let eax: u32;
12+
let edx: u32;
13+
14+
asm!("xgetbv"
15+
: "={eax}"(eax), "={edx}"(edx)
16+
: "{ecx}"(xcr_no)
17+
: :);
18+
19+
((edx as u64) << 32) | (eax as u64)
20+
}

0 commit comments

Comments
 (0)