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

via libc-commits libc-commits at lists.llvm.org
Sat Mar 23 22:24:32 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())
----------------
lntue wrote:

I'm sorry for a late suggestion, but I just noticed that in section F.10.9.5 in C23 standard about exceptional handling of `f*_num` functions is different from their non-num versions:
- if both are `NaN`, then a quiet NaN is returned.
- if at least one of them is signaling NaN, then `FE_INVALID` is raised.

Do you mind updating `fmaximum_num` and `fminimum_num` implementations here to take care of those cases, and updating `f*_num_test` (4 of them in total) accordingly.

Thanks,

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


More information about the libc-commits mailing list