2017-09-26 12:37:39 -07:00
|
|
|
//! Implementation of the `#[simd_test]` macro
|
|
|
|
|
//!
|
2017-10-27 17:55:29 +02:00
|
|
|
//! This macro expands to a `#[test]` function which tests the local machine
|
|
|
|
|
//! for the appropriate cfg before calling the inner test function.
|
2017-09-26 12:37:39 -07:00
|
|
|
|
|
|
|
|
#![feature(proc_macro)]
|
|
|
|
|
|
2017-10-27 17:55:29 +02:00
|
|
|
extern crate proc_macro;
|
2018-02-02 16:08:27 +01:00
|
|
|
extern crate proc_macro2;
|
2017-09-26 12:37:39 -07:00
|
|
|
#[macro_use]
|
|
|
|
|
extern crate quote;
|
|
|
|
|
|
2018-03-07 09:46:16 -06:00
|
|
|
use std::env;
|
|
|
|
|
|
2018-04-03 07:34:02 -07:00
|
|
|
use proc_macro2::{Literal, Span, Term, TokenStream, TokenTree};
|
2017-09-26 12:37:39 -07:00
|
|
|
|
|
|
|
|
fn string(s: &str) -> TokenTree {
|
2018-04-03 07:34:02 -07:00
|
|
|
Literal::string(s).into()
|
2017-09-26 12:37:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[proc_macro_attribute]
|
2017-10-27 17:55:29 +02:00
|
|
|
pub fn simd_test(
|
|
|
|
|
attr: proc_macro::TokenStream, item: proc_macro::TokenStream
|
|
|
|
|
) -> proc_macro::TokenStream {
|
2018-03-22 17:09:01 +01:00
|
|
|
let tokens = TokenStream::from(attr)
|
|
|
|
|
.into_iter()
|
|
|
|
|
.collect::<Vec<_>>();
|
2018-04-27 04:54:15 +02:00
|
|
|
if tokens.len() != 3 {
|
|
|
|
|
panic!("expected #[simd_test(enable = \"feature\")]");
|
2017-09-26 12:37:39 -07:00
|
|
|
}
|
2018-04-03 07:34:02 -07:00
|
|
|
match &tokens[0] {
|
2018-04-27 04:54:15 +02:00
|
|
|
TokenTree::Term(tt) if tt.to_string() == "enable" => {}
|
|
|
|
|
_ => panic!("expected #[simd_test(enable = \"feature\")]"),
|
|
|
|
|
}
|
|
|
|
|
match &tokens[1] {
|
2018-04-03 07:34:02 -07:00
|
|
|
TokenTree::Op(tt) if tt.op() == '=' => {}
|
2018-04-27 04:54:15 +02:00
|
|
|
_ => panic!("expected #[simd_test(enable = \"feature\")]"),
|
2017-09-26 12:37:39 -07:00
|
|
|
}
|
2018-04-27 04:54:15 +02:00
|
|
|
let enable_feature = match &tokens[2] {
|
2018-04-03 07:34:02 -07:00
|
|
|
TokenTree::Literal(tt) => tt.to_string(),
|
2018-04-27 04:54:15 +02:00
|
|
|
_ => panic!("expected #[simd_test(enable = \"feature\")]"),
|
2017-11-13 17:48:40 +01:00
|
|
|
};
|
2018-04-27 04:54:15 +02:00
|
|
|
let enable_feature = enable_feature
|
|
|
|
|
.trim_left_matches('"')
|
|
|
|
|
.trim_right_matches('"');
|
|
|
|
|
let target_features: Vec<String> = enable_feature
|
2017-11-13 17:48:40 +01:00
|
|
|
.replace('+', "")
|
|
|
|
|
.split(',')
|
|
|
|
|
.map(|v| String::from(v))
|
|
|
|
|
.collect();
|
|
|
|
|
|
2018-01-17 09:45:02 -06:00
|
|
|
let enable_feature = string(enable_feature);
|
2017-09-26 12:37:39 -07:00
|
|
|
let item = TokenStream::from(item);
|
|
|
|
|
let name = find_name(item.clone());
|
|
|
|
|
|
2018-03-22 17:09:01 +01:00
|
|
|
let name: TokenStream = name.as_str().parse().expect(&format!(
|
|
|
|
|
"failed to parse name: {}",
|
|
|
|
|
name.clone().as_str()
|
|
|
|
|
));
|
2017-09-26 12:37:39 -07:00
|
|
|
|
2018-03-22 17:40:44 -05:00
|
|
|
let target = env::var("TARGET")
|
|
|
|
|
.expect("TARGET environment variable should be set for rustc");
|
2018-03-10 19:22:54 +01:00
|
|
|
let mut force_test = false;
|
2018-03-22 17:09:01 +01:00
|
|
|
let macro_test = match target.split('-').next().expect(&format!(
|
|
|
|
|
"target triple contained no \"-\": {}",
|
|
|
|
|
target
|
|
|
|
|
)) {
|
2018-03-07 09:46:16 -06:00
|
|
|
"i686" | "x86_64" | "i586" => "is_x86_feature_detected",
|
2018-03-20 15:11:50 +01:00
|
|
|
"arm" | "armv7" => "is_arm_feature_detected",
|
2018-03-07 09:46:16 -06:00
|
|
|
"aarch64" => "is_aarch64_feature_detected",
|
2018-05-16 20:59:28 +02:00
|
|
|
"powerpc64" | "powerpc64le" => "is_powerpc64_feature_detected",
|
2018-03-10 19:22:54 +01:00
|
|
|
"mips" | "mipsel" => {
|
|
|
|
|
// FIXME:
|
|
|
|
|
// On MIPS CI run-time feature detection always returns false due
|
|
|
|
|
// to this qemu bug: https://bugs.launchpad.net/qemu/+bug/1754372
|
|
|
|
|
//
|
|
|
|
|
// This is a workaround to force the MIPS tests to always run on
|
|
|
|
|
// CI.
|
|
|
|
|
force_test = true;
|
|
|
|
|
"is_mips_feature_detected"
|
|
|
|
|
}
|
|
|
|
|
"mips64" | "mips64el" => {
|
|
|
|
|
// FIXME: see above
|
|
|
|
|
force_test = true;
|
|
|
|
|
"is_mips64_feature_detected"
|
|
|
|
|
}
|
2018-03-07 09:46:16 -06:00
|
|
|
t => panic!("unknown target: {}", t),
|
|
|
|
|
};
|
2018-04-03 07:34:02 -07:00
|
|
|
let macro_test = proc_macro2::Term::new(macro_test, Span::call_site());
|
2018-03-07 09:46:16 -06:00
|
|
|
|
2017-11-13 17:48:40 +01:00
|
|
|
let mut cfg_target_features = quote::Tokens::new();
|
|
|
|
|
use quote::ToTokens;
|
|
|
|
|
for feature in target_features {
|
2017-11-21 12:54:06 -06:00
|
|
|
let q = quote_spanned! {
|
2018-01-08 10:10:52 -08:00
|
|
|
proc_macro2::Span::call_site() =>
|
2018-03-07 09:46:16 -06:00
|
|
|
#macro_test!(#feature) &&
|
2017-11-13 17:48:40 +01:00
|
|
|
};
|
|
|
|
|
q.to_tokens(&mut cfg_target_features);
|
|
|
|
|
}
|
|
|
|
|
let q = quote!{ true };
|
|
|
|
|
q.to_tokens(&mut cfg_target_features);
|
|
|
|
|
|
2017-11-21 12:54:06 -06:00
|
|
|
let ret: TokenStream = quote_spanned! {
|
2018-01-08 10:10:52 -08:00
|
|
|
proc_macro2::Span::call_site() =>
|
2017-10-11 17:28:44 +02:00
|
|
|
#[allow(non_snake_case)]
|
2017-09-26 12:37:39 -07:00
|
|
|
#[test]
|
|
|
|
|
fn #name() {
|
2018-03-10 19:22:54 +01:00
|
|
|
if #force_test | (#cfg_target_features) {
|
2017-09-27 10:16:35 -04:00
|
|
|
return unsafe { #name() };
|
2017-10-18 11:35:11 -04:00
|
|
|
} else {
|
|
|
|
|
::stdsimd_test::assert_skip_test_ok(stringify!(#name));
|
2017-09-26 12:37:39 -07:00
|
|
|
}
|
|
|
|
|
|
2018-01-17 09:45:02 -06:00
|
|
|
#[target_feature(enable = #enable_feature)]
|
2017-09-26 12:37:39 -07:00
|
|
|
#item
|
|
|
|
|
}
|
|
|
|
|
}.into();
|
|
|
|
|
ret.into()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn find_name(item: TokenStream) -> Term {
|
|
|
|
|
let mut tokens = item.into_iter();
|
|
|
|
|
while let Some(tok) = tokens.next() {
|
2018-04-03 07:34:02 -07:00
|
|
|
if let TokenTree::Term(word) = tok {
|
2017-09-26 12:37:39 -07:00
|
|
|
if word.as_str() == "fn" {
|
2017-10-27 17:55:29 +02:00
|
|
|
break;
|
2017-09-26 12:37:39 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-03 07:34:02 -07:00
|
|
|
match tokens.next() {
|
|
|
|
|
Some(TokenTree::Term(word)) => word,
|
2017-09-26 12:37:39 -07:00
|
|
|
_ => panic!("failed to find function name"),
|
|
|
|
|
}
|
|
|
|
|
}
|