-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #108 from chfast/clz
Constexpr clz()
- Loading branch information
Showing
10 changed files
with
163 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// intx: extended precision integer library. | ||
// Copyright 2019 Pawel Bylica. | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
#include <benchmark/benchmark.h> | ||
#include <array> | ||
#include <intx/int128.hpp> | ||
|
||
|
||
template <typename T, unsigned ClzFn(T)> | ||
static void clz(benchmark::State& state) | ||
{ | ||
constexpr int input_size = 1000; | ||
std::array<uint64_t, input_size> inputs{}; | ||
for (size_t i = 0; i < inputs.size(); ++i) | ||
{ | ||
const auto s = i % 65; | ||
inputs[i] = s == 64 ? 0 : (uint64_t{1} << 63) >> s; | ||
} | ||
|
||
for (auto _ : state) | ||
{ | ||
for (auto& in : inputs) | ||
in = ClzFn(static_cast<T>(in)); | ||
} | ||
benchmark::DoNotOptimize(inputs.data()); | ||
} | ||
BENCHMARK_TEMPLATE(clz, uint32_t, intx::clz); | ||
BENCHMARK_TEMPLATE(clz, uint32_t, intx::clz_generic); | ||
BENCHMARK_TEMPLATE(clz, uint64_t, intx::clz); | ||
BENCHMARK_TEMPLATE(clz, uint64_t, intx::clz_generic); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// intx: extended precision integer library. | ||
// Copyright 2019 Pawel Bylica. | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
#include <intx/int128.hpp> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
using namespace intx; | ||
|
||
static_assert(clz_generic(uint32_t{0}) == 32, ""); | ||
static_assert(clz_generic(uint32_t{1}) == 31, ""); | ||
static_assert(clz_generic(uint32_t{3}) == 30, ""); | ||
static_assert(clz_generic(uint32_t{9}) == 28, ""); | ||
|
||
static_assert(clz_generic(uint64_t{0}) == 64, ""); | ||
static_assert(clz_generic(uint64_t{1}) == 63, ""); | ||
static_assert(clz_generic(uint64_t{3}) == 62, ""); | ||
static_assert(clz_generic(uint64_t{9}) == 60, ""); | ||
|
||
|
||
TEST(builtins, clz64_single_one) | ||
{ | ||
for (unsigned i = 0; i <= 63; ++i) | ||
{ | ||
const auto input = (uint64_t{1} << 63) >> i; | ||
EXPECT_EQ(clz(input), i); | ||
EXPECT_EQ(clz_generic(input), i); | ||
} | ||
} | ||
|
||
TEST(builtins, clz64_two_ones) | ||
{ | ||
for (unsigned i = 0; i <= 63; ++i) | ||
{ | ||
const auto input = ((uint64_t{1} << 63) >> i) | 1; | ||
EXPECT_EQ(clz(input), i); | ||
EXPECT_EQ(clz_generic(input), i); | ||
} | ||
} | ||
|
||
TEST(builtins, clz32_single_one) | ||
{ | ||
for (unsigned i = 0; i <= 31; ++i) | ||
{ | ||
const auto input = (uint32_t{1} << 31) >> i; | ||
EXPECT_EQ(clz(input), i); | ||
EXPECT_EQ(clz_generic(input), i); | ||
} | ||
} | ||
|
||
TEST(builtins, clz32_two_ones) | ||
{ | ||
for (unsigned i = 0; i <= 31; ++i) | ||
{ | ||
const auto input = ((uint32_t{1} << 31) >> i) | 1; | ||
EXPECT_EQ(clz(input), i); | ||
EXPECT_EQ(clz_generic(input), i); | ||
} | ||
} | ||
|
||
TEST(builtins, clz_zero) | ||
{ | ||
EXPECT_EQ(clz(uint32_t{0}), 32); | ||
EXPECT_EQ(clz_generic(uint32_t{0}), 32); | ||
EXPECT_EQ(clz(uint64_t{0}), 64); | ||
EXPECT_EQ(clz_generic(uint64_t{0}), 64); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters