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

via libc-commits libc-commits at lists.llvm.org
Thu Mar 21 07:50:37 PDT 2024


================
@@ -73,6 +73,17 @@ 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 T canonicalize(T *cx, const T *x) {
+  FPBits<T> sx(*x);
+  if (sx.is_signaling_nan()) {
+    *cx = FPBits<T>::quiet_nan();
----------------
lntue wrote:

As @jcranmer-intel mentioned, you'll need to copy the NaN payload.  Simply extract the sign and the mantissa of `sx`, then pass them to `FPBits<T>::quiet_nan(sign, mantissa)` should work.  And you'll need to raise `FE_INVALID` exception, using `raise_except_if_required` function in `FEnvImpl.h` (add the needed `#include` and dependency in `CMakeLists.txt` also)

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


More information about the libc-commits mailing list