core: Implement feature float_exact_integer_constants#152512
core: Implement feature float_exact_integer_constants#152512okaneco wants to merge 2 commits intorust-lang:mainfrom
float_exact_integer_constants#152512Conversation
|
r? @scottmcm rustbot has assigned @scottmcm. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
You can put this in library/coretests/tests/floats/mod.rs instead, which has macros for handling all the float types. Note that you will need the:
attrs: {
f16: #[cfg(any(miri, target_has_reliable_f16))],
f128: #[cfg(any(miri, target_has_reliable_f128))],
},There was a problem hiding this comment.
Thanks, I completely missed that module. I was going to rewrite to take an optional meta attribute for the original macro I wrote. There were a few more roadbumps along the way, had to also add the reliable f16/f128 in doc tests.
Moved accordingly.
library/core/src/num/f128.rs
Outdated
| /// Values contained inclusively within [`MIN_EXACT_INTEGER`] to | ||
| /// [`MAX_EXACT_INTEGER`] can be losslessly converted between integer | ||
| /// and floating point. |
There was a problem hiding this comment.
It would be good to strengthen that the property is 1:1 rather than being the max roundtripping integer, since based on discussion this is definitely a tripping point. Perhaps something like the following:
Additionally, if a given
x = unknown_int as f128 as i128satisfies the propertyMIN_EXACT_INTEGER <= x <= MAX_EXACT_INTEGER, thenxis guaranteed to have the same value asunknown_int; that is, an integer within this range has a "one to one" mapping withf128.f128::MAX_EXACT_INTEGER + 1technically round trips losslessly withf128but there are multiple values off128that may map tof128::MAX_EXACT_INTEGER + 1, so it does not satisfy this "one to one" property.
There was a problem hiding this comment.
Sure, I incorporated what you wrote and tried not to over-complicate the explanation. I was hoping the line above it could suffice but expected some deliberation on the exact wording.
Maximum integer that can be represented exactly in an
f32value, with no other integer converting to the same floating point value.
| /// [`MIN_EXACT_INTEGER`]: f128::MIN_EXACT_INTEGER | ||
| // #[unstable(feature = "f128", issue = "116909")] | ||
| #[unstable(feature = "float_exact_integer_constants", issue = "152466")] | ||
| pub const MAX_EXACT_INTEGER: i128 = (1 << Self::MANTISSA_DIGITS) - 1; |
There was a problem hiding this comment.
Could you add examples to both constants? It would be good to demo the MAX_EXACT_INTEGER, MAX_EXACT_INTEGER + 1, MAX_EXACT_INTEGER + 2 property.
There was a problem hiding this comment.
I was uncertain about adding examples because every other float const seems to lack examples, but they'd come in handy here.
Added examples.
There was a problem hiding this comment.
I think examples are particularly important here because I would have expected this to be the value without the - 1, so the more opportunities to emphasize its definition the better.
(Things like f32::INFINITY don't have that issue.)
| /// Minimum integer that can be represented exactly in an [`f128`] value, | ||
| /// with no other integer converting to the same floating point value. | ||
| /// | ||
| /// Values contained inclusively within [`MIN_EXACT_INTEGER`] to | ||
| /// [`MAX_EXACT_INTEGER`] can be losslessly converted between integer | ||
| /// and floating point. | ||
| /// | ||
| /// [`MAX_EXACT_INTEGER`]: f128::MAX_EXACT_INTEGER | ||
| /// [`MIN_EXACT_INTEGER`]: f128::MIN_EXACT_INTEGER | ||
| // #[unstable(feature = "f128", issue = "116909")] | ||
| #[unstable(feature = "float_exact_integer_constants", issue = "152466")] | ||
| pub const MIN_EXACT_INTEGER: i128 = -Self::MAX_EXACT_INTEGER; |
There was a problem hiding this comment.
It's worth mentioning that this is just -MAX_EXACT_INTEGER
|
Reminder, once the PR becomes ready for a review, use |
|
r? tgross35 |
a514af5 to
67031fe
Compare
This comment has been minimized.
This comment has been minimized.
|
For the f128 issue, could you try adding a commit that changes the two rust/compiler/rustc_codegen_cranelift/src/codegen_f16_f128.rs Lines 191 to 218 in 47611e1 __fixuntfti but the intrinsic is __fixunstfti.
|
|
Sorry, only the second |
a929ce1 to
21f1b2c
Compare
|
Some changes occurred in compiler/rustc_codegen_cranelift cc @bjorn3 |
This comment has been minimized.
This comment has been minimized.
|
You can use assert_biteq in the tests which should print the values, and is a bit better for this sort of thing anyway. For the failing test I suspect we’re hitting the LLVM20 issue on aarch64 with neon. Why it’s showing up I have no idea, we should be using the version with the patch, but maybe the system libs are built with an older version? After doing a run with assert_biteq to see the values, you could try setting Or if you don’t feel like doing too much digging, you can just mark the f128 tests cfg(false) with a fixme like you originally had, and I’ll chase them down later. Sorry about this mess, I wasn’t aware this would be the first tested exercise of these kind of f128 ops :) |
|
No worries and thanks for all the guidance. Adding the After that, I'll try setting the |
Actually, that's only set for fp16. For fp128, there's no match arm. rust/compiler/rustc_codegen_llvm/src/llvm_util.rs Lines 374 to 399 in 47611e1 rust/compiler/rustc_codegen_llvm/src/llvm_util.rs Lines 401 to 425 in 47611e1 I can try copying that guard, but I don't have a matching issue to leave as a comment...unless we know it was fixed in LLVM 21? |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
8488b61 to
8c3295e
Compare
|
Only the min test is failing, which deals with negative floats. It looks like negative floats are saturating to 0u128 instead of being converted back to i128? // The minimum integer that converts to a unique floating point
// value.
const MIN_EXACT_INTEGER: <Float as TestableFloat>::SInt = Float::MIN_EXACT_INTEGER;
// Same logic as the `max` test, but we work our way leftward
// across the number line from (min_exact + 1) to (min_exact - 2).
let min_plus_one = (MIN_EXACT_INTEGER + 1) as Float as <Float as TestableFloat>::SInt;
// This does an extra round trip back to float for the second operand in
// order to print the results if there is a mismatch
assert_biteq!((MIN_EXACT_INTEGER + 1) as Float, min_plus_one as Float);If this last push doesn't work, I think I'll have to ignore the (min) f128 test in the macro. |
This comment has been minimized.
This comment has been minimized.
|
Thinking back to the first bug with cranelift, it was having trouble finding the |
…uwer Rollup of 14 pull requests Successful merges: - #152323 (Fix ICE in borrowck when recovering `fn_sig` for `-> _`) - #152469 (Remove unused features) - #152515 (Extract `DepKindVTable` constructors to their own module) - #152555 (Port `#[rustc_diagnostic_item]` to the new attribute parsers) - #152218 (Report unconstrained region in hidden types lazily) - #152356 (Improve the `inline_fluent!` macro) - #152392 (Fix ICE in supertrait_vtable_slot when supertrait has missing generics) - #152407 (Add regression test for type_const with unit struct ctor under mGCA) - #152440 (Fix typos and grammar in compiler and build documentation) - #152536 (bootstrap: add explicit UTF-8 encoding to text-mode open() calls) - #152554 (Remove `deprecated_safe` and its corresponding feature gate) - #152556 (doc: move riscv64a23-unknown-linux-gnu to tier 2) - #152563 (Replace "bug" with "issue" in triagebot ping messages) - #152565 (fix missleading error for tuple ctor) Failed merges: - #152512 (core: Implement feature `float_exact_integer_constants`) - #152296 (Port `rust_nonnull_optimization_guaranteed` and `rustc_do_not_const_check` to the new attribute parser)
…uwer Rollup of 14 pull requests Successful merges: - #152323 (Fix ICE in borrowck when recovering `fn_sig` for `-> _`) - #152469 (Remove unused features) - #152515 (Extract `DepKindVTable` constructors to their own module) - #152555 (Port `#[rustc_diagnostic_item]` to the new attribute parsers) - #152218 (Report unconstrained region in hidden types lazily) - #152356 (Improve the `inline_fluent!` macro) - #152392 (Fix ICE in supertrait_vtable_slot when supertrait has missing generics) - #152407 (Add regression test for type_const with unit struct ctor under mGCA) - #152440 (Fix typos and grammar in compiler and build documentation) - #152536 (bootstrap: add explicit UTF-8 encoding to text-mode open() calls) - #152554 (Remove `deprecated_safe` and its corresponding feature gate) - #152556 (doc: move riscv64a23-unknown-linux-gnu to tier 2) - #152563 (Replace "bug" with "issue" in triagebot ping messages) - #152565 (fix missleading error for tuple ctor) Failed merges: - #152512 (core: Implement feature `float_exact_integer_constants`) - #152296 (Port `rust_nonnull_optimization_guaranteed` and `rustc_do_not_const_check` to the new attribute parser)
This comment has been minimized.
This comment has been minimized.
Implement accepted ACP for `MAX_EXACT_INTEGER` and `MIN_EXACT_INTEGER` on `f16`, `f32`, `f64`, and `f128` Add tests to `coretests/tests/floats/mod.rs`
Correct name for intrinsic that converts f128 to u128 Use `to_signed` instead of `from_signed` to ensure proper intrinsic selected for u128/i128
c4582c8 to
6d85426
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
@rustbot ready |
|
@bors r+ |
core: Implement feature `float_exact_integer_constants` Accepted ACP - rust-lang/libs-team#713 (comment) Tracking issue - rust-lang#152466 Implement accepted ACP for `MAX_EXACT_INTEGER` and `MIN_EXACT_INTEGER` on `f16`, `f32`, `f64`, and `f128` Add tests to `coretests/tests/floats/mod.rs`
core: Implement feature `float_exact_integer_constants` Accepted ACP - rust-lang/libs-team#713 (comment) Tracking issue - rust-lang#152466 Implement accepted ACP for `MAX_EXACT_INTEGER` and `MIN_EXACT_INTEGER` on `f16`, `f32`, `f64`, and `f128` Add tests to `coretests/tests/floats/mod.rs`
…uwer Rollup of 7 pull requests Successful merges: - #152001 (mGCA: Validate const literal against expected type) - #152120 (Don't ICE on layout error in vtable computation) - #152512 (core: Implement feature `float_exact_integer_constants`) - #152531 (`proc_macro::bridge`: simplify `ExecutionStrategy` and `DispatcherTrait`) - #152577 (Port #[rustc_proc_macro_decls] to the new attribute parser.) - #152590 (DepGraphQuery: correctly skip adding edges with not-yet-added nodes) - #152612 (Rename `inline_fluent!` to `msg!`)
Accepted ACP - rust-lang/libs-team#713 (comment)
Tracking issue - #152466
Implement accepted ACP for
MAX_EXACT_INTEGERandMIN_EXACT_INTEGERonf16,f32,f64, andf128Add tests to
coretests/tests/floats/mod.rs