-
Notifications
You must be signed in to change notification settings - Fork 951
Float to integer conversion may result in undefined behavior #276
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
Comments
I think maybe we should insert clamping checks for now since that still has not been merged and rust seems to have decided that adding checks was appropriate. |
Yes that sounds reasonable. Float isn't used much anyway and often it's a softfloat implementation (so relatively slow). I think it's reasonable to add checks and look into optimizations later. |
For now I am going to use a |
This has been released with v0.17.0 so now closing. Thanks! |
Once we drop support for LLVM 11, we should start using the LLVM intrinsics instead of doing this manually. |
The Go spec says this about conversions:
In other words, all conversions (including float-to-integer) must be implementation defined and not undefined. At the moment, we use LLVM
fptui
andfptosi
instructions, which have this to say on the matter:This basically means that floating point values that cannot be represented as integers (Infinity, NaN, negative numbers in unsigned integers) will produce an arbitrary value that if used may invoke undefined behavior.
This should be fixed. It is probably easiest to wait until https://reviews.llvm.org/D54749 is merged to avoid bloating the code too much.
Similar issue in Rust: rust-lang/rust#10184
The text was updated successfully, but these errors were encountered: