[libc-commits] [libc] c689c16 - [libc] Add compound assignment operator overloads for BFloat16 (#201301)

via libc-commits libc-commits at lists.llvm.org
Wed Jun 3 06:46:45 PDT 2026


Author: Zorojuro
Date: 2026-06-03T09:46:39-04:00
New Revision: c689c165ba2723285258975a14cd562d41d059ab

URL: https://github.com/llvm/llvm-project/commit/c689c165ba2723285258975a14cd562d41d059ab
DIFF: https://github.com/llvm/llvm-project/commit/c689c165ba2723285258975a14cd562d41d059ab.diff

LOG: [libc] Add compound assignment operator overloads for BFloat16 (#201301)

The current Bfloat16 has normal operator overloads `+` , `-` , `=`,
`!=`, `*`, & `/`.
Later during a function failure `*=` was added in
https://github.com/llvm/llvm-project/pull/182882
For completeness the rest of the operators: `/=`, `+=`, `-=` are added 
These are added along with some smoke test .

Added: 
    

Modified: 
    libc/src/__support/FPUtil/bfloat16.h
    libc/test/src/__support/FPUtil/bfloat16_test.cpp

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/FPUtil/bfloat16.h b/libc/src/__support/FPUtil/bfloat16.h
index 638500d94cef2..ce4e3160a0963 100644
--- a/libc/src/__support/FPUtil/bfloat16.h
+++ b/libc/src/__support/FPUtil/bfloat16.h
@@ -114,10 +114,22 @@ struct BFloat16 {
     return fputil::generic::div<bfloat16>(*this, other);
   }
 
-  LIBC_INLINE constexpr BFloat16 &operator*=(const BFloat16 &other) {
+  LIBC_INLINE constexpr BFloat16 &operator*=(BFloat16 other) {
     *this = *this * other;
     return *this;
   }
+  LIBC_INLINE constexpr BFloat16 &operator+=(BFloat16 other) {
+    *this = *this + other;
+    return *this;
+  }
+  LIBC_INLINE constexpr BFloat16 &operator-=(BFloat16 other) {
+    *this = *this - other;
+    return *this;
+  }
+  LIBC_INLINE constexpr BFloat16 &operator/=(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..28333cfc18441 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, compoundAssignmentOperators) {
 
   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);
+    }
+  }
 }


        


More information about the libc-commits mailing list