[libc-commits] [libc] [libc] Fix compilation when long double is used as float128 (PR #97747)

Mikhail R. Gadelha via libc-commits libc-commits at lists.llvm.org
Thu Jul 4 08:38:32 PDT 2024


https://github.com/mikhailramalho created https://github.com/llvm/llvm-project/pull/97747

Small patch that fixes the compilation when float128 is a long double, e.g., rv32. It fixes a static_cast, adds a missing %= operator and changes a cast to use get_val() in a test case instead

>From 2269280894260206a640cb00f4305d93437ff972 Mon Sep 17 00:00:00 2001
From: "Mikhail R. Gadelha" <mikhail at igalia.com>
Date: Thu, 4 Jul 2024 12:33:23 -0300
Subject: [PATCH] [libc] Fix compilation when long double is used as float128

Small patch that fixes the compilation when float128 is a long double,
e.g., rv32. It fixes a static_cast, adds a missing %= operator and
changes a cast to use get_val() in a test case instead
---
 libc/src/__support/FPUtil/generic/FMod.h | 3 ++-
 libc/src/__support/big_int.h             | 4 ++++
 libc/test/src/math/smoke/CopySignTest.h  | 2 +-
 3 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/libc/src/__support/FPUtil/generic/FMod.h b/libc/src/__support/FPUtil/generic/FMod.h
index f840a92b1a5a2..0b9244ebdc8a8 100644
--- a/libc/src/__support/FPUtil/generic/FMod.h
+++ b/libc/src/__support/FPUtil/generic/FMod.h
@@ -160,7 +160,8 @@ template <typename T> struct FModDivisionInvMultHelper {
 template <typename T, typename U = typename FPBits<T>::StorageType,
           typename DivisionHelper = FModDivisionSimpleHelper<U>>
 class FMod {
-  static_assert(cpp::is_floating_point_v<T> && cpp::is_unsigned_v<U> &&
+  static_assert(cpp::is_floating_point_v<T> &&
+                    is_unsigned_integral_or_big_int_v<U> &&
                     (sizeof(U) * CHAR_BIT > FPBits<T>::FRACTION_LEN),
                 "FMod instantiated with invalid type.");
 
diff --git a/libc/src/__support/big_int.h b/libc/src/__support/big_int.h
index 59a3912ef0f09..82a5251418854 100644
--- a/libc/src/__support/big_int.h
+++ b/libc/src/__support/big_int.h
@@ -731,6 +731,10 @@ struct BigInt {
     return *result.div(other);
   }
 
+  LIBC_INLINE constexpr BigInt operator%=(const BigInt &other) {
+    return *this->div(other);
+  }
+
   LIBC_INLINE constexpr BigInt &operator*=(const BigInt &other) {
     *this = *this * other;
     return *this;
diff --git a/libc/test/src/math/smoke/CopySignTest.h b/libc/test/src/math/smoke/CopySignTest.h
index 1eb323a3aa41f..7dc6f5abe6389 100644
--- a/libc/test/src/math/smoke/CopySignTest.h
+++ b/libc/test/src/math/smoke/CopySignTest.h
@@ -42,9 +42,9 @@ class CopySignTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
     StorageType v = 0;
     for (int i = 0; i <= COUNT; ++i, v += STEP) {
       FPBits x_bits = FPBits(v);
-      T x = T(v);
       if (x_bits.is_nan() || x_bits.is_inf())
         continue;
+      T x = x_bits.get_val();
 
       T res1 = func(x, -x);
       ASSERT_FP_EQ(res1, -x);



More information about the libc-commits mailing list