Skip to content

Commit

Permalink
Rename is_target_feature_detected! (rust-lang#346)
Browse files Browse the repository at this point in the history
This commit renames the `is_target_feature_detected!` macro to have different
names depending on the platform. For example:

* `is_x86_feature_detected!`
* `is_arm_feature_detected!`
* `is_aarch64_feature_detected!`
* `is_powerpc64_feature_detected!`

Each macro already has a platform-specific albeit similar interface. Currently,
though, each macro takes a different set of strings so the hope is that like
with the name of the architecture in the module we can signal the dangers of
using the macro in a platform-agnostic context.

One liberty taken with the macro currently though is to on both the x86 and
x86_64 architectures name the macro `is_x86_feature_detected` rather than also
having an `is_x86_64_feature_detected`. This mirrors, however, how all the
intrinsics are named the same on x86/x86_64.
  • Loading branch information
alexcrichton authored Mar 7, 2018
1 parent 47f06df commit 390f5d6
Show file tree
Hide file tree
Showing 13 changed files with 205 additions and 193 deletions.
18 changes: 9 additions & 9 deletions coresimd/x86/avx2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2127,7 +2127,7 @@ pub unsafe fn _mm256_shuffle_epi8(a: __m256i, b: __m256i) -> __m256i {
/// use std::arch::x86_64::*;
///
/// # fn main() {
/// # if is_target_feature_detected!("avx2") {
/// # if is_x86_feature_detected!("avx2") {
/// # #[target_feature(enable = "avx2")]
/// # unsafe fn worker() {
/// let a = _mm256_setr_epi32(0, 1, 2, 3, 4, 5, 6, 7);
Expand Down Expand Up @@ -2736,7 +2736,7 @@ pub unsafe fn _mm256_subs_epu8(a: __m256i, b: __m256i) -> __m256i {
/// use std::arch::x86_64::*;
///
/// # fn main() {
/// # if is_target_feature_detected!("avx2") {
/// # if is_x86_feature_detected!("avx2") {
/// # #[target_feature(enable = "avx2")]
/// # unsafe fn worker() {
/// let a = _mm256_setr_epi8(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
Expand Down Expand Up @@ -2788,7 +2788,7 @@ pub unsafe fn _mm256_unpackhi_epi8(a: __m256i, b: __m256i) -> __m256i {
/// use std::arch::x86_64::*;
///
/// # fn main() {
/// # if is_target_feature_detected!("avx2") {
/// # if is_x86_feature_detected!("avx2") {
/// # #[target_feature(enable = "avx2")]
/// # unsafe fn worker() {
/// let a = _mm256_setr_epi8(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
Expand Down Expand Up @@ -2839,7 +2839,7 @@ pub unsafe fn _mm256_unpacklo_epi8(a: __m256i, b: __m256i) -> __m256i {
/// use std::arch::x86_64::*;
///
/// # fn main() {
/// # if is_target_feature_detected!("avx2") {
/// # if is_x86_feature_detected!("avx2") {
/// # #[target_feature(enable = "avx2")]
/// # unsafe fn worker() {
/// let a = _mm256_setr_epi16(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
Expand Down Expand Up @@ -2886,7 +2886,7 @@ pub unsafe fn _mm256_unpackhi_epi16(a: __m256i, b: __m256i) -> __m256i {
/// use std::arch::x86_64::*;
///
/// # fn main() {
/// # if is_target_feature_detected!("avx2") {
/// # if is_x86_feature_detected!("avx2") {
/// # #[target_feature(enable = "avx2")]
/// # unsafe fn worker() {
///
Expand Down Expand Up @@ -2934,7 +2934,7 @@ pub unsafe fn _mm256_unpacklo_epi16(a: __m256i, b: __m256i) -> __m256i {
/// use std::arch::x86_64::*;
///
/// # fn main() {
/// # if is_target_feature_detected!("avx2") {
/// # if is_x86_feature_detected!("avx2") {
/// # #[target_feature(enable = "avx2")]
/// # unsafe fn worker() {
/// let a = _mm256_setr_epi32(0, 1, 2, 3, 4, 5, 6, 7);
Expand Down Expand Up @@ -2980,7 +2980,7 @@ pub unsafe fn _mm256_unpackhi_epi32(a: __m256i, b: __m256i) -> __m256i {
/// use std::arch::x86_64::*;
///
/// # fn main() {
/// # if is_target_feature_detected!("avx2") {
/// # if is_x86_feature_detected!("avx2") {
/// # #[target_feature(enable = "avx2")]
/// # unsafe fn worker() {
/// let a = _mm256_setr_epi32(0, 1, 2, 3, 4, 5, 6, 7);
Expand Down Expand Up @@ -3023,7 +3023,7 @@ pub unsafe fn _mm256_unpacklo_epi32(a: __m256i, b: __m256i) -> __m256i {
/// use std::arch::x86_64::*;
///
/// # fn main() {
/// # if is_target_feature_detected!("avx2") {
/// # if is_x86_feature_detected!("avx2") {
/// # #[target_feature(enable = "avx2")]
/// # unsafe fn worker() {
/// let a = _mm256_setr_epi64x(0, 1, 2, 3);
Expand Down Expand Up @@ -3065,7 +3065,7 @@ pub unsafe fn _mm256_unpackhi_epi64(a: __m256i, b: __m256i) -> __m256i {
/// use std::arch::x86_64::*;
///
/// # fn main() {
/// # if is_target_feature_detected!("avx2") {
/// # if is_x86_feature_detected!("avx2") {
/// # #[target_feature(enable = "avx2")]
/// # unsafe fn worker() {
/// let a = _mm256_setr_epi64x(0, 1, 2, 3);
Expand Down
14 changes: 7 additions & 7 deletions coresimd/x86/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ types! {
/// let all_bytes_one = _mm_set1_pi8(1);
/// let two_i32 = _mm_set_pi32(1, 2);
/// # }
/// # if is_target_feature_detected!("mmx") { unsafe { foo() } }
/// # if is_x86_feature_detected!("mmx") { unsafe { foo() } }
/// # }
/// ```
pub struct __m64(i64);
Expand Down Expand Up @@ -123,7 +123,7 @@ types! {
/// let all_bytes_one = _mm_set1_epi8(1);
/// let four_i32 = _mm_set_epi32(1, 2, 3, 4);
/// # }
/// # if is_target_feature_detected!("sse2") { unsafe { foo() } }
/// # if is_x86_feature_detected!("sse2") { unsafe { foo() } }
/// # }
/// ```
pub struct __m128i(i64, i64);
Expand Down Expand Up @@ -166,7 +166,7 @@ types! {
/// let four_ones = _mm_set1_ps(1.0);
/// let four_floats = _mm_set_ps(1.0, 2.0, 3.0, 4.0);
/// # }
/// # if is_target_feature_detected!("sse") { unsafe { foo() } }
/// # if is_x86_feature_detected!("sse") { unsafe { foo() } }
/// # }
/// ```
pub struct __m128(f32, f32, f32, f32);
Expand Down Expand Up @@ -209,7 +209,7 @@ types! {
/// let two_ones = _mm_set1_pd(1.0);
/// let two_floats = _mm_set_pd(1.0, 2.0);
/// # }
/// # if is_target_feature_detected!("sse") { unsafe { foo() } }
/// # if is_x86_feature_detected!("sse") { unsafe { foo() } }
/// # }
/// ```
pub struct __m128d(f64, f64);
Expand Down Expand Up @@ -256,7 +256,7 @@ types! {
/// let all_bytes_one = _mm256_set1_epi8(1);
/// let eight_i32 = _mm256_set_epi32(1, 2, 3, 4, 5, 6, 7, 8);
/// # }
/// # if is_target_feature_detected!("avx") { unsafe { foo() } }
/// # if is_x86_feature_detected!("avx") { unsafe { foo() } }
/// # }
/// ```
pub struct __m256i(i64, i64, i64, i64);
Expand Down Expand Up @@ -299,7 +299,7 @@ types! {
/// let eight_ones = _mm256_set1_ps(1.0);
/// let eight_floats = _mm256_set_ps(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0);
/// # }
/// # if is_target_feature_detected!("sse") { unsafe { foo() } }
/// # if is_x86_feature_detected!("sse") { unsafe { foo() } }
/// # }
/// ```
pub struct __m256(f32, f32, f32, f32, f32, f32, f32, f32);
Expand Down Expand Up @@ -342,7 +342,7 @@ types! {
/// let four_ones = _mm256_set1_pd(1.0);
/// let four_floats = _mm256_set_pd(1.0, 2.0, 3.0, 4.0);
/// # }
/// # if is_target_feature_detected!("avx") { unsafe { foo() } }
/// # if is_x86_feature_detected!("avx") { unsafe { foo() } }
/// # }
/// ```
pub struct __m256d(f64, f64, f64, f64);
Expand Down
4 changes: 2 additions & 2 deletions coresimd/x86/sse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,7 @@ pub unsafe fn _mm_movemask_ps(a: __m128) -> i32 {
/// #
/// # // The real main function
/// # fn main() {
/// # if is_target_feature_detected!("sse") {
/// # if is_x86_feature_detected!("sse") {
/// # #[target_feature(enable = "sse")]
/// # unsafe fn worker() {
/// #
Expand Down Expand Up @@ -936,7 +936,7 @@ pub unsafe fn _mm_loadh_pi(a: __m128, p: *const __m64) -> __m128 {
///
/// # // The real main function
/// # fn main() {
/// # if is_target_feature_detected!("sse") {
/// # if is_x86_feature_detected!("sse") {
/// # #[target_feature(enable = "sse")]
/// # unsafe fn worker() {
/// #
Expand Down
10 changes: 5 additions & 5 deletions coresimd/x86/sse42.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ pub unsafe fn _mm_cmpistrm(a: __m128i, b: __m128i, imm8: i32) -> __m128i {
/// use std::arch::x86_64::*;
///
/// # fn main() {
/// # if is_target_feature_detected!("sse4.2") {
/// # if is_x86_feature_detected!("sse4.2") {
/// # #[target_feature(enable = "sse4.2")]
/// # unsafe fn worker() {
/// let haystack = b"This is a long string of text data\r\n\tthat extends
Expand Down Expand Up @@ -162,7 +162,7 @@ pub unsafe fn _mm_cmpistrm(a: __m128i, b: __m128i, imm8: i32) -> __m128i {
/// use std::arch::x86_64::*;
///
/// # fn main() {
/// # if is_target_feature_detected!("sse4.2") {
/// # if is_x86_feature_detected!("sse4.2") {
/// # #[target_feature(enable = "sse4.2")]
/// # unsafe fn worker() {
/// // Ensure your input is 16 byte aligned
Expand Down Expand Up @@ -208,7 +208,7 @@ pub unsafe fn _mm_cmpistrm(a: __m128i, b: __m128i, imm8: i32) -> __m128i {
/// use std::arch::x86_64::*;
///
/// # fn main() {
/// # if is_target_feature_detected!("sse4.2") {
/// # if is_x86_feature_detected!("sse4.2") {
/// # #[target_feature(enable = "sse4.2")]
/// # unsafe fn worker() {
/// # let b = b":;<=>?@[\\]^_`abc";
Expand Down Expand Up @@ -254,7 +254,7 @@ pub unsafe fn _mm_cmpistrm(a: __m128i, b: __m128i, imm8: i32) -> __m128i {
/// use std::arch::x86_64::*;
///
/// # fn main() {
/// # if is_target_feature_detected!("sse4.2") {
/// # if is_x86_feature_detected!("sse4.2") {
/// # #[target_feature(enable = "sse4.2")]
/// # unsafe fn worker() {
/// # let mut some_utf16_words = [0u16; 8];
Expand Down Expand Up @@ -452,7 +452,7 @@ pub unsafe fn _mm_cmpestrm(
/// use std::arch::x86_64::*;
///
/// # fn main() {
/// # if is_target_feature_detected!("sse4.2") {
/// # if is_x86_feature_detected!("sse4.2") {
/// # #[target_feature(enable = "sse4.2")]
/// # unsafe fn worker() {
///
Expand Down
62 changes: 31 additions & 31 deletions crates/coresimd/tests/cpu-detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,44 @@ extern crate stdsimd;
#[test]
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn x86_all() {
println!("sse: {:?}", is_target_feature_detected!("sse"));
println!("sse2: {:?}", is_target_feature_detected!("sse2"));
println!("sse3: {:?}", is_target_feature_detected!("sse3"));
println!("ssse3: {:?}", is_target_feature_detected!("ssse3"));
println!("sse4.1: {:?}", is_target_feature_detected!("sse4.1"));
println!("sse4.2: {:?}", is_target_feature_detected!("sse4.2"));
println!("sse4a: {:?}", is_target_feature_detected!("sse4a"));
println!("avx: {:?}", is_target_feature_detected!("avx"));
println!("avx2: {:?}", is_target_feature_detected!("avx2"));
println!("avx512f {:?}", is_target_feature_detected!("avx512f"));
println!("avx512cd {:?}", is_target_feature_detected!("avx512cd"));
println!("avx512er {:?}", is_target_feature_detected!("avx512er"));
println!("avx512pf {:?}", is_target_feature_detected!("avx512pf"));
println!("avx512bw {:?}", is_target_feature_detected!("avx512bw"));
println!("avx512dq {:?}", is_target_feature_detected!("avx512dq"));
println!("avx512vl {:?}", is_target_feature_detected!("avx512vl"));
println!("sse: {:?}", is_x86_feature_detected!("sse"));
println!("sse2: {:?}", is_x86_feature_detected!("sse2"));
println!("sse3: {:?}", is_x86_feature_detected!("sse3"));
println!("ssse3: {:?}", is_x86_feature_detected!("ssse3"));
println!("sse4.1: {:?}", is_x86_feature_detected!("sse4.1"));
println!("sse4.2: {:?}", is_x86_feature_detected!("sse4.2"));
println!("sse4a: {:?}", is_x86_feature_detected!("sse4a"));
println!("avx: {:?}", is_x86_feature_detected!("avx"));
println!("avx2: {:?}", is_x86_feature_detected!("avx2"));
println!("avx512f {:?}", is_x86_feature_detected!("avx512f"));
println!("avx512cd {:?}", is_x86_feature_detected!("avx512cd"));
println!("avx512er {:?}", is_x86_feature_detected!("avx512er"));
println!("avx512pf {:?}", is_x86_feature_detected!("avx512pf"));
println!("avx512bw {:?}", is_x86_feature_detected!("avx512bw"));
println!("avx512dq {:?}", is_x86_feature_detected!("avx512dq"));
println!("avx512vl {:?}", is_x86_feature_detected!("avx512vl"));
println!(
"avx512_ifma {:?}",
is_target_feature_detected!("avx512ifma")
is_x86_feature_detected!("avx512ifma")
);
println!(
"avx512_vbmi {:?}",
is_target_feature_detected!("avx512vbmi")
is_x86_feature_detected!("avx512vbmi")
);
println!(
"avx512_vpopcntdq {:?}",
is_target_feature_detected!("avx512vpopcntdq")
is_x86_feature_detected!("avx512vpopcntdq")
);
println!("fma: {:?}", is_target_feature_detected!("fma"));
println!("abm: {:?}", is_target_feature_detected!("abm"));
println!("bmi: {:?}", is_target_feature_detected!("bmi1"));
println!("bmi2: {:?}", is_target_feature_detected!("bmi2"));
println!("tbm: {:?}", is_target_feature_detected!("tbm"));
println!("popcnt: {:?}", is_target_feature_detected!("popcnt"));
println!("lzcnt: {:?}", is_target_feature_detected!("lzcnt"));
println!("fxsr: {:?}", is_target_feature_detected!("fxsr"));
println!("xsave: {:?}", is_target_feature_detected!("xsave"));
println!("xsaveopt: {:?}", is_target_feature_detected!("xsaveopt"));
println!("xsaves: {:?}", is_target_feature_detected!("xsaves"));
println!("xsavec: {:?}", is_target_feature_detected!("xsavec"));
println!("fma: {:?}", is_x86_feature_detected!("fma"));
println!("abm: {:?}", is_x86_feature_detected!("abm"));
println!("bmi: {:?}", is_x86_feature_detected!("bmi1"));
println!("bmi2: {:?}", is_x86_feature_detected!("bmi2"));
println!("tbm: {:?}", is_x86_feature_detected!("tbm"));
println!("popcnt: {:?}", is_x86_feature_detected!("popcnt"));
println!("lzcnt: {:?}", is_x86_feature_detected!("lzcnt"));
println!("fxsr: {:?}", is_x86_feature_detected!("fxsr"));
println!("xsave: {:?}", is_x86_feature_detected!("xsave"));
println!("xsaveopt: {:?}", is_x86_feature_detected!("xsaveopt"));
println!("xsaves: {:?}", is_x86_feature_detected!("xsaves"));
println!("xsavec: {:?}", is_x86_feature_detected!("xsavec"));
}
14 changes: 13 additions & 1 deletion crates/simd-test-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ extern crate proc_macro2;
#[macro_use]
extern crate quote;

use std::env;

use proc_macro2::{Term, TokenNode, TokenStream, TokenTree};
use proc_macro2::Literal;

Expand Down Expand Up @@ -53,12 +55,22 @@ pub fn simd_test(

let name: TokenStream = name.as_str().parse().unwrap();

let target = env::var("TARGET").unwrap();
let macro_test = match target.split('-').next().unwrap() {
"i686" | "x86_64" | "i586" => "is_x86_feature_detected",
"arm" => "is_arm_feature_detected",
"aarch64" => "is_aarch64_feature_detected",
"powerpc64" => "is_powerpc64_feature_detected",
t => panic!("unknown target: {}", t),
};
let macro_test = proc_macro2::Term::intern(macro_test);

let mut cfg_target_features = quote::Tokens::new();
use quote::ToTokens;
for feature in target_features {
let q = quote_spanned! {
proc_macro2::Span::call_site() =>
is_target_feature_detected!(#feature) &&
#macro_test!(#feature) &&
};
q.to_tokens(&mut cfg_target_features);
}
Expand Down
Loading

0 comments on commit 390f5d6

Please sign in to comment.