[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
Sun Mar 24 13:38:36 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:
Something like `EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(aNaN, sNaN), FE_INVALID);` should work. And you'll neet to test with all combinations of NaNs.
https://github.com/llvm/llvm-project/pull/86016
More information about the libc-commits
mailing list