Commit 5cdac587 authored by Eric Niebler's avatar Eric Niebler Committed by Facebook GitHub Bot

Use static_cast instead of function-style cast in ConstexprMath.h

Summary:
Some llvm toolsets mis-parse the function-like C-style casts in ConstexprMath.h as function declarations, resulting in strange compiler errors. Prefer `static_cast` to eliminate the ambiguity.

Note: We cannot use brace-initialization syntax, because that would error on narrowing conversions.

Reviewed By: ispeters, Mizuchi

Differential Revision: D24463342

fbshipit-source-id: 5d5aabb08cd20e91a1a5f5460cc4505cafb3cdfc
parent 41a9dc8f
......@@ -184,7 +184,10 @@ constexpr T constexpr_add_overflow_clamped(T a, T b) {
!std::is_integral<T>::value ? a + b :
// for narrow integral types, just convert to intmax_t.
sizeof(T) < sizeof(M)
? T(constexpr_clamp(M(a) + M(b), M(L::min()), M(L::max()))) :
? T(constexpr_clamp(
static_cast<M>(a) + static_cast<M>(b),
static_cast<M>(L::min()),
static_cast<M>(L::max()))) :
// when a >= 0, cannot add more than `MAX - a` onto a.
!(a < 0) ? a + constexpr_min(b, T(L::max() - a)) :
// a < 0 && b >= 0, `a + b` will always be in valid range of type T.
......@@ -209,7 +212,10 @@ constexpr T constexpr_sub_overflow_clamped(T a, T b) {
std::is_unsigned<T>::value ? (a < b ? 0 : a - b) :
// for narrow signed integral types, just convert to intmax_t.
sizeof(T) < sizeof(M)
? T(constexpr_clamp(M(a) - M(b), M(L::min()), M(L::max()))) :
? T(constexpr_clamp(
static_cast<M>(a) - static_cast<M>(b),
static_cast<M>(L::min()),
static_cast<M>(L::max()))) :
// (a >= 0 && b >= 0) || (a < 0 && b < 0), `a - b` will always be valid.
(a < 0) == (b < 0) ? a - b :
// MIN < b, so `-b` should be in valid range (-MAX <= -b <= MAX),
......
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