-
Notifications
You must be signed in to change notification settings - Fork 232
Add signed division functions #44
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
use core::intrinsics; | ||
|
||
// NOTE This function and the one below are implemented using assembly because they using a custom | ||
// 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)] | ||
|
@@ -30,6 +30,44 @@ pub unsafe fn __aeabi_uldivmod() { | |
intrinsics::unreachable(); | ||
} | ||
|
||
#[naked] | ||
#[cfg_attr(not(test), no_mangle)] | ||
pub unsafe fn __aeabi_idivmod() { | ||
asm!("push {r0, r1, r4, lr} | ||
bl __divsi3 | ||
pop {r1, r2} | ||
muls r2, r2, r0 | ||
subs r1, r1, r2 | ||
pop {r4, pc}"); | ||
intrinsics::unreachable(); | ||
} | ||
|
||
#[naked] | ||
#[cfg_attr(not(test), no_mangle)] | ||
pub unsafe fn __aeabi_ldivmod() { | ||
asm!("push {r4, lr} | ||
sub sp, sp, #16 | ||
add r4, sp, #8 | ||
str r4, [sp] | ||
bl __divmoddi4 | ||
ldr r2, [sp, #8] | ||
ldr r3, [sp, #12] | ||
add sp, sp, #16 | ||
pop {r4, pc}"); | ||
intrinsics::unreachable(); | ||
} | ||
|
||
// TODO: These two functions should be defined as aliases | ||
#[cfg_attr(not(test), no_mangle)] | ||
pub extern "C" fn __aeabi_uidiv(a: u32, b: u32) -> u32 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This intrinsic is supposed to be an alias, right? Could you add a note about that? |
||
::udiv::__udivsi3(a, b) | ||
} | ||
|
||
#[cfg_attr(not(test), no_mangle)] | ||
pub extern "C" fn __aeabi_idiv(a: i32, b: i32) -> i32 { | ||
::sdiv::__divsi3(a, b) | ||
} | ||
|
||
extern "C" { | ||
fn memcpy(dest: *mut u8, src: *const u8, n: usize) -> *mut u8; | ||
fn memmove(dest: *mut u8, src: *const u8, n: usize) -> *mut u8; | ||
|
@@ -90,3 +128,84 @@ pub unsafe extern "C" fn __aeabi_memclr4(dest: *mut u8, n: usize) { | |
pub unsafe extern "C" fn __aeabi_memclr8(dest: *mut u8, n: usize) { | ||
memset(dest, 0, n); | ||
} | ||
|
||
|
||
#[cfg(test)] | ||
mod tests { | ||
use quickcheck::TestResult; | ||
use qc::{U32, U64}; | ||
|
||
quickcheck!{ | ||
fn uldivmod(n: U64, d: U64) -> TestResult { | ||
let (n, d) = (n.0, d.0); | ||
if d == 0 { | ||
TestResult::discard() | ||
} else { | ||
let q: u64; | ||
let r: u64; | ||
unsafe { | ||
// The inline asm is a bit tricky here, LLVM will allocate | ||
// both r0 and r1 when we specify a 64-bit value for {r0}. | ||
asm!("bl __aeabi_uldivmod" | ||
: "={r0}" (q), "={r2}" (r) | ||
: "{r0}" (n), "{r2}" (d) | ||
: "r12", "lr", "flags"); | ||
} | ||
TestResult::from_bool(q == n / d && r == n % d) | ||
} | ||
} | ||
|
||
fn uidivmod(n: U32, d: U32) -> TestResult { | ||
let (n, d) = (n.0, d.0); | ||
if d == 0 { | ||
TestResult::discard() | ||
} else { | ||
let q: u32; | ||
let r: u32; | ||
unsafe { | ||
asm!("bl __aeabi_uidivmod" | ||
: "={r0}" (q), "={r1}" (r) | ||
: "{r0}" (n), "{r1}" (d) | ||
: "r2", "r3", "r12", "lr", "flags"); | ||
} | ||
TestResult::from_bool(q == n / d && r == n % d) | ||
} | ||
} | ||
|
||
fn ldivmod(n: U64, d: U64) -> TestResult { | ||
let (n, d) = (n.0 as i64, d.0 as i64); | ||
if d == 0 { | ||
TestResult::discard() | ||
} else { | ||
let q: i64; | ||
let r: i64; | ||
unsafe { | ||
// The inline asm is a bit tricky here, LLVM will allocate | ||
// both r0 and r1 when we specify a 64-bit value for {r0}. | ||
asm!("bl __aeabi_ldivmod" | ||
: "={r0}" (q), "={r2}" (r) | ||
: "{r0}" (n), "{r2}" (d) | ||
: "r12", "lr", "flags"); | ||
} | ||
TestResult::from_bool(q == n / d && r == n % d) | ||
} | ||
} | ||
|
||
fn idivmod(n: U32, d: U32) -> TestResult { | ||
let (n, d) = (n.0 as i32, d.0 as i32); | ||
if d == 0 { | ||
TestResult::discard() | ||
} else { | ||
let q: i32; | ||
let r: i32; | ||
unsafe { | ||
asm!("bl __aeabi_idivmod" | ||
: "={r0}" (q), "={r1}" (r) | ||
: "{r0}" (n), "{r1}" (d) | ||
: "r2", "r3", "r12", "lr", "flags"); | ||
} | ||
TestResult::from_bool(q == n / d && r == n % d) | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ extern crate rlibc; | |
pub mod arm; | ||
|
||
pub mod udiv; | ||
pub mod sdiv; | ||
pub mod mul; | ||
pub mod shift; | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
use Int; | ||
|
||
macro_rules! div { | ||
($intrinsic:ident: $ty:ty, $uty:ty) => { | ||
/// Returns `a / b` | ||
#[cfg_attr(not(test), no_mangle)] | ||
pub extern "C" fn $intrinsic(a: $ty, b: $ty) -> $ty { | ||
let s_a = a >> (<$ty>::bits() - 1); | ||
let s_b = b >> (<$ty>::bits() - 1); | ||
let a = (a ^ s_a) - s_a; | ||
let b = (b ^ s_b) - s_b; | ||
let s = s_a ^ s_b; | ||
let r = (a as $uty) / (b as $uty); | ||
(r as $ty ^ s) - s | ||
} | ||
} | ||
} | ||
|
||
macro_rules! mod_ { | ||
($intrinsic:ident: $ty:ty, $uty:ty) => { | ||
/// Returns `a % b` | ||
#[cfg_attr(not(test), no_mangle)] | ||
pub extern "C" fn $intrinsic(a: $ty, b: $ty) -> $ty { | ||
let s = b >> (<$ty>::bits() - 1); | ||
let b = (b ^ s) - s; | ||
let s = a >> (<$ty>::bits() - 1); | ||
let a = (a ^ s) - s; | ||
let r = (a as $uty) % (b as $uty); | ||
(r as $ty ^ s) - s | ||
} | ||
} | ||
} | ||
|
||
macro_rules! divmod { | ||
($intrinsic:ident, $div:ident: $ty:ty) => { | ||
/// Returns `a / b` and sets `*rem = n % d` | ||
#[cfg_attr(not(test), no_mangle)] | ||
pub extern "C" fn $intrinsic(a: $ty, b: $ty, rem: &mut $ty) -> $ty { | ||
let r = $div(a, b); | ||
*rem = a - (r * b); | ||
r | ||
} | ||
} | ||
} | ||
|
||
div!(__divsi3: i32, u32); | ||
div!(__divdi3: i64, u64); | ||
mod_!(__modsi3: i32, u32); | ||
mod_!(__moddi3: i64, u64); | ||
divmod!(__divmodsi4, __divsi3: i32); | ||
divmod!(__divmoddi4, __divdi3: i64); | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use quickcheck::TestResult; | ||
use qc::{U32, U64}; | ||
|
||
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) | ||
} | ||
} | ||
|
||
fn moddi3(n: U64, d: U64) -> TestResult { | ||
let (n, d) = (n.0 as i64, d.0 as i64); | ||
if d == 0 { | ||
TestResult::discard() | ||
} else { | ||
let r = super::__moddi3(n, d); | ||
TestResult::from_bool(r == n % d) | ||
} | ||
} | ||
|
||
fn divmoddi4(n: U64, d: U64) -> TestResult { | ||
let (n, d) = (n.0 as i64, d.0 as i64); | ||
if d == 0 { | ||
TestResult::discard() | ||
} else { | ||
let mut r = 0; | ||
let q = super::__divmoddi4(n, d, &mut r); | ||
TestResult::from_bool(q == n / d && r == n % d) | ||
} | ||
} | ||
|
||
fn divsi3(n: U32, d: U32) -> TestResult { | ||
let (n, d) = (n.0 as i32, d.0 as i32); | ||
if d == 0 { | ||
TestResult::discard() | ||
} else { | ||
let q = super::__divsi3(n, d); | ||
TestResult::from_bool(q == n / d) | ||
} | ||
} | ||
|
||
fn modsi3(n: U32, d: U32) -> TestResult { | ||
let (n, d) = (n.0 as i32, d.0 as i32); | ||
if d == 0 { | ||
TestResult::discard() | ||
} else { | ||
let r = super::__modsi3(n, d); | ||
TestResult::from_bool(r == n % d) | ||
} | ||
} | ||
|
||
fn divmodsi4(n: U32, d: U32) -> TestResult { | ||
let (n, d) = (n.0 as i32, d.0 as i32); | ||
if d == 0 { | ||
TestResult::discard() | ||
} else { | ||
let mut r = 0; | ||
let q = super::__divmodsi4(n, d, &mut r); | ||
TestResult::from_bool(q == n / d && r == n % d) | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to add a test for this assembly routine (and the one below)? Just to check its expected signature (
fn(u32, u32) -> (u32,u32)
in this case, I think) works as expected?