Place constexpr functions in anonymous namespace (#10651)

This commit is contained in:
Michał Janiszewski 2020-02-06 08:50:10 +01:00 committed by GitHub
parent 06174adf14
commit 2340206b34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 17 deletions

View File

@ -51,26 +51,29 @@ const constexpr auto ror32 = ror<uint32_t>;
const constexpr auto rol64 = rol<uint64_t>;
const constexpr auto ror64 = ror<uint64_t>;
constexpr bool is_power_of_2(int v)
namespace
{
return v && ((v & (v - 1)) == 0);
}
[[maybe_unused]] constexpr bool is_power_of_2(int v)
{
return v && ((v & (v - 1)) == 0);
}
// Rounds an integer down to the given power of 2. y must be a power of 2.
constexpr int floor2(const int x, const int y)
{
if (!is_power_of_2(y))
throw std::logic_error("floor2 should only operate on power of 2");
return x & ~(y - 1);
}
// Rounds an integer down to the given power of 2. y must be a power of 2.
[[maybe_unused]] constexpr int floor2(const int x, const int y)
{
if (!is_power_of_2(y))
throw std::logic_error("floor2 should only operate on power of 2");
return x & ~(y - 1);
}
// Rounds an integer up to the given power of 2. y must be a power of 2.
constexpr int ceil2(const int x, const int y)
{
if (!is_power_of_2(y))
throw std::logic_error("ceil2 should only operate on power of 2");
return (x + y - 1) & ~(y - 1);
}
// Rounds an integer up to the given power of 2. y must be a power of 2.
[[maybe_unused]] constexpr int ceil2(const int x, const int y)
{
if (!is_power_of_2(y))
throw std::logic_error("ceil2 should only operate on power of 2");
return (x + y - 1) & ~(y - 1);
}
} // namespace
// Gets the name of a symbol as a C string
#define nameof(symbol) #symbol