[libc-commits] [libc] [libc][math][c23] add c23 floating point fmaximum and fminimum functions. (PR #86016)

Job Henandez Lara via libc-commits libc-commits at lists.llvm.org
Sat Mar 23 22:52:45 PDT 2024


================
@@ -58,6 +58,102 @@ LIBC_INLINE T fmax(T x, T y) {
   }
 }
 
+template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
+LIBC_INLINE T fmaximum(T x, T y) {
+  FPBits<T> bitx(x), bity(y);
+
+  if (bitx.is_nan())
+    return x;
+  if (bity.is_nan())
+    return y;
+  if (bitx.sign() != bity.sign())
+    return (bitx.is_neg() ? y : x);
+  return x > y ? x : y;
+}
+
+template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
+LIBC_INLINE T fminimum(T x, T y) {
+  const FPBits<T> bitx(x), bity(y);
+
+  if (bitx.is_nan())
+    return x;
+  if (bity.is_nan())
+    return y;
+  if (bitx.sign() != bity.sign())
+    return (bitx.is_neg()) ? x : y;
+  return x < y ? x : y;
+}
+
+template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
+LIBC_INLINE T fmaximum_num(T x, T y) {
+  FPBits<T> bitx(x), bity(y);
+
+  if (bitx.is_nan())
----------------
Jobhdez wrote:

Here’s the definition I wa
S basing it on 
```
The fmaximum_num function returns the greater of the two values x and y. If one argument is a number and the other is a NaN, even a signaling NaN, the number is returned. Positive zero is treated as greater than negative zero.
```
I took this from here https://www.gnu.org/software/libc/manual/html_node/Misc-FP-Arithmetic.html

https://github.com/llvm/llvm-project/pull/86016


More information about the libc-commits mailing list