Same story as always, i.e. ABI mismatch: - https://github.com/rust-lang/compiler-builtins/pull/462 - https://github.com/rust-lang/compiler-builtins/pull/466 - https://github.com/rust-lang/compiler-builtins/pull/513 I've made sure the changes work by rendering a Mandelbrot fractal: ```rust #[arduino_hal::entry] fn main() -> ! { let dp = arduino_hal::Peripherals::take().unwrap(); let pins = arduino_hal::pins!(dp); let mut serial = arduino_hal::default_serial!(dp, pins, 57600); mandelbrot(&mut serial, 60, 40, -2.05, -1.12, 0.47, 1.12, 100); loop { // } } fn mandelbrot<T>( output: &mut T, viewport_width: i64, viewport_height: i64, x1: f32, y1: f32, x2: f32, y2: f32, max_iterations: i64, ) where T: uWrite, { for viewport_y in 0..viewport_height { let y0 = y1 + (y2 - y1) * ((viewport_y as f32) / (viewport_height as f32)); for viewport_x in 0..viewport_width { let x0 = x1 + (x2 - x1) * ((viewport_x as f32) / (viewport_width as f32)); let mut x = 0.0; let mut y = 0.0; let mut iterations = max_iterations; while x * x + y * y <= 4.0 && iterations > 0 { let xtemp = x * x - y * y + x0; y = 2.0 * x * y + y0; x = xtemp; iterations -= 1; } let ch = "#%=-:,. " .chars() .nth((8.0 * ((iterations as f32) / (max_iterations as f32))) as _) .unwrap(); _ = ufmt::uwrite!(output, "{}", ch); } _ = ufmt::uwriteln!(output, ""); } } ``` ... where without avr_skips, the code printed an image full of only `#`. Note that because libgcc doesn't provide implementations for f64, using those (e.g. swapping f32 to f64 in the code above) will cause linking to fail: ``` undefined reference to `__divdf3' undefined reference to `__muldf3' undefined reference to `__gedf2' undefined reference to `__fixunsdfsi' undefined reference to `__gtdf2' ``` Ideally compiler-builtins could jump right in and provide those, but f64 also require a special calling convention which hasn't been yet exposed through LLVM. Note that because using 64-bit floats on an 8-bit target is a pretty niche thing to do, and because f64 floats don't work correctly anyway at the moment (due to this ABI mismatch), we're not actually breaking anything by skipping those functions, since any code that currently uses f64 on AVR works by accident. Closes https://github.com/rust-lang/rust/issues/108489.
267 lines
6.3 KiB
Rust
267 lines
6.3 KiB
Rust
#![allow(unreachable_code)]
|
|
|
|
use float::Float;
|
|
use int::Int;
|
|
|
|
#[derive(Clone, Copy)]
|
|
enum Result {
|
|
Less,
|
|
Equal,
|
|
Greater,
|
|
Unordered,
|
|
}
|
|
|
|
impl Result {
|
|
fn to_le_abi(self) -> i32 {
|
|
match self {
|
|
Result::Less => -1,
|
|
Result::Equal => 0,
|
|
Result::Greater => 1,
|
|
Result::Unordered => 1,
|
|
}
|
|
}
|
|
|
|
fn to_ge_abi(self) -> i32 {
|
|
match self {
|
|
Result::Less => -1,
|
|
Result::Equal => 0,
|
|
Result::Greater => 1,
|
|
Result::Unordered => -1,
|
|
}
|
|
}
|
|
}
|
|
|
|
fn cmp<F: Float>(a: F, b: F) -> Result {
|
|
let one = F::Int::ONE;
|
|
let zero = F::Int::ZERO;
|
|
let szero = F::SignedInt::ZERO;
|
|
|
|
let sign_bit = F::SIGN_MASK as F::Int;
|
|
let abs_mask = sign_bit - one;
|
|
let exponent_mask = F::EXPONENT_MASK;
|
|
let inf_rep = exponent_mask;
|
|
|
|
let a_rep = a.repr();
|
|
let b_rep = b.repr();
|
|
let a_abs = a_rep & abs_mask;
|
|
let b_abs = b_rep & abs_mask;
|
|
|
|
// If either a or b is NaN, they are unordered.
|
|
if a_abs > inf_rep || b_abs > inf_rep {
|
|
return Result::Unordered;
|
|
}
|
|
|
|
// If a and b are both zeros, they are equal.
|
|
if a_abs | b_abs == zero {
|
|
return Result::Equal;
|
|
}
|
|
|
|
let a_srep = a.signed_repr();
|
|
let b_srep = b.signed_repr();
|
|
|
|
// If at least one of a and b is positive, we get the same result comparing
|
|
// a and b as signed integers as we would with a fp_ting-point compare.
|
|
if a_srep & b_srep >= szero {
|
|
if a_srep < b_srep {
|
|
Result::Less
|
|
} else if a_srep == b_srep {
|
|
Result::Equal
|
|
} else {
|
|
Result::Greater
|
|
}
|
|
// Otherwise, both are negative, so we need to flip the sense of the
|
|
// comparison to get the correct result. (This assumes a twos- or ones-
|
|
// complement integer representation; if integers are represented in a
|
|
// sign-magnitude representation, then this flip is incorrect).
|
|
} else if a_srep > b_srep {
|
|
Result::Less
|
|
} else if a_srep == b_srep {
|
|
Result::Equal
|
|
} else {
|
|
Result::Greater
|
|
}
|
|
}
|
|
|
|
fn unord<F: Float>(a: F, b: F) -> bool {
|
|
let one = F::Int::ONE;
|
|
|
|
let sign_bit = F::SIGN_MASK as F::Int;
|
|
let abs_mask = sign_bit - one;
|
|
let exponent_mask = F::EXPONENT_MASK;
|
|
let inf_rep = exponent_mask;
|
|
|
|
let a_rep = a.repr();
|
|
let b_rep = b.repr();
|
|
let a_abs = a_rep & abs_mask;
|
|
let b_abs = b_rep & abs_mask;
|
|
|
|
a_abs > inf_rep || b_abs > inf_rep
|
|
}
|
|
|
|
intrinsics! {
|
|
#[avr_skip]
|
|
pub extern "C" fn __lesf2(a: f32, b: f32) -> i32 {
|
|
cmp(a, b).to_le_abi()
|
|
}
|
|
|
|
#[avr_skip]
|
|
pub extern "C" fn __gesf2(a: f32, b: f32) -> i32 {
|
|
cmp(a, b).to_ge_abi()
|
|
}
|
|
|
|
#[avr_skip]
|
|
#[arm_aeabi_alias = __aeabi_fcmpun]
|
|
pub extern "C" fn __unordsf2(a: f32, b: f32) -> i32 {
|
|
unord(a, b) as i32
|
|
}
|
|
|
|
#[avr_skip]
|
|
pub extern "C" fn __eqsf2(a: f32, b: f32) -> i32 {
|
|
cmp(a, b).to_le_abi()
|
|
}
|
|
|
|
#[avr_skip]
|
|
pub extern "C" fn __ltsf2(a: f32, b: f32) -> i32 {
|
|
cmp(a, b).to_le_abi()
|
|
}
|
|
|
|
#[avr_skip]
|
|
pub extern "C" fn __nesf2(a: f32, b: f32) -> i32 {
|
|
cmp(a, b).to_le_abi()
|
|
}
|
|
|
|
#[avr_skip]
|
|
pub extern "C" fn __gtsf2(a: f32, b: f32) -> i32 {
|
|
cmp(a, b).to_ge_abi()
|
|
}
|
|
|
|
#[avr_skip]
|
|
pub extern "C" fn __ledf2(a: f64, b: f64) -> i32 {
|
|
cmp(a, b).to_le_abi()
|
|
}
|
|
|
|
#[avr_skip]
|
|
pub extern "C" fn __gedf2(a: f64, b: f64) -> i32 {
|
|
cmp(a, b).to_ge_abi()
|
|
}
|
|
|
|
#[avr_skip]
|
|
#[arm_aeabi_alias = __aeabi_dcmpun]
|
|
pub extern "C" fn __unorddf2(a: f64, b: f64) -> i32 {
|
|
unord(a, b) as i32
|
|
}
|
|
|
|
#[avr_skip]
|
|
pub extern "C" fn __eqdf2(a: f64, b: f64) -> i32 {
|
|
cmp(a, b).to_le_abi()
|
|
}
|
|
|
|
#[avr_skip]
|
|
pub extern "C" fn __ltdf2(a: f64, b: f64) -> i32 {
|
|
cmp(a, b).to_le_abi()
|
|
}
|
|
|
|
#[avr_skip]
|
|
pub extern "C" fn __nedf2(a: f64, b: f64) -> i32 {
|
|
cmp(a, b).to_le_abi()
|
|
}
|
|
|
|
#[avr_skip]
|
|
pub extern "C" fn __gtdf2(a: f64, b: f64) -> i32 {
|
|
cmp(a, b).to_ge_abi()
|
|
}
|
|
}
|
|
|
|
#[cfg(target_arch = "arm")]
|
|
intrinsics! {
|
|
pub extern "aapcs" fn __aeabi_fcmple(a: f32, b: f32) -> i32 {
|
|
(__lesf2(a, b) <= 0) as i32
|
|
}
|
|
|
|
pub extern "aapcs" fn __aeabi_fcmpge(a: f32, b: f32) -> i32 {
|
|
(__gesf2(a, b) >= 0) as i32
|
|
}
|
|
|
|
pub extern "aapcs" fn __aeabi_fcmpeq(a: f32, b: f32) -> i32 {
|
|
(__eqsf2(a, b) == 0) as i32
|
|
}
|
|
|
|
pub extern "aapcs" fn __aeabi_fcmplt(a: f32, b: f32) -> i32 {
|
|
(__ltsf2(a, b) < 0) as i32
|
|
}
|
|
|
|
pub extern "aapcs" fn __aeabi_fcmpgt(a: f32, b: f32) -> i32 {
|
|
(__gtsf2(a, b) > 0) as i32
|
|
}
|
|
|
|
pub extern "aapcs" fn __aeabi_dcmple(a: f64, b: f64) -> i32 {
|
|
(__ledf2(a, b) <= 0) as i32
|
|
}
|
|
|
|
pub extern "aapcs" fn __aeabi_dcmpge(a: f64, b: f64) -> i32 {
|
|
(__gedf2(a, b) >= 0) as i32
|
|
}
|
|
|
|
pub extern "aapcs" fn __aeabi_dcmpeq(a: f64, b: f64) -> i32 {
|
|
(__eqdf2(a, b) == 0) as i32
|
|
}
|
|
|
|
pub extern "aapcs" fn __aeabi_dcmplt(a: f64, b: f64) -> i32 {
|
|
(__ltdf2(a, b) < 0) as i32
|
|
}
|
|
|
|
pub extern "aapcs" fn __aeabi_dcmpgt(a: f64, b: f64) -> i32 {
|
|
(__gtdf2(a, b) > 0) as i32
|
|
}
|
|
|
|
// On hard-float targets LLVM will use native instructions
|
|
// for all VFP intrinsics below
|
|
|
|
pub extern "C" fn __gesf2vfp(a: f32, b: f32) -> i32 {
|
|
(a >= b) as i32
|
|
}
|
|
|
|
pub extern "C" fn __gedf2vfp(a: f64, b: f64) -> i32 {
|
|
(a >= b) as i32
|
|
}
|
|
|
|
pub extern "C" fn __gtsf2vfp(a: f32, b: f32) -> i32 {
|
|
(a > b) as i32
|
|
}
|
|
|
|
pub extern "C" fn __gtdf2vfp(a: f64, b: f64) -> i32 {
|
|
(a > b) as i32
|
|
}
|
|
|
|
pub extern "C" fn __ltsf2vfp(a: f32, b: f32) -> i32 {
|
|
(a < b) as i32
|
|
}
|
|
|
|
pub extern "C" fn __ltdf2vfp(a: f64, b: f64) -> i32 {
|
|
(a < b) as i32
|
|
}
|
|
|
|
pub extern "C" fn __lesf2vfp(a: f32, b: f32) -> i32 {
|
|
(a <= b) as i32
|
|
}
|
|
|
|
pub extern "C" fn __ledf2vfp(a: f64, b: f64) -> i32 {
|
|
(a <= b) as i32
|
|
}
|
|
|
|
pub extern "C" fn __nesf2vfp(a: f32, b: f32) -> i32 {
|
|
(a != b) as i32
|
|
}
|
|
|
|
pub extern "C" fn __nedf2vfp(a: f64, b: f64) -> i32 {
|
|
(a != b) as i32
|
|
}
|
|
|
|
pub extern "C" fn __eqsf2vfp(a: f32, b: f32) -> i32 {
|
|
(a == b) as i32
|
|
}
|
|
|
|
pub extern "C" fn __eqdf2vfp(a: f64, b: f64) -> i32 {
|
|
(a == b) as i32
|
|
}
|
|
}
|