[libc-commits] [libc] [llvm] [libc][stdfix] Add round functions for fixed point types. (PR #81994)

via libc-commits libc-commits at lists.llvm.org
Fri Feb 16 09:29:02 PST 2024


================
@@ -19,6 +21,36 @@
 
 namespace LIBC_NAMESPACE::fixed_point {
 
+// Bit-wise operations are not available for fixed point types yet.
+template <typename T>
+LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_fixed_point_v<T>, T>
+bit_and(T x, T y) {
+  using BitType = typename FXRep<T>::StorageType;
+  static_assert(sizeof(BitType) * CHAR_BIT == sizeof(T) * CHAR_BIT);
+  BitType x_bit = cpp::bit_cast<BitType>(x);
+  BitType y_bit = cpp::bit_cast<BitType>(y);
+  return cpp::bit_cast<T, BitType>(x_bit & y_bit);
+}
+
+template <typename T>
+LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_fixed_point_v<T>, T>
+bit_or(T x, T y) {
+  using BitType = typename FXRep<T>::StorageType;
+  static_assert(sizeof(BitType) * CHAR_BIT == FXRep<T>::TOTAL_LEN);
+  BitType x_bit = cpp::bit_cast<BitType>(x);
+  BitType y_bit = cpp::bit_cast<BitType>(y);
+  return cpp::bit_cast<T, BitType>(x_bit | y_bit);
+}
+
+template <typename T>
+LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_fixed_point_v<T>, T>
+bit_not(T x) {
+  using BitType = typename FXRep<T>::StorageType;
+  static_assert(sizeof(BitType) * CHAR_BIT == sizeof(T) * CHAR_BIT);
+  BitType x_bit = cpp::bit_cast<BitType>(x);
+  return cpp::bit_cast<T, BitType>(~x_bit);
----------------
lntue wrote:

Added comments explaining the reason.

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


More information about the libc-commits mailing list