Convert float intrinsics to the `intrinsics!` macro

master
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;
macro_rules! add {
($abi:tt, $intrinsic:ident: $ty:ty) => {
/// Returns `a + b`
#[allow(unused_parens)]
#[cfg_attr(not(test), no_mangle)]
pub extern $abi fn $intrinsic(a: $ty, b: $ty) -> $ty {
macro_rules! add {
($a:expr, $b:expr, $ty:ty) => ({
let a = $a;
let b = $b;
let one = Wrapping(1 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 {
// NaN + anything = qNaN
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
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 {
// +/-infinity + -/+infinity = qNaN
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 {
// +/-infinity + anything remaining = +/- infinity
return a;
@ -62,7 +61,7 @@ macro_rules! add {
if a_abs.0 == 0 {
// but we need to get the sign right for zero + zero
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 {
return b;
}
@ -124,7 +123,7 @@ macro_rules! add {
a_significand -= b_significand;
// If a == -b, return +zero.
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
@ -149,7 +148,7 @@ macro_rules! add {
// If we have overflowed the type, return +/- infinity:
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 {
@ -177,18 +176,17 @@ macro_rules! add {
if round_guard_sticky == 0x4 { result += result & one; }
<$ty>::from_repr(result.0)
}
}
})
}
#[cfg(target_arch = "arm")]
add!("aapcs", __addsf3: f32);
intrinsics! {
#[aapcs_on_arm]
pub extern "C" fn __addsf3(a: f32, b: f32) -> f32 {
add!(a, b, f32)
}
#[cfg(not(target_arch = "arm"))]
add!("C", __addsf3: f32);
#[cfg(target_arch = "arm")]
add!("aapcs", __adddf3: f64);
#[cfg(not(target_arch = "arm"))]
add!("C", __adddf3: f64);
#[aapcs_on_arm]
pub extern "C" fn __adddf3(a: f64, b: f64) -> f64 {
add!(a, b, f64)
}
}

View File

@ -2,12 +2,8 @@ use float::Float;
use int::Int;
macro_rules! int_to_float {
($intrinsic:ident: $ity:ty, $fty:ty) => {
int_to_float!($intrinsic: $ity, $fty, "C");
};
($intrinsic:ident: $ity:ty, $fty:ty, $abi:tt) => {
pub extern $abi fn $intrinsic(i: $ity) -> $fty {
($i:expr, $ity:ty, $fty:ty) => ({
let i = $i;
if i == 0 {
return 0.0
}
@ -70,29 +66,54 @@ macro_rules! int_to_float {
<$fty>::from_parts(s,
(e + exponent_bias) as <$fty as Float>::Int,
a as <$fty as Float>::Int)
}
}
})
}
macro_rules! int_to_float_unadj_on_win {
($intrinsic:ident: $ity:ty, $fty:ty) => {
#[cfg(all(windows, target_pointer_width="64"))]
int_to_float!($intrinsic: $ity, $fty, "unadjusted");
#[cfg(not(all(windows, target_pointer_width="64")))]
int_to_float!($intrinsic: $ity, $fty, "C");
};
intrinsics! {
pub extern "C" fn __floatsisf(i: i32) -> f32 {
int_to_float!(i, i32, f32)
}
int_to_float!(__floatsisf: i32, f32);
int_to_float!(__floatsidf: 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);
int_to_float!(__floatunsisf: u32, f32);
int_to_float!(__floatunsidf: u32, f64);
int_to_float!(__floatundidf: u64, f64);
int_to_float_unadj_on_win!(__floatuntisf: u128, f32);
int_to_float_unadj_on_win!(__floatuntidf: u128, f64);
pub extern "C" fn __floatsidf(i: i32) -> f64 {
int_to_float!(i, i32, f64)
}
pub extern "C" fn __floatdidf(i: i64) -> f64 {
int_to_float!(i, i64, f64)
}
#[unadjusted_on_win64]
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)]
enum Sign {
@ -101,11 +122,8 @@ enum Sign {
}
macro_rules! float_to_int {
($intrinsic:ident: $fty:ty, $ity:ty) => {
float_to_int!($intrinsic: $fty, $ity, "C");
};
($intrinsic:ident: $fty:ty, $ity:ty, $abi:tt) => {
pub extern $abi fn $intrinsic(f: $fty) -> $ity {
($f:expr, $fty:ty, $ity:ty) => ({
let f = $f;
let fixint_min = <$ity>::min_value();
let fixint_max = <$ity>::max_value();
let fixint_bits = <$ity>::bits() as usize;
@ -151,29 +169,59 @@ macro_rules! float_to_int {
} else {
r
}
}
}
})
}
macro_rules! float_to_int_unadj_on_win {
($intrinsic:ident: $fty:ty, $ity:ty) => {
#[cfg(all(windows, target_pointer_width="64"))]
float_to_int!($intrinsic: $fty, $ity, "unadjusted");
#[cfg(not(all(windows, target_pointer_width="64")))]
float_to_int!($intrinsic: $fty, $ity, "C");
};
intrinsics! {
pub extern "C" fn __fixsfsi(f: f32) -> i32 {
float_to_int!(f, f32, i32)
}
float_to_int!(__fixsfsi: f32, i32);
float_to_int!(__fixsfdi: 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);
pub extern "C" fn __fixsfdi(f: f32) -> i64 {
float_to_int!(f, f32, i64)
}
float_to_int!(__fixunssfsi: f32, u32);
float_to_int!(__fixunssfdi: f32, u64);
float_to_int_unadj_on_win!(__fixunssfti: f32, u128);
float_to_int!(__fixunsdfsi: f64, u32);
float_to_int!(__fixunsdfdi: f64, u64);
float_to_int_unadj_on_win!(__fixunsdfti: f64, u128);
#[unadjusted_on_win64]
pub extern "C" fn __fixsfti(f: f32) -> i128 {
float_to_int!(f, f32, i128)
}
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`
#[cfg_attr(not(test), no_mangle)]
pub extern "C" fn $intrinsic(a: $fty, b: $ity) -> $fty {
let (mut a, mut b) = (a, b);
macro_rules! pow {
($a: expr, $b: expr) => ({
let (mut a, mut b) = ($a, $b);
let recip = b < 0;
let mut r: $fty = 1.0;
let mut r = 1.0;
loop {
if (b & 1) != 0 {
r *= a;
}
b = sdiv!($ity, b, 2);
b = b.checked_div(2).unwrap_or_else(|| ::abort());
if b == 0 {
break;
}
@ -22,9 +20,15 @@ macro_rules! pow {
} else {
r
}
}
}
})
}
pow!(__powisf2: f32, i32);
pow!(__powidf2: f64, i32);
intrinsics! {
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;
macro_rules! sub {
($(#[$attr:meta])*
| $intrinsic:ident: $ty:ty) => {
/// Returns `a - b`
$(#[$attr])*
pub extern "C" fn $intrinsic(a: $ty, b: $ty) -> $ty {
a + <$ty>::from_repr(b.repr() ^ <$ty>::sign_mask())
}
}
intrinsics! {
pub extern "C" fn __subsf3(a: f32, b: f32) -> f32 {
a + f32::from_repr(b.repr() ^ f32::sign_mask())
}
sub!(#[cfg_attr(all(not(test), not(target_arch = "arm")), no_mangle)]
#[cfg_attr(all(not(test), target_arch = "arm"), inline(always))]
| __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);
pub extern "C" fn __subdf3(a: f64, b: f64) -> f64 {
a + f64::from_repr(b.repr() ^ f64::sign_mask())
}
}

View File

@ -1,5 +1,3 @@
use core::intrinsics;
use int::{Int, LargeInt};
macro_rules! udivmod_inner {
@ -13,9 +11,11 @@ macro_rules! udivmod_inner {
// 0 X
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 {
// 0 X
// ---
@ -38,9 +38,7 @@ macro_rules! udivmod_inner {
// 0 0
// NOTE This should be unreachable in safe Rust because the program will panic before
// this intrinsic is called
unsafe {
intrinsics::abort()
}
::abort();
}
if n.low() == 0 {
@ -48,9 +46,11 @@ macro_rules! udivmod_inner {
// ---
// K 0
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
@ -161,9 +161,7 @@ intrinsics! {
if d == 0 {
// NOTE This should be unreachable in safe Rust because the program will panic before
// this intrinsic is called
unsafe {
intrinsics::abort()
}
::abort();
}
if n == 0 {

View File

@ -32,52 +32,6 @@
// 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.
// 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)]
extern crate core;