Skip to content

Commit 594b6f7

Browse files
authored
[libc][math][c++23] Implement comparison operations operator overloads for BFloat16 (#150087)
Signed-off-by: Krishna Pandey <[email protected]>
1 parent f1bb5de commit 594b6f7

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

libc/src/__support/FPUtil/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ add_header_library(
274274
bfloat16.h
275275
DEPENDS
276276
.cast
277+
.comparison_operations
277278
.dyadic_float
278279
libc.src.__support.CPP.bit
279280
libc.src.__support.CPP.type_traits

libc/src/__support/FPUtil/bfloat16.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "src/__support/CPP/bit.h"
1313
#include "src/__support/CPP/type_traits.h"
1414
#include "src/__support/FPUtil/cast.h"
15+
#include "src/__support/FPUtil/comparison_operations.h"
1516
#include "src/__support/FPUtil/dyadic_float.h"
1617
#include "src/__support/macros/config.h"
1718
#include "src/__support/macros/properties/types.h"
@@ -57,6 +58,30 @@ struct BFloat16 {
5758
uint32_t x_bits = static_cast<uint32_t>(bits) << 16U;
5859
return cpp::bit_cast<float>(x_bits);
5960
}
61+
62+
LIBC_INLINE constexpr bool operator==(BFloat16 other) const {
63+
return fputil::equals(*this, other);
64+
}
65+
66+
LIBC_INLINE constexpr bool operator!=(BFloat16 other) const {
67+
return !fputil::equals(*this, other);
68+
}
69+
70+
LIBC_INLINE constexpr bool operator<(BFloat16 other) const {
71+
return fputil::less_than(*this, other);
72+
}
73+
74+
LIBC_INLINE constexpr bool operator<=(BFloat16 other) const {
75+
return fputil::less_than_or_equals(*this, other);
76+
}
77+
78+
LIBC_INLINE constexpr bool operator>(BFloat16 other) const {
79+
return fputil::greater_than(*this, other);
80+
}
81+
82+
LIBC_INLINE constexpr bool operator>=(BFloat16 other) const {
83+
return fputil::greater_than_or_equals(*this, other);
84+
}
6085
}; // struct BFloat16
6186

6287
} // namespace fputil

0 commit comments

Comments
 (0)