Skip to content

Commit dd69d94

Browse files
committed
[x86] expose cpuid and xgetbv intrinsics
1 parent 4c244fb commit dd69d94

File tree

4 files changed

+145
-35
lines changed

4 files changed

+145
-35
lines changed

src/x86/misc.rs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
/// CUPID instruction.
21+
///
22+
/// The `request` parameter is passed in the `eax` register and the `ext`
23+
/// parameter in the `ecx` register.
24+
///
25+
/// The [CPUID Wikipedia page][wiki_cpuid] contains
26+
/// all the information about which flags to set to query which values, and in
27+
/// which registers these are reported.
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(request: u32, ext: 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}"(request), "{ecx}"(ext)
45+
: :);
46+
r
47+
}
48+
49+
/// Does the CPU has the CPUID instruction?
50+
#[cfg(target_arch = "x86_64")]
51+
pub fn has_cpuid() -> bool {
52+
// On x86_64 all CPUs have the instruction
53+
true
54+
}
55+
56+
/// Does the CPU has the CPUID instruction?
57+
#[cfg(target_arch = "x86")]
58+
pub fn has_cpuid() -> bool {
59+
unsafe {
60+
// Read EFLAGS:
61+
let eflags: u32;
62+
asm!("pushf");
63+
asm!("popl %eax" : "={eax}"(eflags) : : : );
64+
// Invert the ID bit in EFLAGS:
65+
let eflags_mod: u32 = eflags | 0x0020_0000;
66+
67+
// Store the modified EFLAGS (ID bit may or may not be inverted)
68+
asm!("pushl %eax" : : "eax"(eflags_mod) : : );
69+
asm!("popf");
70+
71+
// Read EFLAGS again:
72+
let eflags_after: u32;
73+
asm!("pushf");
74+
asm!("popl %eax" : "={eax}"(eflags_after) : : : );
75+
76+
// Check if the ID bit changed:
77+
eflags_after != eflags
78+
}
79+
}
80+
81+
/// Reads the contents of the extended control register `XCR`
82+
/// specified in `xcr_no`.
83+
#[inline(always)]
84+
// FIXME: see
85+
// https://github.com/rust-lang-nursery/stdsimd/issues/167
86+
// #[target_feature = "+xsave"]
87+
#[cfg_attr(test, assert_instr(xgetbv))]
88+
pub unsafe fn xgetbv(xcr_no: u32) -> u64 {
89+
let eax: u32;
90+
let edx: u32;
91+
92+
asm!("xgetbv"
93+
: "={eax}"(eax), "={edx}"(edx)
94+
: "{ecx}"(xcr_no)
95+
: :);
96+
97+
((edx as u64) << 32) | (eax as u64)
98+
}
99+
100+
#[cfg(test)]
101+
mod tests {
102+
use super::*;
103+
104+
fn test_has_cpuid() {
105+
assert!(has_cpuid());
106+
}
107+
}

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 all 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)