Skip to content

Commit

Permalink
Auto merge of #141 - rust-lang-nursery:aapcs, r=alexcrichton
Browse files Browse the repository at this point in the history
use AAPCS calling convention on all aeabi intrinsics

also, on ARM, inline(always) the actual implementation of the intrinsics so we
end with code like this:

```
00000000 <__aeabi_dadd>:
    (implementation here)
```

instead of "trampolines" like this:

```
00000000 <__aeabi_dadd>:
    (shuffle registers)
    (call __adddf3)

00000000 <__adddf3>:
    (implementation here)
```

closes #116

cc #66
r? @alexcrichton
cc @mattico
  • Loading branch information
bors committed Feb 8, 2017
2 parents 3e8aa49 + 47b45d1 commit 9aa3a25
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 71 deletions.
40 changes: 20 additions & 20 deletions src/arm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,44 +62,44 @@ pub unsafe fn __aeabi_ldivmod() {

// TODO: These aeabi_* functions should be defined as aliases
#[cfg_attr(not(test), no_mangle)]
pub extern "C" fn __aeabi_dadd(a: f64, b: f64) -> f64 {
pub extern "aapcs" fn __aeabi_dadd(a: f64, b: f64) -> f64 {
::float::add::__adddf3(a, b)
}

#[cfg_attr(not(test), no_mangle)]
pub extern "C" fn __aeabi_fadd(a: f32, b: f32) -> f32 {
pub extern "aapcs" fn __aeabi_fadd(a: f32, b: f32) -> f32 {
::float::add::__addsf3(a, b)
}

#[cfg(not(all(feature = "c", target_arch = "arm", not(target_os = "ios"), not(thumbv6m))))]
#[cfg_attr(not(test), no_mangle)]
pub extern "C" fn __aeabi_idiv(a: i32, b: i32) -> i32 {
pub extern "aapcs" fn __aeabi_idiv(a: i32, b: i32) -> i32 {
::int::sdiv::__divsi3(a, b)
}

#[cfg_attr(not(test), no_mangle)]
pub extern "C" fn __aeabi_lasr(a: i64, b: u32) -> i64 {
pub extern "aapcs" fn __aeabi_lasr(a: i64, b: u32) -> i64 {
::int::shift::__ashrdi3(a, b)
}

#[cfg_attr(not(test), no_mangle)]
pub extern "C" fn __aeabi_llsl(a: u64, b: u32) -> u64 {
pub extern "aapcs" fn __aeabi_llsl(a: u64, b: u32) -> u64 {
::int::shift::__ashldi3(a, b)
}

#[cfg_attr(not(test), no_mangle)]
pub extern "C" fn __aeabi_llsr(a: u64, b: u32) -> u64 {
pub extern "aapcs" fn __aeabi_llsr(a: u64, b: u32) -> u64 {
::int::shift::__lshrdi3(a, b)
}

#[cfg_attr(not(test), no_mangle)]
pub extern "C" fn __aeabi_lmul(a: u64, b: u64) -> u64 {
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 "C" fn __aeabi_uidiv(a: u32, b: u32) -> u32 {
pub extern "aapcs" fn __aeabi_uidiv(a: u32, b: u32) -> u32 {
::int::udiv::__udivsi3(a, b)
}

Expand All @@ -113,55 +113,55 @@ extern "C" {
// FIXME: The `*4` and `*8` variants should be defined as aliases.

#[cfg_attr(not(test), no_mangle)]
pub unsafe extern "C" fn __aeabi_memcpy(dest: *mut u8, src: *const u8, n: usize) {
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)]
pub unsafe extern "C" fn __aeabi_memcpy4(dest: *mut u8, src: *const u8, n: usize) {
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)]
pub unsafe extern "C" fn __aeabi_memcpy8(dest: *mut u8, src: *const u8, n: usize) {
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)]
pub unsafe extern "C" fn __aeabi_memmove(dest: *mut u8, src: *const u8, n: usize) {
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)]
pub unsafe extern "C" fn __aeabi_memmove4(dest: *mut u8, src: *const u8, n: usize) {
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)]
pub unsafe extern "C" fn __aeabi_memmove8(dest: *mut u8, src: *const u8, n: usize) {
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)]
pub unsafe extern "C" fn __aeabi_memset(dest: *mut u8, n: usize, c: i32) {
pub unsafe extern "aapcs" fn __aeabi_memset(dest: *mut u8, n: usize, c: i32) {
memset(dest, c, n);
}
#[cfg_attr(not(test), no_mangle)]
pub unsafe extern "C" fn __aeabi_memset4(dest: *mut u8, n: usize, c: i32) {
pub unsafe extern "aapcs" fn __aeabi_memset4(dest: *mut u8, n: usize, c: i32) {
memset(dest, c, n);
}
#[cfg_attr(not(test), no_mangle)]
pub unsafe extern "C" fn __aeabi_memset8(dest: *mut u8, n: usize, c: i32) {
pub unsafe extern "aapcs" fn __aeabi_memset8(dest: *mut u8, n: usize, c: i32) {
memset(dest, c, n);
}

#[cfg_attr(not(test), no_mangle)]
pub unsafe extern "C" fn __aeabi_memclr(dest: *mut u8, n: usize) {
pub unsafe extern "aapcs" fn __aeabi_memclr(dest: *mut u8, n: usize) {
memset(dest, 0, n);
}
#[cfg_attr(not(test), no_mangle)]
pub unsafe extern "C" fn __aeabi_memclr4(dest: *mut u8, n: usize) {
pub unsafe extern "aapcs" fn __aeabi_memclr4(dest: *mut u8, n: usize) {
memset(dest, 0, n);
}
#[cfg_attr(not(test), no_mangle)]
pub unsafe extern "C" fn __aeabi_memclr8(dest: *mut u8, n: usize) {
pub unsafe extern "aapcs" fn __aeabi_memclr8(dest: *mut u8, n: usize) {
memset(dest, 0, n);
}

Expand Down
21 changes: 15 additions & 6 deletions src/float/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use core::num::Wrapping;
use float::Float;

macro_rules! add {
($intrinsic:ident: $ty:ty) => {
($abi:tt, $intrinsic:ident: $ty:ty) => {
/// Returns `a + b`
#[allow(unused_parens)]
#[cfg_attr(not(test), no_mangle)]
pub extern fn $intrinsic(a: $ty, b: $ty) -> $ty {
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);

Expand Down Expand Up @@ -181,8 +181,17 @@ macro_rules! add {
}
}

add!(__addsf3: f32);
add!(__adddf3: f64);
#[cfg(target_arch = "arm")]
add!("aapcs", __addsf3: 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);

// NOTE(cfg) for some reason, on arm*-unknown-linux-gnueabi*, our implementation doesn't
// match the output of its gcc_s or compiler-rt counterpart. Until we investigate further, we'll
Expand All @@ -194,14 +203,14 @@ mod tests {
use qc::{F32, F64};

check! {
fn __addsf3(f: extern fn(f32, f32) -> f32,
fn __addsf3(f: extern "C" fn(f32, f32) -> f32,
a: F32,
b: F32)
-> Option<F32> {
Some(F32(f(a.0, b.0)))
}

fn __adddf3(f: extern fn(f64, f64) -> f64,
fn __adddf3(f: extern "C" fn(f64, f64) -> f64,
a: F64,
b: F64) -> Option<F64> {
Some(F64(f(a.0, b.0)))
Expand Down
4 changes: 2 additions & 2 deletions src/float/pow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ mod tests {
use qc::{I32, F32, F64};

check! {
fn __powisf2(f: extern fn(f32, i32) -> f32,
fn __powisf2(f: extern "C" fn(f32, i32) -> f32,
a: F32,
b: I32) -> Option<F32> {
Some(F32(f(a.0, b.0)))
}

fn __powidf2(f: extern fn(f64, i32) -> f64,
fn __powidf2(f: extern "C" fn(f64, i32) -> f64,
a: F64,
b: I32) -> Option<F64> {
Some(F64(f(a.0, b.0)))
Expand Down
29 changes: 19 additions & 10 deletions src/int/mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use int::LargeInt;
use int::Int;

macro_rules! mul {
($intrinsic:ident: $ty:ty) => {
($(#[$attr:meta])+ |
$abi:tt, $intrinsic:ident: $ty:ty) => {
/// Returns `a * b`
#[cfg_attr(not(test), no_mangle)]
pub extern "C" fn $intrinsic(a: $ty, b: $ty) -> $ty {
$(#[$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);
Expand Down Expand Up @@ -73,9 +74,17 @@ macro_rules! mulo {
}

#[cfg(not(all(feature = "c", target_arch = "x86")))]
mul!(__muldi3: u64);
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);

#[cfg(not(target_arch = "arm"))]
mul!(#[cfg_attr(not(test), no_mangle)]
| "C", __multi3: i128);

mul!(__multi3: i128);
#[cfg(target_arch = "arm")]
mul!(#[cfg_attr(not(test), no_mangle)]
| "aapcs", __multi3: i128);

mulo!(__mulosi4: i32);
mulo!(__mulodi4: i64);
Expand All @@ -90,12 +99,12 @@ mod tests {
use qc::{I32, I64, U64};

check! {
fn __muldi3(f: extern fn(u64, u64) -> u64, a: U64, b: U64)
fn __muldi3(f: extern "C" fn(u64, u64) -> u64, a: U64, b: U64)
-> Option<u64> {
Some(f(a.0, b.0))
}

fn __mulosi4(f: extern fn(i32, i32, &mut i32) -> i32,
fn __mulosi4(f: extern "C" fn(i32, i32, &mut i32) -> i32,
a: I32,
b: I32) -> Option<(i32, i32)> {
let (a, b) = (a.0, b.0);
Expand All @@ -107,7 +116,7 @@ mod tests {
Some((r, overflow))
}

fn __mulodi4(f: extern fn(i64, i64, &mut i32) -> i64,
fn __mulodi4(f: extern "C" fn(i64, i64, &mut i32) -> i64,
a: I64,
b: I64) -> Option<(i64, i32)> {
let (a, b) = (a.0, b.0);
Expand All @@ -130,11 +139,11 @@ mod tests_i128 {
use qc::I128;

check! {
fn __multi3(f: extern fn(i128, i128) -> i128, a: I128, b: I128)
fn __multi3(f: extern "C" fn(i128, i128) -> i128, a: I128, b: I128)
-> Option<i128> {
Some(f(a.0, b.0))
}
fn __muloti4(f: extern fn(i128, i128, &mut i32) -> i128,
fn __muloti4(f: extern "C" fn(i128, i128, &mut i32) -> i128,
a: I128,
b: I128) -> Option<(i128, i32)> {
let (a, b) = (a.0, b.0);
Expand Down
43 changes: 31 additions & 12 deletions src/int/sdiv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ macro_rules! mod_ {
}

macro_rules! divmod {
($intrinsic:ident, $div:ident: $ty:ty) => {
($abi:tt, $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 {
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;
Expand Down Expand Up @@ -86,16 +86,20 @@ mod_!(__modti3: i128, u128);
mod_!(__modti3: i128, u128, ::U64x2, ::sconv);

#[cfg(not(all(feature = "c", target_arch = "arm", not(target_os = "ios"))))]
divmod!(__divmodsi4, __divsi3: i32);
divmod!("C", __divmodsi4, __divsi3: i32);

divmod!(__divmoddi4, __divdi3: i64);
#[cfg(target_arch = "arm")]
divmod!("aapcs", __divmoddi4, __divdi3: i64);

#[cfg(not(target_arch = "arm"))]
divmod!("C", __divmoddi4, __divdi3: i64);

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

check! {
fn __divdi3(f: extern fn(i64, i64) -> i64, n: U64, d: U64) -> Option<i64> {
fn __divdi3(f: extern "C" fn(i64, i64) -> i64, n: U64, d: U64) -> Option<i64> {
let (n, d) = (n.0 as i64, d.0 as i64);
if d == 0 {
None
Expand All @@ -104,7 +108,7 @@ mod tests {
}
}

fn __moddi3(f: extern fn(i64, i64) -> i64, n: U64, d: U64) -> Option<i64> {
fn __moddi3(f: extern "C" fn(i64, i64) -> i64, n: U64, d: U64) -> Option<i64> {
let (n, d) = (n.0 as i64, d.0 as i64);
if d == 0 {
None
Expand All @@ -113,7 +117,22 @@ mod tests {
}
}

fn __divmoddi4(f: extern fn(i64, i64, &mut i64) -> i64,
#[cfg(target_arch = "arm")]
fn __divmoddi4(f: extern "aapcs" fn(i64, i64, &mut i64) -> i64,
n: U64,
d: U64) -> Option<(i64, i64)> {
let (n, d) = (n.0 as i64, d.0 as i64);
if d == 0 {
None
} else {
let mut r = 0;
let q = f(n, d, &mut r);
Some((q, r))
}
}

#[cfg(not(target_arch = "arm"))]
fn __divmoddi4(f: extern "C" fn(i64, i64, &mut i64) -> i64,
n: U64,
d: U64) -> Option<(i64, i64)> {
let (n, d) = (n.0 as i64, d.0 as i64);
Expand All @@ -126,7 +145,7 @@ mod tests {
}
}

fn __divsi3(f: extern fn(i32, i32) -> i32,
fn __divsi3(f: extern "C" fn(i32, i32) -> i32,
n: U32,
d: U32) -> Option<i32> {
let (n, d) = (n.0 as i32, d.0 as i32);
Expand All @@ -137,7 +156,7 @@ mod tests {
}
}

fn __modsi3(f: extern fn(i32, i32) -> i32,
fn __modsi3(f: extern "C" fn(i32, i32) -> i32,
n: U32,
d: U32) -> Option<i32> {
let (n, d) = (n.0 as i32, d.0 as i32);
Expand All @@ -148,7 +167,7 @@ mod tests {
}
}

fn __divmodsi4(f: extern fn(i32, i32, &mut i32) -> i32,
fn __divmodsi4(f: extern "C" fn(i32, i32, &mut i32) -> i32,
n: U32,
d: U32) -> Option<(i32, i32)> {
let (n, d) = (n.0 as i32, d.0 as i32);
Expand All @@ -172,7 +191,7 @@ mod tests_i128 {
use qc::U128;
check! {

fn __divti3(f: extern fn(i128, i128) -> i128, n: U128, d: U128) -> Option<i128> {
fn __divti3(f: extern "C" fn(i128, i128) -> i128, n: U128, d: U128) -> Option<i128> {
let (n, d) = (n.0 as i128, d.0 as i128);
if d == 0 {
None
Expand All @@ -181,7 +200,7 @@ mod tests_i128 {
}
}

fn __modti3(f: extern fn(i128, i128) -> i128, n: U128, d: U128) -> Option<i128> {
fn __modti3(f: extern "C" fn(i128, i128) -> i128, n: U128, d: U128) -> Option<i128> {
let (n, d) = (n.0 as i128, d.0 as i128);
if d == 0 {
None
Expand Down
Loading

0 comments on commit 9aa3a25

Please sign in to comment.