[libc-commits] [libc] [libc] Bfloat16 updates (PR #201301)

via libc-commits libc-commits at lists.llvm.org
Wed Jun 3 03:33:47 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Zorojuro (Sukumarsawant)

<details>
<summary>Changes</summary>



---
Full diff: https://github.com/llvm/llvm-project/pull/201301.diff


2 Files Affected:

- (modified) libc/src/__support/FPUtil/bfloat16.h (+22-10) 
- (modified) libc/test/src/__support/FPUtil/bfloat16_test.cpp (+38-1) 


``````````diff
diff --git a/libc/src/__support/FPUtil/bfloat16.h b/libc/src/__support/FPUtil/bfloat16.h
index 638500d94cef2..0816d3442b8fc 100644
--- a/libc/src/__support/FPUtil/bfloat16.h
+++ b/libc/src/__support/FPUtil/bfloat16.h
@@ -68,27 +68,27 @@ struct BFloat16 {
     return static_cast<T>(static_cast<float>(*this));
   }
 
-  LIBC_INLINE constexpr bool operator==(BFloat16 other) const {
+  LIBC_INLINE constexpr bool operator==(const BFloat16 &other) const {
     return fputil::equals(*this, other);
   }
 
-  LIBC_INLINE constexpr bool operator!=(BFloat16 other) const {
+  LIBC_INLINE constexpr bool operator!=(const BFloat16 &other) const {
     return !fputil::equals(*this, other);
   }
 
-  LIBC_INLINE constexpr bool operator<(BFloat16 other) const {
+  LIBC_INLINE constexpr bool operator<(const BFloat16 &other) const {
     return fputil::less_than(*this, other);
   }
 
-  LIBC_INLINE constexpr bool operator<=(BFloat16 other) const {
+  LIBC_INLINE constexpr bool operator<=(const BFloat16 &other) const {
     return fputil::less_than_or_equals(*this, other);
   }
 
-  LIBC_INLINE constexpr bool operator>(BFloat16 other) const {
+  LIBC_INLINE constexpr bool operator>(const BFloat16 &other) const {
     return fputil::greater_than(*this, other);
   }
 
-  LIBC_INLINE constexpr bool operator>=(BFloat16 other) const {
+  LIBC_INLINE constexpr bool operator>=(const BFloat16 &other) const {
     return fputil::greater_than_or_equals(*this, other);
   }
 
@@ -98,19 +98,19 @@ struct BFloat16 {
     return result.get_val();
   }
 
-  LIBC_INLINE constexpr BFloat16 operator+(BFloat16 other) const {
+  LIBC_INLINE constexpr BFloat16 operator+(const BFloat16 &other) const {
     return fputil::generic::add<BFloat16>(*this, other);
   }
 
-  LIBC_INLINE constexpr BFloat16 operator-(BFloat16 other) const {
+  LIBC_INLINE constexpr BFloat16 operator-(const BFloat16 &other) const {
     return fputil::generic::sub<BFloat16>(*this, other);
   }
 
-  LIBC_INLINE constexpr BFloat16 operator*(BFloat16 other) const {
+  LIBC_INLINE constexpr BFloat16 operator*(const BFloat16 &other) const {
     return fputil::generic::mul<bfloat16>(*this, other);
   }
 
-  LIBC_INLINE constexpr BFloat16 operator/(BFloat16 other) const {
+  LIBC_INLINE constexpr BFloat16 operator/(const BFloat16 &other) const {
     return fputil::generic::div<bfloat16>(*this, other);
   }
 
@@ -118,6 +118,18 @@ struct BFloat16 {
     *this = *this * other;
     return *this;
   }
+  LIBC_INLINE constexpr BFloat16 &operator+=(const BFloat16 &other) {
+    *this = *this + other;
+    return *this;
+  }
+  LIBC_INLINE constexpr BFloat16 &operator-=(const BFloat16 &other) {
+    *this = *this - other;
+    return *this;
+  }
+  LIBC_INLINE constexpr BFloat16 &operator/=(const BFloat16 &other) {
+    *this = *this / other;
+    return *this;
+  }
 }; // struct BFloat16
 
 } // namespace fputil
diff --git a/libc/test/src/__support/FPUtil/bfloat16_test.cpp b/libc/test/src/__support/FPUtil/bfloat16_test.cpp
index 8760b2abe3ce3..9ea97ad842d5f 100644
--- a/libc/test/src/__support/FPUtil/bfloat16_test.cpp
+++ b/libc/test/src/__support/FPUtil/bfloat16_test.cpp
@@ -68,12 +68,13 @@ TEST_F(LlvmLibcBfloat16ConversionTest, FromInteger) {
   }
 }
 
-TEST_F(LlvmLibcBfloat16ConversionTest, MultiplyAssign) {
+TEST_F(LlvmLibcBfloat16ConversionTest, shortHandOperators) {
 
   constexpr BFloat16 VAL[] = {zero,           neg_zero,        inf,
                               neg_inf,        min_normal,      max_normal,
                               bfloat16(1.0f), bfloat16(-1.0f), bfloat16(2.0f),
                               bfloat16(3.0f)};
+  // *=
   for (const bfloat16 &x : VAL) {
     for (const bfloat16 &y : VAL) {
       BFloat16 a = x, b = y;
@@ -85,4 +86,40 @@ TEST_F(LlvmLibcBfloat16ConversionTest, MultiplyAssign) {
       EXPECT_FP_EQ_ALL_ROUNDING(mpfr_bfloat, libc_bfloat);
     }
   }
+  // /=
+  for (const bfloat16 &x : VAL) {
+    for (const bfloat16 &y : VAL) {
+      BFloat16 a = x, b = y;
+      MPFRNumber mpfr_a{a}, mpfr_b{b};
+      MPFRNumber mpfr_c = mpfr_a.div(mpfr_b);
+      BFloat16 mpfr_bfloat = mpfr_c.as<BFloat16>();
+      a /= b;
+      BFloat16 libc_bfloat = a;
+      EXPECT_FP_EQ_ALL_ROUNDING(mpfr_bfloat, libc_bfloat);
+    }
+  }
+  // +=
+  for (const bfloat16 &x : VAL) {
+    for (const bfloat16 &y : VAL) {
+      BFloat16 a = x, b = y;
+      MPFRNumber mpfr_a{a}, mpfr_b{b};
+      MPFRNumber mpfr_c = mpfr_a.add(mpfr_b);
+      BFloat16 mpfr_bfloat = mpfr_c.as<BFloat16>();
+      a += b;
+      BFloat16 libc_bfloat = a;
+      EXPECT_FP_EQ_ALL_ROUNDING(mpfr_bfloat, libc_bfloat);
+    }
+  }
+  // -=
+  for (const bfloat16 &x : VAL) {
+    for (const bfloat16 &y : VAL) {
+      BFloat16 a = x, b = y;
+      MPFRNumber mpfr_a{a}, mpfr_b{b};
+      MPFRNumber mpfr_c = mpfr_a.sub(mpfr_b);
+      BFloat16 mpfr_bfloat = mpfr_c.as<BFloat16>();
+      a -= b;
+      BFloat16 libc_bfloat = a;
+      EXPECT_FP_EQ_ALL_ROUNDING(mpfr_bfloat, libc_bfloat);
+    }
+  }
 }

``````````

</details>


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


More information about the libc-commits mailing list