[libc-commits] [libc] [libc][math][c23] Implement canonicalize functions (PR #85940)

via libc-commits libc-commits at lists.llvm.org
Sat Mar 23 13:09:14 PDT 2024


================
@@ -73,6 +76,56 @@ LIBC_INLINE T fdim(T x, T y) {
   return (x > y ? x - y : 0);
 }
 
+template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
+LIBC_INLINE int canonicalize(T &cx, const T &x) {
+  FPBits<T> sx(x);
+  if constexpr (get_fp_type<T>() == FPType::X86_Binary80) {
+    // All the pseudo and unnormal numbers are not canonical.
----------------
lntue wrote:

You can add some testing method in `CanonicalizeTest` class that is only available for x86 long double type, in which you can build these non-canonical bit patterns with `UInt128(exponent) << 64 + UInt128(bits)`, feed them into `FPBits`, and extract the long double value to be called by `canonicalizel`.

Then in `canonicalizel_test.cpp`, you just add an extra `TEST_F` calling that test method, that is wrapped under `#ifdef LIBC_TYPES_LONG_DOUBLE_IS_X86_FLOAT80`.

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


More information about the libc-commits mailing list