Stabilize the x86 has_cpuid intrinsic #730
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR stabilizes the x86
has_cpuid
intrinsic.Motivation
returns
true
if the CPU supports the CPUID instruction, andfalse
otherwise.This intrinsic is useful for implementing run-time feature detection that is able to properly handle
i386
CPUs and older (we have some Rust targets for these, e.g.,i386-apple-ios
). Some users, like @briansmith here, want to tune how run-time feature detection works, which is sometimes a reasonable thing to do both forstd
and#![no_std]
targets. We provide thestd::detect
module also as astd_detect
crate on crates.io which has some extra cargo features that users can customize to tailor run-time feature detection to their application without having to create a new Rust target for it.User and reference level information
The intrinsic API is very simple, the doc comment just states "Does the host support the CPUID instruction?", and the type of the intrinsic is a
fn() -> bool
that answers this question.Implementation details
The inline comments explain the implementation; it just follows the Intel and OSDEV: detecting CPUID availability documentation.
This intrinsic returns
true
as a compile-time constant when it can infer the answer fromcfg(target_arch)
orcfg(target_feature)
, and only performs a run-time computation when this is not the case.Prior art
Both clang and gcc implement this logic, although they do not expose it as its own separate intrinsic.
Alternatives
We could not expose this intrinsic, but that would require people writing their own run-time detection frameworks, e.g., for
#![no_std]
, to use inline assembly instead to query this information.API-wise, I can't think of a different type-signature for this intrinsic.
cc @rust-lang-nursery/libs @rust-lang/libs