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

Shourya Goel via libc-commits libc-commits at lists.llvm.org
Thu Mar 21 07:03:19 PDT 2024


================
@@ -0,0 +1,27 @@
+//===-- Implementation of canonicalizel function
+//----------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===-------------------------------------------------------------------------------===//
+
+#include "src/math/canonicalizel.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(int, canonicalizel,
+                   (long double *cx, const long double *x)) {
+  using FPB = fputil::FPBits<long double>;
----------------
Sh0g0-1758 wrote:

Following from this code : 

```cpp
  else if constexpr (cpp::is_same_v<UnqualT, long double>) {
    if constexpr (__LDBL_MANT_DIG__ == 53)
      return FPType::IEEE754_Binary64;
    else if constexpr (__LDBL_MANT_DIG__ == 64)
      return FPType::X86_Binary80;
    else if constexpr (__LDBL_MANT_DIG__ == 113)
      return FPType::IEEE754_Binary128;
  }
```

I think I can add the logic for long double in `BasicOperations.h` itself with a simple if a modification to check for x86_80 like so : 

```cpp
template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
LIBC_INLINE T canonicalize(T *cx, const T *x) {
  if constexpr (get_fp_type<T>() == FPType::X86_Binary80) {
    // x86 extended precision has a 64-bit mantissa.
    
  }
  FPBits<T> sx(*x);
  if (sx.is_signaling_nan()) {
    *cx = FPBits<T>::quiet_nan();
  } else {
    *cx = *x;
  }
  return 0;
}
```

Since the other types for long double, ie. `IEEE754_Binary64` and `IEEE754_Binary128` are already in canonical form. But from my understanding/research, X86_80 is the same as IEEE754_Binary80 rendering it already in canonical form. I read through the wiki page but that was the explanation for X86_80 special implementation given in `FPBits.h`. Can you please explain what specific transformation is required here, if any. 

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


More information about the libc-commits mailing list