[libc-commits] [libc] [libc][stdfix] Implement fixed point bitsfx functions in llvm libc (PR #128413)

Krishna Pandey via libc-commits libc-commits at lists.llvm.org
Sun Feb 23 10:47:20 PST 2025


https://github.com/krishna2803 updated https://github.com/llvm/llvm-project/pull/128413

>From f6ff5d5438d72f7e0f72d60c9e0e81d9561ea599 Mon Sep 17 00:00:00 2001
From: krishna2803 <kpandey81930 at gmail.com>
Date: Fri, 21 Feb 2025 16:35:30 +0530
Subject: [PATCH 01/11] add: implement bitsfx for signed fixed point numbers

Signed-off-by: krishna2803 <kpandey81930 at gmail.com>
---
 libc/config/baremetal/arm/entrypoints.txt   |  6 ++++++
 libc/config/baremetal/riscv/entrypoints.txt |  6 ++++++
 libc/config/linux/riscv/entrypoints.txt     |  6 ++++++
 libc/config/linux/x86_64/entrypoints.txt    |  6 ++++++
 libc/src/__support/fixed_point/fx_bits.h    |  7 +++++++
 libc/src/stdfix/CMakeLists.txt              | 14 +++++++++++++
 libc/src/stdfix/bitshk.cpp                  | 22 +++++++++++++++++++++
 libc/src/stdfix/bitshk.h                    | 22 +++++++++++++++++++++
 libc/src/stdfix/bitshr.cpp                  | 22 +++++++++++++++++++++
 libc/src/stdfix/bitshr.h                    | 22 +++++++++++++++++++++
 libc/src/stdfix/bitsk.cpp                   | 22 +++++++++++++++++++++
 libc/src/stdfix/bitsk.h                     | 22 +++++++++++++++++++++
 libc/src/stdfix/bitslk.cpp                  | 22 +++++++++++++++++++++
 libc/src/stdfix/bitslk.h                    | 22 +++++++++++++++++++++
 libc/src/stdfix/bitslr.cpp                  | 22 +++++++++++++++++++++
 libc/src/stdfix/bitslr.h                    | 22 +++++++++++++++++++++
 libc/src/stdfix/bitsr.cpp                   | 22 +++++++++++++++++++++
 libc/src/stdfix/bitsr.h                     | 22 +++++++++++++++++++++
 18 files changed, 309 insertions(+)
 create mode 100644 libc/src/stdfix/bitshk.cpp
 create mode 100644 libc/src/stdfix/bitshk.h
 create mode 100644 libc/src/stdfix/bitshr.cpp
 create mode 100644 libc/src/stdfix/bitshr.h
 create mode 100644 libc/src/stdfix/bitsk.cpp
 create mode 100644 libc/src/stdfix/bitsk.h
 create mode 100644 libc/src/stdfix/bitslk.cpp
 create mode 100644 libc/src/stdfix/bitslk.h
 create mode 100644 libc/src/stdfix/bitslr.cpp
 create mode 100644 libc/src/stdfix/bitslr.h
 create mode 100644 libc/src/stdfix/bitsr.cpp
 create mode 100644 libc/src/stdfix/bitsr.h

diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index 370b5462fe9e8..e1dd16108b5ae 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -519,6 +519,12 @@ if(LIBC_COMPILER_HAS_FIXED_POINT)
     libc.src.stdfix.countlsuhk
     libc.src.stdfix.countlsuk
     libc.src.stdfix.countlsulk
+    libc.src.stdfix.bitshr
+    libc.src.stdfix.bitsr
+    libc.src.stdfix.bitslr
+    libc.src.stdfix.bitshk
+    libc.src.stdfix.bitsk
+    libc.src.stdfix.bitslk
   )
 endif()
 
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index 07311a60a17a2..24a3bc2db60d5 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -514,6 +514,12 @@ if(LIBC_COMPILER_HAS_FIXED_POINT)
     libc.src.stdfix.countlsuhk
     libc.src.stdfix.countlsuk
     libc.src.stdfix.countlsulk
+    libc.src.stdfix.bitshr
+    libc.src.stdfix.bitsr
+    libc.src.stdfix.bitslr
+    libc.src.stdfix.bitshk
+    libc.src.stdfix.bitsk
+    libc.src.stdfix.bitslk
   )
 endif()
 
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index a9ba0c257755b..8fb7d76da7a53 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -761,6 +761,12 @@ if(LIBC_COMPILER_HAS_FIXED_POINT)
     libc.src.stdfix.countlsuhk
     libc.src.stdfix.countlsuk
     libc.src.stdfix.countlsulk
+    libc.src.stdfix.bitshr
+    libc.src.stdfix.bitsr
+    libc.src.stdfix.bitslr
+    libc.src.stdfix.bitshk
+    libc.src.stdfix.bitsk
+    libc.src.stdfix.bitslk
   )
 endif()
 
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index a4f6671a59789..4895fe5e12dc3 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -887,6 +887,12 @@ if(LIBC_COMPILER_HAS_FIXED_POINT)
     libc.src.stdfix.countlsuhk
     libc.src.stdfix.countlsuk
     libc.src.stdfix.countlsulk
+    libc.src.stdfix.bitshr
+    libc.src.stdfix.bitsr
+    libc.src.stdfix.bitslr
+    libc.src.stdfix.bitshk
+    libc.src.stdfix.bitsk
+    libc.src.stdfix.bitslk
   )
 endif()
 
diff --git a/libc/src/__support/fixed_point/fx_bits.h b/libc/src/__support/fixed_point/fx_bits.h
index 7509419da0c43..a8a3b18952673 100644
--- a/libc/src/__support/fixed_point/fx_bits.h
+++ b/libc/src/__support/fixed_point/fx_bits.h
@@ -194,6 +194,13 @@ countls(T f) {
   return cpp::countl_zero(value_bits) - FXRep::SIGN_LEN;
 }
 
+// fixed-point to integer conversion
+template <typename From, typename To>
+LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_fixed_point_v<From>, To>
+bitsfx(From f) {
+  return cpp::bit_cast<To, From>(f);
+}
+
 } // namespace fixed_point
 } // namespace LIBC_NAMESPACE_DECL
 
diff --git a/libc/src/stdfix/CMakeLists.txt b/libc/src/stdfix/CMakeLists.txt
index 6fb06b8d7e9ae..8ad9f54c0f10c 100644
--- a/libc/src/stdfix/CMakeLists.txt
+++ b/libc/src/stdfix/CMakeLists.txt
@@ -59,6 +59,20 @@ foreach(suffix IN ITEMS hr r lr hk k lk uhr ur ulr uhk uk ulk)
     DEPENDS
       libc.src.__support.fixed_point.fx_bits
   )
+
+  add_entrypoint_object(
+    bits${suffix}
+    HDRS
+      bits${suffix}.h
+    SRCS
+      bits${suffix}.cpp
+    COMPILE_OPTIONS
+      ${libc_opt_high_flag}
+    DEPENDS
+      libc.src.__support.fixed_point.fx_bits
+      libc.include.llvm-libc-types.stdfix-types
+      libc.include.llvm-libc-macros.stdfix_macros
+  )
 endforeach()
 
 add_entrypoint_object(
diff --git a/libc/src/stdfix/bitshk.cpp b/libc/src/stdfix/bitshk.cpp
new file mode 100644
index 0000000000000..d0a3e128bdd65
--- /dev/null
+++ b/libc/src/stdfix/bitshk.cpp
@@ -0,0 +1,22 @@
+//===-- Implementation of bitshk function  --------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "bitshk.h"
+#include "include/llvm-libc-macros/stdfix-macros.h" // short accum
+#include "include/llvm-libc-types/stdfix-types.h"   // int_hk_t
+#include "src/__support/common.h"                   // LLVM_LIBC_FUNCTION
+#include "src/__support/fixed_point/fx_bits.h"      // fixed_point
+#include "src/__support/macros/config.h"            // LIBC_NAMESPACE_DECL
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int_hk_t, bitshk, (short accum f)) {
+  return fixed_point::bitsfx<short accum, int_hk_t>(f);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdfix/bitshk.h b/libc/src/stdfix/bitshk.h
new file mode 100644
index 0000000000000..a1505e2e56d85
--- /dev/null
+++ b/libc/src/stdfix/bitshk.h
@@ -0,0 +1,22 @@
+//===-- Implementation header for bitshk function ---------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDFIX_BITSHK_H
+#define LLVM_LIBC_SRC_STDFIX_BITSHK_H
+
+#include "include/llvm-libc-macros/stdfix-macros.h" // short accum
+#include "include/llvm-libc-types/stdfix-types.h"   // int_hk_t
+#include "src/__support/macros/config.h"            // LIBC_NAMESPACE_DECL
+
+namespace LIBC_NAMESPACE_DECL {
+
+int_hk_t bitshk(short accum f);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STDFIX_BITSHK_H
diff --git a/libc/src/stdfix/bitshr.cpp b/libc/src/stdfix/bitshr.cpp
new file mode 100644
index 0000000000000..394d1f08f6ae5
--- /dev/null
+++ b/libc/src/stdfix/bitshr.cpp
@@ -0,0 +1,22 @@
+//===-- Implementation of bitshr function  --------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "bitshr.h"
+#include "include/llvm-libc-macros/stdfix-macros.h" // short fract
+#include "include/llvm-libc-types/stdfix-types.h"   // int_hr_t
+#include "src/__support/common.h"                   // LLVM_LIBC_FUNCTION
+#include "src/__support/fixed_point/fx_bits.h"      // fixed_point
+#include "src/__support/macros/config.h"            // LIBC_NAMESPACE_DECL
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int_hr_t, bitshr, (short fract f)) {
+  return fixed_point::bitsfx<short fract, int_hr_t>(f);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdfix/bitshr.h b/libc/src/stdfix/bitshr.h
new file mode 100644
index 0000000000000..d5b4b8f56a7e9
--- /dev/null
+++ b/libc/src/stdfix/bitshr.h
@@ -0,0 +1,22 @@
+//===-- Implementation header for bitshr function ---------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDFIX_BITSHR_H
+#define LLVM_LIBC_SRC_STDFIX_BITSHR_H
+
+#include "include/llvm-libc-macros/stdfix-macros.h" // short fract
+#include "include/llvm-libc-types/stdfix-types.h"   // int_hr_t
+#include "src/__support/macros/config.h"            // LIBC_NAMESPACE_DECL
+
+namespace LIBC_NAMESPACE_DECL {
+
+int_hr_t bitshr(short fract f);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STDFIX_BITSHR_H
diff --git a/libc/src/stdfix/bitsk.cpp b/libc/src/stdfix/bitsk.cpp
new file mode 100644
index 0000000000000..f8c9d77d56e9c
--- /dev/null
+++ b/libc/src/stdfix/bitsk.cpp
@@ -0,0 +1,22 @@
+//===-- Implementation for bitsk function  --------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "bitsk.h"
+#include "include/llvm-libc-macros/stdfix-macros.h" // accum
+#include "include/llvm-libc-types/stdfix-types.h"   // int_k_t
+#include "src/__support/common.h"                   // LLVM_LIBC_FUNCTION
+#include "src/__support/fixed_point/fx_bits.h"      // fixed_point
+#include "src/__support/macros/config.h"            // LIBC_NAMESPACE_DECL
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int_k_t, bitsk, (accum f)) {
+  return fixed_point::bitsfx<accum, int_k_t>(f);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdfix/bitsk.h b/libc/src/stdfix/bitsk.h
new file mode 100644
index 0000000000000..32d5a724dfb0b
--- /dev/null
+++ b/libc/src/stdfix/bitsk.h
@@ -0,0 +1,22 @@
+//===-- Implementation header for bitsk function ----------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDFIX_BITSK_H
+#define LLVM_LIBC_SRC_STDFIX_BITSK_H
+
+#include "include/llvm-libc-macros/stdfix-macros.h" // accum
+#include "include/llvm-libc-types/stdfix-types.h"   // int_k_t
+#include "src/__support/macros/config.h"            // LIBC_NAMESPACE_DECL
+
+namespace LIBC_NAMESPACE_DECL {
+
+int_k_t bitsk(accum f);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STDFIX_BITSK_H
diff --git a/libc/src/stdfix/bitslk.cpp b/libc/src/stdfix/bitslk.cpp
new file mode 100644
index 0000000000000..f4af2a8cd8b99
--- /dev/null
+++ b/libc/src/stdfix/bitslk.cpp
@@ -0,0 +1,22 @@
+//===-- Implementation for bitslk function  -------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "bitslk.h"
+#include "include/llvm-libc-macros/stdfix-macros.h" // long accum
+#include "include/llvm-libc-types/stdfix-types.h"   // int_lk_t
+#include "src/__support/common.h"                   // LLVM_LIBC_FUNCTION
+#include "src/__support/fixed_point/fx_bits.h"      // fixed_point
+#include "src/__support/macros/config.h"            // LIBC_NAMESPACE_DECL
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int_lk_t, bitslk, (long accum f)) {
+  return fixed_point::bitsfx<long accum, int_lk_t>(f);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdfix/bitslk.h b/libc/src/stdfix/bitslk.h
new file mode 100644
index 0000000000000..821116b9a7c1b
--- /dev/null
+++ b/libc/src/stdfix/bitslk.h
@@ -0,0 +1,22 @@
+//===-- Implementation header for bitslk function ---------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDFIX_BITSLK_H
+#define LLVM_LIBC_SRC_STDFIX_BITSLK_H
+
+#include "include/llvm-libc-macros/stdfix-macros.h" // long accum
+#include "include/llvm-libc-types/stdfix-types.h"   // int_lk_t
+#include "src/__support/macros/config.h"            // LIBC_NAMESPACE_DECL
+
+namespace LIBC_NAMESPACE_DECL {
+
+int_lk_t bitslk(long accum f);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STDFIX_BITSLK_H
diff --git a/libc/src/stdfix/bitslr.cpp b/libc/src/stdfix/bitslr.cpp
new file mode 100644
index 0000000000000..3b38aa21a6338
--- /dev/null
+++ b/libc/src/stdfix/bitslr.cpp
@@ -0,0 +1,22 @@
+//===-- Implementation of bitslr function  --------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "bitslr.h"
+#include "include/llvm-libc-macros/stdfix-macros.h" // long fract
+#include "include/llvm-libc-types/stdfix-types.h"   // int_lr_t
+#include "src/__support/common.h"                   // LLVM_LIBC_FUNCTION
+#include "src/__support/fixed_point/fx_bits.h"      // fixed_point
+#include "src/__support/macros/config.h"            // LIBC_NAMESPACE_DECL
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int_lr_t, bitslr, (long fract f)) {
+  return fixed_point::bitsfx<long fract, int_lr_t>(f);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdfix/bitslr.h b/libc/src/stdfix/bitslr.h
new file mode 100644
index 0000000000000..0cb597214f550
--- /dev/null
+++ b/libc/src/stdfix/bitslr.h
@@ -0,0 +1,22 @@
+//===-- Implementation header for bitslr function ---------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDFIX_BITSLR_H
+#define LLVM_LIBC_SRC_STDFIX_BITSLR_H
+
+#include "include/llvm-libc-macros/stdfix-macros.h" // long fract
+#include "include/llvm-libc-types/stdfix-types.h"   // int_lr_t
+#include "src/__support/macros/config.h"            // LIBC_NAMESPACE_DECL
+
+namespace LIBC_NAMESPACE_DECL {
+
+int_lr_t bitslr(long fract f);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STDFIX_BITSLR_H
diff --git a/libc/src/stdfix/bitsr.cpp b/libc/src/stdfix/bitsr.cpp
new file mode 100644
index 0000000000000..2b6ad2cfe189a
--- /dev/null
+++ b/libc/src/stdfix/bitsr.cpp
@@ -0,0 +1,22 @@
+//===-- Implementation of bitsr function  ---------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "bitsr.h"
+#include "include/llvm-libc-macros/stdfix-macros.h" // fract
+#include "include/llvm-libc-types/stdfix-types.h"   // int_r_t
+#include "src/__support/common.h"                   // LLVM_LIBC_FUNCTION
+#include "src/__support/fixed_point/fx_bits.h"      // fixed_point
+#include "src/__support/macros/config.h"            // LIBC_NAMESPACE_DECL
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int_r_t, bitsr, (fract f)) {
+  return fixed_point::bitsfx<fract, int_r_t>(f);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdfix/bitsr.h b/libc/src/stdfix/bitsr.h
new file mode 100644
index 0000000000000..e071f034cd107
--- /dev/null
+++ b/libc/src/stdfix/bitsr.h
@@ -0,0 +1,22 @@
+//===-- Implementation header for bitsr function ----------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDFIX_BITSR_H
+#define LLVM_LIBC_SRC_STDFIX_BITSR_H
+
+#include "include/llvm-libc-macros/stdfix-macros.h" // fract
+#include "include/llvm-libc-types/stdfix-types.h"   // int_r_t
+#include "src/__support/macros/config.h"            // LIBC_NAMESPACE_DECL
+
+namespace LIBC_NAMESPACE_DECL {
+
+int_r_t bitsr(fract f);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STDFIX_BITSR_H

>From f7beac1ffd436f38df5164e1c7bb5cf8d927ed7c Mon Sep 17 00:00:00 2001
From: krishna2803 <kpandey81930 at gmail.com>
Date: Fri, 21 Feb 2025 16:38:31 +0530
Subject: [PATCH 02/11] add: tests for some signed `bitsfx`

Signed-off-by: krishna2803 <kpandey81930 at gmail.com>
---
 libc/test/src/stdfix/BitsFxTest.h    | 72 ++++++++++++++++++++++++++++
 libc/test/src/stdfix/CMakeLists.txt  | 16 +++++++
 libc/test/src/stdfix/bitshk_test.cpp | 14 ++++++
 libc/test/src/stdfix/bitsk_test.cpp  | 14 ++++++
 libc/test/src/stdfix/bitslk_test.cpp | 14 ++++++
 libc/test/src/stdfix/bitslr_test.cpp | 14 ++++++
 libc/test/src/stdfix/bitsr_test.cpp  | 14 ++++++
 7 files changed, 158 insertions(+)
 create mode 100644 libc/test/src/stdfix/BitsFxTest.h
 create mode 100644 libc/test/src/stdfix/bitshk_test.cpp
 create mode 100644 libc/test/src/stdfix/bitsk_test.cpp
 create mode 100644 libc/test/src/stdfix/bitslk_test.cpp
 create mode 100644 libc/test/src/stdfix/bitslr_test.cpp
 create mode 100644 libc/test/src/stdfix/bitsr_test.cpp

diff --git a/libc/test/src/stdfix/BitsFxTest.h b/libc/test/src/stdfix/BitsFxTest.h
new file mode 100644
index 0000000000000..2a9d072a0a0ff
--- /dev/null
+++ b/libc/test/src/stdfix/BitsFxTest.h
@@ -0,0 +1,72 @@
+//===-- Utility class to test bitsfx functions ------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "test/UnitTest/Test.h"
+
+#include "src/__support/fixed_point/fx_rep.h"
+
+template <typename From, typename To>
+class BitsFxTest : public LIBC_NAMESPACE::testing::Test {
+
+  using FXRep = LIBC_NAMESPACE::fixed_point::FXRep<From>;
+  static constexpr From zero = FXRep::ZERO();
+  static constexpr From max = FXRep::MAX();
+  static constexpr From min = FXRep::MIN();
+  static constexpr From one_half = FXRep::ONE_HALF();
+  static constexpr From one_fourth = FXRep::ONE_FOURTH();
+  static constexpr From eps = FXRep::EPS();
+  // (0.42)_10 =
+  // (0.0110101110000101000111101011100001010001111010111000010100011110)_2 =
+  // (0.0x6b851eb851eb851e)_16
+  static constexpr unsigned long long zero_point_forty_two =
+      0x6b851eb851eb851eull;
+
+  static constexpr unsigned long long maxval = ~(1ULL << FXRep::VALUE_LEN);
+  static constexpr unsigned long long minval = -(maxval + 1ULL);
+
+public:
+  typedef To (*BitsFxFunc)(From);
+
+  void testSpecialNumbers(BitsFxFunc func) {
+    EXPECT_EQ(static_cast<To>(0), func(zero));
+    EXPECT_EQ(static_cast<To>(1 << (FXRep::FRACTION_LEN - 1)), func(one_half));
+    EXPECT_EQ(static_cast<To>(1 << (FXRep::FRACTION_LEN - 2)),
+              func(one_fourth));
+    EXPECT_EQ(static_cast<To>(1), func(eps));
+    EXPECT_EQ(static_cast<To>(maxval), func(max));
+    EXPECT_EQ(static_cast<To>(minval), func(min));
+
+    // (0.6875)_10 = (0.1011)_2
+    EXPECT_EQ(static_cast<To>(11 << (FXRep::FRACTION_LEN - 4)), func(0.6875));
+
+    EXPECT_EQ(
+        static_cast<To>(zero_point_forty_two >> (64 - FXRep::FRACTION_LEN)),
+        func(0.42));
+
+    // EXPECT_EQ(static_cast<To>(0), func(5));
+
+    // if constexpr (static_cast<int>(min) <= -16)
+    //   EXPECT_EQ(static_cast<To>(0), func(-16));
+
+    // if constexpr (static_cast<int>(min) <= -10)
+    //   EXPECT_EQ(static_cast<To>(0), func(-10));
+
+    // EXPECT_EQ(static_cast<To>(), func(max));
+
+    // if constexpr (static_cast<int>(max) >= 16.25)
+    //   EXPECT_EQ(static_cast<To>(0), func(16.25));
+
+    // if constexpr (static_cast<int>(max) > -10)
+    //   EXPECT_EQ(static_cast<To>(0), func(16));
+  }
+};
+
+#define LIST_BITSFX_TESTS(From, To, func)                                      \
+  using LlvmLibcBitsFxTest = BitsFxTest<From, To>;                             \
+  TEST_F(LlvmLibcBitsFxTest, SpecialNumbers) { testSpecialNumbers(&func); }    \
+  static_assert(true, "Require semicolon.")
diff --git a/libc/test/src/stdfix/CMakeLists.txt b/libc/test/src/stdfix/CMakeLists.txt
index 8f0226bf41672..8e399d1350619 100644
--- a/libc/test/src/stdfix/CMakeLists.txt
+++ b/libc/test/src/stdfix/CMakeLists.txt
@@ -89,6 +89,22 @@ foreach(suffix IN ITEMS hr r lr hk k lk uhr ur ulr uhk uk ulk)
   )
 endforeach()
 
+foreach(suffix IN ITEMS hk k lk r lr)
+  add_libc_test(
+    bits${suffix}_test
+    SUITE
+      libc-stdfix-tests
+    HDRS
+      BitsFxTest.h
+    SRCS
+      bits${suffix}_test.cpp
+    DEPENDS
+      libc.src.stdfix.bits${suffix}
+      libc.src.__support.fixed_point.fx_rep
+      libc.src.__support.fixed_point.fx_bits
+  )
+endforeach()
+
 add_libc_test(
   uhksqrtus_test
   SUITE
diff --git a/libc/test/src/stdfix/bitshk_test.cpp b/libc/test/src/stdfix/bitshk_test.cpp
new file mode 100644
index 0000000000000..b973d82de9870
--- /dev/null
+++ b/libc/test/src/stdfix/bitshk_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for bitshk ----------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "BitsFxTest.h"
+
+#include "llvm-libc-types/stdfix-types.h" // int_hk_t
+#include "src/stdfix/bitshk.h"
+
+LIST_BITSFX_TESTS(short accum, int_hk_t, LIBC_NAMESPACE::bitshk);
diff --git a/libc/test/src/stdfix/bitsk_test.cpp b/libc/test/src/stdfix/bitsk_test.cpp
new file mode 100644
index 0000000000000..261c74d0d2a9c
--- /dev/null
+++ b/libc/test/src/stdfix/bitsk_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for bitsk -----------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "BitsFxTest.h"
+
+#include "llvm-libc-types/stdfix-types.h" // int_k_t
+#include "src/stdfix/bitsk.h"
+
+LIST_BITSFX_TESTS(accum, int_k_t, LIBC_NAMESPACE::bitsk);
diff --git a/libc/test/src/stdfix/bitslk_test.cpp b/libc/test/src/stdfix/bitslk_test.cpp
new file mode 100644
index 0000000000000..6c40aff616aa9
--- /dev/null
+++ b/libc/test/src/stdfix/bitslk_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for bitslk ----------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "BitsFxTest.h"
+
+#include "llvm-libc-types/stdfix-types.h" // int_lk_t
+#include "src/stdfix/bitslk.h"
+
+LIST_BITSFX_TESTS(long accum, int_lk_t, LIBC_NAMESPACE::bitslk);
diff --git a/libc/test/src/stdfix/bitslr_test.cpp b/libc/test/src/stdfix/bitslr_test.cpp
new file mode 100644
index 0000000000000..10f41348b4ace
--- /dev/null
+++ b/libc/test/src/stdfix/bitslr_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for bitslr ----------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "BitsFxTest.h"
+
+#include "llvm-libc-types/stdfix-types.h" // int_lr_t
+#include "src/stdfix/bitslr.h"
+
+LIST_BITSFX_TESTS(long fract, int_lr_t, LIBC_NAMESPACE::bitslr);
diff --git a/libc/test/src/stdfix/bitsr_test.cpp b/libc/test/src/stdfix/bitsr_test.cpp
new file mode 100644
index 0000000000000..a3a4ac3a4aae4
--- /dev/null
+++ b/libc/test/src/stdfix/bitsr_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for bitsr -----------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "BitsFxTest.h"
+
+#include "llvm-libc-types/stdfix-types.h" // int_r_t
+#include "src/stdfix/bitsr.h"
+
+LIST_BITSFX_TESTS(fract, int_r_t, LIBC_NAMESPACE::bitsr);

>From e98d4e2c15dfc030f024887bc0df8f6220ef4322 Mon Sep 17 00:00:00 2001
From: krishna2803 <kpandey81930 at gmail.com>
Date: Sun, 23 Feb 2025 15:26:06 +0530
Subject: [PATCH 03/11] add: entrypoints for bitsfx

Signed-off-by: krishna2803 <kpandey81930 at gmail.com>
---
 libc/config/baremetal/arm/entrypoints.txt   | 18 ++++++++++++------
 libc/config/baremetal/riscv/entrypoints.txt | 18 ++++++++++++------
 libc/config/linux/riscv/entrypoints.txt     | 18 ++++++++++++------
 libc/config/linux/x86_64/entrypoints.txt    | 18 ++++++++++++------
 4 files changed, 48 insertions(+), 24 deletions(-)

diff --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index e1dd16108b5ae..97705960e7b54 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -507,6 +507,18 @@ if(LIBC_COMPILER_HAS_FIXED_POINT)
     libc.src.stdfix.ukbits
     libc.src.stdfix.lkbits
     libc.src.stdfix.ulkbits
+    libc.src.stdfix.bitshr
+    libc.src.stdfix.bitsr
+    libc.src.stdfix.bitslr
+    libc.src.stdfix.bitshk
+    libc.src.stdfix.bitsk
+    libc.src.stdfix.bitslk
+    libc.src.stdfix.bitsuhr
+    libc.src.stdfix.bitsur
+    libc.src.stdfix.bitsulr
+    libc.src.stdfix.bitsuhk
+    libc.src.stdfix.bitsuk
+    libc.src.stdfix.bitsulk
     libc.src.stdfix.countlshr
     libc.src.stdfix.countlsr
     libc.src.stdfix.countlslr
@@ -519,12 +531,6 @@ if(LIBC_COMPILER_HAS_FIXED_POINT)
     libc.src.stdfix.countlsuhk
     libc.src.stdfix.countlsuk
     libc.src.stdfix.countlsulk
-    libc.src.stdfix.bitshr
-    libc.src.stdfix.bitsr
-    libc.src.stdfix.bitslr
-    libc.src.stdfix.bitshk
-    libc.src.stdfix.bitsk
-    libc.src.stdfix.bitslk
   )
 endif()
 
diff --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index 24a3bc2db60d5..bebbc94f183cb 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -502,6 +502,18 @@ if(LIBC_COMPILER_HAS_FIXED_POINT)
     libc.src.stdfix.ukbits
     libc.src.stdfix.lkbits
     libc.src.stdfix.ulkbits
+    libc.src.stdfix.bitshr
+    libc.src.stdfix.bitsr
+    libc.src.stdfix.bitslr
+    libc.src.stdfix.bitshk
+    libc.src.stdfix.bitsk
+    libc.src.stdfix.bitslk
+    libc.src.stdfix.bitshr
+    libc.src.stdfix.bitsur
+    libc.src.stdfix.bitsulr
+    libc.src.stdfix.bitsuhk
+    libc.src.stdfix.bitsuk
+    libc.src.stdfix.bitsulk
     libc.src.stdfix.countlshr
     libc.src.stdfix.countlsr
     libc.src.stdfix.countlslr
@@ -514,12 +526,6 @@ if(LIBC_COMPILER_HAS_FIXED_POINT)
     libc.src.stdfix.countlsuhk
     libc.src.stdfix.countlsuk
     libc.src.stdfix.countlsulk
-    libc.src.stdfix.bitshr
-    libc.src.stdfix.bitsr
-    libc.src.stdfix.bitslr
-    libc.src.stdfix.bitshk
-    libc.src.stdfix.bitsk
-    libc.src.stdfix.bitslk
   )
 endif()
 
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 8fb7d76da7a53..978bc62635df3 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -749,6 +749,18 @@ if(LIBC_COMPILER_HAS_FIXED_POINT)
     # TODO: https://github.com/llvm/llvm-project/issues/115778
     libc.src.stdfix.lkbits
     libc.src.stdfix.ulkbits
+    libc.src.stdfix.bitshr
+    libc.src.stdfix.bitsr
+    libc.src.stdfix.bitslr
+    libc.src.stdfix.bitshk
+    libc.src.stdfix.bitsk
+    libc.src.stdfix.bitslk
+    libc.src.stdfix.bitsuhr
+    libc.src.stdfix.bitsur
+    libc.src.stdfix.bitsulr
+    libc.src.stdfix.bitsuhk
+    libc.src.stdfix.bitsuk
+    libc.src.stdfix.bitsulk
     libc.src.stdfix.countlshr
     libc.src.stdfix.countlsr
     libc.src.stdfix.countlslr
@@ -761,12 +773,6 @@ if(LIBC_COMPILER_HAS_FIXED_POINT)
     libc.src.stdfix.countlsuhk
     libc.src.stdfix.countlsuk
     libc.src.stdfix.countlsulk
-    libc.src.stdfix.bitshr
-    libc.src.stdfix.bitsr
-    libc.src.stdfix.bitslr
-    libc.src.stdfix.bitshk
-    libc.src.stdfix.bitsk
-    libc.src.stdfix.bitslk
   )
 endif()
 
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 4895fe5e12dc3..dbe8575405a3b 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -875,6 +875,18 @@ if(LIBC_COMPILER_HAS_FIXED_POINT)
     libc.src.stdfix.ukbits
     libc.src.stdfix.lkbits
     libc.src.stdfix.ulkbits
+    libc.src.stdfix.bitshr
+    libc.src.stdfix.bitsr
+    libc.src.stdfix.bitslr
+    libc.src.stdfix.bitshk
+    libc.src.stdfix.bitsk
+    libc.src.stdfix.bitslk
+    libc.src.stdfix.bitsuhr
+    libc.src.stdfix.bitsur
+    libc.src.stdfix.bitsulr
+    libc.src.stdfix.bitsuhk
+    libc.src.stdfix.bitsuk
+    libc.src.stdfix.bitsulk
     libc.src.stdfix.countlshr
     libc.src.stdfix.countlsr
     libc.src.stdfix.countlslr
@@ -887,12 +899,6 @@ if(LIBC_COMPILER_HAS_FIXED_POINT)
     libc.src.stdfix.countlsuhk
     libc.src.stdfix.countlsuk
     libc.src.stdfix.countlsulk
-    libc.src.stdfix.bitshr
-    libc.src.stdfix.bitsr
-    libc.src.stdfix.bitslr
-    libc.src.stdfix.bitshk
-    libc.src.stdfix.bitsk
-    libc.src.stdfix.bitslk
   )
 endif()
 

>From 24b27c8683369ffdc975ee7c1b69652b90141b85 Mon Sep 17 00:00:00 2001
From: krishna2803 <kpandey81930 at gmail.com>
Date: Sun, 23 Feb 2025 15:27:00 +0530
Subject: [PATCH 04/11] chore: change From and To to T and XType respectively

Signed-off-by: krishna2803 <kpandey81930 at gmail.com>
---
 libc/src/__support/fixed_point/fx_bits.h | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/libc/src/__support/fixed_point/fx_bits.h b/libc/src/__support/fixed_point/fx_bits.h
index a8a3b18952673..a9b7fe248f6f1 100644
--- a/libc/src/__support/fixed_point/fx_bits.h
+++ b/libc/src/__support/fixed_point/fx_bits.h
@@ -121,7 +121,7 @@ bit_and(T x, T y) {
   using BitType = typename FXRep<T>::StorageType;
   BitType x_bit = cpp::bit_cast<BitType>(x);
   BitType y_bit = cpp::bit_cast<BitType>(y);
-  // For some reason, bit_cast cannot deduce BitType from the input.
+  // For some reason, bit_cast cannot deduce BitType T the input.
   return cpp::bit_cast<T, BitType>(x_bit & y_bit);
 }
 
@@ -131,7 +131,7 @@ bit_or(T x, T y) {
   using BitType = typename FXRep<T>::StorageType;
   BitType x_bit = cpp::bit_cast<BitType>(x);
   BitType y_bit = cpp::bit_cast<BitType>(y);
-  // For some reason, bit_cast cannot deduce BitType from the input.
+  // For some reason, bit_cast cannot deduce BitType T the input.
   return cpp::bit_cast<T, BitType>(x_bit | y_bit);
 }
 
@@ -140,7 +140,7 @@ LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_fixed_point_v<T>, T>
 bit_not(T x) {
   using BitType = typename FXRep<T>::StorageType;
   BitType x_bit = cpp::bit_cast<BitType>(x);
-  // For some reason, bit_cast cannot deduce BitType from the input.
+  // For some reason, bit_cast cannot deduce BitType T the input.
   return cpp::bit_cast<T, BitType>(static_cast<BitType>(~x_bit));
 }
 
@@ -195,10 +195,10 @@ countls(T f) {
 }
 
 // fixed-point to integer conversion
-template <typename From, typename To>
-LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_fixed_point_v<From>, To>
-bitsfx(From f) {
-  return cpp::bit_cast<To, From>(f);
+template <typename T, typename XType>
+LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_fixed_point_v<T>, XType>
+bitsfx(T f) {
+  return cpp::bit_cast<XType, T>(f);
 }
 
 } // namespace fixed_point

>From 178534206f8afdc55c9e29265a3047331a3dd0ed Mon Sep 17 00:00:00 2001
From: krishna2803 <kpandey81930 at gmail.com>
Date: Sun, 23 Feb 2025 15:27:53 +0530
Subject: [PATCH 05/11] add: bitsu{fx}

Signed-off-by: krishna2803 <kpandey81930 at gmail.com>
---
 libc/src/stdfix/bitsuhk.cpp | 22 ++++++++++++++++++++++
 libc/src/stdfix/bitsuhk.h   | 22 ++++++++++++++++++++++
 libc/src/stdfix/bitsuhr.cpp | 22 ++++++++++++++++++++++
 libc/src/stdfix/bitsuhr.h   | 22 ++++++++++++++++++++++
 libc/src/stdfix/bitsuk.cpp  | 22 ++++++++++++++++++++++
 libc/src/stdfix/bitsuk.h    | 22 ++++++++++++++++++++++
 libc/src/stdfix/bitsulk.cpp | 22 ++++++++++++++++++++++
 libc/src/stdfix/bitsulk.h   | 22 ++++++++++++++++++++++
 libc/src/stdfix/bitsulr.cpp | 22 ++++++++++++++++++++++
 libc/src/stdfix/bitsulr.h   | 22 ++++++++++++++++++++++
 libc/src/stdfix/bitsur.cpp  | 22 ++++++++++++++++++++++
 libc/src/stdfix/bitsur.h    | 22 ++++++++++++++++++++++
 libc/src/stdfix/bitusk.cpp  | 22 ++++++++++++++++++++++
 13 files changed, 286 insertions(+)
 create mode 100755 libc/src/stdfix/bitsuhk.cpp
 create mode 100755 libc/src/stdfix/bitsuhk.h
 create mode 100755 libc/src/stdfix/bitsuhr.cpp
 create mode 100755 libc/src/stdfix/bitsuhr.h
 create mode 100755 libc/src/stdfix/bitsuk.cpp
 create mode 100755 libc/src/stdfix/bitsuk.h
 create mode 100755 libc/src/stdfix/bitsulk.cpp
 create mode 100755 libc/src/stdfix/bitsulk.h
 create mode 100755 libc/src/stdfix/bitsulr.cpp
 create mode 100755 libc/src/stdfix/bitsulr.h
 create mode 100755 libc/src/stdfix/bitsur.cpp
 create mode 100755 libc/src/stdfix/bitsur.h
 create mode 100755 libc/src/stdfix/bitusk.cpp

diff --git a/libc/src/stdfix/bitsuhk.cpp b/libc/src/stdfix/bitsuhk.cpp
new file mode 100755
index 0000000000000..1b0bf59a550f1
--- /dev/null
+++ b/libc/src/stdfix/bitsuhk.cpp
@@ -0,0 +1,22 @@
+//===-- Implementation of bitsuhk function  -------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "bitsuhk.h"
+#include "include/llvm-libc-macros/stdfix-macros.h" // unsigned short accum
+#include "include/llvm-libc-types/stdfix-types.h"   // uint_uhk_t
+#include "src/__support/common.h"                   // LLVM_LIBC_FUNCTION
+#include "src/__support/fixed_point/fx_bits.h"      // fixed_point
+#include "src/__support/macros/config.h"            // LIBC_NAMESPACE_DECL
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(uint_uhk_t, bitsuhk, (unsigned short accum f)) {
+  return fixed_point::bitsfx<unsigned short accum, uint_uhk_t>(f);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdfix/bitsuhk.h b/libc/src/stdfix/bitsuhk.h
new file mode 100755
index 0000000000000..1e80286d77099
--- /dev/null
+++ b/libc/src/stdfix/bitsuhk.h
@@ -0,0 +1,22 @@
+//===-- Implementation header for bitsuhk function --------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDFIX_BITSUHK_H
+#define LLVM_LIBC_SRC_STDFIX_BITSUHK_H
+
+#include "include/llvm-libc-macros/stdfix-macros.h" // unsigned short accum
+#include "include/llvm-libc-types/stdfix-types.h"   // uint_uhk_t
+#include "src/__support/macros/config.h"            // LIBC_NAMESPACE_DECL
+
+namespace LIBC_NAMESPACE_DECL {
+
+uint_uhk_t bitsuhk(unsigned short accum f);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STDFIX_BITSUHK_H
diff --git a/libc/src/stdfix/bitsuhr.cpp b/libc/src/stdfix/bitsuhr.cpp
new file mode 100755
index 0000000000000..66152e14bb209
--- /dev/null
+++ b/libc/src/stdfix/bitsuhr.cpp
@@ -0,0 +1,22 @@
+//===-- Implementation of bitsuhr function  -------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "bitsuhr.h"
+#include "include/llvm-libc-macros/stdfix-macros.h" // unsigned short fract
+#include "include/llvm-libc-types/stdfix-types.h"   // uint_uhr_t
+#include "src/__support/common.h"                   // LLVM_LIBC_FUNCTION
+#include "src/__support/fixed_point/fx_bits.h"      // fixed_point
+#include "src/__support/macros/config.h"            // LIBC_NAMESPACE_DECL
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(uint_uhr_t, bitsuhr, (unsigned short fract f)) {
+  return fixed_point::bitsfx<unsigned short fract, uint_uhr_t>(f);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdfix/bitsuhr.h b/libc/src/stdfix/bitsuhr.h
new file mode 100755
index 0000000000000..0311665bc17f3
--- /dev/null
+++ b/libc/src/stdfix/bitsuhr.h
@@ -0,0 +1,22 @@
+//===-- Implementation header for bitsuhr function --------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDFIX_BITSUHR_H
+#define LLVM_LIBC_SRC_STDFIX_BITSUHR_H
+
+#include "include/llvm-libc-macros/stdfix-macros.h" // unsigned short fract
+#include "include/llvm-libc-types/stdfix-types.h"   // uint_uhr_t
+#include "src/__support/macros/config.h"            // LIBC_NAMESPACE_DECL
+
+namespace LIBC_NAMESPACE_DECL {
+
+uint_uhr_t bitsuhr(unsigned short fract f);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STDFIX_BITSUHR_H
diff --git a/libc/src/stdfix/bitsuk.cpp b/libc/src/stdfix/bitsuk.cpp
new file mode 100755
index 0000000000000..b0a92bd92d4ea
--- /dev/null
+++ b/libc/src/stdfix/bitsuk.cpp
@@ -0,0 +1,22 @@
+//===-- Implementation for bitsuk function  -------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "bitsuk.h"
+#include "include/llvm-libc-macros/stdfix-macros.h" // unsigned accum
+#include "include/llvm-libc-types/stdfix-types.h"   // uint_uk_t
+#include "src/__support/common.h"                   // LLVM_LIBC_FUNCTION
+#include "src/__support/fixed_point/fx_bits.h"      // fixed_point
+#include "src/__support/macros/config.h"            // LIBC_NAMESPACE_DECL
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(uint_uk_t, bitsuk, (unsigned accum f)) {
+  return fixed_point::bitsfx<unsigned accum, uint_uk_t>(f);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdfix/bitsuk.h b/libc/src/stdfix/bitsuk.h
new file mode 100755
index 0000000000000..fce37e82d44c1
--- /dev/null
+++ b/libc/src/stdfix/bitsuk.h
@@ -0,0 +1,22 @@
+//===-- Implementation header for bitsuk function ---------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDFIX_BITSUK_H
+#define LLVM_LIBC_SRC_STDFIX_BITSUK_H
+
+#include "include/llvm-libc-macros/stdfix-macros.h" // unsigned accum
+#include "include/llvm-libc-types/stdfix-types.h"   // uint_uk_t
+#include "src/__support/macros/config.h"            // LIBC_NAMESPACE_DECL
+
+namespace LIBC_NAMESPACE_DECL {
+
+uint_uk_t bitsuk(unsigned accum f);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STDFIX_BITSUK_H
diff --git a/libc/src/stdfix/bitsulk.cpp b/libc/src/stdfix/bitsulk.cpp
new file mode 100755
index 0000000000000..b8f61a16eb61e
--- /dev/null
+++ b/libc/src/stdfix/bitsulk.cpp
@@ -0,0 +1,22 @@
+//===-- Implementation for bitsulk function  ------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "bitsulk.h"
+#include "include/llvm-libc-macros/stdfix-macros.h" // unsigned long accum
+#include "include/llvm-libc-types/stdfix-types.h"   // uint_ulk_t
+#include "src/__support/common.h"                   // LLVM_LIBC_FUNCTION
+#include "src/__support/fixed_point/fx_bits.h"      // fixed_point
+#include "src/__support/macros/config.h"            // LIBC_NAMESPACE_DECL
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(uint_ulk_t, bitsulk, (unsigned long accum f)) {
+  return fixed_point::bitsfx<unsigned long accum, uint_ulk_t>(f);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdfix/bitsulk.h b/libc/src/stdfix/bitsulk.h
new file mode 100755
index 0000000000000..1bf681ee751c6
--- /dev/null
+++ b/libc/src/stdfix/bitsulk.h
@@ -0,0 +1,22 @@
+//===-- Implementation header for bitsulk function --------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDFIX_BITSLK_H
+#define LLVM_LIBC_SRC_STDFIX_BITSLK_H
+
+#include "include/llvm-libc-macros/stdfix-macros.h" // unsigned long accum
+#include "include/llvm-libc-types/stdfix-types.h"   // uint_ulk_t
+#include "src/__support/macros/config.h"            // LIBC_NAMESPACE_DECL
+
+namespace LIBC_NAMESPACE_DECL {
+
+uint_ulk_t bitsulk(unsigned long accum f);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STDFIX_BITSLK_H
diff --git a/libc/src/stdfix/bitsulr.cpp b/libc/src/stdfix/bitsulr.cpp
new file mode 100755
index 0000000000000..9fd1b15bedad9
--- /dev/null
+++ b/libc/src/stdfix/bitsulr.cpp
@@ -0,0 +1,22 @@
+//===-- Implementation of bitsulr function  -------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "bitsulr.h"
+#include "include/llvm-libc-macros/stdfix-macros.h" // unsigned long fract
+#include "include/llvm-libc-types/stdfix-types.h"   // uint_ulr_t
+#include "src/__support/common.h"                   // LLVM_LIBC_FUNCTION
+#include "src/__support/fixed_point/fx_bits.h"      // fixed_point
+#include "src/__support/macros/config.h"            // LIBC_NAMESPACE_DECL
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(uint_ulr_t, bitsulr, (unsigned long fract f)) {
+  return fixed_point::bitsfx<unsigned long fract, uint_ulr_t>(f);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdfix/bitsulr.h b/libc/src/stdfix/bitsulr.h
new file mode 100755
index 0000000000000..cf0f6fbe6698d
--- /dev/null
+++ b/libc/src/stdfix/bitsulr.h
@@ -0,0 +1,22 @@
+//===-- Implementation header for bitsulr function --------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDFIX_BITSULR_H
+#define LLVM_LIBC_SRC_STDFIX_BITSULR_H
+
+#include "include/llvm-libc-macros/stdfix-macros.h" // unsigned long fract
+#include "include/llvm-libc-types/stdfix-types.h"   // uint_ulr_t
+#include "src/__support/macros/config.h"            // LIBC_NAMESPACE_DECL
+
+namespace LIBC_NAMESPACE_DECL {
+
+uint_ulr_t bitsulr(unsigned long fract f);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STDFIX_BITSULR_H
diff --git a/libc/src/stdfix/bitsur.cpp b/libc/src/stdfix/bitsur.cpp
new file mode 100755
index 0000000000000..ffb52de9257bf
--- /dev/null
+++ b/libc/src/stdfix/bitsur.cpp
@@ -0,0 +1,22 @@
+//===-- Implementation of bitsur function ---------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "bitsur.h"
+#include "include/llvm-libc-macros/stdfix-macros.h" // unsigned fract
+#include "include/llvm-libc-types/stdfix-types.h"   // uint_ur_t
+#include "src/__support/common.h"                   // LLVM_LIBC_FUNCTION
+#include "src/__support/fixed_point/fx_bits.h"      // fixed_point
+#include "src/__support/macros/config.h"            // LIBC_NAMESPACE_DECL
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(uint_ur_t, bitsur, (unsigned fract f)) {
+  return fixed_point::bitsfx<unsigned fract, uint_ur_t>(f);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdfix/bitsur.h b/libc/src/stdfix/bitsur.h
new file mode 100755
index 0000000000000..4c938bb65ec8d
--- /dev/null
+++ b/libc/src/stdfix/bitsur.h
@@ -0,0 +1,22 @@
+//===-- Implementation header for bitsur function ---------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDFIX_BITSUR_H
+#define LLVM_LIBC_SRC_STDFIX_BITSUR_H
+
+#include "include/llvm-libc-macros/stdfix-macros.h" // unsigned fract
+#include "include/llvm-libc-types/stdfix-types.h"   // uint_ur_t
+#include "src/__support/macros/config.h"            // LIBC_NAMESPACE_DECL
+
+namespace LIBC_NAMESPACE_DECL {
+
+uint_ur_t bitsur(unsigned fract f);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STDFIX_BITSUR_H
diff --git a/libc/src/stdfix/bitusk.cpp b/libc/src/stdfix/bitusk.cpp
new file mode 100755
index 0000000000000..ac0852e078c60
--- /dev/null
+++ b/libc/src/stdfix/bitusk.cpp
@@ -0,0 +1,22 @@
+//===-- Implementation for bitsuk function --------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "bitsuk.h"
+#include "include/llvm-libc-macros/stdfix-macros.h" // unsigned accum
+#include "include/llvm-libc-types/stdfix-types.h"   // uint_uk_t
+#include "src/__support/common.h"                   // LLVM_LIBC_FUNCTION
+#include "src/__support/fixed_point/fx_bits.h"      // fixed_point
+#include "src/__support/macros/config.h"            // LIBC_NAMESPACE_DECL
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(uint_uk_t, bitsuk, (unsigned accum f)) {
+  return fixed_point::bitsfx<unsigned accum, uint_uk_t>(f);
+}
+
+} // namespace LIBC_NAMESPACE_DECL

>From 752ea6dd78b617de764ad74100b992c56341fb63 Mon Sep 17 00:00:00 2001
From: krishna2803 <kpandey81930 at gmail.com>
Date: Sun, 23 Feb 2025 15:42:48 +0530
Subject: [PATCH 06/11] chore: reposition bitsfx in CMakeLists

Signed-off-by: krishna2803 <kpandey81930 at gmail.com>
---
 libc/src/stdfix/CMakeLists.txt | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/libc/src/stdfix/CMakeLists.txt b/libc/src/stdfix/CMakeLists.txt
index 8ad9f54c0f10c..362af0bf0d55c 100644
--- a/libc/src/stdfix/CMakeLists.txt
+++ b/libc/src/stdfix/CMakeLists.txt
@@ -49,29 +49,29 @@ foreach(suffix IN ITEMS hr r lr hk k lk uhr ur ulr uhk uk ulk)
   )
 
   add_entrypoint_object(
-    countls${suffix}
+    bits${suffix}
     HDRS
-      countls${suffix}.h
+      bits${suffix}.h
     SRCS
-      countls${suffix}.cpp
+      bits${suffix}.cpp
     COMPILE_OPTIONS
       ${libc_opt_high_flag}
     DEPENDS
       libc.src.__support.fixed_point.fx_bits
+      libc.include.llvm-libc-types.stdfix-types
+      libc.include.llvm-libc-macros.stdfix_macros
   )
 
   add_entrypoint_object(
-    bits${suffix}
+    countls${suffix}
     HDRS
-      bits${suffix}.h
+      countls${suffix}.h
     SRCS
-      bits${suffix}.cpp
+      countls${suffix}.cpp
     COMPILE_OPTIONS
       ${libc_opt_high_flag}
     DEPENDS
       libc.src.__support.fixed_point.fx_bits
-      libc.include.llvm-libc-types.stdfix-types
-      libc.include.llvm-libc-macros.stdfix_macros
   )
 endforeach()
 

>From 7099e888b384c7b96d175103471fc3125eeee27a Mon Sep 17 00:00:00 2001
From: krishna2803 <kpandey81930 at gmail.com>
Date: Sun, 23 Feb 2025 15:44:26 +0530
Subject: [PATCH 07/11] add: tests for bitsu{fx} and make the code consistent
 with {fx}bits

Signed-off-by: krishna2803 <kpandey81930 at gmail.com>
---
 libc/test/src/stdfix/BitsFxTest.h     | 64 ++++++++++-----------------
 libc/test/src/stdfix/bitshk_test.cpp  |  2 +-
 libc/test/src/stdfix/bitshr_test.cpp  | 14 ++++++
 libc/test/src/stdfix/bitsk_test.cpp   |  2 +-
 libc/test/src/stdfix/bitslk_test.cpp  |  2 +-
 libc/test/src/stdfix/bitslr_test.cpp  |  2 +-
 libc/test/src/stdfix/bitsr_test.cpp   |  2 +-
 libc/test/src/stdfix/bitsuhk_test.cpp | 15 +++++++
 libc/test/src/stdfix/bitsuhr_test.cpp | 15 +++++++
 libc/test/src/stdfix/bitsuk_test.cpp  | 14 ++++++
 libc/test/src/stdfix/bitsulk_test.cpp | 15 +++++++
 libc/test/src/stdfix/bitsulr_test.cpp | 15 +++++++
 libc/test/src/stdfix/bitsur_test.cpp  | 14 ++++++
 13 files changed, 131 insertions(+), 45 deletions(-)
 create mode 100644 libc/test/src/stdfix/bitshr_test.cpp
 create mode 100644 libc/test/src/stdfix/bitsuhk_test.cpp
 create mode 100644 libc/test/src/stdfix/bitsuhr_test.cpp
 create mode 100644 libc/test/src/stdfix/bitsuk_test.cpp
 create mode 100644 libc/test/src/stdfix/bitsulk_test.cpp
 create mode 100644 libc/test/src/stdfix/bitsulr_test.cpp
 create mode 100644 libc/test/src/stdfix/bitsur_test.cpp

diff --git a/libc/test/src/stdfix/BitsFxTest.h b/libc/test/src/stdfix/BitsFxTest.h
index 2a9d072a0a0ff..6dd81c1e2aa97 100644
--- a/libc/test/src/stdfix/BitsFxTest.h
+++ b/libc/test/src/stdfix/BitsFxTest.h
@@ -10,63 +10,47 @@
 
 #include "src/__support/fixed_point/fx_rep.h"
 
-template <typename From, typename To>
+template <typename T, typename XType>
 class BitsFxTest : public LIBC_NAMESPACE::testing::Test {
 
-  using FXRep = LIBC_NAMESPACE::fixed_point::FXRep<From>;
-  static constexpr From zero = FXRep::ZERO();
-  static constexpr From max = FXRep::MAX();
-  static constexpr From min = FXRep::MIN();
-  static constexpr From one_half = FXRep::ONE_HALF();
-  static constexpr From one_fourth = FXRep::ONE_FOURTH();
-  static constexpr From eps = FXRep::EPS();
+  using FXRep = LIBC_NAMESPACE::fixed_point::FXRep<T>;
+  static constexpr T zero = FXRep::ZERO();
+  static constexpr T max = FXRep::MAX();
+  static constexpr T min = FXRep::MIN();
+  static constexpr T one_half = FXRep::ONE_HALF();
+  static constexpr T one_fourth = FXRep::ONE_FOURTH();
+  static constexpr T eps = FXRep::EPS();
+
   // (0.42)_10 =
   // (0.0110101110000101000111101011100001010001111010111000010100011110)_2 =
   // (0.0x6b851eb851eb851e)_16
   static constexpr unsigned long long zero_point_forty_two =
-      0x6b851eb851eb851eull;
-
-  static constexpr unsigned long long maxval = ~(1ULL << FXRep::VALUE_LEN);
-  static constexpr unsigned long long minval = -(maxval + 1ULL);
+      0x6b851eb851eb851eULL;
 
 public:
-  typedef To (*BitsFxFunc)(From);
+  typedef XType (*BitsFxFunc)(T);
 
   void testSpecialNumbers(BitsFxFunc func) {
-    EXPECT_EQ(static_cast<To>(0), func(zero));
-    EXPECT_EQ(static_cast<To>(1 << (FXRep::FRACTION_LEN - 1)), func(one_half));
-    EXPECT_EQ(static_cast<To>(1 << (FXRep::FRACTION_LEN - 2)),
+    EXPECT_EQ(static_cast<XType>(0), func(zero));
+    EXPECT_EQ(static_cast<XType>(1ULL << (FXRep::FRACTION_LEN - 1)),
+              func(one_half));
+    EXPECT_EQ(static_cast<XType>(1ULL << (FXRep::FRACTION_LEN - 2)),
               func(one_fourth));
-    EXPECT_EQ(static_cast<To>(1), func(eps));
-    EXPECT_EQ(static_cast<To>(maxval), func(max));
-    EXPECT_EQ(static_cast<To>(minval), func(min));
+    EXPECT_EQ(static_cast<XType>(1), func(eps));
 
     // (0.6875)_10 = (0.1011)_2
-    EXPECT_EQ(static_cast<To>(11 << (FXRep::FRACTION_LEN - 4)), func(0.6875));
+    EXPECT_EQ(static_cast<XType>(11ULL << (FXRep::FRACTION_LEN - 4)),
+              func(0.6875));
 
     EXPECT_EQ(
-        static_cast<To>(zero_point_forty_two >> (64 - FXRep::FRACTION_LEN)),
+        static_cast<XType>(zero_point_forty_two >> (64 - FXRep::FRACTION_LEN)),
         func(0.42));
-
-    // EXPECT_EQ(static_cast<To>(0), func(5));
-
-    // if constexpr (static_cast<int>(min) <= -16)
-    //   EXPECT_EQ(static_cast<To>(0), func(-16));
-
-    // if constexpr (static_cast<int>(min) <= -10)
-    //   EXPECT_EQ(static_cast<To>(0), func(-10));
-
-    // EXPECT_EQ(static_cast<To>(), func(max));
-
-    // if constexpr (static_cast<int>(max) >= 16.25)
-    //   EXPECT_EQ(static_cast<To>(0), func(16.25));
-
-    // if constexpr (static_cast<int>(max) > -10)
-    //   EXPECT_EQ(static_cast<To>(0), func(16));
   }
 };
 
-#define LIST_BITSFX_TESTS(From, To, func)                                      \
-  using LlvmLibcBitsFxTest = BitsFxTest<From, To>;                             \
-  TEST_F(LlvmLibcBitsFxTest, SpecialNumbers) { testSpecialNumbers(&func); }    \
+#define LIST_BITSFX_TESTS(Name, T, XType, func)                                \
+  using LlvmLibcBits##Name##Test = BitsFxTest<T, XType>;                       \
+  TEST_F(LlvmLibcBits##Name##Test, SpecialNumbers) {                           \
+    testSpecialNumbers(&func);                                                 \
+  }                                                                            \
   static_assert(true, "Require semicolon.")
diff --git a/libc/test/src/stdfix/bitshk_test.cpp b/libc/test/src/stdfix/bitshk_test.cpp
index b973d82de9870..ca83162d439a6 100644
--- a/libc/test/src/stdfix/bitshk_test.cpp
+++ b/libc/test/src/stdfix/bitshk_test.cpp
@@ -11,4 +11,4 @@
 #include "llvm-libc-types/stdfix-types.h" // int_hk_t
 #include "src/stdfix/bitshk.h"
 
-LIST_BITSFX_TESTS(short accum, int_hk_t, LIBC_NAMESPACE::bitshk);
+LIST_BITSFX_TESTS(hk, short accum, int_hk_t, LIBC_NAMESPACE::bitshk);
diff --git a/libc/test/src/stdfix/bitshr_test.cpp b/libc/test/src/stdfix/bitshr_test.cpp
new file mode 100644
index 0000000000000..220d7f6a69c16
--- /dev/null
+++ b/libc/test/src/stdfix/bitshr_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for bitshr ----------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "BitsFxTest.h"
+
+#include "llvm-libc-types/stdfix-types.h" // int_hr_t
+#include "src/stdfix/bitshr.h"
+
+LIST_BITSFX_TESTS(hr, short fract, int_hr_t, LIBC_NAMESPACE::bitshr);
diff --git a/libc/test/src/stdfix/bitsk_test.cpp b/libc/test/src/stdfix/bitsk_test.cpp
index 261c74d0d2a9c..7e0057bae4657 100644
--- a/libc/test/src/stdfix/bitsk_test.cpp
+++ b/libc/test/src/stdfix/bitsk_test.cpp
@@ -11,4 +11,4 @@
 #include "llvm-libc-types/stdfix-types.h" // int_k_t
 #include "src/stdfix/bitsk.h"
 
-LIST_BITSFX_TESTS(accum, int_k_t, LIBC_NAMESPACE::bitsk);
+LIST_BITSFX_TESTS(k, accum, int_k_t, LIBC_NAMESPACE::bitsk);
diff --git a/libc/test/src/stdfix/bitslk_test.cpp b/libc/test/src/stdfix/bitslk_test.cpp
index 6c40aff616aa9..46c04e2f75511 100644
--- a/libc/test/src/stdfix/bitslk_test.cpp
+++ b/libc/test/src/stdfix/bitslk_test.cpp
@@ -11,4 +11,4 @@
 #include "llvm-libc-types/stdfix-types.h" // int_lk_t
 #include "src/stdfix/bitslk.h"
 
-LIST_BITSFX_TESTS(long accum, int_lk_t, LIBC_NAMESPACE::bitslk);
+LIST_BITSFX_TESTS(lk, long accum, int_lk_t, LIBC_NAMESPACE::bitslk);
diff --git a/libc/test/src/stdfix/bitslr_test.cpp b/libc/test/src/stdfix/bitslr_test.cpp
index 10f41348b4ace..ef68d2831fb9d 100644
--- a/libc/test/src/stdfix/bitslr_test.cpp
+++ b/libc/test/src/stdfix/bitslr_test.cpp
@@ -11,4 +11,4 @@
 #include "llvm-libc-types/stdfix-types.h" // int_lr_t
 #include "src/stdfix/bitslr.h"
 
-LIST_BITSFX_TESTS(long fract, int_lr_t, LIBC_NAMESPACE::bitslr);
+LIST_BITSFX_TESTS(hk, long fract, int_lr_t, LIBC_NAMESPACE::bitslr);
diff --git a/libc/test/src/stdfix/bitsr_test.cpp b/libc/test/src/stdfix/bitsr_test.cpp
index a3a4ac3a4aae4..0aeb980e30382 100644
--- a/libc/test/src/stdfix/bitsr_test.cpp
+++ b/libc/test/src/stdfix/bitsr_test.cpp
@@ -11,4 +11,4 @@
 #include "llvm-libc-types/stdfix-types.h" // int_r_t
 #include "src/stdfix/bitsr.h"
 
-LIST_BITSFX_TESTS(fract, int_r_t, LIBC_NAMESPACE::bitsr);
+LIST_BITSFX_TESTS(r, fract, int_r_t, LIBC_NAMESPACE::bitsr);
diff --git a/libc/test/src/stdfix/bitsuhk_test.cpp b/libc/test/src/stdfix/bitsuhk_test.cpp
new file mode 100644
index 0000000000000..5ddb78383df02
--- /dev/null
+++ b/libc/test/src/stdfix/bitsuhk_test.cpp
@@ -0,0 +1,15 @@
+//===-- Unittests for bitsuhk ---------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "BitsFxTest.h"
+
+#include "llvm-libc-types/stdfix-types.h" // uint_uhk_t
+#include "src/stdfix/bitsuhk.h"
+
+LIST_BITSFX_TESTS(uhk, unsigned short accum, uint_uhk_t,
+                  LIBC_NAMESPACE::bitsuhk);
diff --git a/libc/test/src/stdfix/bitsuhr_test.cpp b/libc/test/src/stdfix/bitsuhr_test.cpp
new file mode 100644
index 0000000000000..6f5d559859456
--- /dev/null
+++ b/libc/test/src/stdfix/bitsuhr_test.cpp
@@ -0,0 +1,15 @@
+//===-- Unittests for bitsuhr ---------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "BitsFxTest.h"
+
+#include "llvm-libc-types/stdfix-types.h" // uint_uhr_t
+#include "src/stdfix/bitsuhr.h"
+
+LIST_BITSFX_TESTS(uhr, unsigned short fract, uint_uhr_t,
+                  LIBC_NAMESPACE::bitsuhr);
diff --git a/libc/test/src/stdfix/bitsuk_test.cpp b/libc/test/src/stdfix/bitsuk_test.cpp
new file mode 100644
index 0000000000000..309c525f3fd2d
--- /dev/null
+++ b/libc/test/src/stdfix/bitsuk_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for bitsuk ----------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "BitsFxTest.h"
+
+#include "llvm-libc-types/stdfix-types.h" // uint_uk_t
+#include "src/stdfix/bitsuk.h"
+
+LIST_BITSFX_TESTS(uk, unsigned accum, uint_uk_t, LIBC_NAMESPACE::bitsuk);
diff --git a/libc/test/src/stdfix/bitsulk_test.cpp b/libc/test/src/stdfix/bitsulk_test.cpp
new file mode 100644
index 0000000000000..cba011d5f222d
--- /dev/null
+++ b/libc/test/src/stdfix/bitsulk_test.cpp
@@ -0,0 +1,15 @@
+//===-- Unittests for bitsulk ---------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "BitsFxTest.h"
+
+#include "llvm-libc-types/stdfix-types.h" // uint_ulk_t
+#include "src/stdfix/bitsulk.h"
+
+LIST_BITSFX_TESTS(ulk, unsigned long accum, uint_ulk_t,
+                  LIBC_NAMESPACE::bitsulk);
diff --git a/libc/test/src/stdfix/bitsulr_test.cpp b/libc/test/src/stdfix/bitsulr_test.cpp
new file mode 100644
index 0000000000000..39b21c424199e
--- /dev/null
+++ b/libc/test/src/stdfix/bitsulr_test.cpp
@@ -0,0 +1,15 @@
+//===-- Unittests for bitsulr ---------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "BitsFxTest.h"
+
+#include "llvm-libc-types/stdfix-types.h" // uint_ulr_t
+#include "src/stdfix/bitsulr.h"
+
+LIST_BITSFX_TESTS(ulr, unsigned long fract, uint_ulr_t,
+                  LIBC_NAMESPACE::bitsulr);
diff --git a/libc/test/src/stdfix/bitsur_test.cpp b/libc/test/src/stdfix/bitsur_test.cpp
new file mode 100644
index 0000000000000..b7c4b0617eb6e
--- /dev/null
+++ b/libc/test/src/stdfix/bitsur_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for bitsur ----------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "BitsFxTest.h"
+
+#include "llvm-libc-types/stdfix-types.h" // uint_ur_t
+#include "src/stdfix/bitsur.h"
+
+LIST_BITSFX_TESTS(ur, unsigned fract, uint_ur_t, LIBC_NAMESPACE::bitsur);

>From 893c49ff2439f8d60399b27d2944d479acddb5d7 Mon Sep 17 00:00:00 2001
From: krishna2803 <kpandey81930 at gmail.com>
Date: Sun, 23 Feb 2025 15:52:54 +0530
Subject: [PATCH 08/11] chore: update CMakeLists for bitsfx tests

Signed-off-by: krishna2803 <kpandey81930 at gmail.com>
---
 libc/test/src/stdfix/CMakeLists.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/test/src/stdfix/CMakeLists.txt b/libc/test/src/stdfix/CMakeLists.txt
index 8e399d1350619..9012255851037 100644
--- a/libc/test/src/stdfix/CMakeLists.txt
+++ b/libc/test/src/stdfix/CMakeLists.txt
@@ -89,7 +89,7 @@ foreach(suffix IN ITEMS hr r lr hk k lk uhr ur ulr uhk uk ulk)
   )
 endforeach()
 
-foreach(suffix IN ITEMS hk k lk r lr)
+foreach(suffix IN ITEMS r lr hk k lk uhr ur ulr uhk uk)
   add_libc_test(
     bits${suffix}_test
     SUITE

>From a52a6660a12385b23fc921bbf66d0ea9fc7bc471 Mon Sep 17 00:00:00 2001
From: krishna2803 <kpandey81930 at gmail.com>
Date: Sun, 23 Feb 2025 15:53:07 +0530
Subject: [PATCH 09/11] chore: update docs

Signed-off-by: krishna2803 <kpandey81930 at gmail.com>
---
 libc/docs/headers/math/stdfix.rst |  2 +-
 libc/include/stdfix.yaml          | 84 +++++++++++++++++++++++++++++++
 2 files changed, 85 insertions(+), 1 deletion(-)

diff --git a/libc/docs/headers/math/stdfix.rst b/libc/docs/headers/math/stdfix.rst
index 4507f2b608bf1..e12780a32203c 100644
--- a/libc/docs/headers/math/stdfix.rst
+++ b/libc/docs/headers/math/stdfix.rst
@@ -69,7 +69,7 @@ The following functions are included in the ISO/IEC TR 18037:2008 standard.
 +===============+================+=============+===============+============+================+=============+================+=============+===============+============+================+=============+
 | abs           |                | |check|     |               | |check|    |                | |check|     |                | |check|     |               | |check|    |                | |check|     |
 +---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
-| bits\*        |                |             |               |            |                |             |                |             |               |            |                |             |
+| bits\*        |                | |check|     | |check|       | |check|    | |check|        | |check|     | |check|        | |check|     | |check|       | |check|    |                | |check|     |
 +---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
 | \*bits        |                |             |               |            |                |             |                |             |               |            |                |             |
 +---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
diff --git a/libc/include/stdfix.yaml b/libc/include/stdfix.yaml
index 0abf2f3a9b3b6..707e5a2b09819 100644
--- a/libc/include/stdfix.yaml
+++ b/libc/include/stdfix.yaml
@@ -148,6 +148,90 @@ functions:
     arguments:
       - type: uint_ulk_t
     guard: LIBC_COMPILER_HAS_FIXED_POINT
+  - name: bitshr
+    standards:
+      - stdc_ext
+    return_type: int_hr_t
+    arguments:
+      - type: short fract
+    guard: LIBC_COMPILER_HAS_FIXED_POINT
+  - name: bitsuhr
+    standards:
+      - stdc_ext
+    return_type: uint_uhr_t
+    arguments:
+      - type: unsigned short fract
+    guard: LIBC_COMPILER_HAS_FIXED_POINT
+  - name: bitsr
+    standards:
+      - stdc_ext
+    return_type: int_r_t
+    arguments:
+      - type: fract
+    guard: LIBC_COMPILER_HAS_FIXED_POINT
+  - name: bitsur
+    standards:
+      - stdc_ext
+    return_type: uint_ur_t
+    arguments:
+      - type: unsigned fract
+    guard: LIBC_COMPILER_HAS_FIXED_POINT
+  - name: bitslr
+    standards:
+      - stdc_ext
+    return_type: int_lr_t
+    arguments:
+      - type: long fract
+    guard: LIBC_COMPILER_HAS_FIXED_POINT
+  - name: bitsulr
+    standards:
+      - stdc_ext
+    return_type: uint_ulr_t
+    arguments:
+      - type: unsigned long fract
+    guard: LIBC_COMPILER_HAS_FIXED_POINT
+  - name: bitshk
+    standards:
+      - stdc_ext
+    return_type: int_hk_t
+    arguments:
+      - type: short accum
+    guard: LIBC_COMPILER_HAS_FIXED_POINT
+  - name: bitsuhk
+    standards:
+      - stdc_ext
+    return_type: uint_uhk_t
+    arguments:
+      - type: unsigned short accum
+    guard: LIBC_COMPILER_HAS_FIXED_POINT
+  - name: bitsk
+    standards:
+      - stdc_ext
+    return_type: int_k_t
+    arguments:
+      - type: accum
+    guard: LIBC_COMPILER_HAS_FIXED_POINT
+  - name: bitsuk
+    standards:
+      - stdc_ext
+    return_type: uint_uk_t
+    arguments:
+      - type: unsigned accum
+    guard: LIBC_COMPILER_HAS_FIXED_POINT
+  - name: bitslk
+    standards:
+      - stdc_ext
+    return_type: int_lk_t
+    arguments:
+      - type: long accum
+    guard: LIBC_COMPILER_HAS_FIXED_POINT
+  - name: bitsulk
+    standards:
+      - stdc_ext
+    return_type: uint_ulk_t
+    arguments:
+      - type: unsigned long accum
+    guard: LIBC_COMPILER_HAS_FIXED_POINT
   - name: roundhk
     standards:
       - stdc_ext

>From aa047ddbe5d41de9c3a28f2b50808d065186c70b Mon Sep 17 00:00:00 2001
From: krishna2803 <kpandey81930 at gmail.com>
Date: Sun, 23 Feb 2025 16:30:18 +0530
Subject: [PATCH 10/11] add: tests for bitsulk

Signed-off-by: krishna2803 <kpandey81930 at gmail.com>
---
 libc/test/src/stdfix/CMakeLists.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/test/src/stdfix/CMakeLists.txt b/libc/test/src/stdfix/CMakeLists.txt
index 9012255851037..5426c059191f7 100644
--- a/libc/test/src/stdfix/CMakeLists.txt
+++ b/libc/test/src/stdfix/CMakeLists.txt
@@ -89,7 +89,7 @@ foreach(suffix IN ITEMS hr r lr hk k lk uhr ur ulr uhk uk ulk)
   )
 endforeach()
 
-foreach(suffix IN ITEMS r lr hk k lk uhr ur ulr uhk uk)
+foreach(suffix IN ITEMS r lr hk k lk uhr ur ulr uhk uk ulk)
   add_libc_test(
     bits${suffix}_test
     SUITE

>From 851fc4b489fd3c99a92eafe36da1c100d76188e2 Mon Sep 17 00:00:00 2001
From: krishna2803 <kpandey81930 at gmail.com>
Date: Sun, 23 Feb 2025 17:42:39 +0530
Subject: [PATCH 11/11] chore: update docs

Signed-off-by: krishna2803 <kpandey81930 at gmail.com>
---
 libc/docs/headers/math/stdfix.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/docs/headers/math/stdfix.rst b/libc/docs/headers/math/stdfix.rst
index e12780a32203c..73a0a596df331 100644
--- a/libc/docs/headers/math/stdfix.rst
+++ b/libc/docs/headers/math/stdfix.rst
@@ -69,7 +69,7 @@ The following functions are included in the ISO/IEC TR 18037:2008 standard.
 +===============+================+=============+===============+============+================+=============+================+=============+===============+============+================+=============+
 | abs           |                | |check|     |               | |check|    |                | |check|     |                | |check|     |               | |check|    |                | |check|     |
 +---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
-| bits\*        |                | |check|     | |check|       | |check|    | |check|        | |check|     | |check|        | |check|     | |check|       | |check|    |                | |check|     |
+| bits\*        | |check|        | |check|     | |check|       | |check|    | |check|        | |check|     | |check|        | |check|     | |check|       | |check|    | |check|        | |check|     |
 +---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+
 | \*bits        |                |             |               |            |                |             |                |             |               |            |                |             |
 +---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+



More information about the libc-commits mailing list