Skip to content

Commit ec3b202

Browse files
committed
fix: Add #[avr_skip] for floats
Same story as always, i.e. ABI mismatch: - rust-lang#462 - rust-lang#466 - rust-lang#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 rust-lang/rust#108489.
1 parent 96fd4ec commit ec3b202

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

src/float/cmp.rs

+14
Original file line numberDiff line numberDiff line change
@@ -99,60 +99,74 @@ fn unord<F: Float>(a: F, b: F) -> bool {
9999
}
100100

101101
intrinsics! {
102+
#[avr_skip]
102103
pub extern "C" fn __lesf2(a: f32, b: f32) -> i32 {
103104
cmp(a, b).to_le_abi()
104105
}
105106

107+
#[avr_skip]
106108
pub extern "C" fn __gesf2(a: f32, b: f32) -> i32 {
107109
cmp(a, b).to_ge_abi()
108110
}
109111

112+
#[avr_skip]
110113
#[arm_aeabi_alias = __aeabi_fcmpun]
111114
pub extern "C" fn __unordsf2(a: f32, b: f32) -> i32 {
112115
unord(a, b) as i32
113116
}
114117

118+
#[avr_skip]
115119
pub extern "C" fn __eqsf2(a: f32, b: f32) -> i32 {
116120
cmp(a, b).to_le_abi()
117121
}
118122

123+
#[avr_skip]
119124
pub extern "C" fn __ltsf2(a: f32, b: f32) -> i32 {
120125
cmp(a, b).to_le_abi()
121126
}
122127

128+
#[avr_skip]
123129
pub extern "C" fn __nesf2(a: f32, b: f32) -> i32 {
124130
cmp(a, b).to_le_abi()
125131
}
126132

133+
#[avr_skip]
127134
pub extern "C" fn __gtsf2(a: f32, b: f32) -> i32 {
128135
cmp(a, b).to_ge_abi()
129136
}
130137

138+
#[avr_skip]
131139
pub extern "C" fn __ledf2(a: f64, b: f64) -> i32 {
132140
cmp(a, b).to_le_abi()
133141
}
134142

143+
#[avr_skip]
135144
pub extern "C" fn __gedf2(a: f64, b: f64) -> i32 {
136145
cmp(a, b).to_ge_abi()
137146
}
138147

148+
#[avr_skip]
139149
#[arm_aeabi_alias = __aeabi_dcmpun]
140150
pub extern "C" fn __unorddf2(a: f64, b: f64) -> i32 {
141151
unord(a, b) as i32
142152
}
143153

154+
#[avr_skip]
144155
pub extern "C" fn __eqdf2(a: f64, b: f64) -> i32 {
145156
cmp(a, b).to_le_abi()
146157
}
147158

159+
#[avr_skip]
148160
pub extern "C" fn __ltdf2(a: f64, b: f64) -> i32 {
149161
cmp(a, b).to_le_abi()
150162
}
151163

164+
#[avr_skip]
152165
pub extern "C" fn __nedf2(a: f64, b: f64) -> i32 {
153166
cmp(a, b).to_le_abi()
154167
}
155168

169+
#[avr_skip]
156170
pub extern "C" fn __gtdf2(a: f64, b: f64) -> i32 {
157171
cmp(a, b).to_ge_abi()
158172
}

src/macros.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -437,12 +437,11 @@ macro_rules! intrinsics {
437437
intrinsics!($($rest)*);
438438
);
439439

440-
// For division and modulo, AVR uses a custom calling convention¹ that does
441-
// not match our definitions here. Ideally we would just use hand-written
442-
// naked functions, but that's quite a lot of code to port² - so for the
443-
// time being we are just ignoring the problematic functions, letting
444-
// avr-gcc (which is required to compile to AVR anyway) link them from
445-
// libgcc.
440+
// For some intrinsics, AVR uses a custom calling convention¹ that does not
441+
// match our definitions here. Ideally we would just use hand-written naked
442+
// functions, but that's quite a lot of code to port² - so for the time
443+
// being we are just ignoring the problematic functions, letting avr-gcc
444+
// (which is required to compile to AVR anyway) link them from libgcc.
446445
//
447446
// ¹ https://gcc.gnu.org/wiki/avr-gcc (see "Exceptions to the Calling
448447
// Convention")

0 commit comments

Comments
 (0)