mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2026-01-15 11:03:00 +01:00
Merge pull request #10309 from tupaschoal/floor-ceil-constexpr
Make floor2 and ceil2 constexpr functions
This commit is contained in:
@@ -25,6 +25,7 @@
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <stdexcept>
|
||||
|
||||
using namespace Numerics;
|
||||
|
||||
@@ -50,11 +51,26 @@ 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)
|
||||
{
|
||||
return v && ((v & (v - 1)) == 0);
|
||||
}
|
||||
|
||||
// Rounds an integer down to the given power of 2. y must be a power of 2.
|
||||
#define floor2(x, y) ((x) & (~((y)-1)))
|
||||
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.
|
||||
#define ceil2(x, y) (((x) + (y)-1) & (~((y)-1)))
|
||||
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);
|
||||
}
|
||||
|
||||
// Gets the name of a symbol as a C string
|
||||
#define nameof(symbol) #symbol
|
||||
|
||||
Reference in New Issue
Block a user