Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upfloating point to floating point casts have undefined behaviour #15536
Comments
This comment has been minimized.
This comment has been minimized.
|
Nominating |
huonw
added
I-nominated
labels
Jul 8, 2014
This comment has been minimized.
This comment has been minimized.
|
I think this is not a 1.0 issue. There is no reasonable backwards incompatible language change we can make here to fix it. The only thing to do is to change the LLVM semantics here to not be UB, but rather be an undefined value. |
This comment has been minimized.
This comment has been minimized.
zwarich
commented
Jul 10, 2014
|
@pcwalton the LLVM semantics here are that the result is an undefined value, not that the instruction exhibits undefined behavior. Otherwise you wouldn't be able to hoist an |
This comment has been minimized.
This comment has been minimized.
|
OK, then I believe this is not a bug, just something to remember to note in the spec. |
This comment has been minimized.
This comment has been minimized.
pnkfelix
added
P-high
and removed
I-nominated
labels
Jul 10, 2014
This comment has been minimized.
This comment has been minimized.
|
@zwarich: Rust allows undefined behaviour if you have an undefined value. An undefined value has no live range so it doesn't correspond to a reserved register with a preserved value and can vary between reads. That means something like |
This comment has been minimized.
This comment has been minimized.
zwarich
commented
Jul 20, 2014
|
@thestinger That's a good point, but that just makes it like the other bugs that aren't in 1.0. |
This comment has been minimized.
This comment has been minimized.
|
Programs can be written depending on the value of undefined behaviour, and changing it to either fail or provide a specific result will break those programs. This isn't C where undefined behaviour is a problem in user code. |
This comment has been minimized.
This comment has been minimized.
|
We may need a UB label for such issues IMO |
arielb1
referenced this issue
Jun 15, 2015
Closed
LLVM undef with overflowing floating-point -> integer casts #24331
This comment has been minimized.
This comment has been minimized.
|
Can anybody reproduce this undefined behaviour? In #![crate_type="rlib"]
const f:f64 = 1.0E+300;
pub static ffull:f64 = f;
pub static ftrunc:f32 = f as f32;
pub static f2i:u64 = f as u64;
pub static i2f:f32 = 0xFFFFFFFFFFFFFFFFu64 as f32;
I asked for additional information on llvm-dev |
This comment has been minimized.
This comment has been minimized.
|
@ranma42 From what I understood from the reference, the LLVM |
pnkfelix
added
T-compiler
T-lang
labels
Feb 9, 2016
nrc
added
P-medium
and removed
P-high
labels
Mar 31, 2016
This comment has been minimized.
This comment has been minimized.
|
(moving this back to P-medium and letting the |
This comment has been minimized.
This comment has been minimized.
|
My thought is to define this as resulting as infinity. |
This comment has been minimized.
This comment has been minimized.
|
@drbo But doing that could be potentially more expensive than just making the result unspecified but without causing undefined behavior. |
Mark-Simulacrum
added
C-bug
and removed
C-enhancement
labels
Jul 21, 2017
Mark-Simulacrum
removed
the
I-wrong
label
Jul 28, 2017
alexcrichton
referenced this issue
Sep 12, 2017
Closed
Tracking issue for 128-bit integer support (RFC 1504) #35118
This comment has been minimized.
This comment has been minimized.
|
I believe this ("if it doesn't fit in the destination type, you get UB/undef") is and always has been FUD caused by individual LLVM developers misunderstanding IEEE 754, just like with signalling NaNs. LLVM's actual long-standing policy (now finally documented) — and the only policy that makes sense for an optimizing C compiler IMO — is that everything happens in the default floating point environment. This means among other things that rounding is always ties-to-even, the status flags are never inspected, and floating point exception handling always uses the default behavior rather than any custom handling. Consequently, it is clear as day that a floating point truncation that exceeds the range of the target type should result in positive or negative infinity. And indeed that is what LLVM's constant folding will do (specifically, what APFloat will do). FWIW this operation is a standard operation (formatOf-convertFormat), defined in §5.4.2 of IEEE 754-2008, but it would not really matter if this was a non-standard operation since those follow the same basic rules. |
This comment has been minimized.
This comment has been minimized.
|
So I recommend:
|
This comment has been minimized.
This comment has been minimized.
|
@nagisa kindly filed an LLVM bug https://bugs.llvm.org/show_bug.cgi?id=36966 |
This comment has been minimized.
This comment has been minimized.
|
Commit https://reviews.llvm.org/rL329065 removed the quoted statement (and also the stuff about undefined rounding mode) and replaced it with
Since this was just a doc clarification and in reality LLVM always behaved as we wanted it to, there's no need to wait until an LLVM update to pick up any code changes related to this. So we can close this issue now. |
huonw commentedJul 8, 2014
http://llvm.org/docs/LangRef.html#fptrunc-to-instruction
e.g.
1e300f64 as f32cc #10184, #10185