Skip to content

Commit

Permalink
test our implementations against gcc_s
Browse files Browse the repository at this point in the history
if it exposes the same intrinsics that we implement -- gcc_s doesn't
implement all the intrinsics for all the architectures.

closes #65
  • Loading branch information
Jorge Aparicio committed Sep 16, 2016
1 parent 9d74cf0 commit 9493c37
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 22 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ version = "0.1.0"
rlibc = { git = "https://github.com/alexcrichton/rlibc", optional = true }

[dev-dependencies]
gcc_s = { path = "gcc_s" }
quickcheck = "0.3.1"

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

[workspace]
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"
51 changes: 51 additions & 0 deletions gcc_s/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#![feature(drop_types_in_const)]

extern crate libloading;

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

use libloading::Library;

static mut GCC_S: Option<Library> = None;

fn gcc_s() -> &'static Library {
unsafe {
static INIT: Once = ONCE_INIT;

INIT.call_once(|| {
GCC_S = Some(Library::new("libgcc_s.so.1").unwrap());
});
GCC_S.as_ref().unwrap()
}
}

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);
18 changes: 14 additions & 4 deletions src/float/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ pub extern fn __aeabi_fadd(a: f32, b: f32) -> f32 {
#[cfg(test)]
mod tests {
use core::{f32, f64};

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

Expand All @@ -213,15 +215,23 @@ 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)

if let Some(addsf3) = gcc_s::addsf3() {
x.eq_repr(unsafe { addsf3(a, b) })
} else {
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)

if let Some(adddf3) = gcc_s::adddf3() {
x.eq_repr(unsafe { adddf3(a, b) })
} else {
x.eq_repr(a + b)
}
}
}

Expand Down
32 changes: 29 additions & 3 deletions src/int/mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,19 @@ mulo!(__mulodi4: i64);

#[cfg(test)]
mod tests {
use gcc_s;
use qc::{I32, I64, U64};

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)

if let Some(muldi3) = gcc_s::muldi3() {
r == unsafe { muldi3(a, b) }
} else {
r == a.wrapping_mul(b)
}
}

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

if let Some(mulosi4) = gcc_s::mulosi4() {
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)
} else {
(r, overflow != 0) == a.overflowing_mul(b)
}
}

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

if let Some(mulodi4) = gcc_s::mulodi4() {
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)
} else {
(r, overflow != 0) == a.overflowing_mul(b)
}
}
}
}
53 changes: 47 additions & 6 deletions src/int/sdiv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ divmod!(__divmoddi4, __divdi3: i64);

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

Expand All @@ -62,7 +63,12 @@ mod tests {
TestResult::discard()
} else {
let q = super::__divdi3(n, d);
TestResult::from_bool(q == n / d)

if let Some(divdi3) = gcc_s::divdi3() {
TestResult::from_bool(q == unsafe { divdi3(n, d) })
} else {
TestResult::from_bool(q == n / d)
}
}
}

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

if let Some(moddi3) = gcc_s::moddi3() {
TestResult::from_bool(r == unsafe { moddi3(n, d) })
} else {
TestResult::from_bool(r == n % d)
}
}
}

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

if let Some(divmoddi4) = gcc_s::divmoddi4() {
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)
} else {
TestResult::from_bool(q == n / d && r == n % d)
}
}
}

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

if let Some(divsi3) = gcc_s::divsi3() {
TestResult::from_bool(q == unsafe { divsi3(n, d)})
} else {
TestResult::from_bool(q == n / d)
}
}
}

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

if let Some(modsi3) = gcc_s::modsi3() {
TestResult::from_bool(r == unsafe { modsi3(n, d) })
} else {
TestResult::from_bool(r == n % d)
}
}
}

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

if let Some(divmodsi4) = gcc_s::divmodsi4() {
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)
} else {
TestResult::from_bool(q == n / d && r == n % d)
}
}
}
}
Expand Down
22 changes: 19 additions & 3 deletions src/int/shift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ lshr!(__lshrdi3: u64);

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

Expand All @@ -71,7 +72,12 @@ mod tests {
TestResult::discard()
} else {
let r = super::__ashldi3(a, b);
TestResult::from_bool(r == a << b)

if let Some(ashldi3) = gcc_s::ashldi3() {
TestResult::from_bool(r == unsafe { ashldi3(a, b) })
} else {
TestResult::from_bool(r == a << b)
}
}
}

Expand All @@ -81,7 +87,12 @@ mod tests {
TestResult::discard()
} else {
let r = super::__ashrdi3(a, b);
TestResult::from_bool(r == a >> b)

if let Some(ashrdi3) = gcc_s::ashrdi3() {
TestResult::from_bool(r == unsafe { ashrdi3(a, b) })
} else {
TestResult::from_bool(r == a >> b)
}
}
}

Expand All @@ -91,7 +102,12 @@ mod tests {
TestResult::discard()
} else {
let r = super::__lshrdi3(a, b);
TestResult::from_bool(r == a >> b)

if let Some(lshrdi3) = gcc_s::lshrdi3() {
TestResult::from_bool(r == unsafe { lshrdi3(a, b) })
} else {
TestResult::from_bool(r == a >> b)
}
}
}
}
Expand Down
Loading

0 comments on commit 9493c37

Please sign in to comment.