-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Tracking issue for {f32,f64}::to_int_unchecked
methods
#67058
Comments
We briefly discussed this in the lang team meeting today, and felt that stabilizing this would help unblock progress on making @Amanieu, would you be up for proposing FCP merge here to stabilize the methods (but not the backing trait)? |
I have some concerns regarding the naming of these methods. First of all, the general convention in all existing methods is to put Secondly, I feel that I propose that these methods be renamed to |
I plan to post a PR soon with the renaming to |
Proposing to merge this with the name @rfcbot fcp merge |
Team member @Amanieu has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
Renaming sounds fine. I’d just like to point out that we may want multiple float-to-int conversion methods (with different semantics) in the future: https://internals.rust-lang.org/t/pre-rfc-add-explicitly-named-numeric-conversion-apis/11395 |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
I've opened up a PR with the rename and stabilization: #70487 |
{f32,f64}::approx_unchecked_to
methods{f32,f64}::to_int_unchecked
methods
In Rust we have arithmetic operations that are checked in debug builds and unchecked in release builds. I think I'd like a FP->int conversion that does the same. Is this a good idea? |
@leonardo-m This has been discussed at length in #10184. An important difference is that unchecked arithmetic can let overflow happen which could could logic bugs, but the uncheked float to int conversion can causes Undefined Behavior which should never happen in Rust without |
Even if we take "unchecked in release mode" to mean "some defined behavior" rather than UB, the trade-offs are very different:
|
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. The RFC will be merged soon. |
…ts, r=SimonSapin Stabilize float::to_int_unchecked This renames and stabilizes unsafe floating point to integer casts, which are intended to be the substitute for the currently unsound `as` behavior, once that changes to safe-but-slower saturating casts. As such, I believe this also likely unblocks rust-lang#10184 (our oldest I-unsound issue!), as once this rolls out to stable it would be far easier IMO to change the behavior of `as` to be safe by default. This does not stabilize the trait or the associated method, as they are deemed internal implementation details (and consumers should not, generally, want to expose them, as in practice all callers likely know statically/without generics what the return type is). Closes rust-lang#67058
I realize this is an old issue but I still want to leave this comment. The way to_int_unchecked works today is that it is unsafe. You need to verify the invariants (float is in range of the integer) before calling it. This is the same as in C++. There is an argument that this function should not be unsafe. On some (most?) hardware there is no undefined behavior for out of bounds. This could be turned into unspecified behavior where the resulting integer value is unspecified but safe. For example on x86 the cvttss2si instruction performs the conversion and this does not have undefined behavior. This means on x86, this function is safe for free (no extra overhead). If it works like this on most platforms, then little is lost by making it safe. We only lose flexibility/performance on (potentially future) platforms this doesn't work. On other platforms we gain something by making this function easier to use. And we gain performance because you can use the function without checking for bounds first. We could also keep to_int_unchecked the way it is and add a new function to_int_unspecified that does what I described. The reason I found this issue is that I implemented that in a crate here. |
I agree that these should return an unspecified value instead of exhibiting undefined behaviour in release mode. However the function should also be allowed to diverge (panic or abort). In practice it should behave like overflowing integer operations, i.e. panic if integer overflow checks are enabled. |
First discussed in issue #10184.
As of Rust 1.39, casting a floating point number to an integer with
as
is Undefined Behavior if the value is out of range.-Z saturating-float-casts
fixes this soundness hole by makingas
“saturate” to the maximum or minimum value of the integer type (or zero forNaN
), but has measurable negative performance impact in some benchmarks. There is some consensus in that thread for enabling saturation by default anyway, but provide anunsafe fn
alternative for users who know through some other mean that their values are in range.PR #66841 adds that method to each of
f32
andf64
.The
FloatToInt
trait (tracking issue: #67057) has macro-generatedimpl
s for all combinations of primitive integer and primitive floating point types:The text was updated successfully, but these errors were encountered: