Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MSVC Warning when compiling format.cpp #2078

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions folly/ConstexprMath.h
Original file line number Diff line number Diff line change
Expand Up @@ -358,18 +358,19 @@ constexpr T constexpr_log2_ceil(T t) {
/// constexpr_trunc
///
/// mimic: std::trunc (C++23)
template <typename T>
template <
typename T,
std::enable_if_t<std::is_floating_point<T>::value, int> = 0>
constexpr T constexpr_trunc(T const t) {
using lim = std::numeric_limits<T>;
using int_type = std::uintmax_t;
using int_lim = std::numeric_limits<int_type>;
static_assert(lim::radix == 2, "non-binary radix");
static_assert(lim::digits <= int_lim::digits, "overwide mantissa");
constexpr auto bound = static_cast<T>(std::uintmax_t(1) << (lim::digits - 1));
constexpr auto is_fp = std::is_floating_point<T>::value;
auto const neg = !constexpr_isnan(t) && t < T(0);
auto const s = neg ? -t : t;
if (!is_fp || constexpr_isnan(t) || t == T(0) || !(s < bound)) {
if (constexpr_isnan(t) || t == T(0) || !(s < bound)) {
return t;
}
if (s < T(1)) {
Expand All @@ -379,6 +380,11 @@ constexpr T constexpr_trunc(T const t) {
return neg ? -r : r;
}

template <typename T, std::enable_if_t<std::is_integral<T>::value, int> = 0>
constexpr T constexpr_trunc(T const t) {
return t;
}

/// constexpr_round
///
/// mimic: std::round (C++23)
Expand Down
18 changes: 18 additions & 0 deletions folly/test/ConstexprMathTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,24 @@ TEST_F(ConstexprMathTest, constexpr_trunc_floating) {
}
}

TEST_F(ConstexprMathTest, constexpr_trunc_integral) {
{
constexpr auto n = 0u;
constexpr auto a = folly::constexpr_trunc(n);
EXPECT_EQ(0u, a);
}
{
constexpr auto n = -1;
constexpr auto a = folly::constexpr_trunc(n);
EXPECT_EQ(-1, a);
}
{
constexpr auto n = 100;
constexpr auto a = folly::constexpr_trunc(n);
EXPECT_EQ(100, a);
}
}

TEST_F(ConstexprMathTest, constexpr_round_floating) {
using lim = std::numeric_limits<double>;

Expand Down
Loading