Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test our implementations against gcc_s #67

Merged
merged 6 commits into from
Sep 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
[package]
authors = ["Jorge Aparicio <[email protected]>"]
build = "build.rs"
name = "rustc_builtins"
version = "0.1.0"

[dependencies]
rlibc = { git = "https://github.com/alexcrichton/rlibc", optional = true }

[dependencies.rlibc]
git = "https://github.com/alexcrichton/rlibc"
optional = true

[dev-dependencies]
quickcheck = "0.3.1"
rand = "0.3.14"

[dev-dependencies.gcc_s]
path = "gcc_s"

[features]
default = ["rlibc/weak"]

[workspace]
7 changes: 7 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use std::env;

fn main() {
if env::var("TARGET").unwrap().ends_with("gnueabihf") {
println!("cargo:rustc-cfg=gnueabihf")
}
}
7 changes: 7 additions & 0 deletions gcc_s/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
authors = ["Jorge Aparicio <[email protected]>"]
name = "gcc_s"
version = "0.1.0"

[dependencies]
libloading = "0.3.0"
68 changes: 68 additions & 0 deletions gcc_s/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#![feature(drop_types_in_const)]

extern crate libloading;

use std::sync::{Once, ONCE_INIT};

use libloading::Library;

static mut GCC_S: Option<Library> = None;

#[cfg(not(windows))]
fn gcc_s() -> &'static Library {
#[cfg(not(target_os = "macos"))]
const LIBGCC_S: &'static str = "libgcc_s.so.1";

#[cfg(target_os = "macos")]
const LIBGCC_S: &'static str = "libgcc_s.1.dylib";

unsafe {
static INIT: Once = ONCE_INIT;

INIT.call_once(|| {
GCC_S = Some(Library::new(LIBGCC_S).unwrap());
});
GCC_S.as_ref().unwrap()
}
}

#[cfg(windows)]
macro_rules! declare {
($symbol:ident: fn($($i:ty),+) -> $o:ty) => {
pub fn $symbol() -> Option<unsafe extern fn($($i),+) -> $o> {
None
}
}
}

#[cfg(not(windows))]
macro_rules! declare {
($symbol:ident: fn($($i:ty),+) -> $o:ty) => {
pub fn $symbol() -> Option<unsafe extern fn($($i),+) -> $o> {
unsafe {
gcc_s().get(concat!("__", stringify!($symbol)).as_bytes()).ok().map(|s| *s)
}
}
}
}

declare!(ashldi3: fn(u64, u32) -> u64);
declare!(ashrdi3: fn(i64, u32) -> i64);
declare!(divdi3: fn(i64, i64) -> i64);
declare!(divmoddi4: fn(i64, i64, &mut i64) -> i64);
declare!(divmodsi4: fn(i32, i32, &mut i32) -> i32);
declare!(divsi3: fn(i32, i32) -> i32);
declare!(lshrdi3: fn(u64, u32) -> u64);
declare!(moddi3: fn(i64, i64) -> i64);
declare!(modsi3: fn(i32, i32) -> i32);
declare!(muldi3: fn(u64, u64) -> u64);
declare!(mulodi4: fn(i64, i64, &mut i32) -> i64);
declare!(mulosi4: fn(i32, i32, &mut i32) -> i32);
declare!(udivdi3: fn(u64, u64) -> u64);
declare!(udivmoddi4: fn(u64, u64, Option<&mut u64>) -> u64);
declare!(udivmodsi4: fn(u32, u32, Option<&mut u32>) -> u32);
declare!(udivsi3: fn(u32, u32) -> u32);
declare!(umoddi3: fn(u64, u64) -> u64);
declare!(umodsi3: fn(u32, u32) -> u32);
declare!(addsf3: fn(f32, f32) -> f32);
declare!(adddf3: fn(f64, f64) -> f64);
2 changes: 1 addition & 1 deletion src/arm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ mod tests {

fn idivmod(n: U32, d: U32) -> TestResult {
let (n, d) = (n.0 as i32, d.0 as i32);
if d == 0 {
if d == 0 || (n == i32::min_value() && d == -1) {
TestResult::discard()
} else {
let q: i32;
Expand Down
29 changes: 24 additions & 5 deletions src/float/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,12 @@ pub extern fn __aeabi_fadd(a: f32, b: f32) -> f32 {
#[cfg(test)]
mod tests {
use core::{f32, f64};
use qc::{U32, U64};

use float::Float;
use qc::{U32, U64};

use gcc_s;
use rand;

// NOTE The tests below have special handing for NaN values.
// Because NaN != NaN, the floating-point representations must be used
Expand All @@ -213,15 +217,30 @@ mod tests {
fn addsf3(a: U32, b: U32) -> bool {
let (a, b) = (f32::from_repr(a.0), f32::from_repr(b.0));
let x = super::__addsf3(a, b);
let y = a + b;
x.eq_repr(y)

match gcc_s::addsf3() {
// NOTE(cfg) for some reason, on hard float targets, our implementation doesn't
// match the output of its gcc_s counterpart. Until we investigate further, we'll
// just avoid testing against gcc_s on those targets. Do note that our
// implementation matches the output of the FPU instruction on *hard* float targets
// and matches its gcc_s counterpart on *soft* float targets.
#[cfg(not(gnueabihf))]
Some(addsf3) if rand::random() => x.eq_repr(unsafe { addsf3(a, b) }),
_ => x.eq_repr(a + b),
}
}

fn adddf3(a: U64, b: U64) -> bool {
let (a, b) = (f64::from_repr(a.0), f64::from_repr(b.0));
let x = super::__adddf3(a, b);
let y = a + b;
x.eq_repr(y)

match gcc_s::adddf3() {
// NOTE(cfg) See NOTE above
#[cfg(not(gnueabihf))]
Some(adddf3) if rand::random() => x.eq_repr(unsafe { adddf3(a, b) }),
_ => x.eq_repr(a + b),

}
}
}

Expand Down
35 changes: 32 additions & 3 deletions src/int/mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,18 @@ mulo!(__mulodi4: i64);
mod tests {
use qc::{I32, I64, U64};

use gcc_s;
use rand;

quickcheck! {
fn muldi(a: U64, b: U64) -> bool {
let (a, b) = (a.0, b.0);
let r = super::__muldi3(a, b);
r == a.wrapping_mul(b)

match gcc_s::muldi3() {
Some(muldi3) if rand::random() => r == unsafe { muldi3(a, b) },
_ => r == a.wrapping_mul(b),
}
}

fn mulosi(a: I32, b: I32) -> bool {
Expand All @@ -88,7 +95,18 @@ mod tests {
if overflow != 0 && overflow != 1 {
return false;
}
(r, overflow != 0) == a.overflowing_mul(b)

match gcc_s::mulosi4() {
Some(mulosi4) if rand::random() => {
let mut gcc_s_overflow = 2;
let gcc_s_r = unsafe {
mulosi4(a, b, &mut gcc_s_overflow)
};

(r, overflow) == (gcc_s_r, gcc_s_overflow)
},
_ => (r, overflow != 0) == a.overflowing_mul(b),
}
}

fn mulodi(a: I64, b: I64) -> bool {
Expand All @@ -98,7 +116,18 @@ mod tests {
if overflow != 0 && overflow != 1 {
return false;
}
(r, overflow != 0) == a.overflowing_mul(b)

match gcc_s::mulodi4() {
Some(mulodi4) if rand::random() => {
let mut gcc_s_overflow = 2;
let gcc_s_r = unsafe {
mulodi4(a, b, &mut gcc_s_overflow)
};

(r, overflow) == (gcc_s_r, gcc_s_overflow)
},
_ => (r, overflow != 0) == a.overflowing_mul(b),
}
}
}
}
63 changes: 56 additions & 7 deletions src/int/sdiv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,26 @@ divmod!(__divmoddi4, __divdi3: i64);

#[cfg(test)]
mod tests {
use quickcheck::TestResult;
use qc::{U32, U64};

use gcc_s;
use quickcheck::TestResult;
use rand;

quickcheck!{
fn divdi3(n: U64, d: U64) -> TestResult {
let (n, d) = (n.0 as i64, d.0 as i64);
if d == 0 {
TestResult::discard()
} else {
let q = super::__divdi3(n, d);
TestResult::from_bool(q == n / d)

match gcc_s::divdi3() {
Some(divdi3) if rand::random() => {
TestResult::from_bool(q == unsafe { divdi3(n, d) })
},
_ => TestResult::from_bool(q == n / d),
}
}
}

Expand All @@ -72,7 +81,13 @@ mod tests {
TestResult::discard()
} else {
let r = super::__moddi3(n, d);
TestResult::from_bool(r == n % d)

match gcc_s::moddi3() {
Some(moddi3) if rand::random() => {
TestResult::from_bool(r == unsafe { moddi3(n, d) })
},
_ => TestResult::from_bool(r == n % d),
}
}
}

Expand All @@ -83,7 +98,18 @@ mod tests {
} else {
let mut r = 0;
let q = super::__divmoddi4(n, d, &mut r);
TestResult::from_bool(q == n / d && r == n % d)

match gcc_s::divmoddi4() {
Some(divmoddi4) if rand::random() => {
let mut gcc_s_r = 0;
let gcc_s_q = unsafe {
divmoddi4(n, d, &mut gcc_s_r)
};

TestResult::from_bool(q == gcc_s_q && r == gcc_s_r)
},
_ => TestResult::from_bool(q == n / d && r == n % d),
}
}
}

Expand All @@ -93,7 +119,13 @@ mod tests {
TestResult::discard()
} else {
let q = super::__divsi3(n, d);
TestResult::from_bool(q == n / d)

match gcc_s::divsi3() {
Some(divsi3) if rand::random() => {
TestResult::from_bool(q == unsafe { divsi3(n, d)})
},
_ => TestResult::from_bool(q == n / d),
}
}
}

Expand All @@ -103,7 +135,13 @@ mod tests {
TestResult::discard()
} else {
let r = super::__modsi3(n, d);
TestResult::from_bool(r == n % d)

match gcc_s::modsi3() {
Some(modsi3) if rand::random() => {
TestResult::from_bool(r == unsafe { modsi3(n, d) })
},
_ => TestResult::from_bool(r == n % d),
}
}
}

Expand All @@ -114,7 +152,18 @@ mod tests {
} else {
let mut r = 0;
let q = super::__divmodsi4(n, d, &mut r);
TestResult::from_bool(q == n / d && r == n % d)

match gcc_s::divmodsi4() {
Some(divmodsi4) if rand::random() => {
let mut gcc_s_r = 0;
let gcc_s_q = unsafe {
divmodsi4(n, d, &mut gcc_s_r)
};

TestResult::from_bool(q == gcc_s_q && r == gcc_s_r)
},
_ => TestResult::from_bool(q == n / d && r == n % d),
}
}
}
}
Expand Down
Loading