diff --git a/include/core/SkTypes.h b/include/core/SkTypes.h index 6793e4c0fb..13b662abf6 100644 --- a/include/core/SkTypes.h +++ b/include/core/SkTypes.h @@ -369,7 +369,7 @@ typedef uint32_t SkMSec; /** Faster than SkToBool for integral conditions. Returns 0 or 1 */ -static inline int Sk32ToBool(uint32_t n) { +static constexpr int Sk32ToBool(uint32_t n) { return (n | (0-n)) >> 31; } @@ -408,11 +408,11 @@ static inline int32_t SkMin32(int32_t a, int32_t b) { return a; } -template const T& SkTMin(const T& a, const T& b) { +template constexpr const T& SkTMin(const T& a, const T& b) { return (a < b) ? a : b; } -template const T& SkTMax(const T& a, const T& b) { +template constexpr const T& SkTMax(const T& a, const T& b) { return (b < a) ? a : b; } @@ -428,7 +428,7 @@ static inline int32_t SkFastMin32(int32_t value, int32_t max) { } /** Returns value pinned between min and max, inclusively. */ -template static inline const T& SkTPin(const T& value, const T& min, const T& max) { +template static constexpr const T& SkTPin(const T& value, const T& min, const T& max) { return SkTMax(SkTMin(value, max), min); } diff --git a/tests/CPlusPlusEleven.cpp b/tests/CPlusPlusEleven.cpp index 5fbd46429d..b5a34b2264 100644 --- a/tests/CPlusPlusEleven.cpp +++ b/tests/CPlusPlusEleven.cpp @@ -28,3 +28,10 @@ DEF_TEST(CPlusPlusEleven_RvalueAndMove, r) { Moveable src1; Moveable dst1(std::move(src1)); Moveable src2, dst2; dst2 = std::move(src2); } + +DEF_TEST(CPlusPlusEleven_constexpr, r) { + static constexpr int x = Sk32ToBool(50); + REPORTER_ASSERT(r, x == 1); + static constexpr int y = SkTPin(100, 0, 10); + REPORTER_ASSERT(r, y == 10); +}