[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
Mon Mar 25 09:00:46 PDT 2024


================
@@ -58,6 +59,126 @@ 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_signaling_nan()) {
+    fputil::raise_except_if_required(FE_INVALID);
+    if (bity.is_nan())
+      return FPBits<T>::quiet_nan().get_val();
+    return y;
+  }
+  if (bity.is_signaling_nan()) {
+    fputil::raise_except_if_required(FE_INVALID);
+    if (bitx.is_nan())
+      return FPBits<T>::quiet_nan().get_val();
+    return x;
+  }
+  if (bitx.is_quiet_nan())
+    return y;
+  if (bity.is_quiet_nan())
+    return x;
----------------
Jobhdez wrote:

I used this code buts now the NaN tests are failing. Do i go back to what I had it?

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


More information about the libc-commits mailing list