Commit 8fba851f authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

constexpr_ceil

Summary: [Folly] `constexpr_ceil`, an integral rounding-up util.

Reviewed By: Orvid

Differential Revision: D6558042

fbshipit-source-id: 6b42add9bf2e3605baf71391130c2a2c88cc4385
parent 9b2b633c
......@@ -115,4 +115,11 @@ constexpr T constexpr_log2(T t) {
return detail::constexpr_log2(T(0), t);
}
template <typename T>
constexpr T constexpr_ceil(T t, T round) {
return round == T(0)
? t
: ((t + (t < T(0) ? T(0) : round - T(1))) / round) * round;
}
} // namespace folly
......@@ -122,3 +122,24 @@ TEST_F(ConstexprMathTest, constexpr_log2_64) {
EXPECT_EQ(6ull, a);
EXPECT_TRUE((std::is_same<decltype(v), decltype(a)>::value));
}
TEST_F(ConstexprMathTest, constexpr_ceil) {
{
constexpr auto roundable = 20ull;
constexpr auto round = 6ull;
constexpr auto rounded = folly::constexpr_ceil(roundable, round);
EXPECT_EQ(24ull, rounded);
}
{
constexpr auto roundable = -20ll;
constexpr auto round = 6ll;
constexpr auto rounded = folly::constexpr_ceil(roundable, round);
EXPECT_EQ(-18ll, rounded);
}
{
constexpr auto roundable = -20ll;
constexpr auto round = 0ll;
constexpr auto rounded = folly::constexpr_ceil(roundable, round);
EXPECT_EQ(-20ll, rounded);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment