Convert float intrinsics to the `intrinsics!` macro

This commit is contained in:
Alex Crichton 2017-06-23 11:05:25 -07:00
parent 93fed264c1
commit 83d63eaa9b
6 changed files with 358 additions and 365 deletions

View File

@ -3,12 +3,11 @@ use core::num::Wrapping;
use float::Float; use float::Float;
macro_rules! add {
($abi:tt, $intrinsic:ident: $ty:ty) => {
/// Returns `a + b` /// Returns `a + b`
#[allow(unused_parens)] macro_rules! add {
#[cfg_attr(not(test), no_mangle)] ($a:expr, $b:expr, $ty:ty) => ({
pub extern $abi fn $intrinsic(a: $ty, b: $ty) -> $ty { let a = $a;
let b = $b;
let one = Wrapping(1 as <$ty as Float>::Int); let one = Wrapping(1 as <$ty as Float>::Int);
let zero = Wrapping(0 as <$ty as Float>::Int); let zero = Wrapping(0 as <$ty as Float>::Int);
@ -36,17 +35,17 @@ macro_rules! add {
b_abs - one >= inf_rep - one { b_abs - one >= inf_rep - one {
// NaN + anything = qNaN // NaN + anything = qNaN
if a_abs > inf_rep { if a_abs > inf_rep {
return (<$ty as Float>::from_repr((a_abs | quiet_bit).0)); return <$ty as Float>::from_repr((a_abs | quiet_bit).0);
} }
// anything + NaN = qNaN // anything + NaN = qNaN
if b_abs > inf_rep { if b_abs > inf_rep {
return (<$ty as Float>::from_repr((b_abs | quiet_bit).0)); return <$ty as Float>::from_repr((b_abs | quiet_bit).0);
} }
if a_abs == inf_rep { if a_abs == inf_rep {
// +/-infinity + -/+infinity = qNaN // +/-infinity + -/+infinity = qNaN
if (a.repr() ^ b.repr()) == sign_bit.0 { if (a.repr() ^ b.repr()) == sign_bit.0 {
return (<$ty as Float>::from_repr(qnan_rep.0)); return <$ty as Float>::from_repr(qnan_rep.0);
} else { } else {
// +/-infinity + anything remaining = +/- infinity // +/-infinity + anything remaining = +/- infinity
return a; return a;
@ -62,7 +61,7 @@ macro_rules! add {
if a_abs.0 == 0 { if a_abs.0 == 0 {
// but we need to get the sign right for zero + zero // but we need to get the sign right for zero + zero
if b_abs.0 == 0 { if b_abs.0 == 0 {
return (<$ty as Float>::from_repr(a.repr() & b.repr())); return <$ty as Float>::from_repr(a.repr() & b.repr());
} else { } else {
return b; return b;
} }
@ -124,7 +123,7 @@ macro_rules! add {
a_significand -= b_significand; a_significand -= b_significand;
// If a == -b, return +zero. // If a == -b, return +zero.
if a_significand.0 == 0 { if a_significand.0 == 0 {
return (<$ty as Float>::from_repr(0)); return <$ty as Float>::from_repr(0);
} }
// If partial cancellation occured, we need to left-shift the result // If partial cancellation occured, we need to left-shift the result
@ -149,7 +148,7 @@ macro_rules! add {
// If we have overflowed the type, return +/- infinity: // If we have overflowed the type, return +/- infinity:
if a_exponent >= Wrapping(max_exponent.0 as i32) { if a_exponent >= Wrapping(max_exponent.0 as i32) {
return (<$ty>::from_repr((inf_rep | result_sign).0)); return <$ty>::from_repr((inf_rep | result_sign).0);
} }
if a_exponent.0 <= 0 { if a_exponent.0 <= 0 {
@ -177,18 +176,17 @@ macro_rules! add {
if round_guard_sticky == 0x4 { result += result & one; } if round_guard_sticky == 0x4 { result += result & one; }
<$ty>::from_repr(result.0) <$ty>::from_repr(result.0)
} })
}
} }
#[cfg(target_arch = "arm")] intrinsics! {
add!("aapcs", __addsf3: f32); #[aapcs_on_arm]
pub extern "C" fn __addsf3(a: f32, b: f32) -> f32 {
add!(a, b, f32)
}
#[cfg(not(target_arch = "arm"))] #[aapcs_on_arm]
add!("C", __addsf3: f32); pub extern "C" fn __adddf3(a: f64, b: f64) -> f64 {
add!(a, b, f64)
#[cfg(target_arch = "arm")] }
add!("aapcs", __adddf3: f64); }
#[cfg(not(target_arch = "arm"))]
add!("C", __adddf3: f64);

View File

@ -2,12 +2,8 @@ use float::Float;
use int::Int; use int::Int;
macro_rules! int_to_float { macro_rules! int_to_float {
($intrinsic:ident: $ity:ty, $fty:ty) => { ($i:expr, $ity:ty, $fty:ty) => ({
int_to_float!($intrinsic: $ity, $fty, "C"); let i = $i;
};
($intrinsic:ident: $ity:ty, $fty:ty, $abi:tt) => {
pub extern $abi fn $intrinsic(i: $ity) -> $fty {
if i == 0 { if i == 0 {
return 0.0 return 0.0
} }
@ -70,29 +66,54 @@ macro_rules! int_to_float {
<$fty>::from_parts(s, <$fty>::from_parts(s,
(e + exponent_bias) as <$fty as Float>::Int, (e + exponent_bias) as <$fty as Float>::Int,
a as <$fty as Float>::Int) a as <$fty as Float>::Int)
} })
}
} }
macro_rules! int_to_float_unadj_on_win { intrinsics! {
($intrinsic:ident: $ity:ty, $fty:ty) => { pub extern "C" fn __floatsisf(i: i32) -> f32 {
#[cfg(all(windows, target_pointer_width="64"))] int_to_float!(i, i32, f32)
int_to_float!($intrinsic: $ity, $fty, "unadjusted");
#[cfg(not(all(windows, target_pointer_width="64")))]
int_to_float!($intrinsic: $ity, $fty, "C");
};
} }
int_to_float!(__floatsisf: i32, f32); pub extern "C" fn __floatsidf(i: i32) -> f64 {
int_to_float!(__floatsidf: i32, f64); int_to_float!(i, i32, f64)
int_to_float!(__floatdidf: i64, f64); }
int_to_float_unadj_on_win!(__floattisf: i128, f32);
int_to_float_unadj_on_win!(__floattidf: i128, f64); pub extern "C" fn __floatdidf(i: i64) -> f64 {
int_to_float!(__floatunsisf: u32, f32); int_to_float!(i, i64, f64)
int_to_float!(__floatunsidf: u32, f64); }
int_to_float!(__floatundidf: u64, f64);
int_to_float_unadj_on_win!(__floatuntisf: u128, f32); #[unadjusted_on_win64]
int_to_float_unadj_on_win!(__floatuntidf: u128, f64); pub extern "C" fn __floattisf(i: i128) -> f32 {
int_to_float!(i, i128, f32)
}
#[unadjusted_on_win64]
pub extern "C" fn __floattidf(i: i128) -> f64 {
int_to_float!(i, i128, f64)
}
pub extern "C" fn __floatunsisf(i: u32) -> f32 {
int_to_float!(i, u32, f32)
}
pub extern "C" fn __floatunsidf(i: u32) -> f64 {
int_to_float!(i, u32, f64)
}
pub extern "C" fn __floatundidf(i: u64) -> f64 {
int_to_float!(i, u64, f64)
}
#[unadjusted_on_win64]
pub extern "C" fn __floatuntisf(i: u128) -> f32 {
int_to_float!(i, u128, f32)
}
#[unadjusted_on_win64]
pub extern "C" fn __floatuntidf(i: u128) -> f64 {
int_to_float!(i, u128, f64)
}
}
#[derive(PartialEq, Debug)] #[derive(PartialEq, Debug)]
enum Sign { enum Sign {
@ -101,11 +122,8 @@ enum Sign {
} }
macro_rules! float_to_int { macro_rules! float_to_int {
($intrinsic:ident: $fty:ty, $ity:ty) => { ($f:expr, $fty:ty, $ity:ty) => ({
float_to_int!($intrinsic: $fty, $ity, "C"); let f = $f;
};
($intrinsic:ident: $fty:ty, $ity:ty, $abi:tt) => {
pub extern $abi fn $intrinsic(f: $fty) -> $ity {
let fixint_min = <$ity>::min_value(); let fixint_min = <$ity>::min_value();
let fixint_max = <$ity>::max_value(); let fixint_max = <$ity>::max_value();
let fixint_bits = <$ity>::bits() as usize; let fixint_bits = <$ity>::bits() as usize;
@ -151,29 +169,59 @@ macro_rules! float_to_int {
} else { } else {
r r
} }
} })
}
} }
macro_rules! float_to_int_unadj_on_win { intrinsics! {
($intrinsic:ident: $fty:ty, $ity:ty) => { pub extern "C" fn __fixsfsi(f: f32) -> i32 {
#[cfg(all(windows, target_pointer_width="64"))] float_to_int!(f, f32, i32)
float_to_int!($intrinsic: $fty, $ity, "unadjusted");
#[cfg(not(all(windows, target_pointer_width="64")))]
float_to_int!($intrinsic: $fty, $ity, "C");
};
} }
float_to_int!(__fixsfsi: f32, i32); pub extern "C" fn __fixsfdi(f: f32) -> i64 {
float_to_int!(__fixsfdi: f32, i64); float_to_int!(f, f32, i64)
float_to_int_unadj_on_win!(__fixsfti: f32, i128); }
float_to_int!(__fixdfsi: f64, i32);
float_to_int!(__fixdfdi: f64, i64);
float_to_int_unadj_on_win!(__fixdfti: f64, i128);
float_to_int!(__fixunssfsi: f32, u32); #[unadjusted_on_win64]
float_to_int!(__fixunssfdi: f32, u64); pub extern "C" fn __fixsfti(f: f32) -> i128 {
float_to_int_unadj_on_win!(__fixunssfti: f32, u128); float_to_int!(f, f32, i128)
float_to_int!(__fixunsdfsi: f64, u32); }
float_to_int!(__fixunsdfdi: f64, u64);
float_to_int_unadj_on_win!(__fixunsdfti: f64, u128); pub extern "C" fn __fixdfsi(f: f64) -> i32 {
float_to_int!(f, f64, i32)
}
pub extern "C" fn __fixdfdi(f: f64) -> i64 {
float_to_int!(f, f64, i64)
}
#[unadjusted_on_win64]
pub extern "C" fn __fixdfti(f: f64) -> i128 {
float_to_int!(f, f64, i128)
}
pub extern "C" fn __fixunssfsi(f: f32) -> u32 {
float_to_int!(f, f32, u32)
}
pub extern "C" fn __fixunssfdi(f: f32) -> u64 {
float_to_int!(f, f32, u64)
}
#[unadjusted_on_win64]
pub extern "C" fn __fixunssfti(f: f32) -> u128 {
float_to_int!(f, f32, u128)
}
pub extern "C" fn __fixunsdfsi(f: f64) -> u32 {
float_to_int!(f, f64, u32)
}
pub extern "C" fn __fixunsdfdi(f: f64) -> u64 {
float_to_int!(f, f64, u64)
}
#[unadjusted_on_win64]
pub extern "C" fn __fixunsdfti(f: f64) -> u128 {
float_to_int!(f, f64, u128)
}
}

View File

@ -1,16 +1,14 @@
macro_rules! pow {
($intrinsic:ident: $fty:ty, $ity:ident) => {
/// Returns `a` raised to the power `b` /// Returns `a` raised to the power `b`
#[cfg_attr(not(test), no_mangle)] macro_rules! pow {
pub extern "C" fn $intrinsic(a: $fty, b: $ity) -> $fty { ($a: expr, $b: expr) => ({
let (mut a, mut b) = (a, b); let (mut a, mut b) = ($a, $b);
let recip = b < 0; let recip = b < 0;
let mut r: $fty = 1.0; let mut r = 1.0;
loop { loop {
if (b & 1) != 0 { if (b & 1) != 0 {
r *= a; r *= a;
} }
b = sdiv!($ity, b, 2); b = b.checked_div(2).unwrap_or_else(|| ::abort());
if b == 0 { if b == 0 {
break; break;
} }
@ -22,9 +20,15 @@ macro_rules! pow {
} else { } else {
r r
} }
} })
}
} }
pow!(__powisf2: f32, i32); intrinsics! {
pow!(__powidf2: f64, i32); pub extern "C" fn __powisf2(a: f32, b: i32) -> f32 {
pow!(a, b)
}
pub extern "C" fn __powidf2(a: f64, b: i32) -> f64 {
pow!(a, b)
}
}

View File

@ -1,20 +1,11 @@
use float::Float; use float::Float;
macro_rules! sub { intrinsics! {
($(#[$attr:meta])* pub extern "C" fn __subsf3(a: f32, b: f32) -> f32 {
| $intrinsic:ident: $ty:ty) => { a + f32::from_repr(b.repr() ^ f32::sign_mask())
/// Returns `a - b`
$(#[$attr])*
pub extern "C" fn $intrinsic(a: $ty, b: $ty) -> $ty {
a + <$ty>::from_repr(b.repr() ^ <$ty>::sign_mask())
}
}
} }
sub!(#[cfg_attr(all(not(test), not(target_arch = "arm")), no_mangle)] pub extern "C" fn __subdf3(a: f64, b: f64) -> f64 {
#[cfg_attr(all(not(test), target_arch = "arm"), inline(always))] a + f64::from_repr(b.repr() ^ f64::sign_mask())
| __subsf3: f32); }
}
sub!(#[cfg_attr(all(not(test), not(target_arch = "arm")), no_mangle)]
#[cfg_attr(all(not(test), target_arch = "arm"), inline(always))]
| __subdf3: f64);

View File

@ -1,5 +1,3 @@
use core::intrinsics;
use int::{Int, LargeInt}; use int::{Int, LargeInt};
macro_rules! udivmod_inner { macro_rules! udivmod_inner {
@ -13,9 +11,11 @@ macro_rules! udivmod_inner {
// 0 X // 0 X
if let Some(rem) = rem { if let Some(rem) = rem {
*rem = <$ty>::from(urem!(n.low(), d.low())); *rem = <$ty>::from(n.low().checked_rem(d.low())
.unwrap_or_else(|| ::abort()));
} }
return <$ty>::from(udiv!(n.low(), d.low())); return <$ty>::from(n.low().checked_div(d.low())
.unwrap_or_else(|| ::abort()));
} else { } else {
// 0 X // 0 X
// --- // ---
@ -38,9 +38,7 @@ macro_rules! udivmod_inner {
// 0 0 // 0 0
// NOTE This should be unreachable in safe Rust because the program will panic before // NOTE This should be unreachable in safe Rust because the program will panic before
// this intrinsic is called // this intrinsic is called
unsafe { ::abort();
intrinsics::abort()
}
} }
if n.low() == 0 { if n.low() == 0 {
@ -48,9 +46,11 @@ macro_rules! udivmod_inner {
// --- // ---
// K 0 // K 0
if let Some(rem) = rem { if let Some(rem) = rem {
*rem = <$ty>::from_parts(0, urem!(n.high(), d.high())); *rem = <$ty>::from_parts(0, n.high().checked_rem(d.high())
.unwrap_or_else(|| ::abort()));
} }
return <$ty>::from(udiv!(n.high(), d.high())); return <$ty>::from(n.high().checked_div(d.high())
.unwrap_or_else(|| ::abort()));
} }
// K K // K K
@ -161,9 +161,7 @@ intrinsics! {
if d == 0 { if d == 0 {
// NOTE This should be unreachable in safe Rust because the program will panic before // NOTE This should be unreachable in safe Rust because the program will panic before
// this intrinsic is called // this intrinsic is called
unsafe { ::abort();
intrinsics::abort()
}
} }
if n == 0 { if n == 0 {

View File

@ -32,52 +32,6 @@
// that follow "x86 naming convention" (e.g. addsf3). Those aeabi intrinsics must adhere to the // that follow "x86 naming convention" (e.g. addsf3). Those aeabi intrinsics must adhere to the
// AAPCS calling convention (`extern "aapcs"`) because that's how LLVM will call them. // AAPCS calling convention (`extern "aapcs"`) because that's how LLVM will call them.
// TODO(rust-lang/rust#37029) use e.g. checked_div(_).unwrap_or_else(|| abort())
macro_rules! udiv {
($a:expr, $b:expr) => {
unsafe {
let a = $a;
let b = $b;
if b == 0 {
::core::intrinsics::abort()
} else {
::core::intrinsics::unchecked_div(a, b)
}
}
}
}
macro_rules! sdiv {
($sty:ident, $a:expr, $b:expr) => {
unsafe {
let a = $a;
let b = $b;
if b == 0 || (b == -1 && a == $sty::min_value()) {
::core::intrinsics::abort()
} else {
::core::intrinsics::unchecked_div(a, b)
}
}
}
}
macro_rules! urem {
($a:expr, $b:expr) => {
unsafe {
let a = $a;
let b = $b;
if b == 0 {
::core::intrinsics::abort()
} else {
::core::intrinsics::unchecked_rem(a, b)
}
}
}
}
#[cfg(test)] #[cfg(test)]
extern crate core; extern crate core;