From f6ff5d5438d72f7e0f72d60c9e0e81d9561ea599 Mon Sep 17 00:00:00 2001 From: krishna2803 Date: Fri, 21 Feb 2025 16:35:30 +0530 Subject: [PATCH 01/17] add: implement bitsfx for signed fixed point numbers Signed-off-by: krishna2803 --- 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 +LIBC_INLINE constexpr cpp::enable_if_t, To> +bitsfx(From f) { + return cpp::bit_cast(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(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(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(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(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(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(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 Date: Fri, 21 Feb 2025 16:38:31 +0530 Subject: [PATCH 02/17] add: tests for some signed `bitsfx` Signed-off-by: krishna2803 --- 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 +class BitsFxTest : public LIBC_NAMESPACE::testing::Test { + + using FXRep = LIBC_NAMESPACE::fixed_point::FXRep; + 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(0), func(zero)); + EXPECT_EQ(static_cast(1 << (FXRep::FRACTION_LEN - 1)), func(one_half)); + EXPECT_EQ(static_cast(1 << (FXRep::FRACTION_LEN - 2)), + func(one_fourth)); + EXPECT_EQ(static_cast(1), func(eps)); + EXPECT_EQ(static_cast(maxval), func(max)); + EXPECT_EQ(static_cast(minval), func(min)); + + // (0.6875)_10 = (0.1011)_2 + EXPECT_EQ(static_cast(11 << (FXRep::FRACTION_LEN - 4)), func(0.6875)); + + EXPECT_EQ( + static_cast(zero_point_forty_two >> (64 - FXRep::FRACTION_LEN)), + func(0.42)); + + // EXPECT_EQ(static_cast(0), func(5)); + + // if constexpr (static_cast(min) <= -16) + // EXPECT_EQ(static_cast(0), func(-16)); + + // if constexpr (static_cast(min) <= -10) + // EXPECT_EQ(static_cast(0), func(-10)); + + // EXPECT_EQ(static_cast(), func(max)); + + // if constexpr (static_cast(max) >= 16.25) + // EXPECT_EQ(static_cast(0), func(16.25)); + + // if constexpr (static_cast(max) > -10) + // EXPECT_EQ(static_cast(0), func(16)); + } +}; + +#define LIST_BITSFX_TESTS(From, To, func) \ + using LlvmLibcBitsFxTest = BitsFxTest; \ + 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 Date: Sun, 23 Feb 2025 15:26:06 +0530 Subject: [PATCH 03/17] add: entrypoints for bitsfx Signed-off-by: krishna2803 --- 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 Date: Sun, 23 Feb 2025 15:27:00 +0530 Subject: [PATCH 04/17] chore: change From and To to T and XType respectively Signed-off-by: krishna2803 --- 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::StorageType; BitType x_bit = cpp::bit_cast(x); BitType y_bit = cpp::bit_cast(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(x_bit & y_bit); } @@ -131,7 +131,7 @@ bit_or(T x, T y) { using BitType = typename FXRep::StorageType; BitType x_bit = cpp::bit_cast(x); BitType y_bit = cpp::bit_cast(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(x_bit | y_bit); } @@ -140,7 +140,7 @@ LIBC_INLINE constexpr cpp::enable_if_t, T> bit_not(T x) { using BitType = typename FXRep::StorageType; BitType x_bit = cpp::bit_cast(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(static_cast(~x_bit)); } @@ -195,10 +195,10 @@ countls(T f) { } // fixed-point to integer conversion -template -LIBC_INLINE constexpr cpp::enable_if_t, To> -bitsfx(From f) { - return cpp::bit_cast(f); +template +LIBC_INLINE constexpr cpp::enable_if_t, XType> +bitsfx(T f) { + return cpp::bit_cast(f); } } // namespace fixed_point From 178534206f8afdc55c9e29265a3047331a3dd0ed Mon Sep 17 00:00:00 2001 From: krishna2803 Date: Sun, 23 Feb 2025 15:27:53 +0530 Subject: [PATCH 05/17] add: bitsu{fx} Signed-off-by: krishna2803 --- 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(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(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(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(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(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(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(f); +} + +} // namespace LIBC_NAMESPACE_DECL From 752ea6dd78b617de764ad74100b992c56341fb63 Mon Sep 17 00:00:00 2001 From: krishna2803 Date: Sun, 23 Feb 2025 15:42:48 +0530 Subject: [PATCH 06/17] chore: reposition bitsfx in CMakeLists Signed-off-by: krishna2803 --- 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 Date: Sun, 23 Feb 2025 15:44:26 +0530 Subject: [PATCH 07/17] add: tests for bitsu{fx} and make the code consistent with {fx}bits Signed-off-by: krishna2803 --- 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 +template class BitsFxTest : public LIBC_NAMESPACE::testing::Test { - using FXRep = LIBC_NAMESPACE::fixed_point::FXRep; - 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; + 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(0), func(zero)); - EXPECT_EQ(static_cast(1 << (FXRep::FRACTION_LEN - 1)), func(one_half)); - EXPECT_EQ(static_cast(1 << (FXRep::FRACTION_LEN - 2)), + EXPECT_EQ(static_cast(0), func(zero)); + EXPECT_EQ(static_cast(1ULL << (FXRep::FRACTION_LEN - 1)), + func(one_half)); + EXPECT_EQ(static_cast(1ULL << (FXRep::FRACTION_LEN - 2)), func(one_fourth)); - EXPECT_EQ(static_cast(1), func(eps)); - EXPECT_EQ(static_cast(maxval), func(max)); - EXPECT_EQ(static_cast(minval), func(min)); + EXPECT_EQ(static_cast(1), func(eps)); // (0.6875)_10 = (0.1011)_2 - EXPECT_EQ(static_cast(11 << (FXRep::FRACTION_LEN - 4)), func(0.6875)); + EXPECT_EQ(static_cast(11ULL << (FXRep::FRACTION_LEN - 4)), + func(0.6875)); EXPECT_EQ( - static_cast(zero_point_forty_two >> (64 - FXRep::FRACTION_LEN)), + static_cast(zero_point_forty_two >> (64 - FXRep::FRACTION_LEN)), func(0.42)); - - // EXPECT_EQ(static_cast(0), func(5)); - - // if constexpr (static_cast(min) <= -16) - // EXPECT_EQ(static_cast(0), func(-16)); - - // if constexpr (static_cast(min) <= -10) - // EXPECT_EQ(static_cast(0), func(-10)); - - // EXPECT_EQ(static_cast(), func(max)); - - // if constexpr (static_cast(max) >= 16.25) - // EXPECT_EQ(static_cast(0), func(16.25)); - - // if constexpr (static_cast(max) > -10) - // EXPECT_EQ(static_cast(0), func(16)); } }; -#define LIST_BITSFX_TESTS(From, To, func) \ - using LlvmLibcBitsFxTest = BitsFxTest; \ - TEST_F(LlvmLibcBitsFxTest, SpecialNumbers) { testSpecialNumbers(&func); } \ +#define LIST_BITSFX_TESTS(Name, T, XType, func) \ + using LlvmLibcBits##Name##Test = BitsFxTest; \ + 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 Date: Sun, 23 Feb 2025 15:52:54 +0530 Subject: [PATCH 08/17] chore: update CMakeLists for bitsfx tests Signed-off-by: krishna2803 --- 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 Date: Sun, 23 Feb 2025 15:53:07 +0530 Subject: [PATCH 09/17] chore: update docs Signed-off-by: krishna2803 --- 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 Date: Sun, 23 Feb 2025 16:30:18 +0530 Subject: [PATCH 10/17] add: tests for bitsulk Signed-off-by: krishna2803 --- 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 Date: Sun, 23 Feb 2025 17:42:39 +0530 Subject: [PATCH 11/17] chore: update docs Signed-off-by: krishna2803 --- 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 | | | | | | | | | | | | | +---------------+----------------+-------------+---------------+------------+----------------+-------------+----------------+-------------+---------------+------------+----------------+-------------+ From 6bd83934dad9a71a7f51ab832523b9f634e75d6c Mon Sep 17 00:00:00 2001 From: krishna2803 Date: Tue, 25 Feb 2025 13:46:59 +0530 Subject: [PATCH 12/17] fix: unintentional rename of `from` to `T` Signed-off-by: krishna2803 --- libc/src/__support/fixed_point/fx_bits.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libc/src/__support/fixed_point/fx_bits.h b/libc/src/__support/fixed_point/fx_bits.h index a9b7fe248f6f1..b05f46bd34660 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::StorageType; BitType x_bit = cpp::bit_cast(x); BitType y_bit = cpp::bit_cast(y); - // For some reason, bit_cast cannot deduce BitType T the input. + // For some reason, bit_cast cannot deduce BitType from the input. return cpp::bit_cast(x_bit & y_bit); } @@ -131,7 +131,7 @@ bit_or(T x, T y) { using BitType = typename FXRep::StorageType; BitType x_bit = cpp::bit_cast(x); BitType y_bit = cpp::bit_cast(y); - // For some reason, bit_cast cannot deduce BitType T the input. + // For some reason, bit_cast cannot deduce BitType from the input. return cpp::bit_cast(x_bit | y_bit); } @@ -140,7 +140,7 @@ LIBC_INLINE constexpr cpp::enable_if_t, T> bit_not(T x) { using BitType = typename FXRep::StorageType; BitType x_bit = cpp::bit_cast(x); - // For some reason, bit_cast cannot deduce BitType T the input. + // For some reason, bit_cast cannot deduce BitType from the input. return cpp::bit_cast(static_cast(~x_bit)); } From 02d120e78b8d505c449ed8606e73834411689afb Mon Sep 17 00:00:00 2001 From: krishna2803 Date: Thu, 27 Feb 2025 01:14:31 +0530 Subject: [PATCH 13/17] feat: add tests for `signed char` Signed-off-by: krishna2803 --- libc/test/UnitTest/LibcTest.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/libc/test/UnitTest/LibcTest.cpp b/libc/test/UnitTest/LibcTest.cpp index afb1368f00905..4395c7cf6e201 100644 --- a/libc/test/UnitTest/LibcTest.cpp +++ b/libc/test/UnitTest/LibcTest.cpp @@ -223,6 +223,7 @@ TEST_SPECIALIZATION(int); TEST_SPECIALIZATION(long); TEST_SPECIALIZATION(long long); +TEST_SPECIALIZATION(signed char); TEST_SPECIALIZATION(unsigned char); TEST_SPECIALIZATION(unsigned short); TEST_SPECIALIZATION(unsigned int); From 66800a7c99d74162ad69de3c269d0ea88a37a97b Mon Sep 17 00:00:00 2001 From: krishna2803 Date: Thu, 27 Feb 2025 01:56:18 +0530 Subject: [PATCH 14/17] feat: add tests for `short fract` Signed-off-by: krishna2803 --- libc/test/src/stdfix/CMakeLists.txt | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/libc/test/src/stdfix/CMakeLists.txt b/libc/test/src/stdfix/CMakeLists.txt index 5426c059191f7..a4611c15d78f3 100644 --- a/libc/test/src/stdfix/CMakeLists.txt +++ b/libc/test/src/stdfix/CMakeLists.txt @@ -75,31 +75,30 @@ foreach(suffix IN ITEMS hr r lr hk k lk uhr ur ulr uhk uk ulk) ) add_libc_test( - countls${suffix}_test + bits${suffix}_test SUITE libc-stdfix-tests HDRS - CountlsTest.h + BitsFxTest.h SRCS - countls${suffix}_test.cpp + bits${suffix}_test.cpp DEPENDS - libc.src.stdfix.countls${suffix} + libc.src.stdfix.bits${suffix} libc.src.__support.fixed_point.fx_rep libc.src.__support.fixed_point.fx_bits + libc.include.llvm-libc-macros.stdfix_macros ) -endforeach() -foreach(suffix IN ITEMS r lr hk k lk uhr ur ulr uhk uk ulk) add_libc_test( - bits${suffix}_test + countls${suffix}_test SUITE libc-stdfix-tests HDRS - BitsFxTest.h + CountlsTest.h SRCS - bits${suffix}_test.cpp + countls${suffix}_test.cpp DEPENDS - libc.src.stdfix.bits${suffix} + libc.src.stdfix.countls${suffix} libc.src.__support.fixed_point.fx_rep libc.src.__support.fixed_point.fx_bits ) From baf2511c5d2c50a3c6bedfd0c66b8d4d58b07132 Mon Sep 17 00:00:00 2001 From: krishna2803 Date: Thu, 27 Feb 2025 01:56:49 +0530 Subject: [PATCH 15/17] feat: add more tests Signed-off-by: krishna2803 --- libc/test/src/stdfix/BitsFxTest.h | 56 ++++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 9 deletions(-) diff --git a/libc/test/src/stdfix/BitsFxTest.h b/libc/test/src/stdfix/BitsFxTest.h index 6dd81c1e2aa97..b622337137e1f 100644 --- a/libc/test/src/stdfix/BitsFxTest.h +++ b/libc/test/src/stdfix/BitsFxTest.h @@ -21,11 +21,14 @@ class BitsFxTest : public LIBC_NAMESPACE::testing::Test { 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 T zero_point_six_eight_seven_five_t = 0.6875; + + static constexpr T negative_zero_point_six_eight_seven_five_t = -0.6875; + + // an arbitrarily chosen special number + static constexpr T special_num_t = 10.71875; + + static constexpr T negative_special_num_t = -10.71875; public: typedef XType (*BitsFxFunc)(T); @@ -40,11 +43,46 @@ class BitsFxTest : public LIBC_NAMESPACE::testing::Test { // (0.6875)_10 = (0.1011)_2 EXPECT_EQ(static_cast(11ULL << (FXRep::FRACTION_LEN - 4)), - func(0.6875)); + func(zero_point_six_eight_seven_five_t)); + + if constexpr (FXRep::SIGN_LEN > 0) + EXPECT_EQ(static_cast(-(11ULL << (FXRep::FRACTION_LEN - 4))), + func(negative_zero_point_six_eight_seven_five_t)); + + if constexpr (FXRep::INTEGRAL_LEN > 0) { + if (static_cast(max) >= 11) + switch (FXRep::FRACTION_LEN) { + case 7: + EXPECT_EQ(static_cast(1372), func(special_num_t)); + break; + case 15: + EXPECT_EQ(static_cast(351232), func(special_num_t)); + break; + case 23: + EXPECT_EQ(static_cast(89915392), func(special_num_t)); + break; + default: + break; + } + } - EXPECT_EQ( - static_cast(zero_point_forty_two >> (64 - FXRep::FRACTION_LEN)), - func(0.42)); + if constexpr (FXRep::SIGN_LEN > 0 && FXRep::INTEGRAL_LEN > 0) { + if (static_cast(min) <= -11) + switch (FXRep::FRACTION_LEN) { + case 7: + EXPECT_EQ(static_cast(-1372), func(negative_special_num_t)); + break; + case 15: + EXPECT_EQ(static_cast(-351232), func(negative_special_num_t)); + break; + case 23: + EXPECT_EQ(static_cast(-89915392), + func(negative_special_num_t)); + break; + default: + break; + } + } } }; From 9d0a7d211a494a89f38146fb0b2876dbdc16db6a Mon Sep 17 00:00:00 2001 From: krishna2803 Date: Thu, 27 Feb 2025 02:01:24 +0530 Subject: [PATCH 16/17] chore: remove unwanted header Signed-off-by: krishna2803 --- libc/test/src/stdfix/CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/libc/test/src/stdfix/CMakeLists.txt b/libc/test/src/stdfix/CMakeLists.txt index a4611c15d78f3..8dc0eb854e65c 100644 --- a/libc/test/src/stdfix/CMakeLists.txt +++ b/libc/test/src/stdfix/CMakeLists.txt @@ -86,7 +86,6 @@ foreach(suffix IN ITEMS hr r lr hk k lk uhr ur ulr uhk uk ulk) libc.src.stdfix.bits${suffix} libc.src.__support.fixed_point.fx_rep libc.src.__support.fixed_point.fx_bits - libc.include.llvm-libc-macros.stdfix_macros ) add_libc_test( From 2f9b642f71f63973e0c3ae65fbdd7bd19dd0533b Mon Sep 17 00:00:00 2001 From: krishna2803 Date: Thu, 27 Feb 2025 04:48:26 +0530 Subject: [PATCH 17/17] refactor: remove switch in lieu of bit shifts for accum tests Signed-off-by: krishna2803 --- libc/test/src/stdfix/BitsFxTest.h | 45 +++++++++++-------------------- 1 file changed, 16 insertions(+), 29 deletions(-) diff --git a/libc/test/src/stdfix/BitsFxTest.h b/libc/test/src/stdfix/BitsFxTest.h index b622337137e1f..eca6ab1bb28ab 100644 --- a/libc/test/src/stdfix/BitsFxTest.h +++ b/libc/test/src/stdfix/BitsFxTest.h @@ -50,38 +50,25 @@ class BitsFxTest : public LIBC_NAMESPACE::testing::Test { func(negative_zero_point_six_eight_seven_five_t)); if constexpr (FXRep::INTEGRAL_LEN > 0) { - if (static_cast(max) >= 11) - switch (FXRep::FRACTION_LEN) { - case 7: - EXPECT_EQ(static_cast(1372), func(special_num_t)); - break; - case 15: - EXPECT_EQ(static_cast(351232), func(special_num_t)); - break; - case 23: - EXPECT_EQ(static_cast(89915392), func(special_num_t)); - break; - default: - break; - } - } + constexpr size_t kMinFbits = 7; + + if (max >= 11 && FXRep::FRACTION_LEN >= kMinFbits) { + // (10.71875)_10 = (1010.1011100)_2 + constexpr long long kExpected = 1372; + EXPECT_EQ( + static_cast(kExpected << (FXRep::FRACTION_LEN - kMinFbits)), + func(special_num_t)); + } - if constexpr (FXRep::SIGN_LEN > 0 && FXRep::INTEGRAL_LEN > 0) { - if (static_cast(min) <= -11) - switch (FXRep::FRACTION_LEN) { - case 7: - EXPECT_EQ(static_cast(-1372), func(negative_special_num_t)); - break; - case 15: - EXPECT_EQ(static_cast(-351232), func(negative_special_num_t)); - break; - case 23: - EXPECT_EQ(static_cast(-89915392), + if constexpr (FXRep::SIGN_LEN > 0) { + if (min <= -11 && FXRep::FRACTION_LEN >= kMinFbits) { + // (-10.71875)_10 = (-1010.1011100)_2 + constexpr long long kExpected = -1372; + EXPECT_EQ(static_cast(kExpected + << (FXRep::FRACTION_LEN - kMinFbits)), func(negative_special_num_t)); - break; - default: - break; } + } } } };