about summary refs log tree commit diff
path: root/tests/ui/consts/const-eval/float_methods.rs
diff options
context:
space:
mode:
authorEduardo Sánchez Muñoz <eduardosm-dev@e64.io>2024-10-14 21:02:13 +0200
committerEduardo Sánchez Muñoz <eduardosm-dev@e64.io>2024-10-15 10:46:33 +0200
commitc09ed3e767a73d83673790f74c357432fa44d320 (patch)
treee3bbe99f105b4decc28ba207f89737ef16b021bc /tests/ui/consts/const-eval/float_methods.rs
parentb73e613e008fd4a07a52ec7cef7c3af7db716b3d (diff)
downloadrust-c09ed3e767a73d83673790f74c357432fa44d320.tar.gz
rust-c09ed3e767a73d83673790f74c357432fa44d320.zip
Make some float methods unstable `const fn`
Some float methods are now `const fn` under the `const_float_methods` feature gate.

In order to support `min`, `max`, `abs` and `copysign`, the implementation of some intrinsics had to be moved from Miri to rustc_const_eval.
Diffstat (limited to 'tests/ui/consts/const-eval/float_methods.rs')
-rw-r--r--tests/ui/consts/const-eval/float_methods.rs47
1 files changed, 47 insertions, 0 deletions
diff --git a/tests/ui/consts/const-eval/float_methods.rs b/tests/ui/consts/const-eval/float_methods.rs
new file mode 100644
index 00000000000..49c31f68c5f
--- /dev/null
+++ b/tests/ui/consts/const-eval/float_methods.rs
@@ -0,0 +1,47 @@
+//@ run-pass
+//! Tests the float intrinsics: min, max, abs, copysign
+
+#![feature(const_float_methods)]
+#![feature(f16, f128)]
+
+const F16_MIN: f16 = 1.0_f16.min(0.5_f16);
+const F16_MAX: f16 = 1.0_f16.max(0.5_f16);
+const F16_ABS: f16 = (-1.0_f16).abs();
+const F16_COPYSIGN: f16 = 1.0_f16.copysign(-2.0_f16);
+
+const F32_MIN: f32 = 1.0_f32.min(0.5_f32);
+const F32_MAX: f32 = 1.0_f32.max(0.5_f32);
+const F32_ABS: f32 = (-1.0_f32).abs();
+const F32_COPYSIGN: f32 = 1.0_f32.copysign(-2.0_f32);
+
+const F64_MIN: f64 = 1.0_f64.min(0.5_f64);
+const F64_MAX: f64 = 1.0_f64.max(0.5_f64);
+const F64_ABS: f64 = (-1.0_f64).abs();
+const F64_COPYSIGN: f64 = 1.0_f64.copysign(-2.0_f64);
+
+const F128_MIN: f128 = 1.0_f128.min(0.5_f128);
+const F128_MAX: f128 = 1.0_f128.max(0.5_f128);
+const F128_ABS: f128 = (-1.0_f128).abs();
+const F128_COPYSIGN: f128 = 1.0_f128.copysign(-2.0_f128);
+
+fn main() {
+    assert_eq!(F16_MIN, 0.5);
+    assert_eq!(F16_MAX, 1.0);
+    assert_eq!(F16_ABS, 1.0);
+    assert_eq!(F16_COPYSIGN, -1.0);
+
+    assert_eq!(F32_MIN, 0.5);
+    assert_eq!(F32_MAX, 1.0);
+    assert_eq!(F32_ABS, 1.0);
+    assert_eq!(F32_COPYSIGN, -1.0);
+
+    assert_eq!(F64_MIN, 0.5);
+    assert_eq!(F64_MAX, 1.0);
+    assert_eq!(F64_ABS, 1.0);
+    assert_eq!(F64_COPYSIGN, -1.0);
+
+    assert_eq!(F128_MIN, 0.5);
+    assert_eq!(F128_MAX, 1.0);
+    assert_eq!(F128_ABS, 1.0);
+    assert_eq!(F128_COPYSIGN, -1.0);
+}