-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Benjamin Schultzer <[email protected]>
- Loading branch information
Showing
51 changed files
with
570 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "libm-test" | ||
version = "0.1.0" | ||
authors = ["Benjamin Schultzer <[email protected]>"] | ||
|
||
[lib] | ||
proc-macro = true | ||
test = false | ||
|
||
[dependencies] | ||
proc-macro2 = { version = "0.4", features = ["nightly"] } | ||
quote = "0.6" | ||
syn = { version = "0.15", features = ["full", "extra-traits"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#![recursion_limit = "256"] | ||
extern crate proc_macro; | ||
extern crate proc_macro2; | ||
#[macro_use] | ||
extern crate quote; | ||
extern crate core; | ||
extern crate syn; | ||
|
||
use proc_macro::TokenStream as TS; | ||
use proc_macro2::TokenStream; | ||
mod nearest; | ||
|
||
#[proc_macro_attribute] | ||
pub fn nearest(_attr: TS, item: TS) -> TS { | ||
let item = match syn::parse::<syn::Item>(item) { | ||
Ok(s) => s, | ||
Err(e) => return e.to_compile_error().into(), | ||
}; | ||
let func = match item { | ||
syn::Item::Fn(ref f) => f, | ||
_ => panic!("must be attached to a function"), | ||
}; | ||
let name = &func.ident; | ||
let mut tests = TokenStream::new(); | ||
match format!("{}", &name).as_ref() { | ||
"ceil" => tests.extend(nearest::ceil(&name)), | ||
|
||
"ceilf" => tests.extend(nearest::ceilf(&name)), | ||
|
||
"floor" => tests.extend(nearest::floor(&name)), | ||
|
||
"floorf" => tests.extend(nearest::floorf(&name)), | ||
|
||
"round" => tests.extend(nearest::round(&name)), | ||
|
||
"roundf" => tests.extend(nearest::roundf(&name)), | ||
|
||
"trunc" => tests.extend(nearest::trunc(&name)), | ||
|
||
"truncf" => tests.extend(nearest::truncf(&name)), | ||
|
||
_ => {} | ||
} | ||
let tts: TokenStream = quote! { | ||
#item | ||
#tests | ||
}; | ||
tts.into() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
use proc_macro2::TokenStream; | ||
|
||
pub fn nearest(func: &syn::Ident) -> TokenStream { | ||
quote! { | ||
#[test] | ||
pub fn infinity_eq_infinity() { | ||
assert_eq!(#func(::core::f32::INFINITY), ::core::f32::INFINITY); | ||
} | ||
#[test] | ||
pub fn neg_infinity_eq_neg_infinity() { | ||
assert_eq!(#func(::core::f32::NEG_INFINITY), ::core::f32::NEG_INFINITY); | ||
} | ||
#[test] | ||
pub fn max_eq_max() { | ||
assert_eq!(#func(::core::f32::MAX), ::core::f32::MAX); | ||
} | ||
#[test] | ||
pub fn neg_max_eq_neg_max() { | ||
assert_eq!(#func(-::core::f32::MAX), -::core::f32::MAX); | ||
} | ||
#[test] | ||
pub fn nan_eq_nan() { | ||
assert!(#func(::core::f32::NAN).is_nan()); | ||
} | ||
#[test] | ||
pub fn neg_nan_eq_nan() { | ||
assert!(#func(-::core::f32::NAN).is_nan()); | ||
} | ||
} | ||
} | ||
pub fn ceilf(func: &syn::Ident) -> TokenStream { | ||
let nearest = nearest(&func); | ||
let neg_min_subnorm_eq_neg_zero = neg_min_subnorm_eq_neg_zero(&func); | ||
let neg_min_positive_eq_neg_zero = neg_min_positive_eq_neg_zero(&func); | ||
quote! { | ||
#neg_min_positive_eq_neg_zero | ||
#[test] | ||
pub fn min_positive_eq_one() { | ||
assert_eq!(#func(::core::f32::MIN_POSITIVE), 1.); | ||
} | ||
#neg_min_subnorm_eq_neg_zero | ||
#[test] | ||
pub fn min_subnorm_eq_one() { | ||
assert_eq!(#func(crate::F32_MIN_SUBNORM), 1.); | ||
} | ||
#[test] | ||
pub fn neg_pi_eq_neg_three() { | ||
assert_eq!(#func(-::core::f32::consts::PI), -3.); | ||
} | ||
#[test] | ||
pub fn pi_eq_four() { | ||
assert_eq!(#func(::core::f32::consts::PI), 4.); | ||
} | ||
#nearest | ||
} | ||
} | ||
|
||
pub fn floorf(func: &syn::Ident) -> TokenStream { | ||
quote! { | ||
#[test] | ||
pub fn neg_min_positive_eq_neg_one() { | ||
assert_eq!(#func(-::core::f32::MIN_POSITIVE), -1.); | ||
} | ||
#[test] | ||
pub fn pi_eq_neg_four() { | ||
assert_eq!(#func(::core::f32::consts::PI), 3.); | ||
} | ||
#[test] | ||
pub fn neg_pi_eq_three() { | ||
assert_eq!(#func(-::core::f32::consts::PI), -4.); | ||
} | ||
#[test] | ||
pub fn min_positive_eq_zero() { | ||
assert_eq!(#func(::core::f32::MIN_POSITIVE), 0.); | ||
} | ||
#[test] | ||
pub fn neg_min_subnorm_eq_one() { | ||
assert_eq!(#func(-crate::F32_MIN_SUBNORM), -1.); | ||
} | ||
} | ||
} | ||
|
||
pub fn neg_min_positive_eq_neg_zero(func: &syn::Ident) -> TokenStream { | ||
// round trunc ceil | ||
quote! { | ||
#[test] | ||
pub fn neg_min_positive_eq_neg_zero() { | ||
assert_eq!(#func(-::core::f32::MIN_POSITIVE), -0.); | ||
} | ||
} | ||
} | ||
pub fn neg_min_subnorm_eq_neg_zero(func: &syn::Ident) -> TokenStream { | ||
// round trunc ceil | ||
quote! { | ||
#[test] | ||
pub fn neg_min_subnorm_eq_neg_zero() { | ||
assert_eq!(#func(-crate::F32_MIN_SUBNORM), -0.); | ||
} | ||
} | ||
} | ||
pub fn min_subnorm_eq_zero(func: &syn::Ident) -> TokenStream { | ||
// round trunc floor | ||
quote! { | ||
#[test] | ||
pub fn min_subnorm_eq_zero() { | ||
assert_eq!(#func(crate::F32_MIN_SUBNORM), 0.); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
use proc_macro2::TokenStream; | ||
|
||
pub fn nearest(func: &syn::Ident) -> TokenStream { | ||
quote! { | ||
#[test] | ||
pub fn infinity_eq_infinity() { | ||
assert_eq!(#func(::core::f64::INFINITY), ::core::f64::INFINITY); | ||
} | ||
#[test] | ||
pub fn neg_infinity_eq_neg_infinity() { | ||
assert_eq!(#func(::core::f64::NEG_INFINITY), ::core::f64::NEG_INFINITY); | ||
} | ||
#[test] | ||
pub fn max_eq_max() { | ||
assert_eq!(#func(::core::f64::MAX), ::core::f64::MAX); | ||
} | ||
#[test] | ||
pub fn neg_max_eq_neg_max() { | ||
assert_eq!(#func(-::core::f64::MAX), -::core::f64::MAX); | ||
} | ||
#[test] | ||
pub fn nan_eq_nan() { | ||
assert!(#func(::core::f64::NAN).is_nan()); | ||
} | ||
#[test] | ||
pub fn neg_nan_eq_nan() { | ||
assert!(#func(-::core::f64::NAN).is_nan()); | ||
} | ||
} | ||
} | ||
pub fn ceil(func: &syn::Ident) -> TokenStream { | ||
let nearest = nearest(&func); | ||
let neg_min_subnorm_eq_neg_zero = neg_min_subnorm_eq_neg_zero(&func); | ||
let neg_min_positive_eq_neg_zero = neg_min_positive_eq_neg_zero(&func); | ||
quote! { | ||
#neg_min_positive_eq_neg_zero | ||
#[test] | ||
pub fn min_positive_eq_one() { | ||
assert_eq!(#func(::core::f64::MIN_POSITIVE), 1.); | ||
} | ||
#neg_min_subnorm_eq_neg_zero | ||
#[test] | ||
pub fn min_subnorm_eq_one() { | ||
assert_eq!(#func(crate::F64_MIN_SUBNORM), 1.); | ||
} | ||
#[test] | ||
pub fn neg_pi_eq_neg_three() { | ||
assert_eq!(#func(-::core::f64::consts::PI), -3.); | ||
} | ||
#[test] | ||
pub fn pi_eq_four() { | ||
assert_eq!(#func(::core::f64::consts::PI), 4.); | ||
} | ||
#nearest | ||
} | ||
} | ||
|
||
pub fn floor(func: &syn::Ident) -> TokenStream { | ||
quote! { | ||
#[test] | ||
pub fn neg_min_positive_eq_neg_one() { | ||
assert_eq!(#func(-::core::f64::MIN_POSITIVE), -1.); | ||
} | ||
#[test] | ||
pub fn pi_eq_neg_four() { | ||
assert_eq!(#func(::core::f64::consts::PI), 3.); | ||
} | ||
#[test] | ||
pub fn neg_pi_eq_three() { | ||
assert_eq!(#func(-::core::f64::consts::PI), -4.); | ||
} | ||
#[test] | ||
pub fn min_positive_eq_zero() { | ||
assert_eq!(#func(::core::f64::MIN_POSITIVE), 0.); | ||
} | ||
#[test] | ||
pub fn neg_min_subnorm_eq_one() { | ||
assert_eq!(#func(-crate::F64_MIN_SUBNORM), -1.); | ||
} | ||
} | ||
} | ||
|
||
pub fn neg_min_positive_eq_neg_zero(func: &syn::Ident) -> TokenStream { | ||
// round trunc ceil | ||
quote! { | ||
#[test] | ||
pub fn neg_min_positive_eq_neg_zero() { | ||
assert_eq!(#func(-::core::f64::MIN_POSITIVE), -0.); | ||
} | ||
} | ||
} | ||
pub fn neg_min_subnorm_eq_neg_zero(func: &syn::Ident) -> TokenStream { | ||
// round trunc ceil | ||
quote! { | ||
#[test] | ||
pub fn neg_min_subnorm_eq_neg_zero() { | ||
assert_eq!(#func(-crate::F64_MIN_SUBNORM), -0.); | ||
} | ||
} | ||
} | ||
pub fn min_subnorm_eq_zero(func: &syn::Ident) -> TokenStream { | ||
// round trunc floor | ||
quote! { | ||
#[test] | ||
pub fn min_subnorm_eq_zero() { | ||
assert_eq!(#func(crate::F64_MIN_SUBNORM), 0.); | ||
} | ||
} | ||
} |
Oops, something went wrong.