MIPS: println! before return statement changes returned value #35673
Description
Hi all,
Asked about this in rust-beginners and was advised it's probably a bug. On MIPS (specifically, on the Tessel 2, technical overview) I'm getting different (incorrect) results from a function that does a bit of math, depending on:
- if I'm accessing a struct variable from self
- if I'm taking method arguments
- if and where the println!'s are in the method
- the size of the primitives I'm using to calculate each intermediate part (not an issue on my OS X desktop or the playground)
Here's a playground link with my ideal variation of the method; ideally, it should compute baud(9600) = 65326. On MIPS, depending on the various factors I've listed above, I've seen varying results including 0 and 4294967295.
From what I can tell, this is falling apart with the floor() call (which appears to be an intrinsic - something I've seen others have difficulty with on MIPS). Take this snippet:
// wanted_f: f64 = 9600;
let part1: f64 = wanted_f / 48_000000f64;
let part2: f64 = 16f64 * part1;
let part3: f64 = 1f64 - part2;
let part4: f64 = 65536f64 * part3;
let part5: f64 = part4.floor();
let part6: u32 = part5 as u32;
println!("1: {}", part1);
println!("2: {}", part2);
println!("3: {}", part3);
println!("4: {}", part4);
println!("5: {}", part5);
println!("6: {}", part6);
Gives the output:
1: 0.0002
2: 0.0032
3: 0.9968
4: 65326.2848
5: 0.0002
6: 0
(where 5: should be 65326). If I add another intermediate local in between part4 and part5 (let inter: f64 = part4.floor()
), both inter
and part5
have the 0.0002 value when printed.
However, I can seemingly coerce it into working by putting a println! in the right place:
let wanted_f: f64 = self.baudrate as f64;
let baud: f64 = 65536f64 * (1f64 - (16f64 * (wanted_f / 48_000000f64)));
println!("inside fn: {}", baud);
return baud.floor() as u32
This returns the correct result, even on MIPS.