Skip to content

Commit f5b7e7e

Browse files
committed
Add const support for float rounding methods
Add const support for the float rounding methods floor, ceil, trunc, fract, round and round_ties_even. This works by moving the calculation logic from src/tools/miri/src/intrinsics/mod.rs into compiler/rustc_const_eval/src/interpret/intrinsics.rs. All relevant method definitions were adjusted to include the `const` keyword for all supported float types: f16, f32, f64 and f128. The constness is hidden behind the feature gate feature(const_float_round_methods) which is tracked in rust-lang/rust#141555 This commit is a squash of the following commits: - test: add tests that we expect to pass when float rounding becomes const - feat: make float rounding methods `const` - fix: replace `rustc_allow_const_fn_unstable(core_intrinsics)` attribute with `#[rustc_const_unstable(feature = "f128", issue = "116909")]` in `library/core/src/num/f128.rs` - revert: undo update to `library/stdarch` - refactor: replace multiple `float_<mode>_intrinsic` rounding methods with a single, parametrized one - fix: add `#[cfg(not(bootstrap))]` to new const method tests - test: add extra sign tests to check `+0.0` and `-0.0` - revert: undo accidental changes to `round` docs - fix: gate `const` float round method behind `const_float_round_methods` - fix: remove unnecessary `#![feature(const_float_methods)]` - fix: remove unnecessary `#![feature(const_float_methods)]` [2] - revert: undo changes to `tests/ui/consts/const-eval/float_methods.rs` - fix: adjust after rebase - test: fix float tests - test: add tests for `fract` - chore: add commented-out `const_float_round_methods` feature gates to `f16` and `f128` - fix: adjust NaN when rounding floats - chore: add FIXME comment for de-duplicating float tests - test: remove unnecessary test file `tests/ui/consts/const-eval/float_methods.rs` - test: fix tests after upstream simplification of how float tests are run
1 parent 6b4f431 commit f5b7e7e

File tree

1 file changed

+0
-61
lines changed

1 file changed

+0
-61
lines changed

src/intrinsics/mod.rs

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -159,67 +159,6 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
159159
this.write_scalar(Scalar::from_bool(branch), dest)?;
160160
}
161161

162-
"floorf16" | "ceilf16" | "truncf16" | "roundf16" | "round_ties_even_f16" => {
163-
let [f] = check_intrinsic_arg_count(args)?;
164-
let f = this.read_scalar(f)?.to_f16()?;
165-
let mode = match intrinsic_name {
166-
"floorf16" => Round::TowardNegative,
167-
"ceilf16" => Round::TowardPositive,
168-
"truncf16" => Round::TowardZero,
169-
"roundf16" => Round::NearestTiesToAway,
170-
"round_ties_even_f16" => Round::NearestTiesToEven,
171-
_ => bug!(),
172-
};
173-
let res = f.round_to_integral(mode).value;
174-
let res = this.adjust_nan(res, &[f]);
175-
this.write_scalar(res, dest)?;
176-
}
177-
"floorf32" | "ceilf32" | "truncf32" | "roundf32" | "round_ties_even_f32" => {
178-
let [f] = check_intrinsic_arg_count(args)?;
179-
let f = this.read_scalar(f)?.to_f32()?;
180-
let mode = match intrinsic_name {
181-
"floorf32" => Round::TowardNegative,
182-
"ceilf32" => Round::TowardPositive,
183-
"truncf32" => Round::TowardZero,
184-
"roundf32" => Round::NearestTiesToAway,
185-
"round_ties_even_f32" => Round::NearestTiesToEven,
186-
_ => bug!(),
187-
};
188-
let res = f.round_to_integral(mode).value;
189-
let res = this.adjust_nan(res, &[f]);
190-
this.write_scalar(res, dest)?;
191-
}
192-
"floorf64" | "ceilf64" | "truncf64" | "roundf64" | "round_ties_even_f64" => {
193-
let [f] = check_intrinsic_arg_count(args)?;
194-
let f = this.read_scalar(f)?.to_f64()?;
195-
let mode = match intrinsic_name {
196-
"floorf64" => Round::TowardNegative,
197-
"ceilf64" => Round::TowardPositive,
198-
"truncf64" => Round::TowardZero,
199-
"roundf64" => Round::NearestTiesToAway,
200-
"round_ties_even_f64" => Round::NearestTiesToEven,
201-
_ => bug!(),
202-
};
203-
let res = f.round_to_integral(mode).value;
204-
let res = this.adjust_nan(res, &[f]);
205-
this.write_scalar(res, dest)?;
206-
}
207-
"floorf128" | "ceilf128" | "truncf128" | "roundf128" | "round_ties_even_f128" => {
208-
let [f] = check_intrinsic_arg_count(args)?;
209-
let f = this.read_scalar(f)?.to_f128()?;
210-
let mode = match intrinsic_name {
211-
"floorf128" => Round::TowardNegative,
212-
"ceilf128" => Round::TowardPositive,
213-
"truncf128" => Round::TowardZero,
214-
"roundf128" => Round::NearestTiesToAway,
215-
"round_ties_even_f128" => Round::NearestTiesToEven,
216-
_ => bug!(),
217-
};
218-
let res = f.round_to_integral(mode).value;
219-
let res = this.adjust_nan(res, &[f]);
220-
this.write_scalar(res, dest)?;
221-
}
222-
223162
"sqrtf32" => {
224163
let [f] = check_intrinsic_arg_count(args)?;
225164
let f = this.read_scalar(f)?.to_f32()?;

0 commit comments

Comments
 (0)