|
1 | 1 | use core::intrinsics;
|
2 | 2 |
|
3 |
| -// NOTE This function and the one below are implemented using assembly because they using a custom |
| 3 | +// NOTE This function and the ones below are implemented using assembly because they using a custom |
4 | 4 | // calling convention which can't be implemented using a normal Rust function
|
5 | 5 | #[naked]
|
6 | 6 | #[cfg_attr(not(test), no_mangle)]
|
@@ -30,6 +30,44 @@ pub unsafe fn __aeabi_uldivmod() {
|
30 | 30 | intrinsics::unreachable();
|
31 | 31 | }
|
32 | 32 |
|
| 33 | +#[naked] |
| 34 | +#[cfg_attr(not(test), no_mangle)] |
| 35 | +pub unsafe fn __aeabi_idivmod() { |
| 36 | + asm!("push {r0, r1, r4, lr} |
| 37 | + bl __divsi3 |
| 38 | + pop {r1, r2} |
| 39 | + muls r2, r2, r0 |
| 40 | + subs r1, r1, r2 |
| 41 | + pop {r4, pc}"); |
| 42 | + intrinsics::unreachable(); |
| 43 | +} |
| 44 | + |
| 45 | +#[naked] |
| 46 | +#[cfg_attr(not(test), no_mangle)] |
| 47 | +pub unsafe fn __aeabi_ldivmod() { |
| 48 | + asm!("push {r4, lr} |
| 49 | + sub sp, sp, #16 |
| 50 | + add r4, sp, #8 |
| 51 | + str r4, [sp] |
| 52 | + bl __divmoddi4 |
| 53 | + ldr r2, [sp, #8] |
| 54 | + ldr r3, [sp, #12] |
| 55 | + add sp, sp, #16 |
| 56 | + pop {r4, pc}"); |
| 57 | + intrinsics::unreachable(); |
| 58 | +} |
| 59 | + |
| 60 | +// TODO: These two functions should be defined as aliases |
| 61 | +#[cfg_attr(not(test), no_mangle)] |
| 62 | +pub extern "C" fn __aeabi_uidiv(a: u32, b: u32) -> u32 { |
| 63 | + ::udiv::__udivsi3(a, b) |
| 64 | +} |
| 65 | + |
| 66 | +#[cfg_attr(not(test), no_mangle)] |
| 67 | +pub extern "C" fn __aeabi_idiv(a: i32, b: i32) -> i32 { |
| 68 | + ::sdiv::__divsi3(a, b) |
| 69 | +} |
| 70 | + |
33 | 71 | extern "C" {
|
34 | 72 | fn memcpy(dest: *mut u8, src: *const u8, n: usize) -> *mut u8;
|
35 | 73 | 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) {
|
90 | 128 | pub unsafe extern "C" fn __aeabi_memclr8(dest: *mut u8, n: usize) {
|
91 | 129 | memset(dest, 0, n);
|
92 | 130 | }
|
| 131 | + |
| 132 | + |
| 133 | +#[cfg(test)] |
| 134 | +mod tests { |
| 135 | + use quickcheck::TestResult; |
| 136 | + use qc::{U32, U64}; |
| 137 | + |
| 138 | + quickcheck!{ |
| 139 | + fn uldivmod(n: U64, d: U64) -> TestResult { |
| 140 | + let (n, d) = (n.0, d.0); |
| 141 | + if d == 0 { |
| 142 | + TestResult::discard() |
| 143 | + } else { |
| 144 | + let q: u64; |
| 145 | + let r: u64; |
| 146 | + unsafe { |
| 147 | + // The inline asm is a bit tricky here, LLVM will allocate |
| 148 | + // both r0 and r1 when we specify a 64-bit value for {r0}. |
| 149 | + asm!("bl __aeabi_uldivmod" |
| 150 | + : "={r0}" (q), "={r2}" (r) |
| 151 | + : "{r0}" (n), "{r2}" (d) |
| 152 | + : "r12", "lr", "flags"); |
| 153 | + } |
| 154 | + TestResult::from_bool(q == n / d && r == n % d) |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + fn uidivmod(n: U32, d: U32) -> TestResult { |
| 159 | + let (n, d) = (n.0, d.0); |
| 160 | + if d == 0 { |
| 161 | + TestResult::discard() |
| 162 | + } else { |
| 163 | + let q: u32; |
| 164 | + let r: u32; |
| 165 | + unsafe { |
| 166 | + asm!("bl __aeabi_uidivmod" |
| 167 | + : "={r0}" (q), "={r1}" (r) |
| 168 | + : "{r0}" (n), "{r1}" (d) |
| 169 | + : "r2", "r3", "r12", "lr", "flags"); |
| 170 | + } |
| 171 | + TestResult::from_bool(q == n / d && r == n % d) |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + fn ldivmod(n: U64, d: U64) -> TestResult { |
| 176 | + let (n, d) = (n.0 as i64, d.0 as i64); |
| 177 | + if d == 0 { |
| 178 | + TestResult::discard() |
| 179 | + } else { |
| 180 | + let q: i64; |
| 181 | + let r: i64; |
| 182 | + unsafe { |
| 183 | + // The inline asm is a bit tricky here, LLVM will allocate |
| 184 | + // both r0 and r1 when we specify a 64-bit value for {r0}. |
| 185 | + asm!("bl __aeabi_ldivmod" |
| 186 | + : "={r0}" (q), "={r2}" (r) |
| 187 | + : "{r0}" (n), "{r2}" (d) |
| 188 | + : "r12", "lr", "flags"); |
| 189 | + } |
| 190 | + TestResult::from_bool(q == n / d && r == n % d) |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + fn idivmod(n: U32, d: U32) -> TestResult { |
| 195 | + let (n, d) = (n.0 as i32, d.0 as i32); |
| 196 | + if d == 0 { |
| 197 | + TestResult::discard() |
| 198 | + } else { |
| 199 | + let q: i32; |
| 200 | + let r: i32; |
| 201 | + unsafe { |
| 202 | + asm!("bl __aeabi_idivmod" |
| 203 | + : "={r0}" (q), "={r1}" (r) |
| 204 | + : "{r0}" (n), "{r1}" (d) |
| 205 | + : "r2", "r3", "r12", "lr", "flags"); |
| 206 | + } |
| 207 | + TestResult::from_bool(q == n / d && r == n % d) |
| 208 | + } |
| 209 | + } |
| 210 | + } |
| 211 | +} |
0 commit comments