about summary refs log tree commit diff
path: root/src/test/codegen
diff options
context:
space:
mode:
authorRobin Kruppe <robin.kruppe@gmail.com>2017-10-09 02:14:00 +0200
committerRobin Kruppe <robin.kruppe@gmail.com>2017-11-07 20:13:19 +0100
commit0d6b52c2f3d442d3edfea31bcc439127a4757e5e (patch)
tree340f0188cb0c4953d2f1bfa10e5a2ad46cf4a039 /src/test/codegen
parent7ade24f67201531778e7674b4b63ebf1a23c9643 (diff)
downloadrust-0d6b52c2f3d442d3edfea31bcc439127a4757e5e.tar.gz
rust-0d6b52c2f3d442d3edfea31bcc439127a4757e5e.zip
Saturating casts between integers and floats (both directions).
This affects regular code generation as well as constant evaluation in trans,
but not the HIR constant evaluator because that one returns an error for
overflowing casts and NaN-to-int casts. That error is conservatively
correct and we should be careful to not accept more code in constant
expressions.
The changes to code generation are guarded by a new -Z flag, to be able
to evaluate the performance impact. The trans constant evaluation changes
are unconditional because they have no run time impact and don't affect
type checking either.
Diffstat (limited to 'src/test/codegen')
-rw-r--r--src/test/codegen/unchecked-float-casts.rs65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/test/codegen/unchecked-float-casts.rs b/src/test/codegen/unchecked-float-casts.rs
new file mode 100644
index 00000000000..64ab19cceee
--- /dev/null
+++ b/src/test/codegen/unchecked-float-casts.rs
@@ -0,0 +1,65 @@
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// compile-flags: -C no-prepopulate-passes
+
+// This file tests that we don't generate any code for saturation if
+// -Z saturating-float-casts is not enabled.
+
+#![crate_type = "lib"]
+#![feature(i128_type)]
+
+// CHECK-LABEL: @f32_to_u32
+#[no_mangle]
+pub fn f32_to_u32(x: f32) -> u32 {
+    // CHECK: fptoui
+    // CHECK-NOT: fcmp
+    // CHECK-NOT: icmp
+    // CHECK-NOT: select
+    x as u32
+}
+
+// CHECK-LABEL: @f32_to_i32
+#[no_mangle]
+pub fn f32_to_i32(x: f32) -> i32 {
+    // CHECK: fptosi
+    // CHECK-NOT: fcmp
+    // CHECK-NOT: icmp
+    // CHECK-NOT: select
+    x as i32
+}
+
+#[no_mangle]
+pub fn f64_to_u8(x: f32) -> u16 {
+    // CHECK-NOT: fcmp
+    // CHECK-NOT: icmp
+    // CHECK-NOT: select
+    x as u16
+}
+
+// CHECK-LABEL: @i32_to_f64
+#[no_mangle]
+pub fn i32_to_f64(x: i32) -> f64 {
+    // CHECK: sitofp
+    // CHECK-NOT: fcmp
+    // CHECK-NOT: icmp
+    // CHECK-NOT: select
+    x as f64
+}
+
+// CHECK-LABEL: @u128_to_f32
+#[no_mangle]
+pub fn u128_to_f32(x: u128) -> f32 {
+    // CHECK: uitofp
+    // CHECK-NOT: fcmp
+    // CHECK-NOT: icmp
+    // CHECK-NOT: select
+    x as f32
+}