Skip to content

Floating-point casts (sometimes) don't work on AVR #108489

Closed
rust-lang/compiler-builtins
#527
@Patryk27

Description

@Patryk27

This code:

#[atmega_hal::entry]
fn main() -> ! {
    let dp = Peripherals::take().unwrap();
    let pins = pins!(dp);

    let mut uart = Usart0::<MHz16>::new(
        dp.USART0,
        pins.pd0,
        pins.pd1.into_output(),
        115200u32.into_baudrate(),
    );

    let value: f32 = core::hint::black_box(1.0);
    let a = value as i64;

    ufmt::uwriteln!(&mut uart, "A: {:?}", a.to_le_bytes());

    loop {
        //
    }
}

... prints (under simavr):

A: [1, 0, 0, 0, 0, 0, 0, 0]

... but moving the code into a loop:

loop {
    let value: f32 = core::hint::black_box(1.0);
    let a = value as i64;

    ufmt::uwriteln!(&mut uart, "A: {:?}", a.to_le_bytes());
}

... breaks something:

A: [0, 0, 0, 128, 0, 0, 0, 128]

I'll try to investigate what's going on, I just thought I'll create an issue in case someone else stumbles upon this one as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-floating-pointArea: Floating point numbers and arithmeticC-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