-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
[AVR] Shift left <<
doesn't work correctly on >= 32-bit integers on a 16-bit platform
#82380
Comments
This probably is related to #82242, because shifting does work for #![no_std]
#![no_main]
extern crate panic_halt;
use arduino_uno::prelude::*;
use ufmt::uwriteln;
#[arduino_uno::entry]
fn main() -> ! {
let dp = arduino_uno::Peripherals::take().unwrap();
let mut pins = arduino_uno::Pins::new(dp.PORTB, dp.PORTC, dp.PORTD);
let mut serial = arduino_uno::Serial::new(
dp.USART0,
pins.d0,
pins.d1.into_output(&mut pins.ddr),
57600.into_baudrate(),
);
for i in 0u16..16u16 {
uwriteln!(&mut serial, "1 << {}\r", i).void_unwrap();
let shl: u16 = 1u16 << i;
uwriteln!(&mut serial, "Shift: {:?}\r", shl.to_le_bytes()).void_unwrap();
let mul: u16 = 1u16 * 2u16.pow(i as u32);
uwriteln!(&mut serial, "Mul and pow: {:?}\r\n", mul.to_le_bytes()).void_unwrap();
}
loop {}
} Output:
|
<<
seems to be broken<<
doesn't work correctly on >= 32-bit integers on a 16-bit platform
Seems to be the same root cause as #82242; the workaround helps 🙂 |
I just bumped into this on nighty-2022-12-09, one and a half years after #82242 got closed. Indeed the workaround in #82242 (comment) solved my issue. However, with #82242 closed already, why is the workaround still needed? Is it simply a matter of some |
There is indeed probably a need for checks and fixups in that general vein specific to |
fwiw, this got fixed with #114048 🙂 |
Shift left seems to not behave correctly, but doing the same resulting operation using multiplication does work as a workaround. This occurs both when using the
<<
operator and when usingoverflowing_shl
. This was tested on an Arduino Nano (actually an Elegoo knockoff with a CH340, link).I tried this code:
Output:
Same code, except `u32` instead of `u64`
Meta
rustc --version --verbose
:This might be related to #82242. As in that issue, I am using this version of nightly because it's the latest that supports AVR, and it must be built using
--release
.Full reproduction repository: https://github.com/jacobmischka/rust-avr-shl
The text was updated successfully, but these errors were encountered: