[llvm-branch-commits] [libc] [libc] Add Operator Overloads for Float80 (PR #214493)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Aug 6 08:37:05 PDT 2026


https://github.com/Sukumarsawant updated https://github.com/llvm/llvm-project/pull/214493

>From deeb89ab7c9a442dc2e85516c7419e27d0a5a55a Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Thu, 6 Aug 2026 19:11:47 +0530
Subject: [PATCH 1/2] feat: add negation and a test for it

---
 libc/src/__support/FPUtil/float80.h             | 7 +++++++
 libc/test/src/__support/FPUtil/float80_test.cpp | 2 ++
 2 files changed, 9 insertions(+)

diff --git a/libc/src/__support/FPUtil/float80.h b/libc/src/__support/FPUtil/float80.h
index d6823f7bedb76..98d2a579c4f3d 100644
--- a/libc/src/__support/FPUtil/float80.h
+++ b/libc/src/__support/FPUtil/float80.h
@@ -95,6 +95,13 @@ struct Float80 {
         x_bits.sign(), x_bits_exp, x_bits.get_explicit_mantissa());
     return static_cast<T>(xd.as_mantissa_type());
   }
+
+  // unary operators
+  LIBC_INLINE LIBC_BIT_CAST_CONSTEXPR Float80 operator-() const {
+    fputil::FPBits<Float80> result(*this);
+    result.set_sign(result.is_pos() ? Sign::NEG : Sign::POS);
+    return result.get_val();
+  }
 };
 
 static_assert(LIBC_NAMESPACE::cpp::is_trivially_constructible<
diff --git a/libc/test/src/__support/FPUtil/float80_test.cpp b/libc/test/src/__support/FPUtil/float80_test.cpp
index 68b38a63c23b4..ef0b34df8044d 100644
--- a/libc/test/src/__support/FPUtil/float80_test.cpp
+++ b/libc/test/src/__support/FPUtil/float80_test.cpp
@@ -21,7 +21,9 @@ TEST(LlvmLibcFloat80Test, temp) { Float80 a(1.0f); }
 TEST(LlvmLibcFloat80Test, IntegerConversion) {
   // Float80 to Integer conversion test
   ASSERT_EQ(static_cast<int>(Float80(0.0f)), 0);
+  ASSERT_EQ(static_cast<int>(Float80(-0.0f)), 0);
   ASSERT_EQ(static_cast<int>(Float80(1.0f)), 1);
+  ASSERT_EQ(static_cast<int>(Float80(-1.0f)), -1);
   ASSERT_EQ(static_cast<long long>(Float80(1000000000.0)),
             static_cast<long long>(1000000000));
   ASSERT_EQ(static_cast<unsigned>(Float80(7.0f)), 7U);

>From 0e3c5815d36496e3d0bd1c09fc2f6e3650f77dea Mon Sep 17 00:00:00 2001
From: Sukumarsawant <sawantsukumar at gmail.com>
Date: Thu, 6 Aug 2026 21:03:12 +0530
Subject: [PATCH 2/2] add tests

---
 libc/src/__support/FPUtil/float80.h           | 21 +++++++++++++++++++
 .../src/__support/FPUtil/float80_test.cpp     | 13 ++++++++++++
 2 files changed, 34 insertions(+)

diff --git a/libc/src/__support/FPUtil/float80.h b/libc/src/__support/FPUtil/float80.h
index 98d2a579c4f3d..726f15a06520a 100644
--- a/libc/src/__support/FPUtil/float80.h
+++ b/libc/src/__support/FPUtil/float80.h
@@ -101,6 +101,27 @@ struct Float80 {
     fputil::FPBits<Float80> result(*this);
     result.set_sign(result.is_pos() ? Sign::NEG : Sign::POS);
     return result.get_val();
+
+    // operator overloads
+    LIBC_INLINE constexpr Float80 operator+(const Float80 &other) const {
+      return fputil::generic::add<Float80>(*this, other);
+    }
+
+    LIBC_INLINE constexpr Float80 operator-(const Float80 &other) const {
+      return fputil::generic::sub<Float80>(*this, other);
+    }
+
+    LIBC_INLINE constexpr Float80 operator*(const Float80 &other) const {
+      return fputil::generic::mul<Float80>(*this, other);
+    }
+
+    LIBC_INLINE constexpr Float80 operator/(const Float80 &other) const {
+      return fputil::generic::div<Float80>(*this, other);
+    }
+
+    LIBC_INLINE constexpr bool operator==(const Float128 &other) const {
+      return fputil::equals(*this, other);
+    }
   }
 };
 
diff --git a/libc/test/src/__support/FPUtil/float80_test.cpp b/libc/test/src/__support/FPUtil/float80_test.cpp
index ef0b34df8044d..ae5623bf2b759 100644
--- a/libc/test/src/__support/FPUtil/float80_test.cpp
+++ b/libc/test/src/__support/FPUtil/float80_test.cpp
@@ -18,6 +18,19 @@ using FPBits = LIBC_NAMESPACE::fputil::FPBits<Float80>;
 
 TEST(LlvmLibcFloat80Test, temp) { Float80 a(1.0f); }
 
+TEST(LlvmLibcFloat80Test, Operators) {
+  Float80 a(1.0f), b(1.0f), c(2.0f), d(3.0f), pa(1.0f), na(-1.0f);
+
+  // Unary operators
+  ASSERT_TRUE(FPBits(-pa) == FPBits(na));
+
+  // Binary operators
+  ASSERT_TRUE(a + b == c);
+  ASSERT_TRUE(a - b == Float128(0.0));
+  ASSERT_TRUE(c * d == Float128(6.0));
+  ASSERT_TRUE(Float80(6.0f) / d) == Float80(2.0f);
+}
+
 TEST(LlvmLibcFloat80Test, IntegerConversion) {
   // Float80 to Integer conversion test
   ASSERT_EQ(static_cast<int>(Float80(0.0f)), 0);



More information about the llvm-branch-commits mailing list