From a1377878c68f5eabb9ecafaa1db736b2c2fb7f3b Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 22 Jun 2017 14:00:31 -0700 Subject: [PATCH 01/48] Test with the 'c' feature enabled on CI --- Cargo.toml | 4 ++++ appveyor.yml | 4 +++- ci/run.sh | 2 ++ examples/intrinsics.rs | 25 ++++++------------------- 4 files changed, 15 insertions(+), 20 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e23c666..69b9c03 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,5 +27,9 @@ utest-cortex-m-qemu = { default-features = false, git = "https://github.com/japa utest-macros = { git = "https://github.com/japaric/utest" } +[[example]] +name = "intrinsics" +required-features = ["c"] + [workspace] diff --git a/appveyor.yml b/appveyor.yml index f5a4c62..2bef109 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -5,7 +5,7 @@ environment: install: - git submodule update --init - - curl -sSf -o rustup-init.exe https://win.rustup.rs + - appveyor-retry appveyor DownloadFile https://win.rustup.rs/ -FileName rustup-init.exe - rustup-init.exe --default-host x86_64-pc-windows-msvc --default-toolchain nightly -y - set PATH=%PATH%;C:\Users\appveyor\.cargo\bin - if "%TARGET%"=="i686-pc-windows-msvc" ( rustup target add %TARGET% ) @@ -18,4 +18,6 @@ test_script: - cargo build --target %TARGET% - cargo build --release --target %TARGET% - cargo test --no-default-features --features gen-tests --target %TARGET% + - cargo test --no-default-features --features "gen-tests c" --target %TARGET% - cargo test --no-default-features --features gen-tests --release --target %TARGET% + - cargo test --no-default-features --features "gen-tests c" --release --target %TARGET% diff --git a/ci/run.sh b/ci/run.sh index 4a28e8a..02b9bf1 100755 --- a/ci/run.sh +++ b/ci/run.sh @@ -34,7 +34,9 @@ case $1 in ;; *) cargo test --no-default-features --features gen-tests --target $1 + cargo test --no-default-features --features 'gen-tests c' --target $1 cargo test --no-default-features --features gen-tests --target $1 --release + cargo test --no-default-features --features 'gen-tests c' --target $1 --release ;; esac diff --git a/examples/intrinsics.rs b/examples/intrinsics.rs index 4c5c884..4015ac3 100644 --- a/examples/intrinsics.rs +++ b/examples/intrinsics.rs @@ -6,17 +6,17 @@ #![allow(unused_features)] #![cfg_attr(thumb, no_main)] #![deny(dead_code)] +#![feature(alloc_system)] #![feature(asm)] #![feature(compiler_builtins_lib)] #![feature(core_float)] #![feature(lang_items)] -#![feature(libc)] #![feature(start)] #![feature(i128_type)] #![no_std] #[cfg(not(thumb))] -extern crate libc; +extern crate alloc_system; extern crate compiler_builtins; // NOTE cfg(not(thumbv6m)) means that the operation is not supported on ARMv6-M at all. Not even @@ -27,7 +27,6 @@ extern crate compiler_builtins; // convention for its intrinsics that's different from other architectures; that's why some function // have an additional comment: the function name is the ARM name for the intrinsic and the comment // in the non-ARM name for the intrinsic. -#[cfg(feature = "c")] mod intrinsics { use core::num::Float; @@ -339,7 +338,6 @@ mod intrinsics { } } -#[cfg(feature = "c")] fn run() { use intrinsics::*; @@ -404,33 +402,20 @@ fn run() { bb(modti3(bb(2), bb(2))); } -#[cfg(all(feature = "c", not(thumb)))] +#[cfg(not(thumb))] #[start] fn main(_: isize, _: *const *const u8) -> isize { run(); - 0 } -#[cfg(all(not(feature = "c"), not(thumb)))] -#[start] -fn main(_: isize, _: *const *const u8) -> isize { - 0 -} - -#[cfg(all(feature = "c", thumb))] +#[cfg(thumb)] #[no_mangle] pub fn _start() -> ! { run(); loop {} } -#[cfg(all(not(feature = "c"), thumb))] -#[no_mangle] -pub fn _start() -> ! { - loop {} -} - // ARM targets need these symbols #[no_mangle] pub fn __aeabi_unwind_cpp_pr0() {} @@ -447,9 +432,11 @@ pub fn _Unwind_Resume() {} #[cfg(not(test))] #[lang = "eh_personality"] #[no_mangle] +#[allow(private_no_mangle_fns)] extern "C" fn eh_personality() {} #[cfg(not(test))] #[lang = "panic_fmt"] #[no_mangle] +#[allow(private_no_mangle_fns)] extern "C" fn panic_fmt() {} From 2f2bb32e40edc558d32009763a72d4120c0764e3 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 22 Jun 2017 22:03:07 -0700 Subject: [PATCH 02/48] Use traits instead of macros for shift intrinsics This is an attempt to tidy up the definition of intrinsics by making them more rust-like at the definition site and using traits instead of macros for the definition. Additionally the helper macro, `intrinsics!`, now fills in a definition for #[cfg]'d off intrinsics when compiling with C code --- src/int/mod.rs | 43 +++++++++++++--- src/int/shift.rs | 130 +++++++++++++++++++++++++++-------------------- src/lib.rs | 3 ++ src/macros.rs | 51 +++++++++++++++++++ 4 files changed, 166 insertions(+), 61 deletions(-) create mode 100644 src/macros.rs diff --git a/src/int/mod.rs b/src/int/mod.rs index 768b6b4..11a31ac 100755 --- a/src/int/mod.rs +++ b/src/int/mod.rs @@ -1,3 +1,5 @@ +use core::ops; + macro_rules! hty { ($ty:ty) => { <$ty as LargeInt>::HighHalf @@ -16,15 +18,25 @@ pub mod shift; pub mod udiv; /// Trait for some basic operations on integers -pub trait Int { +pub trait Int: + Copy + + PartialEq + + ops::Shl + + ops::Shr + + ops::BitOr + + // ops::BitAnd + +{ /// Type with the same width but other signedness - type OtherSign; + type OtherSign: Int; /// Unsigned version of Self - type UnsignedInt; + type UnsignedInt: Int; /// Returns the bitwidth of the int type fn bits() -> u32; + /// Returns the zero representation of this number + fn zero() -> Self; + /// Extracts the sign from self and returns a tuple. /// /// # Examples @@ -36,6 +48,9 @@ pub trait Int { /// assert_eq!(u, 25_u32); /// ``` fn extract_sign(self) -> (bool, Self::UnsignedInt); + + /// Convert to a signed representation + fn unsigned(self) -> Self::UnsignedInt; } macro_rules! int_impl { @@ -44,6 +59,10 @@ macro_rules! int_impl { type OtherSign = $ity; type UnsignedInt = $uty; + fn zero() -> Self { + 0 + } + fn bits() -> u32 { $bits } @@ -51,6 +70,10 @@ macro_rules! int_impl { fn extract_sign(self) -> (bool, $uty) { (false, self) } + + fn unsigned(self) -> $uty { + self + } } impl Int for $ity { @@ -61,6 +84,10 @@ macro_rules! int_impl { $bits } + fn zero() -> Self { + 0 + } + fn extract_sign(self) -> (bool, $uty) { if self < 0 { (true, (!(self as $uty)).wrapping_add(1)) @@ -68,6 +95,10 @@ macro_rules! int_impl { (false, self as $uty) } } + + fn unsigned(self) -> $uty { + self as $uty + } } } } @@ -77,9 +108,9 @@ int_impl!(i64, u64, 64); int_impl!(i128, u128, 128); /// Trait to convert an integer to/from smaller parts -pub trait LargeInt { - type LowHalf; - type HighHalf; +pub trait LargeInt: Int { + type LowHalf: Int; + type HighHalf: Int; fn low(self) -> Self::LowHalf; fn high(self) -> Self::HighHalf; diff --git a/src/int/shift.rs b/src/int/shift.rs index 8b5c4a1..23a49ac 100644 --- a/src/int/shift.rs +++ b/src/int/shift.rs @@ -1,74 +1,94 @@ use int::{Int, LargeInt}; -macro_rules! ashl { - ($intrinsic:ident: $ty:ty) => { - /// Returns `a << b`, requires `b < $ty::bits()` - #[cfg_attr(not(test), no_mangle)] - #[cfg_attr(all(not(test), not(target_arch = "arm")), no_mangle)] - #[cfg_attr(all(not(test), target_arch = "arm"), inline(always))] - pub extern "C" fn $intrinsic(a: $ty, b: u32) -> $ty { - let half_bits = <$ty>::bits() / 2; - if b & half_bits != 0 { - <$ty>::from_parts(0, a.low() << (b - half_bits)) - } else if b == 0 { - a - } else { - <$ty>::from_parts(a.low() << b, (a.high() << b) | (a.low() >> (half_bits - b))) - } +trait Ashl: Int + LargeInt { + /// Returns `a << b`, requires `b < $ty::bits()` + fn ashl(self, offset: u32) -> Self + where Self: LargeInt::LowHalf>, + { + let half_bits = Self::bits() / 2; + if offset & half_bits != 0 { + Self::from_parts(Int::zero(), self.low() << (offset - half_bits)) + } else if offset == 0 { + self + } else { + Self::from_parts(self.low() << offset, + (self.high() << offset) | + (self.low() >> (half_bits - offset))) } } } -macro_rules! ashr { - ($intrinsic:ident: $ty:ty) => { - /// Returns arithmetic `a >> b`, requires `b < $ty::bits()` - #[cfg_attr(not(test), no_mangle)] - #[cfg_attr(all(not(test), not(target_arch = "arm")), no_mangle)] - #[cfg_attr(all(not(test), target_arch = "arm"), inline(always))] - pub extern "C" fn $intrinsic(a: $ty, b: u32) -> $ty { - let half_bits = <$ty>::bits() / 2; - if b & half_bits != 0 { - <$ty>::from_parts((a.high() >> (b - half_bits)) as <$ty as LargeInt>::LowHalf, - a.high() >> (half_bits - 1)) - } else if b == 0 { - a - } else { - let high_unsigned = a.high() as <$ty as LargeInt>::LowHalf; - <$ty>::from_parts((high_unsigned << (half_bits - b)) | (a.low() >> b), - a.high() >> b) - } +impl Ashl for u64 {} +impl Ashl for u128 {} + +trait Ashr: Int + LargeInt { + /// Returns arithmetic `a >> b`, requires `b < $ty::bits()` + fn ashr(self, offset: u32) -> Self + where Self: LargeInt::HighHalf as Int>::UnsignedInt>, + { + let half_bits = Self::bits() / 2; + if offset & half_bits != 0 { + Self::from_parts((self.high() >> (offset - half_bits)).unsigned(), + self.high() >> (half_bits - 1)) + } else if offset == 0 { + self + } else { + let high_unsigned = self.high().unsigned(); + Self::from_parts((high_unsigned << (half_bits - offset)) | (self.low() >> offset), + self.high() >> offset) } } } -macro_rules! lshr { - ($intrinsic:ident: $ty:ty) => { - /// Returns logical `a >> b`, requires `b < $ty::bits()` - #[cfg_attr(not(test), no_mangle)] - pub extern "C" fn $intrinsic(a: $ty, b: u32) -> $ty { - let half_bits = <$ty>::bits() / 2; - if b & half_bits != 0 { - <$ty>::from_parts(a.high() >> (b - half_bits), 0) - } else if b == 0 { - a - } else { - <$ty>::from_parts((a.high() << (half_bits - b)) | (a.low() >> b), a.high() >> b) - } +impl Ashr for i64 {} +impl Ashr for i128 {} + +trait Lshr: Int + LargeInt { + /// Returns logical `a >> b`, requires `b < $ty::bits()` + fn lshr(self, offset: u32) -> Self + where Self: LargeInt::LowHalf>, + { + let half_bits = Self::bits() / 2; + if offset & half_bits != 0 { + Self::from_parts(self.high() >> (offset - half_bits), Int::zero()) + } else if offset == 0 { + self + } else { + Self::from_parts((self.high() << (half_bits - offset)) | + (self.low() >> offset), + self.high() >> offset) } } } -#[cfg(not(all(feature = "c", target_arch = "x86")))] -ashl!(__ashldi3: u64); +impl Lshr for u64 {} +impl Lshr for u128 {} -ashl!(__ashlti3: u128); +intrinsics! { + #[cfg(not(all(feature = "c", target_arch = "x86")))] + pub extern "C" fn __ashldi3(a: u64, b: u32) -> u64 { + a.ashl(b) + } -#[cfg(not(all(feature = "c", target_arch = "x86")))] -ashr!(__ashrdi3: i64); + pub extern "C" fn __ashlti3(a: u128, b: u32) -> u128 { + a.ashl(b) + } -ashr!(__ashrti3: i128); + #[cfg(not(all(feature = "c", target_arch = "x86")))] + pub extern "C" fn __ashrdi3(a: i64, b: u32) -> i64 { + a.ashr(b) + } -#[cfg(not(all(feature = "c", target_arch = "x86")))] -lshr!(__lshrdi3: u64); + pub extern "C" fn __ashrti3(a: i128, b: u32) -> i128 { + a.ashr(b) + } -lshr!(__lshrti3: u128); + #[cfg(not(all(feature = "c", target_arch = "x86")))] + pub extern "C" fn __lshrdi3(a: u64, b: u32) -> u64 { + a.lshr(b) + } + + pub extern "C" fn __lshrti3(a: u128, b: u32) -> u128 { + a.lshr(b) + } +} diff --git a/src/lib.rs b/src/lib.rs index 03f7258..b1e739f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -99,6 +99,9 @@ fn sconv(i: i128) -> U64x2 { #[cfg(test)] extern crate core; +#[macro_use] +mod macros; + pub mod int; pub mod float; diff --git a/src/macros.rs b/src/macros.rs new file mode 100644 index 0000000..a5a535b --- /dev/null +++ b/src/macros.rs @@ -0,0 +1,51 @@ +macro_rules! intrinsics { + () => (); + ( + #[cfg(not(all(feature = "c", $($cfg_clause:tt)*)))] + $(#[$attr:meta])* + pub extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) -> $ret:ty { + $($body:tt)* + } + + $($rest:tt)* + ) => ( + + #[cfg(all(feature = "c", not($($cfg_clause)*)))] + $(#[$attr])* + pub extern $abi fn $name( $($argname: $ty),* ) -> $ret { + extern $abi { + fn $name($($argname: $ty),*) -> $ret; + } + unsafe { + $name($($argname),*) + } + } + + #[cfg(not(all(feature = "c", not($($cfg_clause)*))))] + intrinsics! { + $(#[$attr])* + pub extern $abi fn $name( $($argname: $ty),* ) -> $ret { + $($body)* + } + } + + intrinsics!($($rest)*); + ); + + ( + $(#[$attr:meta])* + pub extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) -> $ret:ty { + $($body:tt)* + } + + $($rest:tt)* + ) => ( + $(#[$attr])* + #[cfg_attr(not(test), no_mangle)] + pub extern $abi fn $name( $($argname: $ty),* ) -> $ret { + $($body)* + } + + intrinsics!($($rest)*); + ); +} From eeb44abacff68127c5d12de64066ad3fcd9397bd Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 22 Jun 2017 22:04:40 -0700 Subject: [PATCH 03/48] Remove executable bit from mod.rs --- src/int/mod.rs | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 src/int/mod.rs diff --git a/src/int/mod.rs b/src/int/mod.rs old mode 100755 new mode 100644 From 275d1032b59455ee0fabc97e2e51d650f8390780 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 22 Jun 2017 22:36:37 -0700 Subject: [PATCH 04/48] Port mul intrinsics to traits Also add a few features to the `intrinsics!` macro --- src/int/mod.rs | 68 ++++++++++++++++++++- src/int/mul.rs | 159 +++++++++++++++++++++++++------------------------ src/macros.rs | 66 ++++++++++++++++++++ 3 files changed, 211 insertions(+), 82 deletions(-) diff --git a/src/int/mod.rs b/src/int/mod.rs index 11a31ac..02e3412 100644 --- a/src/int/mod.rs +++ b/src/int/mod.rs @@ -20,11 +20,19 @@ pub mod udiv; /// Trait for some basic operations on integers pub trait Int: Copy + - PartialEq + + PartialEq + + PartialOrd + + ops::AddAssign + + ops::Add + + ops::Sub + + ops::Div + ops::Shl + ops::Shr + ops::BitOr + - // ops::BitAnd + + ops::BitXor + + ops::BitAnd + + ops::BitAndAssign + + ops::Not + { /// Type with the same width but other signedness type OtherSign: Int; @@ -34,8 +42,8 @@ pub trait Int: /// Returns the bitwidth of the int type fn bits() -> u32; - /// Returns the zero representation of this number fn zero() -> Self; + fn one() -> Self; /// Extracts the sign from self and returns a tuple. /// @@ -51,6 +59,12 @@ pub trait Int: /// Convert to a signed representation fn unsigned(self) -> Self::UnsignedInt; + + // copied from primitive integers, but put in a trait + fn max_value() -> Self; + fn min_value() -> Self; + fn wrapping_add(self, other: Self) -> Self; + fn wrapping_mul(self, other: Self) -> Self; } macro_rules! int_impl { @@ -63,6 +77,10 @@ macro_rules! int_impl { 0 } + fn one() -> Self { + 1 + } + fn bits() -> u32 { $bits } @@ -74,6 +92,22 @@ macro_rules! int_impl { fn unsigned(self) -> $uty { self } + + fn max_value() -> Self { + ::max_value() + } + + fn min_value() -> Self { + ::min_value() + } + + fn wrapping_add(self, other: Self) -> Self { + ::wrapping_add(self, other) + } + + fn wrapping_mul(self, other: Self) -> Self { + ::wrapping_mul(self, other) + } } impl Int for $ity { @@ -88,6 +122,10 @@ macro_rules! int_impl { 0 } + fn one() -> Self { + 1 + } + fn extract_sign(self) -> (bool, $uty) { if self < 0 { (true, (!(self as $uty)).wrapping_add(1)) @@ -99,6 +137,22 @@ macro_rules! int_impl { fn unsigned(self) -> $uty { self as $uty } + + fn max_value() -> Self { + ::max_value() + } + + fn min_value() -> Self { + ::min_value() + } + + fn wrapping_add(self, other: Self) -> Self { + ::wrapping_add(self, other) + } + + fn wrapping_mul(self, other: Self) -> Self { + ::wrapping_mul(self, other) + } } } } @@ -113,7 +167,9 @@ pub trait LargeInt: Int { type HighHalf: Int; fn low(self) -> Self::LowHalf; + fn low_as_high(low: Self::LowHalf) -> Self::HighHalf; fn high(self) -> Self::HighHalf; + fn high_as_low(low: Self::HighHalf) -> Self::LowHalf; fn from_parts(low: Self::LowHalf, high: Self::HighHalf) -> Self; } @@ -126,9 +182,15 @@ macro_rules! large_int { fn low(self) -> $tylow { self as $tylow } + fn low_as_high(low: $tylow) -> $tyhigh { + low as $tyhigh + } fn high(self) -> $tyhigh { (self >> $halfbits) as $tyhigh } + fn high_as_low(high: $tyhigh) -> $tylow { + high as $tylow + } fn from_parts(low: $tylow, high: $tyhigh) -> $ty { low as $ty | ((high as $ty) << $halfbits) } diff --git a/src/int/mul.rs b/src/int/mul.rs index 5381edd..73f95a3 100644 --- a/src/int/mul.rs +++ b/src/int/mul.rs @@ -1,95 +1,96 @@ +use core::ops; + use int::LargeInt; use int::Int; -macro_rules! mul { - ($(#[$attr:meta])+ | - $abi:tt, $intrinsic:ident: $ty:ty) => { - /// Returns `a * b` - $(#[$attr])+ - pub extern $abi fn $intrinsic(a: $ty, b: $ty) -> $ty { - let half_bits = <$ty>::bits() / 4; - let lower_mask = !0 >> half_bits; - let mut low = (a.low() & lower_mask).wrapping_mul(b.low() & lower_mask); - let mut t = low >> half_bits; - low &= lower_mask; - t += (a.low() >> half_bits).wrapping_mul(b.low() & lower_mask); - low += (t & lower_mask) << half_bits; - let mut high = (t >> half_bits) as hty!($ty); - t = low >> half_bits; - low &= lower_mask; - t += (b.low() >> half_bits).wrapping_mul(a.low() & lower_mask); - low += (t & lower_mask) << half_bits; - high += (t >> half_bits) as hty!($ty); - high += (a.low() >> half_bits).wrapping_mul(b.low() >> half_bits) as hty!($ty); - high = high.wrapping_add(a.high().wrapping_mul(b.low() as hty!($ty))) - .wrapping_add((a.low() as hty!($ty)).wrapping_mul(b.high())); - <$ty>::from_parts(low, high) - } +trait Mul: LargeInt { + fn mul(self, other: Self) -> Self { + let half_bits = Self::bits() / 4; + let lower_mask = !<::LowHalf>::zero() >> half_bits; + let mut low = (self.low() & lower_mask).wrapping_mul(other.low() & lower_mask); + let mut t = low >> half_bits; + low &= lower_mask; + t += (self.low() >> half_bits).wrapping_mul(other.low() & lower_mask); + low += (t & lower_mask) << half_bits; + let mut high = Self::low_as_high(t >> half_bits); + t = low >> half_bits; + low &= lower_mask; + t += (other.low() >> half_bits).wrapping_mul(self.low() & lower_mask); + low += (t & lower_mask) << half_bits; + high += Self::low_as_high(t >> half_bits); + high += Self::low_as_high((self.low() >> half_bits).wrapping_mul(other.low() >> half_bits)); + high = high.wrapping_add(self.high().wrapping_mul(Self::low_as_high(other.low()))) + .wrapping_add(Self::low_as_high(self.low()).wrapping_mul(other.high())); + Self::from_parts(low, high) } } -macro_rules! mulo { - ($intrinsic:ident: $ty:ty) => { - // Default is "C" ABI - mulo!($intrinsic: $ty, "C"); - }; - ($intrinsic:ident: $ty:ty, $abi:tt) => { - /// Returns `a * b` and sets `*overflow = 1` if `a * b` overflows - #[cfg_attr(not(test), no_mangle)] - pub extern $abi fn $intrinsic(a: $ty, b: $ty, overflow: &mut i32) -> $ty { - *overflow = 0; - let result = a.wrapping_mul(b); - if a == <$ty>::min_value() { - if b != 0 && b != 1 { - *overflow = 1; - } - return result; - } - if b == <$ty>::min_value() { - if a != 0 && a != 1 { - *overflow = 1; - } - return result; - } +impl Mul for u64 {} +impl Mul for i128 {} - let sa = a >> (<$ty>::bits() - 1); - let abs_a = (a ^ sa) - sa; - let sb = b >> (<$ty>::bits() - 1); - let abs_b = (b ^ sb) - sb; - if abs_a < 2 || abs_b < 2 { - return result; +trait Mulo: Int + ops::Neg { + fn mulo(self, other: Self, overflow: &mut i32) -> Self { + *overflow = 0; + let result = self.wrapping_mul(other); + if self == Self::min_value() { + if other != Self::zero() && other != Self::one() { + *overflow = 1; } - if sa == sb { - if abs_a > <$ty>::max_value() / abs_b { - *overflow = 1; - } - } else { - if abs_a > <$ty>::min_value() / -abs_b { - *overflow = 1; - } - } - result + return result; } + if other == Self::min_value() { + if self != Self::zero() && self != Self::one() { + *overflow = 1; + } + return result; + } + + let sa = self >> (Self::bits() - 1); + let abs_a = (self ^ sa) - sa; + let sb = other >> (Self::bits() - 1); + let abs_b = (other ^ sb) - sb; + let two = Self::one() + Self::one(); + if abs_a < two || abs_b < two { + return result; + } + if sa == sb { + if abs_a > Self::max_value() / abs_b { + *overflow = 1; + } + } else { + if abs_a > Self::min_value() / -abs_b { + *overflow = 1; + } + } + result } } -#[cfg(not(all(feature = "c", target_arch = "x86")))] -mul!(#[cfg_attr(all(not(test), not(target_arch = "arm")), no_mangle)] - #[cfg_attr(all(not(test), target_arch = "arm"), inline(always))] - | "C", __muldi3: u64); +impl Mulo for i32 {} +impl Mulo for i64 {} +impl Mulo for i128 {} -#[cfg(not(target_arch = "arm"))] -mul!(#[cfg_attr(not(test), no_mangle)] - | "C", __multi3: i128); +intrinsics! { + #[cfg(not(all(feature = "c", target_arch = "x86")))] + pub extern "C" fn __muldi3(a: u64, b: u64) -> u64 { + a.mul(b) + } -#[cfg(target_arch = "arm")] -mul!(#[cfg_attr(not(test), no_mangle)] - | "aapcs", __multi3: i128); + #[aapcs_on_arm] + pub extern "C" fn __multi3(a: i128, b: i128) -> i128 { + a.mul(b) + } -mulo!(__mulosi4: i32); -mulo!(__mulodi4: i64); + pub extern "C" fn __mulosi4(a: i32, b: i32, oflow: &mut i32) -> i32 { + a.mulo(b, oflow) + } -#[cfg(all(windows, target_pointer_width="64"))] -mulo!(__muloti4: i128, "unadjusted"); -#[cfg(not(all(windows, target_pointer_width="64")))] -mulo!(__muloti4: i128); + pub extern "C" fn __mulodi4(a: i64, b: i64, oflow: &mut i32) -> i64 { + a.mulo(b, oflow) + } + + #[unadjusted_on_win64] + pub extern "C" fn __muloti4(a: i128, b: i128, oflow: &mut i32) -> i128 { + a.mulo(b, oflow) + } +} diff --git a/src/macros.rs b/src/macros.rs index a5a535b..c0c94b7 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1,5 +1,10 @@ macro_rules! intrinsics { () => (); + + // Anything which has a `not(feature = "c")` we'll generate a shim function + // which calls out to the C function if the `c` feature is enabled. + // Otherwise if the `c` feature isn't enabled then we'll just have a normal + // intrinsic. ( #[cfg(not(all(feature = "c", $($cfg_clause:tt)*)))] $(#[$attr:meta])* @@ -32,6 +37,67 @@ macro_rules! intrinsics { intrinsics!($($rest)*); ); + // We recognize the `#[aapcs_only_on_arm]` attribute here and generate the + // same intrinsic but force it to have the `"aapcs"` calling convention on + // ARM and `"C"` elsewhere. + ( + #[aapcs_on_arm] + $(#[$attr:meta])* + pub extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) -> $ret:ty { + $($body:tt)* + } + + $($rest:tt)* + ) => ( + #[cfg(target_arch = "arm")] + intrinsics! { + $(#[$attr])* + pub extern "aapcs" fn $name( $($argname: $ty),* ) -> $ret { + $($body)* + } + } + + #[cfg(not(target_arch = "arm"))] + intrinsics! { + $(#[$attr])* + pub extern $abi fn $name( $($argname: $ty),* ) -> $ret { + $($body)* + } + } + + intrinsics!($($rest)*); + ); + + // Like aapcs above we recognize an attribute for the "unadjusted" abi on + // win64 for some methods. + ( + #[unadjusted_on_win64] + $(#[$attr:meta])* + pub extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) -> $ret:ty { + $($body:tt)* + } + + $($rest:tt)* + ) => ( + #[cfg(all(windows, target_pointer_width = "64"))] + intrinsics! { + $(#[$attr])* + pub extern "unadjusted" fn $name( $($argname: $ty),* ) -> $ret { + $($body)* + } + } + + #[cfg(not(all(windows, target_pointer_width = "64")))] + intrinsics! { + $(#[$attr])* + pub extern $abi fn $name( $($argname: $ty),* ) -> $ret { + $($body)* + } + } + + intrinsics!($($rest)*); + ); + ( $(#[$attr:meta])* pub extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) -> $ret:ty { From 7886ae275b20de63c11bd5872633e84e3c50aa27 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 22 Jun 2017 23:09:28 -0700 Subject: [PATCH 05/48] Port sdiv to traits + `intrinsics!` Enhance `intrinsics!` along the way! --- src/int/mod.rs | 37 ++++++++++- src/int/mul.rs | 2 +- src/int/sdiv.rs | 159 +++++++++++++++++++++++------------------------ src/int/shift.rs | 6 +- src/lib.rs | 4 ++ src/macros.rs | 59 +++++++++++++++++- 6 files changed, 180 insertions(+), 87 deletions(-) diff --git a/src/int/mod.rs b/src/int/mod.rs index 02e3412..9ad3d88 100644 --- a/src/int/mod.rs +++ b/src/int/mod.rs @@ -57,14 +57,17 @@ pub trait Int: /// ``` fn extract_sign(self) -> (bool, Self::UnsignedInt); - /// Convert to a signed representation fn unsigned(self) -> Self::UnsignedInt; + fn from_unsigned(unsigned: Self::UnsignedInt) -> Self; // copied from primitive integers, but put in a trait fn max_value() -> Self; fn min_value() -> Self; fn wrapping_add(self, other: Self) -> Self; fn wrapping_mul(self, other: Self) -> Self; + fn wrapping_sub(self, other: Self) -> Self; + fn checked_div(self, other: Self) -> Option; + fn checked_rem(self, other: Self) -> Option; } macro_rules! int_impl { @@ -93,6 +96,10 @@ macro_rules! int_impl { self } + fn from_unsigned(me: $uty) -> Self { + me + } + fn max_value() -> Self { ::max_value() } @@ -108,6 +115,18 @@ macro_rules! int_impl { fn wrapping_mul(self, other: Self) -> Self { ::wrapping_mul(self, other) } + + fn wrapping_sub(self, other: Self) -> Self { + ::wrapping_sub(self, other) + } + + fn checked_div(self, other: Self) -> Option { + ::checked_div(self, other) + } + + fn checked_rem(self, other: Self) -> Option { + ::checked_rem(self, other) + } } impl Int for $ity { @@ -138,6 +157,10 @@ macro_rules! int_impl { self as $uty } + fn from_unsigned(me: $uty) -> Self { + me as $ity + } + fn max_value() -> Self { ::max_value() } @@ -153,6 +176,18 @@ macro_rules! int_impl { fn wrapping_mul(self, other: Self) -> Self { ::wrapping_mul(self, other) } + + fn wrapping_sub(self, other: Self) -> Self { + ::wrapping_sub(self, other) + } + + fn checked_div(self, other: Self) -> Option { + ::checked_div(self, other) + } + + fn checked_rem(self, other: Self) -> Option { + ::checked_rem(self, other) + } } } } diff --git a/src/int/mul.rs b/src/int/mul.rs index 73f95a3..4b0831d 100644 --- a/src/int/mul.rs +++ b/src/int/mul.rs @@ -71,7 +71,7 @@ impl Mulo for i64 {} impl Mulo for i128 {} intrinsics! { - #[cfg(not(all(feature = "c", target_arch = "x86")))] + #[use_c_shim_if(target_arch = "x86")] pub extern "C" fn __muldi3(a: u64, b: u64) -> u64 { a.mul(b) } diff --git a/src/int/sdiv.rs b/src/int/sdiv.rs index 5576898..310eb9f 100644 --- a/src/int/sdiv.rs +++ b/src/int/sdiv.rs @@ -1,102 +1,101 @@ use int::Int; -macro_rules! div { - ($intrinsic:ident: $ty:ty, $uty:ty) => { - div!($intrinsic: $ty, $uty, $ty, |i| {i}); - }; - ($intrinsic:ident: $ty:ty, $uty:ty, $tyret:ty, $conv:expr) => { - /// Returns `a / b` - #[cfg_attr(not(test), no_mangle)] - pub extern "C" fn $intrinsic(a: $ty, b: $ty) -> $tyret { - let s_a = a >> (<$ty>::bits() - 1); - let s_b = b >> (<$ty>::bits() - 1); - // NOTE it's OK to overflow here because of the `as $uty` cast below - // This whole operation is computing the absolute value of the inputs - // So some overflow will happen when dealing with e.g. `i64::MIN` - // where the absolute value is `(-i64::MIN) as u64` - let a = (a ^ s_a).wrapping_sub(s_a); - let b = (b ^ s_b).wrapping_sub(s_b); - let s = s_a ^ s_b; +trait Div: Int { + /// Returns `a / b` + fn div(self, other: Self) -> Self { + let s_a = self >> (Self::bits() - 1); + let s_b = other >> (Self::bits() - 1); + // NOTE it's OK to overflow here because of the `as $uty` cast below + // This whole operation is computing the absolute value of the inputs + // So some overflow will happen when dealing with e.g. `i64::MIN` + // where the absolute value is `(-i64::MIN) as u64` + let a = (self ^ s_a).wrapping_sub(s_a); + let b = (other ^ s_b).wrapping_sub(s_b); + let s = s_a ^ s_b; - let r = udiv!(a as $uty, b as $uty); - ($conv)((r as $ty ^ s) - s) - } + let r = a.unsigned().checked_div(b.unsigned()) + .unwrap_or_else(|| ::abort()); + (Self::from_unsigned(r) ^ s) - s } } -macro_rules! mod_ { - ($intrinsic:ident: $ty:ty, $uty:ty) => { - mod_!($intrinsic: $ty, $uty, $ty, |i| {i}); - }; - ($intrinsic:ident: $ty:ty, $uty:ty, $tyret:ty, $conv:expr) => { - /// Returns `a % b` - #[cfg_attr(not(test), no_mangle)] - pub extern "C" fn $intrinsic(a: $ty, b: $ty) -> $tyret { - let s = b >> (<$ty>::bits() - 1); - // NOTE(wrapping_sub) see comment in the `div` macro - let b = (b ^ s).wrapping_sub(s); - let s = a >> (<$ty>::bits() - 1); - let a = (a ^ s).wrapping_sub(s); +impl Div for i32 {} +impl Div for i64 {} +impl Div for i128 {} - let r = urem!(a as $uty, b as $uty); - ($conv)((r as $ty ^ s) - s) - } +trait Mod: Int { + /// Returns `a % b` + fn mod_(self, other: Self) -> Self { + let s = other >> (Self::bits() - 1); + // NOTE(wrapping_sub) see comment in the `div` + let b = (other ^ s).wrapping_sub(s); + let s = self >> (Self::bits() - 1); + let a = (self ^ s).wrapping_sub(s); + + let r = a.unsigned().checked_rem(b.unsigned()) + .unwrap_or_else(|| ::abort()); + (Self::from_unsigned(r) ^ s) - s } } -macro_rules! divmod { - ($abi:tt, $intrinsic:ident, $div:ident: $ty:ty) => { - /// Returns `a / b` and sets `*rem = n % d` - #[cfg_attr(not(test), no_mangle)] - pub extern $abi fn $intrinsic(a: $ty, b: $ty, rem: &mut $ty) -> $ty { - #[cfg(all(feature = "c", any(target_arch = "x86")))] - extern { - fn $div(a: $ty, b: $ty) -> $ty; - } +impl Mod for i32 {} +impl Mod for i64 {} +impl Mod for i128 {} - let r = match () { - #[cfg(not(all(feature = "c", any(target_arch = "x86"))))] - () => $div(a, b), - #[cfg(all(feature = "c", any(target_arch = "x86")))] - () => unsafe { $div(a, b) }, - }; - // NOTE won't overflow because it's using the result from the - // previous division - *rem = a - r.wrapping_mul(b); - r - } +trait Divmod: Int { + /// Returns `a / b` and sets `*rem = n % d` + fn divmod(self, other: Self, rem: &mut Self, div: F) -> Self + where F: Fn(Self, Self) -> Self, + { + let r = div(self, other); + // NOTE won't overflow because it's using the result from the + // previous division + *rem = self - r.wrapping_mul(other); + r } } -#[cfg(not(all(feature = "c", target_arch = "arm", not(target_os = "ios"), not(thumbv6m))))] -div!(__divsi3: i32, u32); +impl Divmod for i32 {} +impl Divmod for i64 {} -#[cfg(not(all(feature = "c", target_arch = "x86")))] -div!(__divdi3: i64, u64); +intrinsics! { + #[use_c_shim_if(all(target_arch = "arm", not(target_os = "ios"), not(thumbv6m)))] + pub extern "C" fn __divsi3(a: i32, b: i32) -> i32 { + a.div(b) + } -#[cfg(not(all(windows, target_pointer_width="64")))] -div!(__divti3: i128, u128); + #[use_c_shim_if(target_arch = "x86")] + pub extern "C" fn __divdi3(a: i64, b: i64) -> i64 { + a.div(b) + } -#[cfg(all(windows, target_pointer_width="64"))] -div!(__divti3: i128, u128, ::U64x2, ::sconv); + #[win64_128bit_abi_hack] + pub extern "C" fn __divti3(a: i128, b: i128) -> i128 { + a.div(b) + } -#[cfg(not(all(feature = "c", target_arch = "arm", not(target_os = "ios"))))] -mod_!(__modsi3: i32, u32); + #[use_c_shim_if(all(target_arch = "arm", not(target_os = "ios")))] + pub extern "C" fn __modsi3(a: i32, b: i32) -> i32 { + a.mod_(b) + } -#[cfg(not(all(feature = "c", target_arch = "x86")))] -mod_!(__moddi3: i64, u64); + #[use_c_shim_if(target_arch = "x86")] + pub extern "C" fn __moddi3(a: i64, b: i64) -> i64 { + a.mod_(b) + } -#[cfg(not(all(windows, target_pointer_width="64")))] -mod_!(__modti3: i128, u128); + #[win64_128bit_abi_hack] + pub extern "C" fn __modti3(a: i128, b: i128) -> i128 { + a.mod_(b) + } -#[cfg(all(windows, target_pointer_width="64"))] -mod_!(__modti3: i128, u128, ::U64x2, ::sconv); + #[use_c_shim_if(all(target_arch = "arm", not(target_os = "ios")))] + pub extern "C" fn __divmodsi4(a: i32, b: i32, rem: &mut i32) -> i32 { + a.divmod(b, rem, |a, b| __divsi3(a, b)) + } -#[cfg(not(all(feature = "c", target_arch = "arm", not(target_os = "ios"))))] -divmod!("C", __divmodsi4, __divsi3: i32); - -#[cfg(target_arch = "arm")] -divmod!("aapcs", __divmoddi4, __divdi3: i64); - -#[cfg(not(target_arch = "arm"))] -divmod!("C", __divmoddi4, __divdi3: i64); + #[aapcs_on_arm] + pub extern "C" fn __divmoddi4(a: i64, b: i64, rem: &mut i64) -> i64 { + a.divmod(b, rem, |a, b| __divdi3(a, b)) + } +} diff --git a/src/int/shift.rs b/src/int/shift.rs index 23a49ac..746d02d 100644 --- a/src/int/shift.rs +++ b/src/int/shift.rs @@ -65,7 +65,7 @@ impl Lshr for u64 {} impl Lshr for u128 {} intrinsics! { - #[cfg(not(all(feature = "c", target_arch = "x86")))] + #[use_c_shim_if(target_arch = "x86")] pub extern "C" fn __ashldi3(a: u64, b: u32) -> u64 { a.ashl(b) } @@ -74,7 +74,7 @@ intrinsics! { a.ashl(b) } - #[cfg(not(all(feature = "c", target_arch = "x86")))] + #[use_c_shim_if(target_arch = "x86")] pub extern "C" fn __ashrdi3(a: i64, b: u32) -> i64 { a.ashr(b) } @@ -83,7 +83,7 @@ intrinsics! { a.ashr(b) } - #[cfg(not(all(feature = "c", target_arch = "x86")))] + #[use_c_shim_if(target_arch = "x86")] pub extern "C" fn __lshrdi3(a: u64, b: u32) -> u64 { a.lshr(b) } diff --git a/src/lib.rs b/src/lib.rs index b1e739f..a55752a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -99,6 +99,10 @@ fn sconv(i: i128) -> U64x2 { #[cfg(test)] extern crate core; +fn abort() -> ! { + unsafe { core::intrinsics::abort() } +} + #[macro_use] mod macros; diff --git a/src/macros.rs b/src/macros.rs index c0c94b7..5c98774 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -6,7 +6,7 @@ macro_rules! intrinsics { // Otherwise if the `c` feature isn't enabled then we'll just have a normal // intrinsic. ( - #[cfg(not(all(feature = "c", $($cfg_clause:tt)*)))] + #[use_c_shim_if($($cfg_clause:tt)*)] $(#[$attr:meta])* pub extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) -> $ret:ty { $($body:tt)* @@ -37,7 +37,7 @@ macro_rules! intrinsics { intrinsics!($($rest)*); ); - // We recognize the `#[aapcs_only_on_arm]` attribute here and generate the + // We recognize the `#[aapcs_on_arm]` attribute here and generate the // same intrinsic but force it to have the `"aapcs"` calling convention on // ARM and `"C"` elsewhere. ( @@ -98,6 +98,39 @@ macro_rules! intrinsics { intrinsics!($($rest)*); ); + // Another attribute we recognize is an "abi hack" for win64 to get the 128 + // bit calling convention correct. + ( + #[win64_128bit_abi_hack] + $(#[$attr:meta])* + pub extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) -> $ret:ty { + $($body:tt)* + } + + $($rest:tt)* + ) => ( + #[cfg(all(windows, target_pointer_width = "64"))] + intrinsics! { + $(#[$attr])* + pub extern $abi fn $name( $($argname: $ty),* ) + -> ::macros::win64_abi_hack::U64x2 + { + let e: $ret = { $($body)* }; + ::macros::win64_abi_hack::U64x2::from(e) + } + } + + #[cfg(not(all(windows, target_pointer_width = "64")))] + intrinsics! { + $(#[$attr])* + pub extern $abi fn $name( $($argname: $ty),* ) -> $ret { + $($body)* + } + } + + intrinsics!($($rest)*); + ); + ( $(#[$attr:meta])* pub extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) -> $ret:ty { @@ -115,3 +148,25 @@ macro_rules! intrinsics { intrinsics!($($rest)*); ); } + +// Hack for LLVM expectations for ABI on windows +#[cfg(all(windows, target_pointer_width="64"))] +pub mod win64_abi_hack { + #[repr(simd)] + pub struct U64x2(u64, u64); + + impl From for U64x2 { + fn from(i: i128) -> U64x2 { + use int::LargeInt; + let j = i as u128; + U64x2(j.low(), j.high()) + } + } + + impl From for U64x2 { + fn from(i: u128) -> U64x2 { + use int::LargeInt; + U64x2(i.low(), i.high()) + } + } +} From d17042106ff80e7033a223a398b0f23b3142049d Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 22 Jun 2017 23:23:36 -0700 Subject: [PATCH 06/48] Port udiv to the `intrinsics!` macro --- src/int/udiv.rs | 274 ++++++++++++++++++++---------------------------- 1 file changed, 115 insertions(+), 159 deletions(-) diff --git a/src/int/udiv.rs b/src/int/udiv.rs index e8db746..5ef46de 100644 --- a/src/int/udiv.rs +++ b/src/int/udiv.rs @@ -1,141 +1,7 @@ use core::intrinsics; + use int::{Int, LargeInt}; -/// Returns `n / d` -#[cfg(not(all(feature = "c", target_arch = "arm", not(target_os = "ios"), not(thumbv6m))))] -#[cfg_attr(all(not(test), not(target_arch = "arm")), no_mangle)] -#[cfg_attr(all(not(test), target_arch = "arm"), inline(always))] -pub extern "C" fn __udivsi3(n: u32, d: u32) -> u32 { - // Special cases - if d == 0 { - // NOTE This should be unreachable in safe Rust because the program will panic before - // this intrinsic is called - unsafe { - intrinsics::abort() - } - } - - if n == 0 { - return 0; - } - - let mut sr = d.leading_zeros().wrapping_sub(n.leading_zeros()); - - // d > n - if sr > u32::bits() - 1 { - return 0; - } - - // d == 1 - if sr == u32::bits() - 1 { - return n; - } - - sr += 1; - - // 1 <= sr <= u32::bits() - 1 - let mut q = n << (u32::bits() - sr); - let mut r = n >> sr; - - let mut carry = 0; - for _ in 0..sr { - // r:q = ((r:q) << 1) | carry - r = (r << 1) | (q >> (u32::bits() - 1)); - q = (q << 1) | carry; - - // carry = 0; - // if r > d { - // r -= d; - // carry = 1; - // } - - let s = (d.wrapping_sub(r).wrapping_sub(1)) as i32 >> (u32::bits() - 1); - carry = (s & 1) as u32; - r -= d & s as u32; - } - - (q << 1) | carry -} - -/// Returns `n % d` -#[cfg(not(all(feature = "c", target_arch = "arm", not(target_os = "ios"))))] -#[cfg_attr(not(test), no_mangle)] -pub extern "C" fn __umodsi3(n: u32, d: u32) -> u32 { - #[cfg(all(feature = "c", target_arch = "arm", not(target_os = "ios")))] - extern "C" { - fn __udivsi3(n: u32, d: u32) -> u32; - } - - let q = match () { - #[cfg(all(feature = "c", target_arch = "arm", not(target_os = "ios")))] - () => unsafe { __udivsi3(n, d) }, - #[cfg(not(all(feature = "c", target_arch = "arm", not(target_os = "ios"))))] - () => __udivsi3(n, d), - }; - - n - q * d -} - -/// Returns `n / d` and sets `*rem = n % d` -#[cfg(not(all(feature = "c", target_arch = "arm", not(target_os = "ios"), not(thumbv6m))))] -#[cfg_attr(not(test), no_mangle)] -pub extern "C" fn __udivmodsi4(n: u32, d: u32, rem: Option<&mut u32>) -> u32 { - #[cfg(all(feature = "c", target_arch = "arm", not(target_os = "ios"), not(thumbv6m)))] - extern "C" { - fn __udivsi3(n: u32, d: u32) -> u32; - } - - let q = match () { - #[cfg(all(feature = "c", target_arch = "arm", not(target_os = "ios"), not(thumbv6m)))] - () => unsafe { __udivsi3(n, d) }, - #[cfg(not(all(feature = "c", target_arch = "arm", not(target_os = "ios"), not(thumbv6m))))] - () => __udivsi3(n, d), - }; - if let Some(rem) = rem { - *rem = n - (q * d); - } - q -} - -macro_rules! div_mod_intrinsics { - ($udiv_intr:ident, $umod_intr:ident : $ty:ty) => { - div_mod_intrinsics!($udiv_intr, $umod_intr : $ty, - __udivmoddi4); - }; - ($udiv_intr:ident, $umod_intr:ident : $ty:ty, $divmod_intr:expr) => { - div_mod_intrinsics!($udiv_intr, $umod_intr : $ty, - $divmod_intr, $ty, |i|{ i }); - }; - ($udiv_intr:ident, $umod_intr:ident : $ty:ty, $divmod_intr:expr, - $tyret:ty, $conv:expr) => { - /// Returns `n / d` - #[cfg_attr(not(test), no_mangle)] - pub extern "C" fn $udiv_intr(n: $ty, d: $ty) -> $tyret { - let r = $divmod_intr(n, d, None); - ($conv)(r) - } - - /// Returns `n % d` - #[cfg_attr(not(test), no_mangle)] - pub extern "C" fn $umod_intr(a: $ty, b: $ty) -> $tyret { - use core::mem; - - let mut rem = unsafe { mem::uninitialized() }; - $divmod_intr(a, b, Some(&mut rem)); - ($conv)(rem) - } - } -} - -#[cfg(not(all(feature = "c", target_arch = "x86")))] -div_mod_intrinsics!(__udivdi3, __umoddi3: u64); - -#[cfg(not(all(windows, target_pointer_width="64")))] -div_mod_intrinsics!(__udivti3, __umodti3: u128, u128_div_mod); - -#[cfg(all(windows, target_pointer_width="64"))] -div_mod_intrinsics!(__udivti3, __umodti3: u128, u128_div_mod, ::U64x2, ::conv); - macro_rules! udivmod_inner { ($n:expr, $d:expr, $rem:expr, $ty:ty) => {{ let (n, d, rem) = ($n, $d, $rem); @@ -285,30 +151,120 @@ macro_rules! udivmod_inner { }} } -/// Returns `n / d` and sets `*rem = n % d` -#[cfg_attr(not(test), no_mangle)] -pub extern "C" fn __udivmoddi4(n: u64, d: u64, rem: Option<&mut u64>) -> u64 { - udivmod_inner!(n, d, rem, u64) -} - -macro_rules! udivmodti4 { - ($tyret:ty, $conv:expr) => { - /// Returns `n / d` and sets `*rem = n % d` - #[cfg_attr(not(test), no_mangle)] - pub extern "C" fn __udivmodti4(n: u128, d: u128, rem: Option<&mut u128>) -> $tyret { - let r = u128_div_mod(n, d, rem); - ($conv)(r) +intrinsics! { + #[use_c_shim_if(all(target_arch = "arm", + not(target_os = "ios"), + not(thumbv6m)))] + /// Returns `n / d` + pub extern "C" fn __udivsi3(n: u32, d: u32) -> u32 { + // Special cases + if d == 0 { + // NOTE This should be unreachable in safe Rust because the program will panic before + // this intrinsic is called + unsafe { + intrinsics::abort() + } } + + if n == 0 { + return 0; + } + + let mut sr = d.leading_zeros().wrapping_sub(n.leading_zeros()); + + // d > n + if sr > u32::bits() - 1 { + return 0; + } + + // d == 1 + if sr == u32::bits() - 1 { + return n; + } + + sr += 1; + + // 1 <= sr <= u32::bits() - 1 + let mut q = n << (u32::bits() - sr); + let mut r = n >> sr; + + let mut carry = 0; + for _ in 0..sr { + // r:q = ((r:q) << 1) | carry + r = (r << 1) | (q >> (u32::bits() - 1)); + q = (q << 1) | carry; + + // carry = 0; + // if r > d { + // r -= d; + // carry = 1; + // } + + let s = (d.wrapping_sub(r).wrapping_sub(1)) as i32 >> (u32::bits() - 1); + carry = (s & 1) as u32; + r -= d & s as u32; + } + + (q << 1) | carry + } + + #[use_c_shim_if(all(target_arch = "arm", not(target_os = "ios")))] + /// Returns `n % d` + pub extern "C" fn __umodsi3(n: u32, d: u32) -> u32 { + let q = __udivsi3(n, d); + n - q * d + } + + #[use_c_shim_if(all(target_arch = "arm", + not(target_os = "ios"), + not(thumbv6m)))] + /// Returns `n / d` and sets `*rem = n % d` + pub extern "C" fn __udivmodsi4(n: u32, d: u32, rem: Option<&mut u32>) -> u32 { + let q = __udivsi3(n, d); + if let Some(rem) = rem { + *rem = n - (q * d); + } + q + } + + #[use_c_shim_if(target_arch = "x86")] + /// Returns `n / d` + pub extern "C" fn __udivdi3(n: u64, d: u64) -> u64 { + __udivmoddi4(n, d, None) + } + + #[use_c_shim_if(target_arch = "x86")] + /// Returns `n % d` + pub extern "C" fn __umoddi3(n: u64, d: u64) -> u64 { + let mut rem = 0; + __udivmoddi4(n, d, Some(&mut rem)); + rem + } + + #[win64_128bit_abi_hack] + /// Returns `n / d` + pub extern "C" fn __udivti3(n: u128, d: u128) -> u128 { + __udivmodti4(n, d, None) + } + + #[win64_128bit_abi_hack] + /// Returns `n % d` + pub extern "C" fn __umodti3(n: u128, d: u128) -> u128 { + let mut rem = 0; + __udivmodti4(n, d, Some(&mut rem)); + rem + } + + /// Returns `n / d` and sets `*rem = n % d` + pub extern "C" fn __udivmoddi4(n: u64, d: u64, rem: Option<&mut u64>) -> u64 { + udivmod_inner!(n, d, rem, u64) + } + + #[win64_128bit_abi_hack] + /// Returns `n / d` and sets `*rem = n % d` + pub extern "C" fn __udivmodti4(n: u128, + d: u128, + rem: Option<&mut u128>) -> u128 { + udivmod_inner!(n, d, rem, u128) } } - -/// Returns `n / d` and sets `*rem = n % d` -fn u128_div_mod(n: u128, d: u128, rem: Option<&mut u128>) -> u128 { - udivmod_inner!(n, d, rem, u128) -} - -#[cfg(all(windows, target_pointer_width="64"))] -udivmodti4!(::U64x2, ::conv); - -#[cfg(not(all(windows, target_pointer_width="64")))] -udivmodti4!(u128, |i|{ i }); From 12a0038250e4bf61da4c305d76b968767f5b1311 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 22 Jun 2017 23:36:50 -0700 Subject: [PATCH 07/48] Fix the abi hack on windows --- src/lib.rs | 18 ------------------ src/macros.rs | 22 +++++++++++++++------- 2 files changed, 15 insertions(+), 25 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a55752a..6d44fb1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -78,24 +78,6 @@ macro_rules! urem { } } -// Hack for LLVM expectations for ABI on windows -#[cfg(all(windows, target_pointer_width="64"))] -#[repr(simd)] -pub struct U64x2(u64, u64); - -#[cfg(all(windows, target_pointer_width="64"))] -fn conv(i: u128) -> U64x2 { - use int::LargeInt; - U64x2(i.low(), i.high()) -} - -#[cfg(all(windows, target_pointer_width="64"))] -fn sconv(i: i128) -> U64x2 { - use int::LargeInt; - let j = i as u128; - U64x2(j.low(), j.high()) -} - #[cfg(test)] extern crate core; diff --git a/src/macros.rs b/src/macros.rs index 5c98774..70e44da 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -110,13 +110,21 @@ macro_rules! intrinsics { $($rest:tt)* ) => ( #[cfg(all(windows, target_pointer_width = "64"))] - intrinsics! { - $(#[$attr])* - pub extern $abi fn $name( $($argname: $ty),* ) - -> ::macros::win64_abi_hack::U64x2 - { - let e: $ret = { $($body)* }; - ::macros::win64_abi_hack::U64x2::from(e) + $(#[$attr])* + pub extern $abi fn $name( $($argname: $ty),* ) -> $ret { + $($body)* + } + + #[cfg(all(windows, target_pointer_width = "64"))] + pub mod $name { + + intrinsics! { + pub extern $abi fn $name( $($argname: $ty),* ) + -> ::macros::win64_abi_hack::U64x2 + { + let e: $ret = super::$name($($argname),*); + ::macros::win64_abi_hack::U64x2::from(e) + } } } From a4120adc693c8ad6595619333a6bad543125a05f Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 22 Jun 2017 23:41:28 -0700 Subject: [PATCH 08/48] Try to fix linkage on windows --- examples/intrinsics.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/examples/intrinsics.rs b/examples/intrinsics.rs index 4015ac3..5336422 100644 --- a/examples/intrinsics.rs +++ b/examples/intrinsics.rs @@ -416,6 +416,10 @@ pub fn _start() -> ! { loop {} } +#[cfg(windows)] +#[link(name = "kernel32")] +extern {} + // ARM targets need these symbols #[no_mangle] pub fn __aeabi_unwind_cpp_pr0() {} From 7f90c48a7a0f6be1ffea344834207114d28293d2 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 22 Jun 2017 23:44:27 -0700 Subject: [PATCH 09/48] Test 64-bit linux --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 108b12a..fa9a278 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,7 +28,7 @@ matrix: - env: TARGET=thumbv7m-linux-eabi - env: TARGET=x86_64-apple-darwin os: osx -env: TARGET=x86_64-unknown-linux-gnu + - env: TARGET=x86_64-unknown-linux-gnu before_install: - test "$TRAVIS_OS_NAME" = "osx" || docker run --rm --privileged multiarch/qemu-user-static:register From 215fa06fb673b1e14e0a0500567026b21fbe735d Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 23 Jun 2017 06:53:09 -0700 Subject: [PATCH 10/48] More windows linkage guesses --- examples/intrinsics.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/intrinsics.rs b/examples/intrinsics.rs index 5336422..cabe917 100644 --- a/examples/intrinsics.rs +++ b/examples/intrinsics.rs @@ -418,6 +418,7 @@ pub fn _start() -> ! { #[cfg(windows)] #[link(name = "kernel32")] +#[link(name = "msvcrt")] extern {} // ARM targets need these symbols From 1b78d79bf544991a061e7e090d4d45231f929f0a Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 23 Jun 2017 06:58:05 -0700 Subject: [PATCH 11/48] Require the `compiler-builtins` target for the example --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 69b9c03..08bf5fd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,7 +29,7 @@ utest-macros = { git = "https://github.com/japaric/utest" } [[example]] name = "intrinsics" -required-features = ["c"] +required-features = ["c", "compiler-builtins"] [workspace] From f0e58591b36f31277d932d629e0a3b33def239c2 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 23 Jun 2017 07:01:18 -0700 Subject: [PATCH 12/48] Build the intrinsics example separately --- appveyor.yml | 4 ++-- ci/run.sh | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 2bef109..66cd96e 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -15,8 +15,8 @@ install: build: false test_script: - - cargo build --target %TARGET% - - cargo build --release --target %TARGET% + - cargo build --target %TARGET% --example intrinsics + - cargo build --release --target %TARGET% --example intrinsics - cargo test --no-default-features --features gen-tests --target %TARGET% - cargo test --no-default-features --features "gen-tests c" --target %TARGET% - cargo test --no-default-features --features gen-tests --release --target %TARGET% diff --git a/ci/run.sh b/ci/run.sh index 02b9bf1..0d52e73 100755 --- a/ci/run.sh +++ b/ci/run.sh @@ -46,7 +46,7 @@ case $1 in xargo build --features c --target $1 --example intrinsics ;; *) - cargo build --no-default-features --features c --target $1 --example intrinsics + cargo build --features c --target $1 --example intrinsics ;; esac From 8abb0ab826bc1083102300f08fcbf61e5c97f2db Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 23 Jun 2017 07:36:08 -0700 Subject: [PATCH 13/48] Build tweaks --- appveyor.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 66cd96e..2dce7ce 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -15,8 +15,10 @@ install: build: false test_script: - - cargo build --target %TARGET% --example intrinsics - - cargo build --release --target %TARGET% --example intrinsics + - cargo build --target %TARGET% + - cargo build --target %TARGET% --features c --example intrinsics + - cargo build --release --target %TARGET% + - cargo build --release --target %TARGET% --features c --example intrinsics - cargo test --no-default-features --features gen-tests --target %TARGET% - cargo test --no-default-features --features "gen-tests c" --target %TARGET% - cargo test --no-default-features --features gen-tests --release --target %TARGET% From 47ff81325cc07b6e57bd22cdb75ad9d29b615de6 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 23 Jun 2017 07:52:38 -0700 Subject: [PATCH 14/48] Use more intrinsics on msvc --- src/int/mul.rs | 2 +- src/int/sdiv.rs | 4 ++-- src/int/shift.rs | 6 +++--- src/int/udiv.rs | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/int/mul.rs b/src/int/mul.rs index 4b0831d..cb3fbd6 100644 --- a/src/int/mul.rs +++ b/src/int/mul.rs @@ -71,7 +71,7 @@ impl Mulo for i64 {} impl Mulo for i128 {} intrinsics! { - #[use_c_shim_if(target_arch = "x86")] + #[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))] pub extern "C" fn __muldi3(a: u64, b: u64) -> u64 { a.mul(b) } diff --git a/src/int/sdiv.rs b/src/int/sdiv.rs index 310eb9f..8d85630 100644 --- a/src/int/sdiv.rs +++ b/src/int/sdiv.rs @@ -64,7 +64,7 @@ intrinsics! { a.div(b) } - #[use_c_shim_if(target_arch = "x86")] + #[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))] pub extern "C" fn __divdi3(a: i64, b: i64) -> i64 { a.div(b) } @@ -79,7 +79,7 @@ intrinsics! { a.mod_(b) } - #[use_c_shim_if(target_arch = "x86")] + #[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))] pub extern "C" fn __moddi3(a: i64, b: i64) -> i64 { a.mod_(b) } diff --git a/src/int/shift.rs b/src/int/shift.rs index 746d02d..b0103ce 100644 --- a/src/int/shift.rs +++ b/src/int/shift.rs @@ -65,7 +65,7 @@ impl Lshr for u64 {} impl Lshr for u128 {} intrinsics! { - #[use_c_shim_if(target_arch = "x86")] + #[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))] pub extern "C" fn __ashldi3(a: u64, b: u32) -> u64 { a.ashl(b) } @@ -74,7 +74,7 @@ intrinsics! { a.ashl(b) } - #[use_c_shim_if(target_arch = "x86")] + #[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))] pub extern "C" fn __ashrdi3(a: i64, b: u32) -> i64 { a.ashr(b) } @@ -83,7 +83,7 @@ intrinsics! { a.ashr(b) } - #[use_c_shim_if(target_arch = "x86")] + #[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))] pub extern "C" fn __lshrdi3(a: u64, b: u32) -> u64 { a.lshr(b) } diff --git a/src/int/udiv.rs b/src/int/udiv.rs index 5ef46de..77e9c3f 100644 --- a/src/int/udiv.rs +++ b/src/int/udiv.rs @@ -227,13 +227,13 @@ intrinsics! { q } - #[use_c_shim_if(target_arch = "x86")] + #[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))] /// Returns `n / d` pub extern "C" fn __udivdi3(n: u64, d: u64) -> u64 { __udivmoddi4(n, d, None) } - #[use_c_shim_if(target_arch = "x86")] + #[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))] /// Returns `n % d` pub extern "C" fn __umoddi3(n: u64, d: u64) -> u64 { let mut rem = 0; From 4c41b5649f6848b95b662741c0650af2e5648666 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 23 Jun 2017 07:54:31 -0700 Subject: [PATCH 15/48] Fix use_c_shim_if --- src/macros.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index 70e44da..740eae2 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -15,7 +15,7 @@ macro_rules! intrinsics { $($rest:tt)* ) => ( - #[cfg(all(feature = "c", not($($cfg_clause)*)))] + #[cfg(all(feature = "c", $($cfg_clause)*))] $(#[$attr])* pub extern $abi fn $name( $($argname: $ty),* ) -> $ret { extern $abi { @@ -26,7 +26,7 @@ macro_rules! intrinsics { } } - #[cfg(not(all(feature = "c", not($($cfg_clause)*))))] + #[cfg(not(all(feature = "c", $($cfg_clause)*)))] intrinsics! { $(#[$attr])* pub extern $abi fn $name( $($argname: $ty),* ) -> $ret { From 4540bd43147f197b3855e3b006969aa361dbaa8a Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 23 Jun 2017 08:19:11 -0700 Subject: [PATCH 16/48] Fix travis intrinsics builds --- ci/run.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ci/run.sh b/ci/run.sh index 0d52e73..0f25a53 100755 --- a/ci/run.sh +++ b/ci/run.sh @@ -54,11 +54,11 @@ esac # TODO(#79) fix the undefined references problem for debug-assertions+lto case $1 in thumb*) - RUSTFLAGS="-C debug-assertions=no" xargo rustc --no-default-features --features c --target $1 --example intrinsics -- -C lto -C link-arg=-nostartfiles + RUSTFLAGS="-C debug-assertions=no" xargo rustc --features c --target $1 --example intrinsics -- -C lto -C link-arg=-nostartfiles xargo rustc --no-default-features --features c --target $1 --example intrinsics --release -- -C lto ;; *) - RUSTFLAGS="-C debug-assertions=no" cargo rustc --no-default-features --features c --target $1 --example intrinsics -- -C lto + RUSTFLAGS="-C debug-assertions=no" cargo rustc --features c --target $1 --example intrinsics -- -C lto cargo rustc --no-default-features --features c --target $1 --example intrinsics --release -- -C lto ;; esac From 0be8b20cb73cca80ba9113407c88844b68e07880 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 23 Jun 2017 08:35:24 -0700 Subject: [PATCH 17/48] Fix features needed for the intrinsics example --- ci/run.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ci/run.sh b/ci/run.sh index 0f25a53..8ac8845 100755 --- a/ci/run.sh +++ b/ci/run.sh @@ -55,11 +55,11 @@ esac case $1 in thumb*) RUSTFLAGS="-C debug-assertions=no" xargo rustc --features c --target $1 --example intrinsics -- -C lto -C link-arg=-nostartfiles - xargo rustc --no-default-features --features c --target $1 --example intrinsics --release -- -C lto + xargo rustc --features c --target $1 --example intrinsics --release -- -C lto ;; *) RUSTFLAGS="-C debug-assertions=no" cargo rustc --features c --target $1 --example intrinsics -- -C lto - cargo rustc --no-default-features --features c --target $1 --example intrinsics --release -- -C lto + cargo rustc --features c --target $1 --example intrinsics --release -- -C lto ;; esac From 696b821bb7185ac9ae40067f7e0e4dd5b7e7aa29 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 23 Jun 2017 09:59:49 -0700 Subject: [PATCH 18/48] Enable `mem` for intrinsics on linux --- ci/run.sh | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ci/run.sh b/ci/run.sh index 8ac8845..6977455 100755 --- a/ci/run.sh +++ b/ci/run.sh @@ -43,10 +43,10 @@ esac # Verify that we haven't drop any intrinsic/symbol case $1 in thumb*) - xargo build --features c --target $1 --example intrinsics + xargo build --features 'c mem' --target $1 --example intrinsics ;; *) - cargo build --features c --target $1 --example intrinsics + cargo build --features 'c mem' --target $1 --example intrinsics ;; esac @@ -54,12 +54,12 @@ esac # TODO(#79) fix the undefined references problem for debug-assertions+lto case $1 in thumb*) - RUSTFLAGS="-C debug-assertions=no" xargo rustc --features c --target $1 --example intrinsics -- -C lto -C link-arg=-nostartfiles - xargo rustc --features c --target $1 --example intrinsics --release -- -C lto + RUSTFLAGS="-C debug-assertions=no" xargo rustc --features 'c mem' --target $1 --example intrinsics -- -C lto -C link-arg=-nostartfiles + xargo rustc --features 'c mem' --target $1 --example intrinsics --release -- -C lto ;; *) - RUSTFLAGS="-C debug-assertions=no" cargo rustc --features c --target $1 --example intrinsics -- -C lto - cargo rustc --features c --target $1 --example intrinsics --release -- -C lto + RUSTFLAGS="-C debug-assertions=no" cargo rustc --features 'c mem' --target $1 --example intrinsics -- -C lto + cargo rustc --features 'c mem' --target $1 --example intrinsics --release -- -C lto ;; esac From 560018cc839a38864fe628d20850dd2eb5e4ddca Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 23 Jun 2017 10:44:29 -0700 Subject: [PATCH 19/48] Less verbose output on symbol check --- ci/run.sh | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/ci/run.sh b/ci/run.sh index 6977455..e5c8924 100755 --- a/ci/run.sh +++ b/ci/run.sh @@ -77,7 +77,7 @@ case $1 in ;; esac -case $TRAVIS_OS_NAME in +case "$TRAVIS_OS_NAME" in osx) # NOTE OSx's nm doesn't accept the `--defined-only` or provide an equivalent. # Use GNU nm instead @@ -89,14 +89,15 @@ case $TRAVIS_OS_NAME in ;; esac -if [ $TRAVIS_OS_NAME = osx ]; then +if [ "$TRAVIS_OS_NAME" = osx ]; then path=target/${1}/debug/deps/libcompiler_builtins-*.rlib else path=/target/${1}/debug/deps/libcompiler_builtins-*.rlib fi for rlib in $(echo $path); do - stdout=$($PREFIX$NM -g --defined-only $rlib) + set +x + stdout=$($PREFIX$NM -g --defined-only $rlib 2>&1) # NOTE On i586, It's normal that the get_pc_thunk symbol appears several times so ignore it set +e @@ -105,6 +106,7 @@ for rlib in $(echo $path); do if test $? = 0; then exit 1 fi + set -ex done true From 93fed264c135c6d0828e7b399ee26785841c59e1 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 23 Jun 2017 10:44:59 -0700 Subject: [PATCH 20/48] Remove executable bit from conv.rs --- src/float/conv.rs | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 src/float/conv.rs diff --git a/src/float/conv.rs b/src/float/conv.rs old mode 100755 new mode 100644 From 83d63eaa9b649e8d344458ab4ef9d503d3bfee20 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 23 Jun 2017 11:05:25 -0700 Subject: [PATCH 21/48] Convert float intrinsics to the `intrinsics!` macro --- src/float/add.rs | 342 +++++++++++++++++++++++----------------------- src/float/conv.rs | 238 +++++++++++++++++++------------- src/float/pow.rs | 52 +++---- src/float/sub.rs | 23 +--- src/int/udiv.rs | 22 ++- src/lib.rs | 46 ------- 6 files changed, 358 insertions(+), 365 deletions(-) diff --git a/src/float/add.rs b/src/float/add.rs index 5024a72..1b1ac3f 100644 --- a/src/float/add.rs +++ b/src/float/add.rs @@ -3,192 +3,190 @@ use core::num::Wrapping; use float::Float; +/// Returns `a + b` 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 { - let one = Wrapping(1 as <$ty as Float>::Int); - let zero = Wrapping(0 as <$ty as Float>::Int); + ($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); - let bits = Wrapping(<$ty>::bits() as <$ty as Float>::Int); - let significand_bits = Wrapping(<$ty>::significand_bits() as <$ty as Float>::Int); - let exponent_bits = bits - significand_bits - one; - let max_exponent = (one << exponent_bits.0 as usize) - one; + let bits = Wrapping(<$ty>::bits() as <$ty as Float>::Int); + let significand_bits = Wrapping(<$ty>::significand_bits() as <$ty as Float>::Int); + let exponent_bits = bits - significand_bits - one; + let max_exponent = (one << exponent_bits.0 as usize) - one; - let implicit_bit = one << significand_bits.0 as usize; - let significand_mask = implicit_bit - one; - let sign_bit = one << (significand_bits + exponent_bits).0 as usize; - let abs_mask = sign_bit - one; - let exponent_mask = abs_mask ^ significand_mask; - let inf_rep = exponent_mask; - let quiet_bit = implicit_bit >> 1; - let qnan_rep = exponent_mask | quiet_bit; + let implicit_bit = one << significand_bits.0 as usize; + let significand_mask = implicit_bit - one; + let sign_bit = one << (significand_bits + exponent_bits).0 as usize; + let abs_mask = sign_bit - one; + let exponent_mask = abs_mask ^ significand_mask; + let inf_rep = exponent_mask; + let quiet_bit = implicit_bit >> 1; + let qnan_rep = exponent_mask | quiet_bit; - let mut a_rep = Wrapping(a.repr()); - let mut b_rep = Wrapping(b.repr()); - let a_abs = a_rep & abs_mask; - let b_abs = b_rep & abs_mask; + let mut a_rep = Wrapping(a.repr()); + let mut b_rep = Wrapping(b.repr()); + let a_abs = a_rep & abs_mask; + let b_abs = b_rep & abs_mask; - // Detect if a or b is zero, infinity, or NaN. - if a_abs - one >= inf_rep - one || - 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)); - } - // anything + NaN = qNaN - if b_abs > inf_rep { - return (<$ty as Float>::from_repr((b_abs | quiet_bit).0)); + // Detect if a or b is zero, infinity, or NaN. + if a_abs - one >= inf_rep - one || + 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); + } + // anything + NaN = qNaN + if b_abs > inf_rep { + 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); + } else { + // +/-infinity + anything remaining = +/- infinity + return a; } + } - 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)); - } else { - // +/-infinity + anything remaining = +/- infinity - return a; - } - } + // anything remaining + +/-infinity = +/-infinity + if b_abs == inf_rep { + return b; + } - // anything remaining + +/-infinity = +/-infinity - if b_abs == inf_rep { + // zero + anything = anything + 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()); + } else { return b; } - - // zero + anything = anything - 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())); - } else { - return b; - } - } - - // anything + zero = anything - if b_abs.0 == 0 { - return a; - } } - // Swap a and b if necessary so that a has the larger absolute value. - if b_abs > a_abs { - mem::swap(&mut a_rep, &mut b_rep); + // anything + zero = anything + if b_abs.0 == 0 { + return a; } - - // Extract the exponent and significand from the (possibly swapped) a and b. - let mut a_exponent = Wrapping((a_rep >> significand_bits.0 as usize & max_exponent).0 as i32); - let mut b_exponent = Wrapping((b_rep >> significand_bits.0 as usize & max_exponent).0 as i32); - let mut a_significand = a_rep & significand_mask; - let mut b_significand = b_rep & significand_mask; - - // normalize any denormals, and adjust the exponent accordingly. - if a_exponent.0 == 0 { - let (exponent, significand) = <$ty>::normalize(a_significand.0); - a_exponent = Wrapping(exponent); - a_significand = Wrapping(significand); - } - if b_exponent.0 == 0 { - let (exponent, significand) = <$ty>::normalize(b_significand.0); - b_exponent = Wrapping(exponent); - b_significand = Wrapping(significand); - } - - // The sign of the result is the sign of the larger operand, a. If they - // have opposite signs, we are performing a subtraction; otherwise addition. - let result_sign = a_rep & sign_bit; - let subtraction = ((a_rep ^ b_rep) & sign_bit) != zero; - - // Shift the significands to give us round, guard and sticky, and or in the - // implicit significand bit. (If we fell through from the denormal path it - // was already set by normalize(), but setting it twice won't hurt - // anything.) - a_significand = (a_significand | implicit_bit) << 3; - b_significand = (b_significand | implicit_bit) << 3; - - // Shift the significand of b by the difference in exponents, with a sticky - // bottom bit to get rounding correct. - let align = Wrapping((a_exponent - b_exponent).0 as <$ty as Float>::Int); - if align.0 != 0 { - if align < bits { - let sticky = ((b_significand << (bits - align).0 as usize).0 != 0) as <$ty as Float>::Int; - b_significand = (b_significand >> align.0 as usize) | Wrapping(sticky); - } else { - b_significand = one; // sticky; b is known to be non-zero. - } - } - if subtraction { - a_significand -= b_significand; - // If a == -b, return +zero. - if a_significand.0 == 0 { - return (<$ty as Float>::from_repr(0)); - } - - // If partial cancellation occured, we need to left-shift the result - // and adjust the exponent: - if a_significand < implicit_bit << 3 { - let shift = a_significand.0.leading_zeros() as i32 - - (implicit_bit << 3).0.leading_zeros() as i32; - a_significand <<= shift as usize; - a_exponent -= Wrapping(shift); - } - } else /* addition */ { - a_significand += b_significand; - - // If the addition carried up, we need to right-shift the result and - // adjust the exponent: - if (a_significand & implicit_bit << 4).0 != 0 { - let sticky = ((a_significand & one).0 != 0) as <$ty as Float>::Int; - a_significand = a_significand >> 1 | Wrapping(sticky); - a_exponent += Wrapping(1); - } - } - - // 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)); - } - - if a_exponent.0 <= 0 { - // Result is denormal before rounding; the exponent is zero and we - // need to shift the significand. - let shift = Wrapping((Wrapping(1) - a_exponent).0 as <$ty as Float>::Int); - let sticky = ((a_significand << (bits - shift).0 as usize).0 != 0) as <$ty as Float>::Int; - a_significand = a_significand >> shift.0 as usize | Wrapping(sticky); - a_exponent = Wrapping(0); - } - - // Low three bits are round, guard, and sticky. - let round_guard_sticky: i32 = (a_significand.0 & 0x7) as i32; - - // Shift the significand into place, and mask off the implicit bit. - let mut result = a_significand >> 3 & significand_mask; - - // Insert the exponent and sign. - result |= Wrapping(a_exponent.0 as <$ty as Float>::Int) << significand_bits.0 as usize; - result |= result_sign; - - // Final rounding. The result may overflow to infinity, but that is the - // correct result in that case. - if round_guard_sticky > 0x4 { result += one; } - if round_guard_sticky == 0x4 { result += result & one; } - - <$ty>::from_repr(result.0) } - } + + // Swap a and b if necessary so that a has the larger absolute value. + if b_abs > a_abs { + mem::swap(&mut a_rep, &mut b_rep); + } + + // Extract the exponent and significand from the (possibly swapped) a and b. + let mut a_exponent = Wrapping((a_rep >> significand_bits.0 as usize & max_exponent).0 as i32); + let mut b_exponent = Wrapping((b_rep >> significand_bits.0 as usize & max_exponent).0 as i32); + let mut a_significand = a_rep & significand_mask; + let mut b_significand = b_rep & significand_mask; + + // normalize any denormals, and adjust the exponent accordingly. + if a_exponent.0 == 0 { + let (exponent, significand) = <$ty>::normalize(a_significand.0); + a_exponent = Wrapping(exponent); + a_significand = Wrapping(significand); + } + if b_exponent.0 == 0 { + let (exponent, significand) = <$ty>::normalize(b_significand.0); + b_exponent = Wrapping(exponent); + b_significand = Wrapping(significand); + } + + // The sign of the result is the sign of the larger operand, a. If they + // have opposite signs, we are performing a subtraction; otherwise addition. + let result_sign = a_rep & sign_bit; + let subtraction = ((a_rep ^ b_rep) & sign_bit) != zero; + + // Shift the significands to give us round, guard and sticky, and or in the + // implicit significand bit. (If we fell through from the denormal path it + // was already set by normalize(), but setting it twice won't hurt + // anything.) + a_significand = (a_significand | implicit_bit) << 3; + b_significand = (b_significand | implicit_bit) << 3; + + // Shift the significand of b by the difference in exponents, with a sticky + // bottom bit to get rounding correct. + let align = Wrapping((a_exponent - b_exponent).0 as <$ty as Float>::Int); + if align.0 != 0 { + if align < bits { + let sticky = ((b_significand << (bits - align).0 as usize).0 != 0) as <$ty as Float>::Int; + b_significand = (b_significand >> align.0 as usize) | Wrapping(sticky); + } else { + b_significand = one; // sticky; b is known to be non-zero. + } + } + if subtraction { + a_significand -= b_significand; + // If a == -b, return +zero. + if a_significand.0 == 0 { + return <$ty as Float>::from_repr(0); + } + + // If partial cancellation occured, we need to left-shift the result + // and adjust the exponent: + if a_significand < implicit_bit << 3 { + let shift = a_significand.0.leading_zeros() as i32 + - (implicit_bit << 3).0.leading_zeros() as i32; + a_significand <<= shift as usize; + a_exponent -= Wrapping(shift); + } + } else /* addition */ { + a_significand += b_significand; + + // If the addition carried up, we need to right-shift the result and + // adjust the exponent: + if (a_significand & implicit_bit << 4).0 != 0 { + let sticky = ((a_significand & one).0 != 0) as <$ty as Float>::Int; + a_significand = a_significand >> 1 | Wrapping(sticky); + a_exponent += Wrapping(1); + } + } + + // 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); + } + + if a_exponent.0 <= 0 { + // Result is denormal before rounding; the exponent is zero and we + // need to shift the significand. + let shift = Wrapping((Wrapping(1) - a_exponent).0 as <$ty as Float>::Int); + let sticky = ((a_significand << (bits - shift).0 as usize).0 != 0) as <$ty as Float>::Int; + a_significand = a_significand >> shift.0 as usize | Wrapping(sticky); + a_exponent = Wrapping(0); + } + + // Low three bits are round, guard, and sticky. + let round_guard_sticky: i32 = (a_significand.0 & 0x7) as i32; + + // Shift the significand into place, and mask off the implicit bit. + let mut result = a_significand >> 3 & significand_mask; + + // Insert the exponent and sign. + result |= Wrapping(a_exponent.0 as <$ty as Float>::Int) << significand_bits.0 as usize; + result |= result_sign; + + // Final rounding. The result may overflow to infinity, but that is the + // correct result in that case. + if round_guard_sticky > 0x4 { result += one; } + 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) + } +} diff --git a/src/float/conv.rs b/src/float/conv.rs index 8905f12..edf9ee6 100644 --- a/src/float/conv.rs +++ b/src/float/conv.rs @@ -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,79 +122,106 @@ 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 { - let fixint_min = <$ity>::min_value(); - let fixint_max = <$ity>::max_value(); - let fixint_bits = <$ity>::bits() as usize; - let fixint_unsigned = fixint_min == 0; + ($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; + let fixint_unsigned = fixint_min == 0; - let sign_bit = <$fty>::sign_mask(); - let significand_bits = <$fty>::significand_bits() as usize; - let exponent_bias = <$fty>::exponent_bias() as usize; - //let exponent_max = <$fty>::exponent_max() as usize; + let sign_bit = <$fty>::sign_mask(); + let significand_bits = <$fty>::significand_bits() as usize; + let exponent_bias = <$fty>::exponent_bias() as usize; + //let exponent_max = <$fty>::exponent_max() as usize; - // Break a into sign, exponent, significand - let a_rep = <$fty>::repr(f); - let a_abs = a_rep & !sign_bit; + // Break a into sign, exponent, significand + let a_rep = <$fty>::repr(f); + let a_abs = a_rep & !sign_bit; - // this is used to work around -1 not being available for unsigned - let sign = if (a_rep & sign_bit) == 0 { Sign::Positive } else { Sign::Negative }; - let mut exponent = (a_abs >> significand_bits) as usize; - let significand = (a_abs & <$fty>::significand_mask()) | <$fty>::implicit_bit(); + // this is used to work around -1 not being available for unsigned + let sign = if (a_rep & sign_bit) == 0 { Sign::Positive } else { Sign::Negative }; + let mut exponent = (a_abs >> significand_bits) as usize; + let significand = (a_abs & <$fty>::significand_mask()) | <$fty>::implicit_bit(); - // if < 1 or unsigned & negative - if exponent < exponent_bias || - fixint_unsigned && sign == Sign::Negative { - return 0 - } - exponent -= exponent_bias; - - // If the value is infinity, saturate. - // If the value is too large for the integer type, 0. - if exponent >= (if fixint_unsigned {fixint_bits} else {fixint_bits -1}) { - return if sign == Sign::Positive {fixint_max} else {fixint_min} - } - // If 0 <= exponent < significand_bits, right shift to get the result. - // Otherwise, shift left. - // (sign - 1) will never overflow as negative signs are already returned as 0 for unsigned - let r = if exponent < significand_bits { - (significand >> (significand_bits - exponent)) as $ity - } else { - (significand as $ity) << (exponent - significand_bits) - }; - - if sign == Sign::Negative { - (!r).wrapping_add(1) - } else { - r - } + // if < 1 or unsigned & negative + if exponent < exponent_bias || + fixint_unsigned && sign == Sign::Negative { + return 0 } + exponent -= exponent_bias; + + // If the value is infinity, saturate. + // If the value is too large for the integer type, 0. + if exponent >= (if fixint_unsigned {fixint_bits} else {fixint_bits -1}) { + return if sign == Sign::Positive {fixint_max} else {fixint_min} + } + // If 0 <= exponent < significand_bits, right shift to get the result. + // Otherwise, shift left. + // (sign - 1) will never overflow as negative signs are already returned as 0 for unsigned + let r = if exponent < significand_bits { + (significand >> (significand_bits - exponent)) as $ity + } else { + (significand as $ity) << (exponent - significand_bits) + }; + + if sign == Sign::Negative { + (!r).wrapping_add(1) + } else { + r + } + }) +} + +intrinsics! { + pub extern "C" fn __fixsfsi(f: f32) -> i32 { + float_to_int!(f, f32, i32) + } + + pub extern "C" fn __fixsfdi(f: f32) -> i64 { + float_to_int!(f, f32, i64) + } + + #[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) } } - -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"); - }; -} - -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); - -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); diff --git a/src/float/pow.rs b/src/float/pow.rs index 5e28d95..f5c5757 100644 --- a/src/float/pow.rs +++ b/src/float/pow.rs @@ -1,30 +1,34 @@ +/// Returns `a` raised to the power `b` 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); - let recip = b < 0; - let mut r: $fty = 1.0; - loop { - if (b & 1) != 0 { - r *= a; - } - b = sdiv!($ity, b, 2); - if b == 0 { - break; - } - a *= a; + ($a: expr, $b: expr) => ({ + let (mut a, mut b) = ($a, $b); + let recip = b < 0; + let mut r = 1.0; + loop { + if (b & 1) != 0 { + r *= a; } - - if recip { - 1.0 / r - } else { - r + b = b.checked_div(2).unwrap_or_else(|| ::abort()); + if b == 0 { + break; } + a *= a; } - } + + if recip { + 1.0 / r + } 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) + } +} diff --git a/src/float/sub.rs b/src/float/sub.rs index a4fd884..a82a943 100644 --- a/src/float/sub.rs +++ b/src/float/sub.rs @@ -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()) + } + + pub extern "C" fn __subdf3(a: f64, b: f64) -> f64 { + a + f64::from_repr(b.repr() ^ f64::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); diff --git a/src/int/udiv.rs b/src/int/udiv.rs index 77e9c3f..90ac729 100644 --- a/src/int/udiv.rs +++ b/src/int/udiv.rs @@ -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 { diff --git a/src/lib.rs b/src/lib.rs index 6d44fb1..3fa7923 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; From ac3715de0f35ab75c7816ce0d6bad26b68369f2f Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 23 Jun 2017 11:13:31 -0700 Subject: [PATCH 22/48] Remove defined intrinsics from build.rs No need to load these from C! --- build.rs | 32 -------------------------------- 1 file changed, 32 deletions(-) diff --git a/build.rs b/build.rs index c8179fe..171622c 100644 --- a/build.rs +++ b/build.rs @@ -4066,28 +4066,8 @@ mod c { "divxc3.c", "extendsfdf2.c", "extendhfsf2.c", - "ffsdi2.c", - "fixdfdi.c", - "fixdfsi.c", - "fixsfdi.c", - "fixsfsi.c", - "fixunsdfdi.c", - "fixunsdfsi.c", - "fixunssfdi.c", - "fixunssfsi.c", - "fixunsxfdi.c", - "fixunsxfsi.c", - "fixxfdi.c", - "floatdidf.c", "floatdisf.c", - "floatdixf.c", - "floatsidf.c", - "floatsisf.c", - "floatundidf.c", "floatundisf.c", - "floatundixf.c", - "floatunsidf.c", - "floatunsisf.c", "int_util.c", "muldc3.c", "muldf3.c", @@ -4124,18 +4104,6 @@ mod c { "cmpti2.c", "ctzti2.c", "ffsti2.c", - "fixdfti.c", - "fixsfti.c", - "fixunsdfti.c", - "fixunssfti.c", - "fixunsxfti.c", - "fixxfti.c", - "floattidf.c", - "floattisf.c", - "floattixf.c", - "floatuntidf.c", - "floatuntisf.c", - "floatuntixf.c", "mulvti3.c", "negti2.c", "negvti2.c", From 7c4745061a28e1182d8a12a2618d298beefb983b Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 23 Jun 2017 11:21:15 -0700 Subject: [PATCH 23/48] Remove executable bit on arm.rs --- src/arm.rs | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 src/arm.rs diff --git a/src/arm.rs b/src/arm.rs old mode 100755 new mode 100644 From 5a444d58f26a61231e5271be86226427c999226b Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 23 Jun 2017 11:21:36 -0700 Subject: [PATCH 24/48] Remove unused rust file --- src/qc.rs | 311 ------------------------------------------------------ 1 file changed, 311 deletions(-) delete mode 100644 src/qc.rs diff --git a/src/qc.rs b/src/qc.rs deleted file mode 100644 index 675bcc6..0000000 --- a/src/qc.rs +++ /dev/null @@ -1,311 +0,0 @@ -// When testing functions, QuickCheck (QC) uses small values for integer (`u*`/`i*`) arguments -// (~ `[-100, 100]`), but these values don't stress all the code paths in our intrinsics. Here we -// create newtypes over the primitive integer types with the goal of having full control over the -// random values that will be used to test our intrinsics. - -use std::boxed::Box; -use std::fmt; -use core::{f32, f64}; - -use quickcheck::{Arbitrary, Gen}; - -use int::LargeInt; -use float::Float; - -// Generates values in the full range of the integer type -macro_rules! arbitrary { - ($TY:ident : $ty:ident) => { - #[derive(Clone, Copy, PartialEq)] - pub struct $TY(pub $ty); - - impl Arbitrary for $TY { - fn arbitrary(g: &mut G) -> $TY - where G: Gen - { - // NOTE Generate edge cases with a 10% chance - let t = if g.gen_weighted_bool(10) { - *g.choose(&[ - $ty::min_value(), - 0, - $ty::max_value(), - ]).unwrap() - } else { - g.gen() - }; - - $TY(t) - } - - fn shrink(&self) -> Box> { - struct Shrinker { - x: $ty, - } - - impl Iterator for Shrinker { - type Item = $TY; - - fn next(&mut self) -> Option<$TY> { - self.x /= 2; - if self.x == 0 { - None - } else { - Some($TY(self.x)) - } - } - } - - if self.0 == 0 { - ::quickcheck::empty_shrinker() - } else { - Box::new(Shrinker { x: self.0 }) - } - } - } - - impl fmt::Debug for $TY { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - fmt::Debug::fmt(&self.0, f) - } - } - } -} - -arbitrary!(I32: i32); -arbitrary!(U32: u32); - -// These integers are "too large". If we generate e.g. `u64` values in the full range then there's -// only `1 / 2^32` chance of seeing a value smaller than `2^32` (i.e. whose higher "word" (32-bits) -// is `0`)! But this is an important group of values to tests because we have special code paths for -// them. Instead we'll generate e.g. `u64` integers this way: uniformly pick between (a) setting the -// low word to 0 and generating a random high word, (b) vice versa: high word to 0 and random low -// word or (c) generate both words randomly. This let's cover better the code paths in our -// intrinsics. -macro_rules! arbitrary_large { - ($TY:ident : $ty:ident) => { - #[derive(Clone, Copy, PartialEq)] - pub struct $TY(pub $ty); - - impl Arbitrary for $TY { - fn arbitrary(g: &mut G) -> $TY - where G: Gen - { - // NOTE Generate edge cases with a 10% chance - let t = if g.gen_weighted_bool(10) { - *g.choose(&[ - $ty::min_value(), - 0, - $ty::max_value(), - ]).unwrap() - } else { - match g.gen_range(0, 3) { - 0 => $ty::from_parts(g.gen(), g.gen()), - 1 => $ty::from_parts(0, g.gen()), - 2 => $ty::from_parts(g.gen(), 0), - _ => unreachable!(), - } - }; - - $TY(t) - } - - fn shrink(&self) -> Box> { - struct Shrinker { - x: $ty, - } - - impl Iterator for Shrinker { - type Item = $TY; - - fn next(&mut self) -> Option<$TY> { - self.x /= 2; - if self.x == 0 { - None - } else { - Some($TY(self.x)) - } - } - } - - if self.0 == 0 { - ::quickcheck::empty_shrinker() - } else { - Box::new(Shrinker { x: self.0 }) - } - } - } - - impl fmt::Debug for $TY { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - fmt::Debug::fmt(&self.0, f) - } - } - } -} - -arbitrary_large!(I64: i64); -arbitrary_large!(U64: u64); -arbitrary_large!(I128: i128); -arbitrary_large!(U128: u128); - -macro_rules! arbitrary_float { - ($TY:ident : $ty:ident) => { - #[derive(Clone, Copy)] - pub struct $TY(pub $ty); - - impl Arbitrary for $TY { - fn arbitrary(g: &mut G) -> $TY - where G: Gen - { - let special = [ - -0.0, 0.0, $ty::NAN, $ty::INFINITY, -$ty::INFINITY - ]; - - if g.gen_weighted_bool(10) { // Random special case - $TY(*g.choose(&special).unwrap()) - } else if g.gen_weighted_bool(10) { // NaN variants - let sign: bool = g.gen(); - let exponent: <$ty as Float>::Int = g.gen(); - let significand: <$ty as Float>::Int = 0; - $TY($ty::from_parts(sign, exponent, significand)) - } else if g.gen() { // Denormalized - let sign: bool = g.gen(); - let exponent: <$ty as Float>::Int = 0; - let significand: <$ty as Float>::Int = g.gen(); - $TY($ty::from_parts(sign, exponent, significand)) - } else { // Random anything - let sign: bool = g.gen(); - let exponent: <$ty as Float>::Int = g.gen(); - let significand: <$ty as Float>::Int = g.gen(); - $TY($ty::from_parts(sign, exponent, significand)) - } - } - - fn shrink(&self) -> Box> { - ::quickcheck::empty_shrinker() - } - } - - impl fmt::Debug for $TY { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - fmt::Debug::fmt(&self.0, f) - } - } - - impl PartialEq for $TY { - fn eq(&self, other: &$TY) -> bool { - self.0.eq_repr(other.0) - } - } - } -} - -arbitrary_float!(F32: f32); -arbitrary_float!(F64: f64); - -// Convenience macro to test intrinsics against their reference implementations. -// -// Each intrinsic is tested against both the `gcc_s` library as well as -// `compiler-rt`. These libraries are defined in the `gcc_s` crate as well as -// the `compiler-rt` crate in this repository. Both load a dynamic library and -// lookup symbols through that dynamic library to ensure that we're using the -// right intrinsic. -// -// This macro hopefully allows you to define a bare minimum of how to test an -// intrinsic without worrying about these implementation details. A sample -// invocation looks like: -// -// -// check! { -// // First argument is the function we're testing (either from this lib -// // or a dynamically loaded one. Further arguments are all generated by -// // quickcheck. -// fn __my_intrinsic(f: extern fn(i32) -> i32, -// a: I32) -// -> Option<(i32, i64)> { -// -// // Discard tests by returning Some -// if a.0 == 0 { -// return None -// } -// -// // Return the result via `Some` if the test can run -// let mut other_result = 0; -// let result = f(a.0, &mut other_result); -// Some((result, other_result)) -// } -// } -// -// If anything returns `None` then the test is discarded, otherwise the two -// results are compared for equality and the test fails if this equality check -// fails. -macro_rules! check { - ($( - $(#[$cfg:meta])* - fn $name:ident($f:ident: extern $abi:tt fn($($farg:ty),*) -> $fret:ty, - $($arg:ident: $t:ty),*) - -> Option<$ret:ty> - { - $($code:tt)* - } - )*) => ( - $( - $(#[$cfg])* - fn $name($f: extern $abi fn($($farg),*) -> $fret, - $($arg: $t),*) -> Option<$ret> { - $($code)* - } - )* - - mod _test { - use qc::*; - use std::mem; - use quickcheck::TestResult; - - $( - $(#[$cfg])* - #[test] - fn $name() { - fn my_check($($arg:$t),*) -> TestResult { - let my_answer = super::$name(super::super::$name, - $($arg),*); - let compiler_rt_fn = ::compiler_rt::get(stringify!($name)); - let compiler_rt_answer = unsafe { - super::$name(mem::transmute(compiler_rt_fn), - $($arg),*) - }; - let gcc_s_answer = - match ::gcc_s::get(stringify!($name)) { - Some(f) => unsafe { - Some(super::$name(mem::transmute(f), - $($arg),*)) - }, - None => None, - }; - - let print_values = || { - print!("{} - Args: ", stringify!($name)); - $(print!("{:?} ", $arg);)* - print!("\n"); - println!(" compiler-builtins: {:?}", my_answer); - println!(" compiler_rt: {:?}", compiler_rt_answer); - println!(" gcc_s: {:?}", gcc_s_answer); - }; - - if my_answer != compiler_rt_answer { - print_values(); - TestResult::from_bool(false) - } else if gcc_s_answer.is_some() && - my_answer != gcc_s_answer.unwrap() { - print_values(); - TestResult::from_bool(false) - } else { - TestResult::from_bool(true) - } - } - - ::quickcheck::quickcheck(my_check as fn($($t),*) -> TestResult) - } - )* - } - ) -} From a16ebb0dcb902a696d7c9a5432cce7482b9ce886 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 23 Jun 2017 11:52:22 -0700 Subject: [PATCH 25/48] Use `nm` to weed out panics --- ci/run.sh | 13 ++++++++++++- src/float/conv.rs | 2 ++ src/int/mul.rs | 4 ++-- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/ci/run.sh b/ci/run.sh index e5c8924..3316a43 100755 --- a/ci/run.sh +++ b/ci/run.sh @@ -63,7 +63,6 @@ case $1 in ;; esac -# Look out for duplicated symbols when we include the compiler-rt (C) implementation PREFIX=$(echo $1 | sed -e 's/unknown-//')- case $1 in armv7-*) @@ -95,6 +94,7 @@ else path=/target/${1}/debug/deps/libcompiler_builtins-*.rlib fi +# Look out for duplicated symbols when we include the compiler-rt (C) implementation for rlib in $(echo $path); do set +x stdout=$($PREFIX$NM -g --defined-only $rlib 2>&1) @@ -109,4 +109,15 @@ for rlib in $(echo $path); do set -ex done +# Ensure no references to a panicking function +for rlib in $(echo $path); do + set +x + $PREFIX$NM -u $rlib 2>&1 | grep panicking + + if test $? = 0; then + exit 1 + fi + set -ex +done + true diff --git a/src/float/conv.rs b/src/float/conv.rs index edf9ee6..053c681 100644 --- a/src/float/conv.rs +++ b/src/float/conv.rs @@ -78,6 +78,7 @@ intrinsics! { int_to_float!(i, i32, f64) } + #[use_c_shim_if(any(target_arch = "x86", target_arch = "x86_64"))] pub extern "C" fn __floatdidf(i: i64) -> f64 { int_to_float!(i, i64, f64) } @@ -100,6 +101,7 @@ intrinsics! { int_to_float!(i, u32, f64) } + #[use_c_shim_if(any(target_arch = "x86", target_arch = "x86_64"))] pub extern "C" fn __floatundidf(i: u64) -> f64 { int_to_float!(i, u64, f64) } diff --git a/src/int/mul.rs b/src/int/mul.rs index cb3fbd6..9241ee6 100644 --- a/src/int/mul.rs +++ b/src/int/mul.rs @@ -54,11 +54,11 @@ trait Mulo: Int + ops::Neg { return result; } if sa == sb { - if abs_a > Self::max_value() / abs_b { + if abs_a > Self::max_value().checked_div(abs_b).unwrap_or_else(|| ::abort()) { *overflow = 1; } } else { - if abs_a > Self::min_value() / -abs_b { + if abs_a > Self::min_value().checked_div(-abs_b).unwrap_or_else(|| ::abort()) { *overflow = 1; } } From 5172f8c218a4d8af54cdf776d917235fffff293e Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 23 Jun 2017 12:02:28 -0700 Subject: [PATCH 26/48] Tweak usage of C shims on MSVC --- src/float/conv.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/float/conv.rs b/src/float/conv.rs index 053c681..ca95b11 100644 --- a/src/float/conv.rs +++ b/src/float/conv.rs @@ -78,7 +78,10 @@ intrinsics! { int_to_float!(i, i32, f64) } - #[use_c_shim_if(any(target_arch = "x86", target_arch = "x86_64"))] + #[use_c_shim_if(any( + all(not(target_env = "msvc"), target_arch = "x86", target_arch = "x86_64"), + all(target_env = "msvc", target_arch = "x86_64"), + ))] pub extern "C" fn __floatdidf(i: i64) -> f64 { int_to_float!(i, i64, f64) } @@ -101,7 +104,8 @@ intrinsics! { int_to_float!(i, u32, f64) } - #[use_c_shim_if(any(target_arch = "x86", target_arch = "x86_64"))] + #[use_c_shim_if(all(any(target_arch = "x86", target_arch = "x86_64"), + not(target_env = "msvc")))] pub extern "C" fn __floatundidf(i: u64) -> f64 { int_to_float!(i, u64, f64) } From a839d53a023e5a2d740ddb58960f685fc9ccde90 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 23 Jun 2017 13:28:24 -0700 Subject: [PATCH 27/48] Deal with floatdidf on x86_64 Apparently LLVM will lower this down to just an instruction --- build.rs | 2 -- src/float/conv.rs | 13 ++++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/build.rs b/build.rs index 171622c..e66b3ab 100644 --- a/build.rs +++ b/build.rs @@ -4132,7 +4132,6 @@ mod c { if target_arch == "x86_64" { sources.extend( &[ - "x86_64/floatdidf.c", "x86_64/floatdisf.c", "x86_64/floatdixf.c", ], @@ -4148,7 +4147,6 @@ mod c { &[ "x86_64/chkstk.S", "x86_64/chkstk2.S", - "x86_64/floatdidf.c", "x86_64/floatdisf.c", "x86_64/floatdixf.c", "x86_64/floatundidf.S", diff --git a/src/float/conv.rs b/src/float/conv.rs index ca95b11..ca05c28 100644 --- a/src/float/conv.rs +++ b/src/float/conv.rs @@ -78,12 +78,15 @@ intrinsics! { int_to_float!(i, i32, f64) } - #[use_c_shim_if(any( - all(not(target_env = "msvc"), target_arch = "x86", target_arch = "x86_64"), - all(target_env = "msvc", target_arch = "x86_64"), - ))] + #[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))] pub extern "C" fn __floatdidf(i: i64) -> f64 { - int_to_float!(i, i64, f64) + // On x86_64 LLVM will use native instructions for this conversion, we + // can just do it directly + if cfg!(target_arch = "x86_64") { + i as f64 + } else { + int_to_float!(i, i64, f64) + } } #[unadjusted_on_win64] From 7de57cd4f99fd7a0da0a911f9f9cc79f1d06b909 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 23 Jun 2017 15:16:07 -0700 Subject: [PATCH 28/48] Handle aeabi aliasing Objects in compiler-rt may have two symbols, so this makes sure that we don't bring in those objects by accident by defining the aliases ourselves. --- ci/run.sh | 30 +++++++++++---------- src/arm.rs | 58 --------------------------------------- src/float/add.rs | 2 ++ src/float/conv.rs | 14 ++++++++++ src/float/sub.rs | 2 ++ src/int/mul.rs | 1 + src/int/sdiv.rs | 1 + src/int/shift.rs | 3 +++ src/int/udiv.rs | 1 + src/macros.rs | 69 +++++++++++++++++++++++++++++++++++++---------- 10 files changed, 95 insertions(+), 86 deletions(-) diff --git a/ci/run.sh b/ci/run.sh index 3316a43..f61b90f 100755 --- a/ci/run.sh +++ b/ci/run.sh @@ -50,19 +50,6 @@ case $1 in ;; esac -# Verify that there are no undefined symbols to `panic` within our implementations -# TODO(#79) fix the undefined references problem for debug-assertions+lto -case $1 in - thumb*) - RUSTFLAGS="-C debug-assertions=no" xargo rustc --features 'c mem' --target $1 --example intrinsics -- -C lto -C link-arg=-nostartfiles - xargo rustc --features 'c mem' --target $1 --example intrinsics --release -- -C lto - ;; - *) - RUSTFLAGS="-C debug-assertions=no" cargo rustc --features 'c mem' --target $1 --example intrinsics -- -C lto - cargo rustc --features 'c mem' --target $1 --example intrinsics --release -- -C lto - ;; -esac - PREFIX=$(echo $1 | sed -e 's/unknown-//')- case $1 in armv7-*) @@ -109,9 +96,24 @@ for rlib in $(echo $path); do set -ex done +rm -f $path + +# Verify that there are no undefined symbols to `panic` within our implementations +# TODO(#79) fix the undefined references problem for debug-assertions+lto +case $1 in + thumb*) + RUSTFLAGS="-C debug-assertions=no" xargo rustc --features 'c mem' --target $1 --example intrinsics -- -C lto -C link-arg=-nostartfiles + xargo rustc --features 'c mem' --target $1 --example intrinsics --release -- -C lto + ;; + *) + RUSTFLAGS="-C debug-assertions=no" cargo rustc --features 'c mem' --target $1 --example intrinsics -- -C lto + cargo rustc --features 'c mem' --target $1 --example intrinsics --release -- -C lto + ;; +esac + # Ensure no references to a panicking function for rlib in $(echo $path); do - set +x + set +ex $PREFIX$NM -u $rlib 2>&1 | grep panicking if test $? = 0; then diff --git a/src/arm.rs b/src/arm.rs index cf960c5..6e1c8f2 100644 --- a/src/arm.rs +++ b/src/arm.rs @@ -60,64 +60,6 @@ pub unsafe fn __aeabi_ldivmod() { intrinsics::unreachable(); } -#[cfg_attr(not(test), no_mangle)] -pub extern "aapcs" fn __aeabi_dadd(a: f64, b: f64) -> f64 { - ::float::add::__adddf3(a, b) -} - -#[cfg_attr(not(test), no_mangle)] -pub extern "aapcs" fn __aeabi_fadd(a: f32, b: f32) -> f32 { - ::float::add::__addsf3(a, b) -} - -#[cfg_attr(not(test), no_mangle)] -pub extern "aapcs" fn __aeabi_dsub(a: f64, b: f64) -> f64 { - ::float::sub::__subdf3(a, b) -} - -#[cfg_attr(not(test), no_mangle)] -pub extern "aapcs" fn __aeabi_fsub(a: f32, b: f32) -> f32 { - ::float::sub::__subsf3(a, b) -} - -#[cfg(not(all(feature = "c", target_arch = "arm", not(target_os = "ios"), not(thumbv6m))))] -#[cfg_attr(not(test), no_mangle)] -pub extern "aapcs" fn __aeabi_idiv(a: i32, b: i32) -> i32 { - ::int::sdiv::__divsi3(a, b) -} - -#[cfg_attr(not(test), no_mangle)] -pub extern "aapcs" fn __aeabi_lasr(a: i64, b: u32) -> i64 { - ::int::shift::__ashrdi3(a, b) -} - -#[cfg_attr(not(test), no_mangle)] -pub extern "aapcs" fn __aeabi_llsl(a: u64, b: u32) -> u64 { - ::int::shift::__ashldi3(a, b) -} - -#[cfg_attr(not(test), no_mangle)] -pub extern "aapcs" fn __aeabi_llsr(a: u64, b: u32) -> u64 { - ::int::shift::__lshrdi3(a, b) -} - -#[cfg_attr(not(test), no_mangle)] -pub extern "aapcs" fn __aeabi_lmul(a: u64, b: u64) -> u64 { - ::int::mul::__muldi3(a, b) -} - -#[cfg(not(all(feature = "c", target_arch = "arm", not(target_os = "ios"), not(thumbv6m))))] -#[cfg_attr(not(test), no_mangle)] -pub extern "aapcs" fn __aeabi_uidiv(a: u32, b: u32) -> u32 { - ::int::udiv::__udivsi3(a, b) -} - -#[cfg(not(feature = "c"))] -#[cfg_attr(not(test), no_mangle)] -pub extern "C" fn __aeabi_ui2d(a: u32) -> f64 { - ::float::conv::__floatunsidf(a) -} - // TODO: These aeabi_* functions should be defined as aliases #[cfg(not(feature = "mem"))] extern "C" { diff --git a/src/float/add.rs b/src/float/add.rs index 1b1ac3f..696d886 100644 --- a/src/float/add.rs +++ b/src/float/add.rs @@ -181,11 +181,13 @@ macro_rules! add { intrinsics! { #[aapcs_on_arm] + #[arm_aeabi_alias = __aeabi_fadd] pub extern "C" fn __addsf3(a: f32, b: f32) -> f32 { add!(a, b, f32) } #[aapcs_on_arm] + #[arm_aeabi_alias = __aeabi_dadd] pub extern "C" fn __adddf3(a: f64, b: f64) -> f64 { add!(a, b, f64) } diff --git a/src/float/conv.rs b/src/float/conv.rs index ca05c28..8fc2a2a 100644 --- a/src/float/conv.rs +++ b/src/float/conv.rs @@ -70,15 +70,18 @@ macro_rules! int_to_float { } intrinsics! { + #[arm_aeabi_alias = __aeabi_i2f] pub extern "C" fn __floatsisf(i: i32) -> f32 { int_to_float!(i, i32, f32) } + #[arm_aeabi_alias = __aeabi_i2d] pub extern "C" fn __floatsidf(i: i32) -> f64 { int_to_float!(i, i32, f64) } #[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))] + #[arm_aeabi_alias = __aeabi_l2d] pub extern "C" fn __floatdidf(i: i64) -> f64 { // On x86_64 LLVM will use native instructions for this conversion, we // can just do it directly @@ -99,16 +102,19 @@ intrinsics! { int_to_float!(i, i128, f64) } + #[arm_aeabi_alias = __aeabi_ui2f] pub extern "C" fn __floatunsisf(i: u32) -> f32 { int_to_float!(i, u32, f32) } + #[arm_aeabi_alias = __aeabi_ui2d] pub extern "C" fn __floatunsidf(i: u32) -> f64 { int_to_float!(i, u32, f64) } #[use_c_shim_if(all(any(target_arch = "x86", target_arch = "x86_64"), not(target_env = "msvc")))] + #[arm_aeabi_alias = __aeabi_ul2d] pub extern "C" fn __floatundidf(i: u64) -> f64 { int_to_float!(i, u64, f64) } @@ -182,10 +188,12 @@ macro_rules! float_to_int { } intrinsics! { + #[arm_aeabi_alias = __aeabi_f2iz] pub extern "C" fn __fixsfsi(f: f32) -> i32 { float_to_int!(f, f32, i32) } + #[arm_aeabi_alias = __aeabi_f2lz] pub extern "C" fn __fixsfdi(f: f32) -> i64 { float_to_int!(f, f32, i64) } @@ -195,10 +203,12 @@ intrinsics! { float_to_int!(f, f32, i128) } + #[arm_aeabi_alias = __aeabi_d2iz] pub extern "C" fn __fixdfsi(f: f64) -> i32 { float_to_int!(f, f64, i32) } + #[arm_aeabi_alias = __aeabi_d2lz] pub extern "C" fn __fixdfdi(f: f64) -> i64 { float_to_int!(f, f64, i64) } @@ -208,10 +218,12 @@ intrinsics! { float_to_int!(f, f64, i128) } + #[arm_aeabi_alias = __aeabi_f2uiz] pub extern "C" fn __fixunssfsi(f: f32) -> u32 { float_to_int!(f, f32, u32) } + #[arm_aeabi_alias = __aeabi_f2ulz] pub extern "C" fn __fixunssfdi(f: f32) -> u64 { float_to_int!(f, f32, u64) } @@ -221,10 +233,12 @@ intrinsics! { float_to_int!(f, f32, u128) } + #[arm_aeabi_alias = __aeabi_d2uiz] pub extern "C" fn __fixunsdfsi(f: f64) -> u32 { float_to_int!(f, f64, u32) } + #[arm_aeabi_alias = __aeabi_d2ulz] pub extern "C" fn __fixunsdfdi(f: f64) -> u64 { float_to_int!(f, f64, u64) } diff --git a/src/float/sub.rs b/src/float/sub.rs index a82a943..4fa436d 100644 --- a/src/float/sub.rs +++ b/src/float/sub.rs @@ -1,10 +1,12 @@ use float::Float; intrinsics! { + #[arm_aeabi_alias = __aeabi_fsub] pub extern "C" fn __subsf3(a: f32, b: f32) -> f32 { a + f32::from_repr(b.repr() ^ f32::sign_mask()) } + #[arm_aeabi_alias = __aeabi_dsub] pub extern "C" fn __subdf3(a: f64, b: f64) -> f64 { a + f64::from_repr(b.repr() ^ f64::sign_mask()) } diff --git a/src/int/mul.rs b/src/int/mul.rs index 9241ee6..d61a6b4 100644 --- a/src/int/mul.rs +++ b/src/int/mul.rs @@ -72,6 +72,7 @@ impl Mulo for i128 {} intrinsics! { #[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))] + #[arm_aeabi_alias = __aeabi_lmul] pub extern "C" fn __muldi3(a: u64, b: u64) -> u64 { a.mul(b) } diff --git a/src/int/sdiv.rs b/src/int/sdiv.rs index 8d85630..6ce7ffd 100644 --- a/src/int/sdiv.rs +++ b/src/int/sdiv.rs @@ -60,6 +60,7 @@ impl Divmod for i64 {} intrinsics! { #[use_c_shim_if(all(target_arch = "arm", not(target_os = "ios"), not(thumbv6m)))] + #[arm_aeabi_alias = __aeabi_idiv] pub extern "C" fn __divsi3(a: i32, b: i32) -> i32 { a.div(b) } diff --git a/src/int/shift.rs b/src/int/shift.rs index b0103ce..a9b6c05 100644 --- a/src/int/shift.rs +++ b/src/int/shift.rs @@ -66,6 +66,7 @@ impl Lshr for u128 {} intrinsics! { #[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))] + #[arm_aeabi_alias = __aeabi_llsl] pub extern "C" fn __ashldi3(a: u64, b: u32) -> u64 { a.ashl(b) } @@ -75,6 +76,7 @@ intrinsics! { } #[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))] + #[arm_aeabi_alias = __aeabi_lasr] pub extern "C" fn __ashrdi3(a: i64, b: u32) -> i64 { a.ashr(b) } @@ -84,6 +86,7 @@ intrinsics! { } #[use_c_shim_if(all(target_arch = "x86", not(target_env = "msvc")))] + #[arm_aeabi_alias = __aeabi_llsr] pub extern "C" fn __lshrdi3(a: u64, b: u32) -> u64 { a.lshr(b) } diff --git a/src/int/udiv.rs b/src/int/udiv.rs index 90ac729..27baff9 100644 --- a/src/int/udiv.rs +++ b/src/int/udiv.rs @@ -155,6 +155,7 @@ intrinsics! { #[use_c_shim_if(all(target_arch = "arm", not(target_os = "ios"), not(thumbv6m)))] + #[arm_aeabi_alias = __aeabi_uidiv] /// Returns `n / d` pub extern "C" fn __udivsi3(n: u32, d: u32) -> u32 { // Special cases diff --git a/src/macros.rs b/src/macros.rs index 740eae2..2e65e96 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -7,7 +7,7 @@ macro_rules! intrinsics { // intrinsic. ( #[use_c_shim_if($($cfg_clause:tt)*)] - $(#[$attr:meta])* + $(#[$($attr:tt)*])* pub extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) -> $ret:ty { $($body:tt)* } @@ -16,7 +16,6 @@ macro_rules! intrinsics { ) => ( #[cfg(all(feature = "c", $($cfg_clause)*))] - $(#[$attr])* pub extern $abi fn $name( $($argname: $ty),* ) -> $ret { extern $abi { fn $name($($argname: $ty),*) -> $ret; @@ -28,7 +27,7 @@ macro_rules! intrinsics { #[cfg(not(all(feature = "c", $($cfg_clause)*)))] intrinsics! { - $(#[$attr])* + $(#[$($attr)*])* pub extern $abi fn $name( $($argname: $ty),* ) -> $ret { $($body)* } @@ -42,7 +41,7 @@ macro_rules! intrinsics { // ARM and `"C"` elsewhere. ( #[aapcs_on_arm] - $(#[$attr:meta])* + $(#[$($attr:tt)*])* pub extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) -> $ret:ty { $($body:tt)* } @@ -51,7 +50,7 @@ macro_rules! intrinsics { ) => ( #[cfg(target_arch = "arm")] intrinsics! { - $(#[$attr])* + $(#[$($attr)*])* pub extern "aapcs" fn $name( $($argname: $ty),* ) -> $ret { $($body)* } @@ -59,7 +58,7 @@ macro_rules! intrinsics { #[cfg(not(target_arch = "arm"))] intrinsics! { - $(#[$attr])* + $(#[$($attr)*])* pub extern $abi fn $name( $($argname: $ty),* ) -> $ret { $($body)* } @@ -72,7 +71,7 @@ macro_rules! intrinsics { // win64 for some methods. ( #[unadjusted_on_win64] - $(#[$attr:meta])* + $(#[$($attr:tt)*])* pub extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) -> $ret:ty { $($body:tt)* } @@ -81,7 +80,7 @@ macro_rules! intrinsics { ) => ( #[cfg(all(windows, target_pointer_width = "64"))] intrinsics! { - $(#[$attr])* + $(#[$($attr)*])* pub extern "unadjusted" fn $name( $($argname: $ty),* ) -> $ret { $($body)* } @@ -89,7 +88,7 @@ macro_rules! intrinsics { #[cfg(not(all(windows, target_pointer_width = "64")))] intrinsics! { - $(#[$attr])* + $(#[$($attr)*])* pub extern $abi fn $name( $($argname: $ty),* ) -> $ret { $($body)* } @@ -102,7 +101,7 @@ macro_rules! intrinsics { // bit calling convention correct. ( #[win64_128bit_abi_hack] - $(#[$attr:meta])* + $(#[$($attr:tt)*])* pub extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) -> $ret:ty { $($body:tt)* } @@ -110,7 +109,7 @@ macro_rules! intrinsics { $($rest:tt)* ) => ( #[cfg(all(windows, target_pointer_width = "64"))] - $(#[$attr])* + $(#[$($attr)*])* pub extern $abi fn $name( $($argname: $ty),* ) -> $ret { $($body)* } @@ -130,7 +129,49 @@ macro_rules! intrinsics { #[cfg(not(all(windows, target_pointer_width = "64")))] intrinsics! { - $(#[$attr])* + $(#[$($attr)*])* + pub extern $abi fn $name( $($argname: $ty),* ) -> $ret { + $($body)* + } + } + + intrinsics!($($rest)*); + ); + + // A bunch of intrinsics on ARM are aliased in the standard compiler-rt + // build under `__aeabi_*` aliases, and LLVM will call these instead of the + // original function. Handle that here + ( + #[arm_aeabi_alias = $alias:ident] + $(#[$($attr:tt)*])* + pub extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) -> $ret:ty { + $($body:tt)* + } + + $($rest:tt)* + ) => ( + #[cfg(target_arch = "arm")] + $(#[$($attr)*])* + pub extern $abi fn $name( $($argname: $ty),* ) -> $ret { + $($body)* + } + + #[cfg(target_arch = "arm")] + pub mod $name { + intrinsics! { + pub extern "aapcs" fn $alias( $($argname: $ty),* ) -> $ret { + super::$name($($argname),*) + } + + pub extern $abi fn $name( $($argname: $ty),* ) -> $ret { + super::$name($($argname),*) + } + } + } + + #[cfg(not(target_arch = "arm"))] + intrinsics! { + $(#[$($attr)*])* pub extern $abi fn $name( $($argname: $ty),* ) -> $ret { $($body)* } @@ -140,14 +181,14 @@ macro_rules! intrinsics { ); ( - $(#[$attr:meta])* + $(#[$($attr:tt)*])* pub extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) -> $ret:ty { $($body:tt)* } $($rest:tt)* ) => ( - $(#[$attr])* + $(#[$($attr)*])* #[cfg_attr(not(test), no_mangle)] pub extern $abi fn $name( $($argname: $ty),* ) -> $ret { $($body)* From eabb6fab4a4b63ef51bbab284918b6c16218bef7 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 23 Jun 2017 16:28:50 -0700 Subject: [PATCH 29/48] Shore up docs in the macros module --- src/macros.rs | 89 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 77 insertions(+), 12 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index 2e65e96..6e34066 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1,10 +1,63 @@ +//! Macros shared throughout the compiler-builtins implementation + +/// The "main macro" used for defining intrinsics. +/// +/// The compiler-builtins library is super platform-specific with tons of crazy +/// little tweaks for various platforms. As a result it *could* involve a lot fo +/// #[cfg] and macro soup, but the intention is that this macro alleviates a lof +/// of that complexity. Ideally this macro has all the weird ABI things +/// platforms need and elsewhere in this library it just looks like normal Rust +/// code. +/// +/// This macro is structured to be invoked with a bunch of functions that looks +/// like: +/// +/// intrinsics! { +/// pub extern "C" fn foo(a: i32) -> u32 { +/// // ... +/// } +/// +/// #[nonstandard_attribute] +/// pub extern "C" fn bar(a: i32) -> u32 { +/// // ... +/// } +/// } +/// +/// Each function is defined in a manner that looks like a normal Rust function. +/// The macro then accepts a few nonstandard attributes that can decorate +/// various functions. Each of the attributes is documented below with what it +/// can do, and each of them slightly tweaks how further expansion happens. +/// +/// A quick overview of attributes supported right now are: +/// +/// * `use_c_shim_if` - takes a #[cfg] directive and falls back to the +/// C-compiled version if `feature = "c"` is specified. +/// * `aapcs_on_arm` - forces the ABI of the function to be `"aapcs"` on ARM and +/// the specified ABI everywhere else. +/// * `unadjusted_on_win64` - like `aapcs_on_arm` this switches to the +/// `"unadjusted"` abi on Win64 and the specified abi elsewhere. +/// * `win64_128bit_abi_hack` - this attribute is used for 128-bit integer +/// intrinsics where the ABI is slightly tweaked on Windows platforms, but +/// it's a normal ABI elsewhere for returning a 128 bit integer. +/// * `arm_aeabi_alias` - handles the "aliasing" of various intrinsics on ARM +/// their otherwise typical names to other prefixed ones. +/// macro_rules! intrinsics { () => (); - // Anything which has a `not(feature = "c")` we'll generate a shim function - // which calls out to the C function if the `c` feature is enabled. - // Otherwise if the `c` feature isn't enabled then we'll just have a normal - // intrinsic. + // Right now there's a bunch of architecture-optimized intrinsics in the + // stock compiler-rt implementation. Not all of these have been ported over + // to Rust yet so when the `c` feature of this crate is enabled we fall back + // to the architecture-specific versions which should be more optimized. The + // purpose of this macro is to easily allow specifying this. + // + // The argument to `use_c_shim_if` is a `#[cfg]` directive which, when true, + // will cause this crate's exported version of `$name` to just redirect to + // the C implementation. No symbol named `$name` will be in the object file + // for this crate itself. + // + // When the `#[cfg]` directive is false, or when the `c` feature is + // disabled, the provided implementation is used instead. ( #[use_c_shim_if($($cfg_clause:tt)*)] $(#[$($attr:tt)*])* @@ -97,8 +150,13 @@ macro_rules! intrinsics { intrinsics!($($rest)*); ); - // Another attribute we recognize is an "abi hack" for win64 to get the 128 - // bit calling convention correct. + // Some intrinsics on win64 which return a 128-bit integer have an.. unusual + // calling convention. That's managed here with this "abi hack" which alters + // the generated symbol's ABI. + // + // This will still define a function in this crate with the given name and + // signature, but the actual symbol for the intrinsic may have a slightly + // different ABI on win64. ( #[win64_128bit_abi_hack] $(#[$($attr:tt)*])* @@ -119,10 +177,10 @@ macro_rules! intrinsics { intrinsics! { pub extern $abi fn $name( $($argname: $ty),* ) - -> ::macros::win64_abi_hack::U64x2 + -> ::macros::win64_128bit_abi_hack::U64x2 { let e: $ret = super::$name($($argname),*); - ::macros::win64_abi_hack::U64x2::from(e) + ::macros::win64_128bit_abi_hack::U64x2::from(e) } } } @@ -140,7 +198,8 @@ macro_rules! intrinsics { // A bunch of intrinsics on ARM are aliased in the standard compiler-rt // build under `__aeabi_*` aliases, and LLVM will call these instead of the - // original function. Handle that here + // original function. The aliasing here is used to generate these symbols in + // the object file. ( #[arm_aeabi_alias = $alias:ident] $(#[$($attr:tt)*])* @@ -151,7 +210,6 @@ macro_rules! intrinsics { $($rest:tt)* ) => ( #[cfg(target_arch = "arm")] - $(#[$($attr)*])* pub extern $abi fn $name( $($argname: $ty),* ) -> $ret { $($body)* } @@ -180,6 +238,12 @@ macro_rules! intrinsics { intrinsics!($($rest)*); ); + // This is the final catch-all rule. At this point we just generate an + // intrinsic with a conditional `#[no_mangle]` directive to avoid + // interfereing with duplicate symbols and whatnot. + // + // After the intrinsic is defined we just continue with the rest of the + // input we were given. ( $(#[$($attr:tt)*])* pub extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) -> $ret:ty { @@ -198,9 +262,10 @@ macro_rules! intrinsics { ); } -// Hack for LLVM expectations for ABI on windows +// Hack for LLVM expectations for ABI on windows. This is used by the +// `#[win64_128bit_abi_hack]` attribute recognized above #[cfg(all(windows, target_pointer_width="64"))] -pub mod win64_abi_hack { +pub mod win64_128bit_abi_hack { #[repr(simd)] pub struct U64x2(u64, u64); From 635e519b86c33e51fa0b1d1622c83d14ff119581 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 23 Jun 2017 20:20:42 -0700 Subject: [PATCH 30/48] Remove the travis cache --- .travis.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index fa9a278..8a9d1fa 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,3 @@ -cache: cargo dist: trusty language: rust rust: nightly @@ -52,8 +51,6 @@ script: else sh ci/run.sh $TARGET; fi - # Travis can't cache files that are not readable by "others" - - chmod -R a+r $HOME/.cargo notifications: email: From d513c92b015599313ade4bbfa8ca33436a4498f4 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 23 Jun 2017 20:35:25 -0700 Subject: [PATCH 31/48] Try testing MinGW targets --- appveyor.yml | 21 +++++++++++++++++++-- examples/intrinsics.rs | 28 ++++++++++++++++------------ 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 2dce7ce..70049bf 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -3,12 +3,29 @@ environment: - TARGET: i686-pc-windows-msvc - TARGET: x86_64-pc-windows-msvc + # Ensure MinGW works, but we need to download the 32-bit MinGW compiler from a + # custom location. + - TARGET: i686-pc-windows-gnu + MINGW_URL: https://s3.amazonaws.com/rust-lang-ci + MINGW_ARCHIVE: i686-4.9.2-release-win32-dwarf-rt_v4-rev4.7z + MINGW_DIR: mingw32 + - TARGET: x86_64-pc-windows-gnu + MSYS_BITS: 64 + install: - git submodule update --init - appveyor-retry appveyor DownloadFile https://win.rustup.rs/ -FileName rustup-init.exe - rustup-init.exe --default-host x86_64-pc-windows-msvc --default-toolchain nightly -y - - set PATH=%PATH%;C:\Users\appveyor\.cargo\bin - - if "%TARGET%"=="i686-pc-windows-msvc" ( rustup target add %TARGET% ) + - if NOT "%TARGET%" == "x86_64-pc-windows-msvc" rustup target add %TARGET% + + # Use the system msys if we can + - if defined MSYS_BITS set PATH=C:\msys64\mingw%MSYS_BITS%\bin;C:\msys64\usr\bin;%PATH% + + # download a custom compiler otherwise + - if defined MINGW_URL appveyor DownloadFile %MINGW_URL%/%MINGW_ARCHIVE% + - if defined MINGW_URL 7z x -y %MINGW_ARCHIVE% > nul + - if defined MINGW_URL set PATH=C:\Python27;%CD%\%MINGW_DIR%\bin;C:\msys64\usr\bin;%PATH% + - rustc -Vv - cargo -V diff --git a/examples/intrinsics.rs b/examples/intrinsics.rs index cabe917..7db241e 100644 --- a/examples/intrinsics.rs +++ b/examples/intrinsics.rs @@ -12,12 +12,14 @@ #![feature(core_float)] #![feature(lang_items)] #![feature(start)] +#![feature(panic_unwind)] #![feature(i128_type)] #![no_std] #[cfg(not(thumb))] extern crate alloc_system; extern crate compiler_builtins; +extern crate panic_unwind; // NOTE cfg(not(thumbv6m)) means that the operation is not supported on ARMv6-M at all. Not even // compiler-rt provides a C/assembly implementation. @@ -400,6 +402,20 @@ fn run() { bb(umodti3(bb(2), bb(2))); bb(divti3(bb(2), bb(2))); bb(modti3(bb(2), bb(2))); + + something_with_a_dtor(&|| assert_eq!(bb(1), 1)); +} + +fn something_with_a_dtor(f: &Fn()) { + struct A<'a>(&'a (Fn() + 'a)); + + impl<'a> Drop for A<'a> { + fn drop(&mut self) { + (self.0)(); + } + } + let _a = A(f); + f(); } #[cfg(not(thumb))] @@ -428,18 +444,6 @@ pub fn __aeabi_unwind_cpp_pr0() {} #[no_mangle] pub fn __aeabi_unwind_cpp_pr1() {} -// Avoid "undefined reference to `_Unwind_Resume`" errors -#[allow(non_snake_case)] -#[no_mangle] -pub fn _Unwind_Resume() {} - -// Lang items -#[cfg(not(test))] -#[lang = "eh_personality"] -#[no_mangle] -#[allow(private_no_mangle_fns)] -extern "C" fn eh_personality() {} - #[cfg(not(test))] #[lang = "panic_fmt"] #[no_mangle] From 94bc9953a1fe702459327ff50b6aa0cb7e6da931 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 23 Jun 2017 20:43:51 -0700 Subject: [PATCH 32/48] Don't build chkstk on x86_64 --- build.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/build.rs b/build.rs index e66b3ab..7c09987 100644 --- a/build.rs +++ b/build.rs @@ -4145,8 +4145,6 @@ mod c { if target_arch == "x86_64" { sources.extend( &[ - "x86_64/chkstk.S", - "x86_64/chkstk2.S", "x86_64/floatdisf.c", "x86_64/floatdixf.c", "x86_64/floatundidf.S", From b42d26706531a3b357857a4bc73a3a7386b6f887 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 23 Jun 2017 21:09:24 -0700 Subject: [PATCH 33/48] Don't compile assembly on x86_64 Windows They've all got the wrong ABI... --- build.rs | 24 ++++++++++++++---------- src/float/conv.rs | 2 +- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/build.rs b/build.rs index 7c09987..06e1155 100644 --- a/build.rs +++ b/build.rs @@ -4142,16 +4142,20 @@ mod c { sources.extend(&["gcc_personality_v0.c"]); } - if target_arch == "x86_64" { - sources.extend( - &[ - "x86_64/floatdisf.c", - "x86_64/floatdixf.c", - "x86_64/floatundidf.S", - "x86_64/floatundisf.S", - "x86_64/floatundixf.S", - ], - ); + // None of these seem to be used on x86_64 windows, and they've all + // got the wrong ABI anyway, so we want to avoid them. + if target_os != "windows" { + if target_arch == "x86_64" { + sources.extend( + &[ + "x86_64/floatdisf.c", + "x86_64/floatdixf.c", + "x86_64/floatundidf.S", + "x86_64/floatundisf.S", + "x86_64/floatundixf.S", + ], + ); + } } if target_arch == "x86" { diff --git a/src/float/conv.rs b/src/float/conv.rs index 8fc2a2a..ec77f2b 100644 --- a/src/float/conv.rs +++ b/src/float/conv.rs @@ -113,7 +113,7 @@ intrinsics! { } #[use_c_shim_if(all(any(target_arch = "x86", target_arch = "x86_64"), - not(target_env = "msvc")))] + not(windows)))] #[arm_aeabi_alias = __aeabi_ul2d] pub extern "C" fn __floatundidf(i: u64) -> f64 { int_to_float!(i, u64, f64) From 4dac0be751ad028ccc75b26d27eb75b47cad1378 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 23 Jun 2017 21:11:31 -0700 Subject: [PATCH 34/48] Don't derive Debug for Sign --- src/float/conv.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/float/conv.rs b/src/float/conv.rs index ec77f2b..e12349c 100644 --- a/src/float/conv.rs +++ b/src/float/conv.rs @@ -130,7 +130,7 @@ intrinsics! { } } -#[derive(PartialEq, Debug)] +#[derive(PartialEq)] enum Sign { Positive, Negative From a2bdb4b3793fd9d5eafffb7619d8764f39cd61ac Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 23 Jun 2017 21:23:52 -0700 Subject: [PATCH 35/48] Use the same CI script on AppVeyor --- appveyor.yml | 14 +++----------- ci/run.sh | 6 +++--- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 70049bf..6a60213 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -10,7 +10,6 @@ environment: MINGW_ARCHIVE: i686-4.9.2-release-win32-dwarf-rt_v4-rev4.7z MINGW_DIR: mingw32 - TARGET: x86_64-pc-windows-gnu - MSYS_BITS: 64 install: - git submodule update --init @@ -18,8 +17,8 @@ install: - rustup-init.exe --default-host x86_64-pc-windows-msvc --default-toolchain nightly -y - if NOT "%TARGET%" == "x86_64-pc-windows-msvc" rustup target add %TARGET% - # Use the system msys if we can - - if defined MSYS_BITS set PATH=C:\msys64\mingw%MSYS_BITS%\bin;C:\msys64\usr\bin;%PATH% + # Use the system msys + - set PATH=C:\msys64\mingw64\bin;C:\msys64\usr\bin;%PATH% # download a custom compiler otherwise - if defined MINGW_URL appveyor DownloadFile %MINGW_URL%/%MINGW_ARCHIVE% @@ -32,11 +31,4 @@ install: build: false test_script: - - cargo build --target %TARGET% - - cargo build --target %TARGET% --features c --example intrinsics - - cargo build --release --target %TARGET% - - cargo build --release --target %TARGET% --features c --example intrinsics - - cargo test --no-default-features --features gen-tests --target %TARGET% - - cargo test --no-default-features --features "gen-tests c" --target %TARGET% - - cargo test --no-default-features --features gen-tests --release --target %TARGET% - - cargo test --no-default-features --features "gen-tests c" --release --target %TARGET% + - sh ci/run.sh %TARGET% diff --git a/ci/run.sh b/ci/run.sh index f61b90f..c727bdf 100755 --- a/ci/run.sh +++ b/ci/run.sh @@ -75,10 +75,10 @@ case "$TRAVIS_OS_NAME" in ;; esac -if [ "$TRAVIS_OS_NAME" = osx ]; then - path=target/${1}/debug/deps/libcompiler_builtins-*.rlib -else +if [ -d /target ]; then path=/target/${1}/debug/deps/libcompiler_builtins-*.rlib +else + path=target/${1}/debug/deps/libcompiler_builtins-*.rlib fi # Look out for duplicated symbols when we include the compiler-rt (C) implementation From 2147753559ab02142fca34bad3a8de3fee9b0cf0 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 23 Jun 2017 21:31:54 -0700 Subject: [PATCH 36/48] Remove usage of unwrap_or_else --- src/float/pow.rs | 4 +++- src/int/mod.rs | 27 +++++++++++++++++---------- src/int/mul.rs | 4 ++-- src/int/sdiv.rs | 6 ++---- src/int/udiv.rs | 12 ++++-------- 5 files changed, 28 insertions(+), 25 deletions(-) diff --git a/src/float/pow.rs b/src/float/pow.rs index f5c5757..bc15dc0 100644 --- a/src/float/pow.rs +++ b/src/float/pow.rs @@ -1,3 +1,5 @@ +use int::Int; + /// Returns `a` raised to the power `b` macro_rules! pow { ($a: expr, $b: expr) => ({ @@ -8,7 +10,7 @@ macro_rules! pow { if (b & 1) != 0 { r *= a; } - b = b.checked_div(2).unwrap_or_else(|| ::abort()); + b = b.aborting_div(2); if b == 0 { break; } diff --git a/src/int/mod.rs b/src/int/mod.rs index 9ad3d88..0334a4a 100644 --- a/src/int/mod.rs +++ b/src/int/mod.rs @@ -66,8 +66,15 @@ pub trait Int: fn wrapping_add(self, other: Self) -> Self; fn wrapping_mul(self, other: Self) -> Self; fn wrapping_sub(self, other: Self) -> Self; - fn checked_div(self, other: Self) -> Option; - fn checked_rem(self, other: Self) -> Option; + fn aborting_div(self, other: Self) -> Self; + fn aborting_rem(self, other: Self) -> Self; +} + +fn unwrap(t: Option) -> T { + match t { + Some(t) => t, + None => ::abort(), + } } macro_rules! int_impl { @@ -120,12 +127,12 @@ macro_rules! int_impl { ::wrapping_sub(self, other) } - fn checked_div(self, other: Self) -> Option { - ::checked_div(self, other) + fn aborting_div(self, other: Self) -> Self { + unwrap(::checked_div(self, other)) } - fn checked_rem(self, other: Self) -> Option { - ::checked_rem(self, other) + fn aborting_rem(self, other: Self) -> Self { + unwrap(::checked_rem(self, other)) } } @@ -181,12 +188,12 @@ macro_rules! int_impl { ::wrapping_sub(self, other) } - fn checked_div(self, other: Self) -> Option { - ::checked_div(self, other) + fn aborting_div(self, other: Self) -> Self { + unwrap(::checked_div(self, other)) } - fn checked_rem(self, other: Self) -> Option { - ::checked_rem(self, other) + fn aborting_rem(self, other: Self) -> Self { + unwrap(::checked_rem(self, other)) } } } diff --git a/src/int/mul.rs b/src/int/mul.rs index d61a6b4..98a8987 100644 --- a/src/int/mul.rs +++ b/src/int/mul.rs @@ -54,11 +54,11 @@ trait Mulo: Int + ops::Neg { return result; } if sa == sb { - if abs_a > Self::max_value().checked_div(abs_b).unwrap_or_else(|| ::abort()) { + if abs_a > Self::max_value().aborting_div(abs_b) { *overflow = 1; } } else { - if abs_a > Self::min_value().checked_div(-abs_b).unwrap_or_else(|| ::abort()) { + if abs_a > Self::min_value().aborting_div(-abs_b) { *overflow = 1; } } diff --git a/src/int/sdiv.rs b/src/int/sdiv.rs index 6ce7ffd..6dd090f 100644 --- a/src/int/sdiv.rs +++ b/src/int/sdiv.rs @@ -13,8 +13,7 @@ trait Div: Int { let b = (other ^ s_b).wrapping_sub(s_b); let s = s_a ^ s_b; - let r = a.unsigned().checked_div(b.unsigned()) - .unwrap_or_else(|| ::abort()); + let r = a.unsigned().aborting_div(b.unsigned()); (Self::from_unsigned(r) ^ s) - s } } @@ -32,8 +31,7 @@ trait Mod: Int { let s = self >> (Self::bits() - 1); let a = (self ^ s).wrapping_sub(s); - let r = a.unsigned().checked_rem(b.unsigned()) - .unwrap_or_else(|| ::abort()); + let r = a.unsigned().aborting_rem(b.unsigned()); (Self::from_unsigned(r) ^ s) - s } } diff --git a/src/int/udiv.rs b/src/int/udiv.rs index 27baff9..471f01d 100644 --- a/src/int/udiv.rs +++ b/src/int/udiv.rs @@ -11,11 +11,9 @@ macro_rules! udivmod_inner { // 0 X if let Some(rem) = rem { - *rem = <$ty>::from(n.low().checked_rem(d.low()) - .unwrap_or_else(|| ::abort())); + *rem = <$ty>::from(n.low().aborting_rem(d.low())); } - return <$ty>::from(n.low().checked_div(d.low()) - .unwrap_or_else(|| ::abort())); + return <$ty>::from(n.low().aborting_div(d.low())) } else { // 0 X // --- @@ -46,11 +44,9 @@ macro_rules! udivmod_inner { // --- // K 0 if let Some(rem) = rem { - *rem = <$ty>::from_parts(0, n.high().checked_rem(d.high()) - .unwrap_or_else(|| ::abort())); + *rem = <$ty>::from_parts(0, n.high().aborting_rem(d.high())); } - return <$ty>::from(n.high().checked_div(d.high()) - .unwrap_or_else(|| ::abort())); + return <$ty>::from(n.high().aborting_div(d.high())) } // K K From 5e28b7e2b1c500abbfbe21be1b214e447f9ac067 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 23 Jun 2017 21:36:36 -0700 Subject: [PATCH 37/48] Try to fix run.sh on AppVeyor --- appveyor.yml | 2 ++ ci/run.sh | 38 ++++++++++++++++++++------------------ 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 6a60213..1eaaf76 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,4 +1,6 @@ environment: + DEBUG_LTO_BUILD_DOESNT_WORK: 1 + INTRINSICS_FAILS_WITH_MEM_FEATURE: 1 matrix: - TARGET: i686-pc-windows-msvc - TARGET: x86_64-pc-windows-msvc diff --git a/ci/run.sh b/ci/run.sh index c727bdf..eb60636 100755 --- a/ci/run.sh +++ b/ci/run.sh @@ -1,5 +1,19 @@ set -ex +case $1 in + thumb*) + cargo=xargo + ;; + *) + cargo=cargo + ;; +esac + +INTRINSICS_FEATURES="c" +if [ -z "$INTRINSICS_FAILS_WITH_MEM_FEATURE" ]; then + INTRINSICS_FEATURES="$INTRINSICS_FEATURES mem" +fi + # Test our implementation case $1 in thumb*) @@ -41,14 +55,7 @@ case $1 in esac # Verify that we haven't drop any intrinsic/symbol -case $1 in - thumb*) - xargo build --features 'c mem' --target $1 --example intrinsics - ;; - *) - cargo build --features 'c mem' --target $1 --example intrinsics - ;; -esac +$cargo build --features "$INTRINSICS_FEATURES" --target $1 --example intrinsics PREFIX=$(echo $1 | sed -e 's/unknown-//')- case $1 in @@ -100,16 +107,11 @@ rm -f $path # Verify that there are no undefined symbols to `panic` within our implementations # TODO(#79) fix the undefined references problem for debug-assertions+lto -case $1 in - thumb*) - RUSTFLAGS="-C debug-assertions=no" xargo rustc --features 'c mem' --target $1 --example intrinsics -- -C lto -C link-arg=-nostartfiles - xargo rustc --features 'c mem' --target $1 --example intrinsics --release -- -C lto - ;; - *) - RUSTFLAGS="-C debug-assertions=no" cargo rustc --features 'c mem' --target $1 --example intrinsics -- -C lto - cargo rustc --features 'c mem' --target $1 --example intrinsics --release -- -C lto - ;; -esac +if [ -z "$DEBUG_LTO_BUILD_DOESNT_WORK" ]; then + RUSTFLAGS="-C debug-assertions=no" \ + $cargo rustc --features "$INTRINSICS_FEATURES" --target $1 --example intrinsics -- -C lto +fi +$cargo rustc --features "$INTRINSICS_FEATURES" --target $1 --example intrinsics --release -- -C lto # Ensure no references to a panicking function for rlib in $(echo $path); do From 76430488e616cfb2927f1c178b24c3813d010b55 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 23 Jun 2017 21:38:55 -0700 Subject: [PATCH 38/48] Fix appveyor PATH --- appveyor.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/appveyor.yml b/appveyor.yml index 1eaaf76..ebc9035 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -17,6 +17,7 @@ install: - git submodule update --init - appveyor-retry appveyor DownloadFile https://win.rustup.rs/ -FileName rustup-init.exe - rustup-init.exe --default-host x86_64-pc-windows-msvc --default-toolchain nightly -y + - set PATH=%PATH%;C:\Users\appveyor\.cargo\bin - if NOT "%TARGET%" == "x86_64-pc-windows-msvc" rustup target add %TARGET% # Use the system msys From 734ec3d31c4df97951d2db1e3d3f2f0211189051 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sat, 24 Jun 2017 10:04:00 -0700 Subject: [PATCH 39/48] Tweak testing and such: * Don't run `intrinsics` tests on thumb * Disable `compiler_builtins` attribute on `feature = "gen-tests"` * Disable mangling on `feature = "gen-tests"` instead of `cfg(test)` --- Cargo.toml | 7 +++++-- appveyor.yml | 10 +++++++++- ci/run.sh | 32 ++++++++++++++++++++++++-------- src/lib.rs | 3 ++- src/macros.rs | 4 ++-- 5 files changed, 42 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 08bf5fd..c717311 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,12 @@ compiler-builtins = [] default = ["compiler-builtins"] mem = [] rustbuild = ["compiler-builtins"] + # generate tests +# +# Note that this is an internal-only feature used in testing, this should not +# be relied on with crates.io! Enabling this may expose you to breaking +# changes. gen-tests = ["cast", "rand"] [target.'cfg(all(target_arch = "arm", not(any(target_env = "gnu", target_env = "musl")), target_os = "linux"))'.dev-dependencies] @@ -26,10 +31,8 @@ test = { git = "https://github.com/japaric/utest" } utest-cortex-m-qemu = { default-features = false, git = "https://github.com/japaric/utest" } utest-macros = { git = "https://github.com/japaric/utest" } - [[example]] name = "intrinsics" required-features = ["c", "compiler-builtins"] - [workspace] diff --git a/appveyor.yml b/appveyor.yml index ebc9035..e51ddab 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,17 +1,25 @@ environment: - DEBUG_LTO_BUILD_DOESNT_WORK: 1 + # It's... a little unclear why the memcpy symbols clash on linux but not on + # other platforms. Would be great to not differ on this though! INTRINSICS_FAILS_WITH_MEM_FEATURE: 1 + matrix: - TARGET: i686-pc-windows-msvc - TARGET: x86_64-pc-windows-msvc # Ensure MinGW works, but we need to download the 32-bit MinGW compiler from a # custom location. + # + # Note that the MinGW builds have tons of references to + # `rust_eh_unwind_resume` in the debug LTO builds that aren't optimized out, + # so we skip that test for now. Would be great to not skip it! - TARGET: i686-pc-windows-gnu MINGW_URL: https://s3.amazonaws.com/rust-lang-ci MINGW_ARCHIVE: i686-4.9.2-release-win32-dwarf-rt_v4-rev4.7z MINGW_DIR: mingw32 + DEBUG_LTO_BUILD_DOESNT_WORK: 1 - TARGET: x86_64-pc-windows-gnu + DEBUG_LTO_BUILD_DOESNT_WORK: 1 install: - git submodule update --init diff --git a/ci/run.sh b/ci/run.sh index eb60636..f1acace 100755 --- a/ci/run.sh +++ b/ci/run.sh @@ -10,6 +10,10 @@ case $1 in esac INTRINSICS_FEATURES="c" + +# Some architectures like ARM apparently seem to require the `mem` feature +# enabled to successfully compile the `intrinsics` example, and... we're not +# sure why! if [ -z "$INTRINSICS_FAILS_WITH_MEM_FEATURE" ]; then INTRINSICS_FEATURES="$INTRINSICS_FEATURES mem" fi @@ -47,16 +51,13 @@ case $1 in done ;; *) - cargo test --no-default-features --features gen-tests --target $1 - cargo test --no-default-features --features 'gen-tests c' --target $1 - cargo test --no-default-features --features gen-tests --target $1 --release - cargo test --no-default-features --features 'gen-tests c' --target $1 --release + cargo test --features gen-tests --target $1 + cargo test --features 'gen-tests c' --target $1 + cargo test --features gen-tests --target $1 --release + cargo test --features 'gen-tests c' --target $1 --release ;; esac -# Verify that we haven't drop any intrinsic/symbol -$cargo build --features "$INTRINSICS_FEATURES" --target $1 --example intrinsics - PREFIX=$(echo $1 | sed -e 's/unknown-//')- case $1 in armv7-*) @@ -105,7 +106,22 @@ done rm -f $path -# Verify that there are no undefined symbols to `panic` within our implementations +# Verification of the `intrinsics` program doesn't work on thumb targets right +# now. +case $1 in + thumb*) + exit 0 + ;; + *) + ;; +esac + +# Verify that we haven't drop any intrinsic/symbol +$cargo build --features "$INTRINSICS_FEATURES" --target $1 --example intrinsics + +# Verify that there are no undefined symbols to `panic` within our +# implementations +# # TODO(#79) fix the undefined references problem for debug-assertions+lto if [ -z "$DEBUG_LTO_BUILD_DOESNT_WORK" ]; then RUSTFLAGS="-C debug-assertions=no" \ diff --git a/src/lib.rs b/src/lib.rs index 3fa7923..3f8a57e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,7 @@ #![cfg_attr(not(stage0), deny(warnings))] #![cfg_attr(not(test), no_std)] -#![cfg_attr(feature = "compiler-builtins", compiler_builtins)] +#![cfg_attr(all(feature = "compiler-builtins", + not(feature = "gen-tests")), compiler_builtins)] #![crate_name = "compiler_builtins"] #![crate_type = "rlib"] #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png", diff --git a/src/macros.rs b/src/macros.rs index 6e34066..6bf9d26 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -240,7 +240,7 @@ macro_rules! intrinsics { // This is the final catch-all rule. At this point we just generate an // intrinsic with a conditional `#[no_mangle]` directive to avoid - // interfereing with duplicate symbols and whatnot. + // interfereing with duplicate symbols and whatnot during testing. // // After the intrinsic is defined we just continue with the rest of the // input we were given. @@ -253,7 +253,7 @@ macro_rules! intrinsics { $($rest:tt)* ) => ( $(#[$($attr)*])* - #[cfg_attr(not(test), no_mangle)] + #[cfg_attr(not(feature = "gen-tests"), no_mangle)] pub extern $abi fn $name( $($argname: $ty),* ) -> $ret { $($body)* } From 5d6d3fde6a79302651699a825a87585e4690d155 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sat, 24 Jun 2017 10:12:17 -0700 Subject: [PATCH 40/48] Add a FIXME for Windows --- ci/run.sh | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/ci/run.sh b/ci/run.sh index f1acace..2709617 100755 --- a/ci/run.sh +++ b/ci/run.sh @@ -94,9 +94,17 @@ for rlib in $(echo $path); do set +x stdout=$($PREFIX$NM -g --defined-only $rlib 2>&1) - # NOTE On i586, It's normal that the get_pc_thunk symbol appears several times so ignore it + # NOTE On i586, It's normal that the get_pc_thunk symbol appears several + # times so ignore it + # + # FIXME(#167) - we shouldn't ignore `__builtin_cl` style symbols here. set +e - echo "$stdout" | sort | uniq -d | grep -v __x86.get_pc_thunk | grep 'T __' + echo "$stdout" | \ + sort | \ + uniq -d | \ + grep -v __x86.get_pc_thunk | \ + grep -v __builtin_cl | \ + grep 'T __' if test $? = 0; then exit 1 From 91b1291dc5d452b39de89e5e6f52cd265c68d922 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sat, 24 Jun 2017 10:22:49 -0700 Subject: [PATCH 41/48] Don't try to work with `cargo test` --- ci/run.sh | 8 ++++---- src/lib.rs | 3 +-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/ci/run.sh b/ci/run.sh index 2709617..bda4415 100755 --- a/ci/run.sh +++ b/ci/run.sh @@ -51,10 +51,10 @@ case $1 in done ;; *) - cargo test --features gen-tests --target $1 - cargo test --features 'gen-tests c' --target $1 - cargo test --features gen-tests --target $1 --release - cargo test --features 'gen-tests c' --target $1 --release + cargo test --no-default-features --features gen-tests --target $1 + cargo test --no-default-features --features 'gen-tests c' --target $1 + cargo test --no-default-features --features gen-tests --target $1 --release + cargo test --no-default-features --features 'gen-tests c' --target $1 --release ;; esac diff --git a/src/lib.rs b/src/lib.rs index 3f8a57e..3fa7923 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,6 @@ #![cfg_attr(not(stage0), deny(warnings))] #![cfg_attr(not(test), no_std)] -#![cfg_attr(all(feature = "compiler-builtins", - not(feature = "gen-tests")), compiler_builtins)] +#![cfg_attr(feature = "compiler-builtins", compiler_builtins)] #![crate_name = "compiler_builtins"] #![crate_type = "rlib"] #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png", From d051480effe462d677a9cbc3d0562667cb722863 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sat, 24 Jun 2017 11:34:10 -0700 Subject: [PATCH 42/48] Don't generate unmangled aeabi with gen-tests The symbols they delgate to also don't exist... --- src/arm.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/arm.rs b/src/arm.rs index 6e1c8f2..8d5808c 100644 --- a/src/arm.rs +++ b/src/arm.rs @@ -7,6 +7,7 @@ use mem::{memcpy, memmove, memset}; // calling convention which can't be implemented using a normal Rust function #[naked] #[cfg_attr(not(test), no_mangle)] +#[cfg(not(feature = "gen-tests"))] pub unsafe fn __aeabi_uidivmod() { asm!("push {lr} sub sp, sp, #4 @@ -20,6 +21,7 @@ pub unsafe fn __aeabi_uidivmod() { #[naked] #[cfg_attr(not(test), no_mangle)] +#[cfg(not(feature = "gen-tests"))] pub unsafe fn __aeabi_uldivmod() { asm!("push {r4, lr} sub sp, sp, #16 @@ -35,6 +37,7 @@ pub unsafe fn __aeabi_uldivmod() { #[naked] #[cfg_attr(not(test), no_mangle)] +#[cfg(not(feature = "gen-tests"))] pub unsafe fn __aeabi_idivmod() { asm!("push {r0, r1, r4, lr} bl __divsi3 @@ -47,6 +50,7 @@ pub unsafe fn __aeabi_idivmod() { #[naked] #[cfg_attr(not(test), no_mangle)] +#[cfg(not(feature = "gen-tests"))] pub unsafe fn __aeabi_ldivmod() { asm!("push {r4, lr} sub sp, sp, #16 From 4859aba5f4e6e4ae5053e68393ca0e996f2bfd49 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sat, 24 Jun 2017 11:36:05 -0700 Subject: [PATCH 43/48] Don't build gcc_personality_v0 --- build.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/build.rs b/build.rs index 06e1155..bfb0ad9 100644 --- a/build.rs +++ b/build.rs @@ -4138,10 +4138,6 @@ mod c { ); } } else { - if target_os != "freebsd" && target_os != "netbsd" { - sources.extend(&["gcc_personality_v0.c"]); - } - // None of these seem to be used on x86_64 windows, and they've all // got the wrong ABI anyway, so we want to avoid them. if target_os != "windows" { From acaa2f071aa072348f389c3e6af311f6a4c2527b Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sat, 24 Jun 2017 11:43:57 -0700 Subject: [PATCH 44/48] Don't check for references to panics with debug assertions --- ci/run.sh | 3 ++- src/arm.rs | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/ci/run.sh b/ci/run.sh index bda4415..008d9ca 100755 --- a/ci/run.sh +++ b/ci/run.sh @@ -125,7 +125,8 @@ case $1 in esac # Verify that we haven't drop any intrinsic/symbol -$cargo build --features "$INTRINSICS_FEATURES" --target $1 --example intrinsics +RUSTFLAGS="-C debug-assertions=no" \ + $cargo build --features "$INTRINSICS_FEATURES" --target $1 --example intrinsics # Verify that there are no undefined symbols to `panic` within our # implementations diff --git a/src/arm.rs b/src/arm.rs index 8d5808c..1e234b7 100644 --- a/src/arm.rs +++ b/src/arm.rs @@ -1,3 +1,4 @@ +#[cfg(not(feature = "gen-tests"))] use core::intrinsics; #[cfg(feature = "mem")] From 5c74fb13d9c0620d3f7a829c11e60db3f7509abc Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sat, 24 Jun 2017 12:22:31 -0700 Subject: [PATCH 45/48] Enable 128-bit integer tests on Windows Closes #158 --- tests/divti3.rs | 3 +-- tests/modti3.rs | 3 +-- tests/udivmodti4.rs | 3 +-- tests/udivti3.rs | 3 +-- tests/umodti3.rs | 3 +-- 5 files changed, 5 insertions(+), 10 deletions(-) diff --git a/tests/divti3.rs b/tests/divti3.rs index c86785f..7919f8f 100644 --- a/tests/divti3.rs +++ b/tests/divti3.rs @@ -6,6 +6,5 @@ test), no_std)] // FIXME(#137) -// FIXME(#158) -#[cfg(not(any(target_arch = "mips", windows)))] +#[cfg(not(target_arch = "mips"))] include!(concat!(env!("OUT_DIR"), "/divti3.rs")); diff --git a/tests/modti3.rs b/tests/modti3.rs index 2ce42ac..62129cd 100644 --- a/tests/modti3.rs +++ b/tests/modti3.rs @@ -6,6 +6,5 @@ test), no_std)] // FIXME(#137) -// FIXME(#158) -#[cfg(not(any(target_arch = "mips", windows)))] +#[cfg(not(target_arch = "mips"))] include!(concat!(env!("OUT_DIR"), "/modti3.rs")); diff --git a/tests/udivmodti4.rs b/tests/udivmodti4.rs index 5629d61..8185ec0 100644 --- a/tests/udivmodti4.rs +++ b/tests/udivmodti4.rs @@ -6,6 +6,5 @@ test), no_std)] // FIXME(#137) -// FIXME(#158) -#[cfg(not(any(target_arch = "mips", windows)))] +#[cfg(not(target_arch = "mips"))] include!(concat!(env!("OUT_DIR"), "/udivmodti4.rs")); diff --git a/tests/udivti3.rs b/tests/udivti3.rs index 3509225..cefddda 100644 --- a/tests/udivti3.rs +++ b/tests/udivti3.rs @@ -6,6 +6,5 @@ test), no_std)] // FIXME(#137) -// FIXME(#158) -#[cfg(not(any(target_arch = "mips", windows)))] +#[cfg(not(target_arch = "mips"))] include!(concat!(env!("OUT_DIR"), "/udivti3.rs")); diff --git a/tests/umodti3.rs b/tests/umodti3.rs index 5807bcf..57e651b 100644 --- a/tests/umodti3.rs +++ b/tests/umodti3.rs @@ -6,6 +6,5 @@ test), no_std)] // FIXME(#137) -// FIXME(#158) -#[cfg(not(any(target_arch = "mips", windows)))] +#[cfg(not(target_arch = "mips"))] include!(concat!(env!("OUT_DIR"), "/umodti3.rs")); From c193113721600e94722c3ce44ea2ca562caf9b76 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sat, 24 Jun 2017 12:44:00 -0700 Subject: [PATCH 46/48] Don't test mangled names on thumb We are both the "real compiler-rt" and the "to be tested one". --- Cargo.toml | 1 + ci/run.sh | 9 +++++---- src/arm.rs | 37 ++++++++++++++++--------------------- src/macros.rs | 2 +- src/mem.rs | 8 ++++---- 5 files changed, 27 insertions(+), 30 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c717311..070d8f8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ compiler-builtins = [] default = ["compiler-builtins"] mem = [] rustbuild = ["compiler-builtins"] +mangled-names = [] # generate tests # diff --git a/ci/run.sh b/ci/run.sh index 008d9ca..d285b36 100755 --- a/ci/run.sh +++ b/ci/run.sh @@ -51,10 +51,11 @@ case $1 in done ;; *) - cargo test --no-default-features --features gen-tests --target $1 - cargo test --no-default-features --features 'gen-tests c' --target $1 - cargo test --no-default-features --features gen-tests --target $1 --release - cargo test --no-default-features --features 'gen-tests c' --target $1 --release + run="cargo test --no-default-features --target $1" + $run --features 'gen-tests mangled-names' + $run --features 'gen-tests mangled-names' --release + $run --features 'gen-tests mangled-names c' + $run --features 'gen-tests mangled-names c' --release ;; esac diff --git a/src/arm.rs b/src/arm.rs index 1e234b7..81e0afd 100644 --- a/src/arm.rs +++ b/src/arm.rs @@ -1,4 +1,3 @@ -#[cfg(not(feature = "gen-tests"))] use core::intrinsics; #[cfg(feature = "mem")] @@ -7,8 +6,7 @@ use mem::{memcpy, memmove, memset}; // NOTE This function and the ones below are implemented using assembly because they using a custom // calling convention which can't be implemented using a normal Rust function #[naked] -#[cfg_attr(not(test), no_mangle)] -#[cfg(not(feature = "gen-tests"))] +#[cfg_attr(not(feature = "mangled-names"), no_mangle)] pub unsafe fn __aeabi_uidivmod() { asm!("push {lr} sub sp, sp, #4 @@ -21,8 +19,7 @@ pub unsafe fn __aeabi_uidivmod() { } #[naked] -#[cfg_attr(not(test), no_mangle)] -#[cfg(not(feature = "gen-tests"))] +#[cfg_attr(not(feature = "mangled-names"), no_mangle)] pub unsafe fn __aeabi_uldivmod() { asm!("push {r4, lr} sub sp, sp, #16 @@ -37,8 +34,7 @@ pub unsafe fn __aeabi_uldivmod() { } #[naked] -#[cfg_attr(not(test), no_mangle)] -#[cfg(not(feature = "gen-tests"))] +#[cfg_attr(not(feature = "mangled-names"), no_mangle)] pub unsafe fn __aeabi_idivmod() { asm!("push {r0, r1, r4, lr} bl __divsi3 @@ -50,8 +46,7 @@ pub unsafe fn __aeabi_idivmod() { } #[naked] -#[cfg_attr(not(test), no_mangle)] -#[cfg(not(feature = "gen-tests"))] +#[cfg_attr(not(feature = "mangled-names"), no_mangle)] pub unsafe fn __aeabi_ldivmod() { asm!("push {r4, lr} sub sp, sp, #16 @@ -75,55 +70,55 @@ extern "C" { // FIXME: The `*4` and `*8` variants should be defined as aliases. -#[cfg_attr(not(test), no_mangle)] +#[cfg_attr(not(feature = "mangled-names"), no_mangle)] pub unsafe extern "aapcs" fn __aeabi_memcpy(dest: *mut u8, src: *const u8, n: usize) { memcpy(dest, src, n); } -#[cfg_attr(not(test), no_mangle)] +#[cfg_attr(not(feature = "mangled-names"), no_mangle)] pub unsafe extern "aapcs" fn __aeabi_memcpy4(dest: *mut u8, src: *const u8, n: usize) { memcpy(dest, src, n); } -#[cfg_attr(not(test), no_mangle)] +#[cfg_attr(not(feature = "mangled-names"), no_mangle)] pub unsafe extern "aapcs" fn __aeabi_memcpy8(dest: *mut u8, src: *const u8, n: usize) { memcpy(dest, src, n); } -#[cfg_attr(not(test), no_mangle)] +#[cfg_attr(not(feature = "mangled-names"), no_mangle)] pub unsafe extern "aapcs" fn __aeabi_memmove(dest: *mut u8, src: *const u8, n: usize) { memmove(dest, src, n); } -#[cfg_attr(not(test), no_mangle)] +#[cfg_attr(not(feature = "mangled-names"), no_mangle)] pub unsafe extern "aapcs" fn __aeabi_memmove4(dest: *mut u8, src: *const u8, n: usize) { memmove(dest, src, n); } -#[cfg_attr(not(test), no_mangle)] +#[cfg_attr(not(feature = "mangled-names"), no_mangle)] pub unsafe extern "aapcs" fn __aeabi_memmove8(dest: *mut u8, src: *const u8, n: usize) { memmove(dest, src, n); } // Note the different argument order -#[cfg_attr(not(test), no_mangle)] +#[cfg_attr(not(feature = "mangled-names"), no_mangle)] pub unsafe extern "aapcs" fn __aeabi_memset(dest: *mut u8, n: usize, c: i32) { memset(dest, c, n); } -#[cfg_attr(not(test), no_mangle)] +#[cfg_attr(not(feature = "mangled-names"), no_mangle)] pub unsafe extern "aapcs" fn __aeabi_memset4(dest: *mut u8, n: usize, c: i32) { memset(dest, c, n); } -#[cfg_attr(not(test), no_mangle)] +#[cfg_attr(not(feature = "mangled-names"), no_mangle)] pub unsafe extern "aapcs" fn __aeabi_memset8(dest: *mut u8, n: usize, c: i32) { memset(dest, c, n); } -#[cfg_attr(not(test), no_mangle)] +#[cfg_attr(not(feature = "mangled-names"), no_mangle)] pub unsafe extern "aapcs" fn __aeabi_memclr(dest: *mut u8, n: usize) { memset(dest, 0, n); } -#[cfg_attr(not(test), no_mangle)] +#[cfg_attr(not(feature = "mangled-names"), no_mangle)] pub unsafe extern "aapcs" fn __aeabi_memclr4(dest: *mut u8, n: usize) { memset(dest, 0, n); } -#[cfg_attr(not(test), no_mangle)] +#[cfg_attr(not(feature = "mangled-names"), no_mangle)] pub unsafe extern "aapcs" fn __aeabi_memclr8(dest: *mut u8, n: usize) { memset(dest, 0, n); } diff --git a/src/macros.rs b/src/macros.rs index 6bf9d26..188508f 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -253,7 +253,7 @@ macro_rules! intrinsics { $($rest:tt)* ) => ( $(#[$($attr)*])* - #[cfg_attr(not(feature = "gen-tests"), no_mangle)] + #[cfg_attr(not(feature = "mangled-names"), no_mangle)] pub extern $abi fn $name( $($argname: $ty),* ) -> $ret { $($body)* } diff --git a/src/mem.rs b/src/mem.rs index a7d267c..cb8baec 100644 --- a/src/mem.rs +++ b/src/mem.rs @@ -5,7 +5,7 @@ type c_int = i16; #[cfg(not(target_pointer_width = "16"))] type c_int = i32; -#[no_mangle] +#[cfg_attr(not(feature = "mangled-names"), no_mangle)] pub unsafe extern "C" fn memcpy(dest: *mut u8, src: *const u8, n: usize) @@ -18,7 +18,7 @@ pub unsafe extern "C" fn memcpy(dest: *mut u8, dest } -#[no_mangle] +#[cfg_attr(not(feature = "mangled-names"), no_mangle)] pub unsafe extern "C" fn memmove(dest: *mut u8, src: *const u8, n: usize) @@ -41,7 +41,7 @@ pub unsafe extern "C" fn memmove(dest: *mut u8, dest } -#[no_mangle] +#[cfg_attr(not(feature = "mangled-names"), no_mangle)] pub unsafe extern "C" fn memset(s: *mut u8, c: c_int, n: usize) -> *mut u8 { let mut i = 0; while i < n { @@ -51,7 +51,7 @@ pub unsafe extern "C" fn memset(s: *mut u8, c: c_int, n: usize) -> *mut u8 { s } -#[no_mangle] +#[cfg_attr(not(feature = "mangled-names"), no_mangle)] pub unsafe extern "C" fn memcmp(s1: *const u8, s2: *const u8, n: usize) -> i32 { let mut i = 0; while i < n { From afe5c717a981eb593469d6294e5a64c201ba3091 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sat, 24 Jun 2017 21:51:34 -0700 Subject: [PATCH 47/48] Address review comments --- src/macros.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index 188508f..f6d7db7 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -3,8 +3,8 @@ /// The "main macro" used for defining intrinsics. /// /// The compiler-builtins library is super platform-specific with tons of crazy -/// little tweaks for various platforms. As a result it *could* involve a lot fo -/// #[cfg] and macro soup, but the intention is that this macro alleviates a lof +/// little tweaks for various platforms. As a result it *could* involve a lot of +/// #[cfg] and macro soup, but the intention is that this macro alleviates a lot /// of that complexity. Ideally this macro has all the weird ABI things /// platforms need and elsewhere in this library it just looks like normal Rust /// code. @@ -220,10 +220,6 @@ macro_rules! intrinsics { pub extern "aapcs" fn $alias( $($argname: $ty),* ) -> $ret { super::$name($($argname),*) } - - pub extern $abi fn $name( $($argname: $ty),* ) -> $ret { - super::$name($($argname),*) - } } } From 0ebbcaede47fe814051919847dbf95760c62405c Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sun, 25 Jun 2017 09:25:54 -0700 Subject: [PATCH 48/48] Enable the `intrinsics` program on thumb --- ci/run.sh | 12 +----------- examples/intrinsics.rs | 14 ++++++++++++-- src/arm.rs | 2 +- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/ci/run.sh b/ci/run.sh index d285b36..9562b7f 100755 --- a/ci/run.sh +++ b/ci/run.sh @@ -115,19 +115,9 @@ done rm -f $path -# Verification of the `intrinsics` program doesn't work on thumb targets right -# now. -case $1 in - thumb*) - exit 0 - ;; - *) - ;; -esac - # Verify that we haven't drop any intrinsic/symbol RUSTFLAGS="-C debug-assertions=no" \ - $cargo build --features "$INTRINSICS_FEATURES" --target $1 --example intrinsics + $cargo build --features "$INTRINSICS_FEATURES" --target $1 --example intrinsics -v # Verify that there are no undefined symbols to `panic` within our # implementations diff --git a/examples/intrinsics.rs b/examples/intrinsics.rs index 7db241e..e7c55e8 100644 --- a/examples/intrinsics.rs +++ b/examples/intrinsics.rs @@ -12,13 +12,14 @@ #![feature(core_float)] #![feature(lang_items)] #![feature(start)] -#![feature(panic_unwind)] #![feature(i128_type)] +#![cfg_attr(windows, feature(panic_unwind))] #![no_std] #[cfg(not(thumb))] extern crate alloc_system; extern crate compiler_builtins; +#[cfg(windows)] extern crate panic_unwind; // NOTE cfg(not(thumbv6m)) means that the operation is not supported on ARMv6-M at all. Not even @@ -444,7 +445,16 @@ pub fn __aeabi_unwind_cpp_pr0() {} #[no_mangle] pub fn __aeabi_unwind_cpp_pr1() {} -#[cfg(not(test))] +#[cfg(not(windows))] +#[allow(non_snake_case)] +#[no_mangle] +pub fn _Unwind_Resume() {} + +#[cfg(not(windows))] +#[lang = "eh_personality"] +#[no_mangle] +pub extern "C" fn eh_personality() {} + #[lang = "panic_fmt"] #[no_mangle] #[allow(private_no_mangle_fns)] diff --git a/src/arm.rs b/src/arm.rs index 81e0afd..389bf9a 100644 --- a/src/arm.rs +++ b/src/arm.rs @@ -37,7 +37,7 @@ pub unsafe fn __aeabi_uldivmod() { #[cfg_attr(not(feature = "mangled-names"), no_mangle)] pub unsafe fn __aeabi_idivmod() { asm!("push {r0, r1, r4, lr} - bl __divsi3 + bl __aeabi_idiv pop {r1, r2} muls r2, r2, r0 subs r1, r1, r2