about summary refs log tree commit diff
diff options
context:
space:
mode:
authorTrevor Gross <tmgross@umich.edu>2025-01-23 22:30:39 -0600
committerGitHub <noreply@github.com>2025-01-23 22:30:39 -0600
commitdf5ac49390a9157877625ac132f9739f90efd6de (patch)
treefa3384e2dd9eb9ce3ec823223f548a74a83bda36
parent453bebf7c1fda9ff78a41d7739f0d930d1e27755 (diff)
parent6d5105c00636a4f8a8c8c76217db7f59ebd1f846 (diff)
downloadrust-df5ac49390a9157877625ac132f9739f90efd6de.tar.gz
rust-df5ac49390a9157877625ac132f9739f90efd6de.zip
Merge pull request rust-lang/libm#466 from tgross35/generic-fmin-fmax
Add `fminf16`, `fmaxf16`, `fminf128`, and `fmaxf128`
-rw-r--r--library/compiler-builtins/libm/crates/libm-macros/src/shared.rs4
-rw-r--r--library/compiler-builtins/libm/crates/libm-test/benches/random.rs4
-rw-r--r--library/compiler-builtins/libm/crates/libm-test/src/mpfloat.rs4
-rw-r--r--library/compiler-builtins/libm/crates/libm-test/tests/compare_built_musl.rs4
-rw-r--r--library/compiler-builtins/libm/crates/util/src/main.rs4
-rw-r--r--library/compiler-builtins/libm/etc/function-definitions.json40
-rw-r--r--library/compiler-builtins/libm/etc/function-list.txt4
-rw-r--r--library/compiler-builtins/libm/src/math/fmax.rs11
-rw-r--r--library/compiler-builtins/libm/src/math/fmaxf.rs11
-rw-r--r--library/compiler-builtins/libm/src/math/fmaxf128.rs5
-rw-r--r--library/compiler-builtins/libm/src/math/fmaxf16.rs5
-rw-r--r--library/compiler-builtins/libm/src/math/fmin.rs11
-rw-r--r--library/compiler-builtins/libm/src/math/fminf.rs11
-rw-r--r--library/compiler-builtins/libm/src/math/fminf128.rs5
-rw-r--r--library/compiler-builtins/libm/src/math/fminf16.rs5
-rw-r--r--library/compiler-builtins/libm/src/math/generic/fmax.rs14
-rw-r--r--library/compiler-builtins/libm/src/math/generic/fmin.rs13
-rw-r--r--library/compiler-builtins/libm/src/math/generic/mod.rs4
-rw-r--r--library/compiler-builtins/libm/src/math/mod.rs8
19 files changed, 123 insertions, 44 deletions
diff --git a/library/compiler-builtins/libm/crates/libm-macros/src/shared.rs b/library/compiler-builtins/libm/crates/libm-macros/src/shared.rs
index b233e34f182..fbe0702a60d 100644
--- a/library/compiler-builtins/libm/crates/libm-macros/src/shared.rs
+++ b/library/compiler-builtins/libm/crates/libm-macros/src/shared.rs
@@ -47,7 +47,7 @@ const ALL_OPERATIONS_NESTED: &[(FloatTy, Signature, Option<Signature>, &[&str])]
         FloatTy::F16,
         Signature { args: &[Ty::F16, Ty::F16], returns: &[Ty::F16] },
         None,
-        &["copysignf16", "fdimf16"],
+        &["copysignf16", "fdimf16", "fmaxf16", "fminf16"],
     ),
     (
         // `(f32, f32) -> f32`
@@ -90,7 +90,7 @@ const ALL_OPERATIONS_NESTED: &[(FloatTy, Signature, Option<Signature>, &[&str])]
         FloatTy::F128,
         Signature { args: &[Ty::F128, Ty::F128], returns: &[Ty::F128] },
         None,
-        &["copysignf128", "fdimf128"],
+        &["copysignf128", "fdimf128", "fmaxf128", "fminf128"],
     ),
     (
         // `(f32, f32, f32) -> f32`
diff --git a/library/compiler-builtins/libm/crates/libm-test/benches/random.rs b/library/compiler-builtins/libm/crates/libm-test/benches/random.rs
index d0ecd851e34..aac8379fd42 100644
--- a/library/compiler-builtins/libm/crates/libm-test/benches/random.rs
+++ b/library/compiler-builtins/libm/crates/libm-test/benches/random.rs
@@ -127,6 +127,10 @@ libm_macros::for_each_function! {
         | fdimf16
         | floorf128
         | floorf16
+        | fmaxf128
+        | fmaxf16
+        | fminf128
+        | fminf16
         | rintf128
         | rintf16
         | roundf128
diff --git a/library/compiler-builtins/libm/crates/libm-test/src/mpfloat.rs b/library/compiler-builtins/libm/crates/libm-test/src/mpfloat.rs
index 4422ab88d0d..da674c16273 100644
--- a/library/compiler-builtins/libm/crates/libm-test/src/mpfloat.rs
+++ b/library/compiler-builtins/libm/crates/libm-test/src/mpfloat.rs
@@ -192,8 +192,8 @@ libm_macros::for_each_function! {
         fabs | fabsf => abs,
         fdim | fdimf | fdimf16 | fdimf128  => positive_diff,
         fma | fmaf => mul_add,
-        fmax | fmaxf => max,
-        fmin | fminf => min,
+        fmax | fmaxf | fmaxf16 | fmaxf128 => max,
+        fmin | fminf | fminf16 | fminf128 => min,
         lgamma | lgammaf => ln_gamma,
         log | logf => ln,
         log1p | log1pf => ln_1p,
diff --git a/library/compiler-builtins/libm/crates/libm-test/tests/compare_built_musl.rs b/library/compiler-builtins/libm/crates/libm-test/tests/compare_built_musl.rs
index 0fc1b0df13d..ca070e8f6ac 100644
--- a/library/compiler-builtins/libm/crates/libm-test/tests/compare_built_musl.rs
+++ b/library/compiler-builtins/libm/crates/libm-test/tests/compare_built_musl.rs
@@ -89,6 +89,10 @@ libm_macros::for_each_function! {
         fdimf16,
         floorf128,
         floorf16,
+        fmaxf128,
+        fmaxf16,
+        fminf128,
+        fminf16,
         rintf128,
         rintf16,
         roundf128,
diff --git a/library/compiler-builtins/libm/crates/util/src/main.rs b/library/compiler-builtins/libm/crates/util/src/main.rs
index aaedda6d152..eb8e3758956 100644
--- a/library/compiler-builtins/libm/crates/util/src/main.rs
+++ b/library/compiler-builtins/libm/crates/util/src/main.rs
@@ -96,6 +96,10 @@ fn do_eval(basis: &str, op: &str, inputs: &[&str]) {
             | fdimf16
             | floorf128
             | floorf16
+            | fmaxf128
+            | fmaxf16
+            | fminf128
+            | fminf16
             | rintf128
             | rintf16
             | roundf128
diff --git a/library/compiler-builtins/libm/etc/function-definitions.json b/library/compiler-builtins/libm/etc/function-definitions.json
index 8c5903e93b9..b6653295c10 100644
--- a/library/compiler-builtins/libm/etc/function-definitions.json
+++ b/library/compiler-builtins/libm/etc/function-definitions.json
@@ -379,29 +379,61 @@
     "fmax": {
         "sources": [
             "src/libm_helper.rs",
-            "src/math/fmax.rs"
+            "src/math/fmax.rs",
+            "src/math/generic/fmax.rs"
         ],
         "type": "f64"
     },
     "fmaxf": {
         "sources": [
-            "src/math/fmaxf.rs"
+            "src/math/fmaxf.rs",
+            "src/math/generic/fmax.rs"
         ],
         "type": "f32"
     },
+    "fmaxf128": {
+        "sources": [
+            "src/math/fmaxf128.rs",
+            "src/math/generic/fmax.rs"
+        ],
+        "type": "f128"
+    },
+    "fmaxf16": {
+        "sources": [
+            "src/math/fmaxf16.rs",
+            "src/math/generic/fmax.rs"
+        ],
+        "type": "f16"
+    },
     "fmin": {
         "sources": [
             "src/libm_helper.rs",
-            "src/math/fmin.rs"
+            "src/math/fmin.rs",
+            "src/math/generic/fmin.rs"
         ],
         "type": "f64"
     },
     "fminf": {
         "sources": [
-            "src/math/fminf.rs"
+            "src/math/fminf.rs",
+            "src/math/generic/fmin.rs"
         ],
         "type": "f32"
     },
+    "fminf128": {
+        "sources": [
+            "src/math/fminf128.rs",
+            "src/math/generic/fmin.rs"
+        ],
+        "type": "f128"
+    },
+    "fminf16": {
+        "sources": [
+            "src/math/fminf16.rs",
+            "src/math/generic/fmin.rs"
+        ],
+        "type": "f16"
+    },
     "fmod": {
         "sources": [
             "src/libm_helper.rs",
diff --git a/library/compiler-builtins/libm/etc/function-list.txt b/library/compiler-builtins/libm/etc/function-list.txt
index 0b6eed828b5..25b92e58bad 100644
--- a/library/compiler-builtins/libm/etc/function-list.txt
+++ b/library/compiler-builtins/libm/etc/function-list.txt
@@ -55,8 +55,12 @@ fma
 fmaf
 fmax
 fmaxf
+fmaxf128
+fmaxf16
 fmin
 fminf
+fminf128
+fminf16
 fmod
 fmodf
 frexp
diff --git a/library/compiler-builtins/libm/src/math/fmax.rs b/library/compiler-builtins/libm/src/math/fmax.rs
index 93c97bc611c..d5d9b513b84 100644
--- a/library/compiler-builtins/libm/src/math/fmax.rs
+++ b/library/compiler-builtins/libm/src/math/fmax.rs
@@ -1,12 +1,5 @@
+/// Return the greater of two arguments or, if either argument is NaN, the other argument.
 #[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
 pub fn fmax(x: f64, y: f64) -> f64 {
-    // IEEE754 says: maxNum(x, y) is the canonicalized number y if x < y, x if y < x, the
-    // canonicalized number if one operand is a number and the other a quiet NaN. Otherwise it
-    // is either x or y, canonicalized (this means results might differ among implementations).
-    // When either x or y is a signalingNaN, then the result is according to 6.2.
-    //
-    // Since we do not support sNaN in Rust yet, we do not need to handle them.
-    // FIXME(nagisa): due to https://bugs.llvm.org/show_bug.cgi?id=33303 we canonicalize by
-    // multiplying by 1.0. Should switch to the `canonicalize` when it works.
-    (if x.is_nan() || x < y { y } else { x }) * 1.0
+    super::generic::fmax(x, y)
 }
diff --git a/library/compiler-builtins/libm/src/math/fmaxf.rs b/library/compiler-builtins/libm/src/math/fmaxf.rs
index 60774664752..3197d5cf2ca 100644
--- a/library/compiler-builtins/libm/src/math/fmaxf.rs
+++ b/library/compiler-builtins/libm/src/math/fmaxf.rs
@@ -1,12 +1,5 @@
+/// Return the greater of two arguments or, if either argument is NaN, the other argument.
 #[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
 pub fn fmaxf(x: f32, y: f32) -> f32 {
-    // IEEE754 says: maxNum(x, y) is the canonicalized number y if x < y, x if y < x, the
-    // canonicalized number if one operand is a number and the other a quiet NaN. Otherwise it
-    // is either x or y, canonicalized (this means results might differ among implementations).
-    // When either x or y is a signalingNaN, then the result is according to 6.2.
-    //
-    // Since we do not support sNaN in Rust yet, we do not need to handle them.
-    // FIXME(nagisa): due to https://bugs.llvm.org/show_bug.cgi?id=33303 we canonicalize by
-    // multiplying by 1.0. Should switch to the `canonicalize` when it works.
-    (if x.is_nan() || x < y { y } else { x }) * 1.0
+    super::generic::fmax(x, y)
 }
diff --git a/library/compiler-builtins/libm/src/math/fmaxf128.rs b/library/compiler-builtins/libm/src/math/fmaxf128.rs
new file mode 100644
index 00000000000..bace9ab53f2
--- /dev/null
+++ b/library/compiler-builtins/libm/src/math/fmaxf128.rs
@@ -0,0 +1,5 @@
+/// Return the greater of two arguments or, if either argument is NaN, the other argument.
+#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
+pub fn fmaxf128(x: f128, y: f128) -> f128 {
+    super::generic::fmax(x, y)
+}
diff --git a/library/compiler-builtins/libm/src/math/fmaxf16.rs b/library/compiler-builtins/libm/src/math/fmaxf16.rs
new file mode 100644
index 00000000000..fea15be8f14
--- /dev/null
+++ b/library/compiler-builtins/libm/src/math/fmaxf16.rs
@@ -0,0 +1,5 @@
+/// Return the greater of two arguments or, if either argument is NaN, the other argument.
+#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
+pub fn fmaxf16(x: f16, y: f16) -> f16 {
+    super::generic::fmax(x, y)
+}
diff --git a/library/compiler-builtins/libm/src/math/fmin.rs b/library/compiler-builtins/libm/src/math/fmin.rs
index ab1509f34a6..df8ff7c32e3 100644
--- a/library/compiler-builtins/libm/src/math/fmin.rs
+++ b/library/compiler-builtins/libm/src/math/fmin.rs
@@ -1,12 +1,5 @@
+/// Return the lesser of two arguments or, if either argument is NaN, the other argument.
 #[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
 pub fn fmin(x: f64, y: f64) -> f64 {
-    // IEEE754 says: minNum(x, y) is the canonicalized number x if x < y, y if y < x, the
-    // canonicalized number if one operand is a number and the other a quiet NaN. Otherwise it
-    // is either x or y, canonicalized (this means results might differ among implementations).
-    // When either x or y is a signalingNaN, then the result is according to 6.2.
-    //
-    // Since we do not support sNaN in Rust yet, we do not need to handle them.
-    // FIXME(nagisa): due to https://bugs.llvm.org/show_bug.cgi?id=33303 we canonicalize by
-    // multiplying by 1.0. Should switch to the `canonicalize` when it works.
-    (if y.is_nan() || x < y { x } else { y }) * 1.0
+    super::generic::fmin(x, y)
 }
diff --git a/library/compiler-builtins/libm/src/math/fminf.rs b/library/compiler-builtins/libm/src/math/fminf.rs
index 0049e7117ae..b2cdfe89d64 100644
--- a/library/compiler-builtins/libm/src/math/fminf.rs
+++ b/library/compiler-builtins/libm/src/math/fminf.rs
@@ -1,12 +1,5 @@
+/// Return the lesser of two arguments or, if either argument is NaN, the other argument.
 #[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
 pub fn fminf(x: f32, y: f32) -> f32 {
-    // IEEE754 says: minNum(x, y) is the canonicalized number x if x < y, y if y < x, the
-    // canonicalized number if one operand is a number and the other a quiet NaN. Otherwise it
-    // is either x or y, canonicalized (this means results might differ among implementations).
-    // When either x or y is a signalingNaN, then the result is according to 6.2.
-    //
-    // Since we do not support sNaN in Rust yet, we do not need to handle them.
-    // FIXME(nagisa): due to https://bugs.llvm.org/show_bug.cgi?id=33303 we canonicalize by
-    // multiplying by 1.0. Should switch to the `canonicalize` when it works.
-    (if y.is_nan() || x < y { x } else { y }) * 1.0
+    super::generic::fmin(x, y)
 }
diff --git a/library/compiler-builtins/libm/src/math/fminf128.rs b/library/compiler-builtins/libm/src/math/fminf128.rs
new file mode 100644
index 00000000000..a9224c22aa5
--- /dev/null
+++ b/library/compiler-builtins/libm/src/math/fminf128.rs
@@ -0,0 +1,5 @@
+/// Return the lesser of two arguments or, if either argument is NaN, the other argument.
+#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
+pub fn fminf128(x: f128, y: f128) -> f128 {
+    super::generic::fmin(x, y)
+}
diff --git a/library/compiler-builtins/libm/src/math/fminf16.rs b/library/compiler-builtins/libm/src/math/fminf16.rs
new file mode 100644
index 00000000000..6d936be347a
--- /dev/null
+++ b/library/compiler-builtins/libm/src/math/fminf16.rs
@@ -0,0 +1,5 @@
+/// Return the lesser of two arguments or, if either argument is NaN, the other argument.
+#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
+pub fn fminf16(x: f16, y: f16) -> f16 {
+    super::generic::fmin(x, y)
+}
diff --git a/library/compiler-builtins/libm/src/math/generic/fmax.rs b/library/compiler-builtins/libm/src/math/generic/fmax.rs
new file mode 100644
index 00000000000..97803052bc2
--- /dev/null
+++ b/library/compiler-builtins/libm/src/math/generic/fmax.rs
@@ -0,0 +1,14 @@
+use super::super::Float;
+
+#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
+pub fn fmax<F: Float>(x: F, y: F) -> F {
+    // IEEE754 says: maxNum(x, y) is the canonicalized number y if x < y, x if y < x, the
+    // canonicalized number if one operand is a number and the other a quiet NaN. Otherwise it
+    // is either x or y, canonicalized (this means results might differ among implementations).
+    // When either x or y is a signalingNaN, then the result is according to 6.2.
+    //
+    // Since we do not support sNaN in Rust yet, we do not need to handle them.
+    // FIXME(nagisa): due to https://bugs.llvm.org/show_bug.cgi?id=33303 we canonicalize by
+    // multiplying by 1.0. Should switch to the `canonicalize` when it works.
+    (if x.is_nan() || x < y { y } else { x }) * F::ONE
+}
diff --git a/library/compiler-builtins/libm/src/math/generic/fmin.rs b/library/compiler-builtins/libm/src/math/generic/fmin.rs
new file mode 100644
index 00000000000..697f7200486
--- /dev/null
+++ b/library/compiler-builtins/libm/src/math/generic/fmin.rs
@@ -0,0 +1,13 @@
+use super::super::Float;
+
+pub fn fmin<F: Float>(x: F, y: F) -> F {
+    // IEEE754 says: minNum(x, y) is the canonicalized number x if x < y, y if y < x, the
+    // canonicalized number if one operand is a number and the other a quiet NaN. Otherwise it
+    // is either x or y, canonicalized (this means results might differ among implementations).
+    // When either x or y is a signalingNaN, then the result is according to 6.2.
+    //
+    // Since we do not support sNaN in Rust yet, we do not need to handle them.
+    // FIXME(nagisa): due to https://bugs.llvm.org/show_bug.cgi?id=33303 we canonicalize by
+    // multiplying by 1.0. Should switch to the `canonicalize` when it works.
+    (if y.is_nan() || x < y { x } else { y }) * F::ONE
+}
diff --git a/library/compiler-builtins/libm/src/math/generic/mod.rs b/library/compiler-builtins/libm/src/math/generic/mod.rs
index 1f557719f42..819781a219a 100644
--- a/library/compiler-builtins/libm/src/math/generic/mod.rs
+++ b/library/compiler-builtins/libm/src/math/generic/mod.rs
@@ -3,6 +3,8 @@ mod copysign;
 mod fabs;
 mod fdim;
 mod floor;
+mod fmax;
+mod fmin;
 mod rint;
 mod round;
 mod scalbn;
@@ -14,6 +16,8 @@ pub use copysign::copysign;
 pub use fabs::fabs;
 pub use fdim::fdim;
 pub use floor::floor;
+pub use fmax::fmax;
+pub use fmin::fmin;
 pub use rint::rint;
 pub use round::round;
 pub use scalbn::scalbn;
diff --git a/library/compiler-builtins/libm/src/math/mod.rs b/library/compiler-builtins/libm/src/math/mod.rs
index 8db17a02dfc..cb83b2587ee 100644
--- a/library/compiler-builtins/libm/src/math/mod.rs
+++ b/library/compiler-builtins/libm/src/math/mod.rs
@@ -346,6 +346,8 @@ cfg_if! {
         mod fabsf16;
         mod fdimf16;
         mod floorf16;
+        mod fmaxf16;
+        mod fminf16;
         mod rintf16;
         mod roundf16;
         mod sqrtf16;
@@ -356,6 +358,8 @@ cfg_if! {
         pub use self::fabsf16::fabsf16;
         pub use self::fdimf16::fdimf16;
         pub use self::floorf16::floorf16;
+        pub use self::fmaxf16::fmaxf16;
+        pub use self::fminf16::fminf16;
         pub use self::rintf16::rintf16;
         pub use self::roundf16::roundf16;
         pub use self::sqrtf16::sqrtf16;
@@ -370,6 +374,8 @@ cfg_if! {
         mod fabsf128;
         mod fdimf128;
         mod floorf128;
+        mod fmaxf128;
+        mod fminf128;
         mod rintf128;
         mod roundf128;
         mod sqrtf128;
@@ -380,6 +386,8 @@ cfg_if! {
         pub use self::fabsf128::fabsf128;
         pub use self::fdimf128::fdimf128;
         pub use self::floorf128::floorf128;
+        pub use self::fmaxf128::fmaxf128;
+        pub use self::fminf128::fminf128;
         pub use self::rintf128::rintf128;
         pub use self::roundf128::roundf128;
         pub use self::sqrtf128::sqrtf128;