Skip to content

Commit

Permalink
Merge pull request #1290 from LLNL/feature/kweiss/rename-bit-operations
Browse files Browse the repository at this point in the history
Renames some bit/byte operations to match newer C++ standards
  • Loading branch information
kennyweiss committed Mar 15, 2024
2 parents 6abb2de + a657a50 commit 4864dfc
Show file tree
Hide file tree
Showing 14 changed files with 155 additions and 117 deletions.
3 changes: 3 additions & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ The Axom project release numbers follow [Semantic Versioning](http://semver.org/
- Primal: `intersection_volume()` operators changed from returning a signed
volume to an unsigned volume.
- Primal's `BoundingBox::contains(BoundingBox)` now returns `true` when the input is empty
- Renamed axom's bit utility functions to conform to `C++20` standard: `popCount() -> popcount()`,
`trailingZeros() -> countr_zero()` and `leadingZeros() -> countl_zero()`
- Renamed `axom::utilities::swapEndian() -> byteswap()` to conform to `C++23` standard

### Fixed
- quest's `SamplingShaper` now properly handles material names containing underscores
Expand Down
4 changes: 2 additions & 2 deletions src/axom/core/FlatMap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -669,11 +669,11 @@ FlatMap<KeyType, ValueType, Hash>::FlatMap(IndexType bucket_count)
bucket_count = axom::utilities::max(minBuckets, bucket_count);
// Get the smallest power-of-two number of groups satisfying:
// N * GroupSize - 1 >= minBuckets
// TODO: we should add a leadingZeros overload for 64-bit integers
// TODO: we should add a countl_zero overload for 64-bit integers
{
std::int32_t numGroups =
std::ceil((bucket_count + 1) / (double)BucketsPerGroup);
m_numGroups2 = 31 - (axom::utilities::leadingZeros(numGroups));
m_numGroups2 = 31 - (axom::utilities::countl_zero(numGroups));
}

IndexType numGroupsRounded = 1 << m_numGroups2;
Expand Down
1 change: 1 addition & 0 deletions src/axom/core/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ set(core_serial_tests
set(core_test_depends
core
gtest
fmt
)

#------------------------------------------------------------------------------
Expand Down
46 changes: 23 additions & 23 deletions src/axom/core/tests/core_bit_utilities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,23 @@ TEST(core_bit_utilities, trailingZeroes)
constexpr int BITS = axom::utilities::BitTraits<std::uint64_t>::BITS_PER_WORD;
ASSERT_EQ(64, BITS);

// Axom's trailingZeros will return 64 when given 0
// Axom's countr_zero will return 64 when given 0
{
EXPECT_EQ(BITS, axom::utilities::trailingZeros(ZERO));
EXPECT_EQ(BITS, axom::utilities::countr_zero(ZERO));
}

// Test with a known trailing bit
for(int i = 0; i < BITS; ++i)
{
std::uint64_t val = ::shifted(i);
EXPECT_EQ(i, axom::utilities::trailingZeros(val));
EXPECT_EQ(i, axom::utilities::countr_zero(val));

// Value doesn't change when you set bits to left of trailing bit
for(int j = i + 1; j < BITS; ++j)
{
std::uint64_t val2 = ::shifted(i) + ::shifted(j);
EXPECT_EQ(axom::utilities::trailingZeros(val),
axom::utilities::trailingZeros(val2));
EXPECT_EQ(axom::utilities::countr_zero(val),
axom::utilities::countr_zero(val2));
}
}

Expand All @@ -73,28 +73,28 @@ TEST(core_bit_utilities, trailingZeroes)
break;
}
}
EXPECT_EQ(bit, axom::utilities::trailingZeros(rand_val));
EXPECT_EQ(bit, axom::utilities::countr_zero(rand_val));
}
}

TEST(core_bit_utilities, popCount)
TEST(core_bit_utilities, popcount)
{
constexpr std::uint64_t ZERO = std::uint64_t(0);
constexpr int BITS = axom::utilities::BitTraits<std::uint64_t>::BITS_PER_WORD;
ASSERT_EQ(64, BITS);

// Test pop count when zero bits are set
{
EXPECT_EQ(0, axom::utilities::popCount(ZERO));
EXPECT_EQ(BITS, axom::utilities::popCount(~ZERO));
EXPECT_EQ(0, axom::utilities::popcount(ZERO));
EXPECT_EQ(BITS, axom::utilities::popcount(~ZERO));
}

// Test pop count when one bit is set
for(int i = 0; i < BITS; ++i)
{
std::uint64_t val = ::shifted(i);
EXPECT_EQ(1, axom::utilities::popCount(val));
EXPECT_EQ(BITS - 1, axom::utilities::popCount(~val));
EXPECT_EQ(1, axom::utilities::popcount(val));
EXPECT_EQ(BITS - 1, axom::utilities::popcount(~val));
}

// Test pop count when two bits are set
Expand All @@ -103,8 +103,8 @@ TEST(core_bit_utilities, popCount)
for(int j = 0; j < i; ++j)
{
std::uint64_t val = shifted(i) + shifted(j);
EXPECT_EQ(2, axom::utilities::popCount(val));
EXPECT_EQ(BITS - 2, axom::utilities::popCount(~val));
EXPECT_EQ(2, axom::utilities::popcount(val));
EXPECT_EQ(BITS - 2, axom::utilities::popcount(~val));
}
}

Expand All @@ -116,8 +116,8 @@ TEST(core_bit_utilities, popCount)
for(int k = 0; k < j; ++k)
{
std::uint64_t val = shifted(i) + shifted(j) + shifted(k);
EXPECT_EQ(3, axom::utilities::popCount(val));
EXPECT_EQ(BITS - 3, axom::utilities::popCount(~val));
EXPECT_EQ(3, axom::utilities::popcount(val));
EXPECT_EQ(BITS - 3, axom::utilities::popcount(~val));
}
}
}
Expand All @@ -136,30 +136,30 @@ TEST(core_bit_utilities, popCount)
}
}

EXPECT_EQ(bits, axom::utilities::popCount(val));
EXPECT_EQ(bits, axom::utilities::popcount(val));
}
}

TEST(core_bit_utilities, leadingZeros)
TEST(core_bit_utilities, countl_zero)
{
constexpr std::int32_t ZERO = std::int32_t(0);
constexpr int BITS = axom::utilities::BitTraits<std::uint32_t>::BITS_PER_WORD;
ASSERT_EQ(32, BITS);

// Axom's leadingZeros will return 32 when given 0
EXPECT_EQ(BITS, axom::utilities::leadingZeros(ZERO));
// Axom's countl_zero will return 32 when given 0
EXPECT_EQ(BITS, axom::utilities::countl_zero(ZERO));

for(int i = 0; i < BITS; ++i)
{
std::int32_t val = ::shifted(i);
EXPECT_EQ(BITS - i - 1, axom::utilities::leadingZeros(val));
EXPECT_EQ(BITS - i - 1, axom::utilities::countl_zero(val));

// Value doesn't change if you set bits to right of leading zero
for(int j = 0; j < i; ++j)
{
std::int32_t val2 = ::shifted(i) + ::shifted(j);
EXPECT_EQ(axom::utilities::leadingZeros(val),
axom::utilities::leadingZeros(val2));
EXPECT_EQ(axom::utilities::countl_zero(val),
axom::utilities::countl_zero(val2));
}
}

Expand All @@ -177,6 +177,6 @@ TEST(core_bit_utilities, leadingZeros)
break;
}
}
EXPECT_EQ(bit, axom::utilities::leadingZeros(rand_val));
EXPECT_EQ(bit, axom::utilities::countl_zero(rand_val));
}
}

0 comments on commit 4864dfc

Please sign in to comment.