From 1e67c3bff81eeaecdd3e2e243bcbf5c3950c4a89 Mon Sep 17 00:00:00 2001 From: Tulio Leao Date: Sat, 30 Nov 2019 08:41:45 -0300 Subject: [PATCH 1/3] Make floor2 and ceil2 constexpr functions --- src/openrct2/common.h | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/openrct2/common.h b/src/openrct2/common.h index 1af006ac29..05a112f365 100644 --- a/src/openrct2/common.h +++ b/src/openrct2/common.h @@ -50,11 +50,24 @@ const constexpr auto ror32 = ror; const constexpr auto rol64 = rol; const constexpr auto ror64 = ror; +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) +{ + assert(is_power_of_2(y)); + 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) +{ + assert(is_power_of_2(y)); + return (((x) + (y)-1) & (~((y)-1))); +} // Gets the name of a symbol as a C string #define nameof(symbol) #symbol From c4089acd63532daffe41e89c08fabb9af1d3df7e Mon Sep 17 00:00:00 2001 From: Tulio Leao Date: Sun, 1 Dec 2019 22:45:16 -0300 Subject: [PATCH 2/3] Removed parentheses for improved readability --- src/openrct2/common.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/openrct2/common.h b/src/openrct2/common.h index 05a112f365..8ad6ac6a0d 100644 --- a/src/openrct2/common.h +++ b/src/openrct2/common.h @@ -59,14 +59,14 @@ constexpr bool is_power_of_2(int v) constexpr int floor2(const int x, const int y) { assert(is_power_of_2(y)); - return ((x) & (~((y)-1))); + 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) { assert(is_power_of_2(y)); - return (((x) + (y)-1) & (~((y)-1))); + return (x + y - 1) & ~(y - 1); } // Gets the name of a symbol as a C string From aa02a8c7cef1655fdea5dd1c01ba1ec12b1f7725 Mon Sep 17 00:00:00 2001 From: Tulio Leao Date: Tue, 3 Dec 2019 23:13:12 -0300 Subject: [PATCH 3/3] Throw exception if floor2 and ceil2 are misused --- src/openrct2/common.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/openrct2/common.h b/src/openrct2/common.h index 8ad6ac6a0d..71e0267300 100644 --- a/src/openrct2/common.h +++ b/src/openrct2/common.h @@ -25,6 +25,7 @@ #include #include #include +#include using namespace Numerics; @@ -58,14 +59,16 @@ constexpr bool is_power_of_2(int v) // 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) { - assert(is_power_of_2(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) { - assert(is_power_of_2(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); }