Skip to content

Commit 19e63bd

Browse files
committed
Auto merge of #45900 - rkruppe:u128-to-f32-saturation-by-default, r=alexcrichton
Make saturating u128 -> f32 casts the default behavior ... rather than being gated by `-Z saturating-float-casts`. There are several reasons for this: 1. Const eval already implements this behavior. 2. Unlike with float->int casts, this behavior is uncontroversially the right behavior and it is not as performance critical. Thus there is no particular need to make the bug fix for u128->f32 casts opt-in. 3. Having two orthogonal features under one flag is silly, and never should have happened in the first place. 4. Benchmarking float->int casts with the -Z flag should not pick up performance changes due to the u128->f32 casts (assuming there are any). Fixes #41799
2 parents b087ded + 5952441 commit 19e63bd

File tree

5 files changed

+64
-45
lines changed

5 files changed

+64
-45
lines changed

src/librustc/session/config.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1138,8 +1138,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
11381138
tls_model: Option<String> = (None, parse_opt_string, [TRACKED],
11391139
"choose the TLS model to use (rustc --print tls-models for details)"),
11401140
saturating_float_casts: bool = (false, parse_bool, [TRACKED],
1141-
"make casts between integers and floats safe: clip out-of-range inputs to the min/max \
1142-
integer or to infinity respectively, and turn `NAN` into 0 when casting to integers"),
1141+
"make float->int casts UB-free: numbers outside the integer type's range are clipped to \
1142+
the max/min integer respectively, and NaN is mapped to 0"),
11431143
}
11441144

11451145
pub fn default_lib_output() -> CrateType {

src/librustc_trans/mir/rvalue.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,7 @@ fn cast_int_to_float(bcx: &Builder,
827827
// It's only u128 -> f32 that can cause overflows (i.e., should yield infinity).
828828
// LLVM's uitofp produces undef in those cases, so we manually check for that case.
829829
let is_u128_to_f32 = !signed && int_ty.int_width() == 128 && float_ty.float_width() == 32;
830-
if is_u128_to_f32 && bcx.sess().opts.debugging_opts.saturating_float_casts {
830+
if is_u128_to_f32 {
831831
// All inputs greater or equal to (f32::MAX + 0.5 ULP) are rounded to infinity,
832832
// and for everything else LLVM's uitofp works just fine.
833833
let max = C_big_integral(int_ty, MAX_F32_PLUS_HALF_ULP);

src/test/codegen/unchecked-float-casts.rs

+2-21
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,10 @@ pub fn f32_to_i32(x: f32) -> i32 {
3737
}
3838

3939
#[no_mangle]
40-
pub fn f64_to_u8(x: f32) -> u16 {
40+
pub fn f64_to_u16(x: f64) -> u16 {
41+
// CHECK: fptoui
4142
// CHECK-NOT: fcmp
4243
// CHECK-NOT: icmp
4344
// CHECK-NOT: select
4445
x as u16
4546
}
46-
47-
// CHECK-LABEL: @i32_to_f64
48-
#[no_mangle]
49-
pub fn i32_to_f64(x: i32) -> f64 {
50-
// CHECK: sitofp
51-
// CHECK-NOT: fcmp
52-
// CHECK-NOT: icmp
53-
// CHECK-NOT: select
54-
x as f64
55-
}
56-
57-
// CHECK-LABEL: @u128_to_f32
58-
#[no_mangle]
59-
pub fn u128_to_f32(x: u128) -> f32 {
60-
// CHECK: uitofp
61-
// CHECK-NOT: fcmp
62-
// CHECK-NOT: icmp
63-
// CHECK-NOT: select
64-
x as f32
65-
}

src/test/run-pass/saturating-float-casts.rs

+1-21
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// Tests saturating float->int casts. See u128-as-f32.rs for the opposite direction.
1112
// compile-flags: -Z saturating-float-casts
1213

1314
#![feature(test, i128, i128_type, stmt_expr_attributes)]
@@ -139,26 +140,5 @@ pub fn main() {
139140
// nextDown(f32::MAX) = 2^128 - 2 * 2^104
140141
const SECOND_LARGEST_F32: f32 = 340282326356119256160033759537265639424.;
141142
test_c!(SECOND_LARGEST_F32, f32 -> u128, 0xfffffe00000000000000000000000000);
142-
143-
// int->float:
144-
// f32::MAX - 0.5 ULP and smaller should be rounded down
145-
test_c!(0xfffffe00000000000000000000000000, u128 -> f32, SECOND_LARGEST_F32);
146-
test_c!(0xfffffe7fffffffffffffffffffffffff, u128 -> f32, SECOND_LARGEST_F32);
147-
test_c!(0xfffffe80000000000000000000000000, u128 -> f32, SECOND_LARGEST_F32);
148-
// numbers within < 0.5 ULP of f32::MAX it should be rounded to f32::MAX
149-
test_c!(0xfffffe80000000000000000000000001, u128 -> f32, f32::MAX);
150-
test_c!(0xfffffeffffffffffffffffffffffffff, u128 -> f32, f32::MAX);
151-
test_c!(0xffffff00000000000000000000000000, u128 -> f32, f32::MAX);
152-
test_c!(0xffffff00000000000000000000000001, u128 -> f32, f32::MAX);
153-
test_c!(0xffffff7fffffffffffffffffffffffff, u128 -> f32, f32::MAX);
154-
// f32::MAX + 0.5 ULP and greater should be rounded to infinity
155-
test_c!(0xffffff80000000000000000000000000, u128 -> f32, f32::INFINITY);
156-
test_c!(0xffffff80000000f00000000000000000, u128 -> f32, f32::INFINITY);
157-
test_c!(0xffffff87ffffffffffffffff00000001, u128 -> f32, f32::INFINITY);
158-
159-
// u128->f64 should not be affected by the u128->f32 checks
160-
test_c!(0xffffff80000000000000000000000000, u128 -> f64,
161-
340282356779733661637539395458142568448.0);
162-
test_c!(u128::MAX, u128 -> f64, 340282366920938463463374607431768211455.0);
163143
}
164144
}

src/test/run-pass/u128-as-f32.rs

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// ignore-emscripten u128 not supported
12+
13+
#![feature(test, i128, i128_type)]
14+
#![deny(overflowing_literals)]
15+
extern crate test;
16+
17+
use std::f32;
18+
use std::u128;
19+
use test::black_box;
20+
21+
macro_rules! test {
22+
($val:expr, $src_ty:ident -> $dest_ty:ident, $expected:expr) => ({
23+
{
24+
const X: $src_ty = $val;
25+
const Y: $dest_ty = X as $dest_ty;
26+
assert_eq!(Y, $expected,
27+
"const eval {} -> {}", stringify!($src_ty), stringify!($dest_ty));
28+
}
29+
// black_box disables constant evaluation to test run-time conversions:
30+
assert_eq!(black_box::<$src_ty>($val) as $dest_ty, $expected,
31+
"run-time {} -> {}", stringify!($src_ty), stringify!($dest_ty));
32+
});
33+
}
34+
35+
pub fn main() {
36+
// nextDown(f32::MAX) = 2^128 - 2 * 2^104
37+
const SECOND_LARGEST_F32: f32 = 340282326356119256160033759537265639424.;
38+
39+
// f32::MAX - 0.5 ULP and smaller should be rounded down
40+
test!(0xfffffe00000000000000000000000000, u128 -> f32, SECOND_LARGEST_F32);
41+
test!(0xfffffe7fffffffffffffffffffffffff, u128 -> f32, SECOND_LARGEST_F32);
42+
test!(0xfffffe80000000000000000000000000, u128 -> f32, SECOND_LARGEST_F32);
43+
// numbers within < 0.5 ULP of f32::MAX it should be rounded to f32::MAX
44+
test!(0xfffffe80000000000000000000000001, u128 -> f32, f32::MAX);
45+
test!(0xfffffeffffffffffffffffffffffffff, u128 -> f32, f32::MAX);
46+
test!(0xffffff00000000000000000000000000, u128 -> f32, f32::MAX);
47+
test!(0xffffff00000000000000000000000001, u128 -> f32, f32::MAX);
48+
test!(0xffffff7fffffffffffffffffffffffff, u128 -> f32, f32::MAX);
49+
// f32::MAX + 0.5 ULP and greater should be rounded to infinity
50+
test!(0xffffff80000000000000000000000000, u128 -> f32, f32::INFINITY);
51+
test!(0xffffff80000000f00000000000000000, u128 -> f32, f32::INFINITY);
52+
test!(0xffffff87ffffffffffffffff00000001, u128 -> f32, f32::INFINITY);
53+
54+
// u128->f64 should not be affected by the u128->f32 checks
55+
test!(0xffffff80000000000000000000000000, u128 -> f64,
56+
340282356779733661637539395458142568448.0);
57+
test!(u128::MAX, u128 -> f64, 340282366920938463463374607431768211455.0);
58+
}

0 commit comments

Comments
 (0)