From e6c09ae4b2acd421a29706f86e66eaa422262ad0 Mon Sep 17 00:00:00 2001 From: Derek Mauro Date: Mon, 26 Jun 2023 12:30:38 -0700 Subject: Fix the check for #include Previously this was guarded with macros that are defined by itself. Note that libc++ also had a bug that was fixed last week https://github.com/llvm/llvm-project/commit/a4f0764aefd6ea41e83a5974f582a1a7e985667d PiperOrigin-RevId: 543511181 Change-Id: I1b8efa32f721ad450f8d1803c6c6c8373ad0371c --- absl/numeric/bits.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'absl/numeric/bits.h') diff --git a/absl/numeric/bits.h b/absl/numeric/bits.h index df81b9a9..5466af07 100644 --- a/absl/numeric/bits.h +++ b/absl/numeric/bits.h @@ -38,13 +38,13 @@ #include #include -#if (defined(__cpp_lib_int_pow2) && __cpp_lib_int_pow2 >= 202002L) || \ - (defined(__cpp_lib_bitops) && __cpp_lib_bitops >= 201907L) +#include "absl/base/config.h" + +#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L #include #endif #include "absl/base/attributes.h" -#include "absl/base/config.h" #include "absl/numeric/internal/bits.h" namespace absl { -- cgit v1.2.3 From bba65bd11506292aad0258ab516fe9eabf507e18 Mon Sep 17 00:00:00 2001 From: Eric Fiselier Date: Wed, 28 Jun 2023 11:09:29 -0700 Subject: Refactor bit tests to allow for the testing of more types PiperOrigin-RevId: 544107572 Change-Id: I8016ee690ad5df78bf80ba0786e528fba4e51907 --- absl/numeric/bits.h | 2 +- absl/numeric/bits_test.cc | 68 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) (limited to 'absl/numeric/bits.h') diff --git a/absl/numeric/bits.h b/absl/numeric/bits.h index 5466af07..5ed36f52 100644 --- a/absl/numeric/bits.h +++ b/absl/numeric/bits.h @@ -49,8 +49,8 @@ namespace absl { ABSL_NAMESPACE_BEGIN - #if !(defined(__cpp_lib_bitops) && __cpp_lib_bitops >= 201907L) + // rotating template ABSL_MUST_USE_RESULT constexpr diff --git a/absl/numeric/bits_test.cc b/absl/numeric/bits_test.cc index 7c942aae..14955eb3 100644 --- a/absl/numeric/bits_test.cc +++ b/absl/numeric/bits_test.cc @@ -15,6 +15,7 @@ #include "absl/numeric/bits.h" #include +#include #include "gmock/gmock.h" #include "gtest/gtest.h" @@ -24,6 +25,73 @@ namespace absl { ABSL_NAMESPACE_BEGIN namespace { +template +class IntegerTypesTest : public ::testing::Test {}; + +using OneByteIntegerTypes = ::testing::Types< + unsigned char, + uint8_t + >; + +TYPED_TEST_SUITE(IntegerTypesTest, OneByteIntegerTypes); + +TYPED_TEST(IntegerTypesTest, HandlesTypes) { + using UIntType = TypeParam; + + EXPECT_EQ(rotl(UIntType{0x12}, 0), uint8_t{0x12}); + EXPECT_EQ(rotr(UIntType{0x12}, -4), uint8_t{0x21}); + static_assert(rotl(UIntType{0x12}, 0) == uint8_t{0x12}, ""); + + static_assert(rotr(UIntType{0x12}, 0) == uint8_t{0x12}, ""); + EXPECT_EQ(rotr(UIntType{0x12}, 0), uint8_t{0x12}); + +#if ABSL_INTERNAL_HAS_CONSTEXPR_CLZ + static_assert(countl_zero(UIntType{}) == 8, ""); + static_assert(countl_zero(static_cast(-1)) == 0, ""); + + static_assert(countl_one(UIntType{}) == 0, ""); + static_assert(countl_one(static_cast(-1)) == 8, ""); + + static_assert(countr_zero(UIntType{}) == 8, ""); + static_assert(countr_zero(static_cast(-1)) == 0, ""); + + static_assert(countr_one(UIntType{}) == 0, ""); + static_assert(countr_one(static_cast(-1)) == 8, ""); + + static_assert(popcount(UIntType{}) == 0, ""); + static_assert(popcount(UIntType{1}) == 1, ""); + static_assert(popcount(static_cast(-1)) == 8, ""); + + static_assert(bit_width(UIntType{}) == 0, ""); + static_assert(bit_width(UIntType{1}) == 1, ""); + static_assert(bit_width(UIntType{3}) == 2, ""); + static_assert(bit_width(static_cast(-1)) == 8, ""); +#endif + + EXPECT_EQ(countl_zero(UIntType{}), 8); + EXPECT_EQ(countl_zero(static_cast(-1)), 0); + + EXPECT_EQ(countl_one(UIntType{}), 0); + EXPECT_EQ(countl_one(static_cast(-1)), 8); + + EXPECT_EQ(countr_zero(UIntType{}), 8); + EXPECT_EQ(countr_zero(static_cast(-1)), 0); + + EXPECT_EQ(countr_one(UIntType{}), 0); + EXPECT_EQ(countr_one(static_cast(-1)), 8); + + EXPECT_EQ(popcount(UIntType{}), 0); + EXPECT_EQ(popcount(UIntType{1}), 1); + + EXPECT_FALSE(has_single_bit(UIntType{})); + EXPECT_FALSE(has_single_bit(static_cast(-1))); + + EXPECT_EQ(bit_width(UIntType{}), 0); + EXPECT_EQ(bit_width(UIntType{1}), 1); + EXPECT_EQ(bit_width(UIntType{3}), 2); + EXPECT_EQ(bit_width(static_cast(-1)), 8); +} + TEST(Rotate, Left) { static_assert(rotl(uint8_t{0x12}, 0) == uint8_t{0x12}, ""); static_assert(rotl(uint16_t{0x1234}, 0) == uint16_t{0x1234}, ""); -- cgit v1.2.3