Skip to content

Bit-shifts with dynamic RHSs are broken on AVR on large numbers (u64, u128, i64, i128) #106135

Closed
@Patryk27

Description

@Patryk27

Bit-shifts where the right-hand side is of statically unknown value yield spurious results - e.g.:

#[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);

    let x = 1;
    let y = hint::black_box(1);
    let val: u64 = x << y;

    for b in val.to_le_bytes() {
        _ = ufmt::uwrite!(&mut serial, "{} ", b);
    }

    _ = ufmt::uwriteln!(&mut serial, "");

    loop {
        //
    }
}

... prints:

0 0 0 4 0 0 0 0

For u128 we get something even wilder:

1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0

Smaller types, such as u32, seem to behave correctly:

2 0 0 0

My rough guess lays in the vicinity of AVRShiftExpand::expand() or compiler-builtins (ABI mismatch?), but I'm yet to confirm it.

Note that this is mostly an informational report - I'll try to fix this bug;

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.O-AVRTarget: AVR processors (ATtiny, ATmega, etc.)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions