Chromium Code Reviews| Index: base/numerics/clamped_math.h |
| diff --git a/base/numerics/clamped_math.h b/base/numerics/clamped_math.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..86614b15afe5ab325cf9646f6224b37b549b01c4 |
| --- /dev/null |
| +++ b/base/numerics/clamped_math.h |
| @@ -0,0 +1,263 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef BASE_NUMERICS_CLAMPED_MATH_H_ |
| +#define BASE_NUMERICS_CLAMPED_MATH_H_ |
| + |
| +#include <stddef.h> |
| + |
| +#include <limits> |
| +#include <type_traits> |
| + |
| +#include "base/numerics/clamped_math_impl.h" |
| + |
| +namespace base { |
| +namespace internal { |
| + |
| +template <typename T> |
| +class ClampedNumeric { |
| + static_assert(std::is_arithmetic<T>::value, |
| + "ClampedNumeric<T>: T must be a numeric type."); |
| + |
| + public: |
| + using type = T; |
| + |
| + constexpr ClampedNumeric() : value_(0) {} |
| + |
| + // Copy constructor. |
| + template <typename Src> |
| + constexpr ClampedNumeric(const ClampedNumeric<Src>& rhs) |
| + : value_(saturated_cast<T>(rhs.value_)) {} |
| + |
| + template <typename Src> |
| + friend class ClampedNumeric; |
| + |
| + // This is not an explicit constructor because we implicitly upgrade regular |
| + // numerics to ClampedNumerics to make them easier to use. |
| + template <typename Src> |
| + constexpr ClampedNumeric(Src value) // NOLINT(runtime/explicit) |
| + : value_(saturated_cast<T>(value)) { |
| + static_assert(std::is_arithmetic<Src>::value, "Argument must be numeric."); |
| + } |
| + |
| + // This is not an explicit constructor because we want a seamless conversion |
| + // from StrictNumeric types. |
| + template <typename Src> |
| + constexpr ClampedNumeric( |
| + StrictNumeric<Src> value) // NOLINT(runtime/explicit) |
| + : value_(saturated_cast<T>(static_cast<Src>(value))) {} |
| + |
| + // Returns a checked numeric of the specified type, cast from the current |
| + // ClampedNumeric. If the current state is invalid or the destination cannot |
|
dcheng
2017/06/28 07:25:06
"current state is invalid" => how does that happen
jschuh
2017/06/28 12:32:37
Oops. Copy pasta.
|
| + // represent the result then the returned ClampedNumeric will be invalid. |
| + template <typename Dst> |
| + constexpr ClampedNumeric<typename UnderlyingType<Dst>::type> Cast() const { |
| + return *this; |
| + } |
| + |
| + // Prototypes for the supported arithmetic operator overloads. |
| + template <typename Src> |
| + ClampedNumeric& operator+=(const Src rhs); |
| + template <typename Src> |
| + ClampedNumeric& operator-=(const Src rhs); |
| + template <typename Src> |
| + ClampedNumeric& operator*=(const Src rhs); |
| + template <typename Src> |
| + ClampedNumeric& operator/=(const Src rhs); |
| + template <typename Src> |
| + ClampedNumeric& operator%=(const Src rhs); |
| + template <typename Src> |
| + ClampedNumeric& operator<<=(const Src rhs); |
| + template <typename Src> |
| + ClampedNumeric& operator>>=(const Src rhs); |
| + template <typename Src> |
| + ClampedNumeric& operator&=(const Src rhs); |
| + template <typename Src> |
| + ClampedNumeric& operator|=(const Src rhs); |
| + template <typename Src> |
| + ClampedNumeric& operator^=(const Src rhs); |
| + |
| + constexpr ClampedNumeric operator-() const { |
| + return ClampedNumeric<T>( |
| + std::is_signed<T>::value |
| + ? ((std::is_floating_point<T>::value || |
| + NegateWrapper(value_) != std::numeric_limits<T>::lowest()) |
| + ? NegateWrapper(value_) |
| + : std::numeric_limits<T>::max()) |
| + : T(0)); // Clamped unsigned negation is always zero. |
| + } |
| + |
| + constexpr ClampedNumeric operator~() const { |
| + return ClampedNumeric<decltype(InvertWrapper(T()))>(InvertWrapper(value_)); |
| + } |
| + |
| + constexpr ClampedNumeric Abs() const { |
| + return ClampedNumeric<T>( |
| + (!std::is_signed<T>::value || std::is_floating_point<T>::value || |
| + AbsWrapper(value_) != std::numeric_limits<T>::lowest()) |
| + ? AbsWrapper(value_) |
| + : std::numeric_limits<T>::max()); |
| + } |
| + |
| + template <typename U> |
| + constexpr ClampedNumeric<typename MathWrapper<ClampedMaxOp, T, U>::type> Max( |
| + const U rhs) const { |
| + using result_type = typename MathWrapper<ClampedMaxOp, T, U>::type; |
| + return ClampedNumeric<result_type>( |
| + ClampedMaxOp<T, U>(value_, Wrapper<U>::value(rhs))); |
| + } |
| + |
| + template <typename U> |
| + constexpr ClampedNumeric<typename MathWrapper<ClampedMinOp, T, U>::type> Min( |
| + const U rhs) const { |
| + using result_type = typename MathWrapper<ClampedMinOp, T, U>::type; |
| + return ClampedNumeric<result_type>( |
| + ClampedMinOp<T, U>(value_, Wrapper<U>::value(rhs))); |
| + } |
| + |
| + // This function is available only for integral types. It returns an unsigned |
| + // integer of the same width as the source type, containing the absolute value |
| + // of the source, and properly handling signed min. |
| + constexpr ClampedNumeric<typename UnsignedOrFloatForSize<T>::type> |
| + UnsignedAbs() const { |
| + return ClampedNumeric<typename UnsignedOrFloatForSize<T>::type>( |
| + SafeUnsignedAbs(value_)); |
| + } |
| + |
| + ClampedNumeric& operator++() { |
| + *this += 1; |
| + return *this; |
| + } |
| + |
| + ClampedNumeric operator++(int) { |
| + ClampedNumeric value = *this; |
| + *this += 1; |
| + return value; |
| + } |
| + |
| + ClampedNumeric& operator--() { |
| + *this -= 1; |
| + return *this; |
| + } |
| + |
| + ClampedNumeric operator--(int) { |
| + ClampedNumeric value = *this; |
| + *this -= 1; |
| + return value; |
| + } |
| + |
| + // These perform the actual math operations on the ClampedNumerics. |
| + // Binary arithmetic operations. |
| + template <template <typename, typename, typename> class M, |
|
dcheng
2017/06/28 07:25:06
TBH, I found these templates pretty hard to read d
jschuh
2017/06/28 12:32:37
It's the Clamped*Op struct. But yeah, I just used
dcheng
2017/06/30 07:39:30
Yeah I think that might be better. I would be OK d
|
| + typename L, |
| + typename R> |
| + static ClampedNumeric MathOp(const L lhs, const R rhs) { |
| + using Math = typename MathWrapper<M, L, R>::math; |
| + return ClampedNumeric<T>( |
| + Math::template Do<T>(Wrapper<L>::value(lhs), Wrapper<R>::value(rhs))); |
| + } |
| + |
| + // Assignment arithmetic operations. |
| + template <template <typename, typename, typename> class M, typename R> |
| + ClampedNumeric& MathOp(const R rhs) { |
| + using Math = typename MathWrapper<M, T, R>::math; |
| + *this = |
| + ClampedNumeric<T>(Math::template Do<T>(value_, Wrapper<R>::value(rhs))); |
| + return *this; |
| + } |
| + |
| + template <typename Dst> |
| + constexpr operator Dst() const { |
| + return saturated_cast<typename ArithmeticOrUnderlyingEnum<Dst>::type>( |
| + value_); |
| + } |
| + |
| + private: |
| + T value_; |
| + |
| + // These wrappers allow us to handle state the same way for both |
| + // ClampedNumeric and POD arithmetic types. |
| + template <typename Src> |
| + struct Wrapper { |
| + static constexpr Src value(Src value) { |
| + return static_cast<typename UnderlyingType<Src>::type>(value); |
| + } |
| + }; |
| +}; |
| + |
| +// Convience wrapper to return a new ClampedNumeric from the provided arithmetic |
| +// or ClampedNumericType. |
| +template <typename T> |
| +constexpr ClampedNumeric<typename UnderlyingType<T>::type> MakeClampedNum( |
| + const T value) { |
| + return value; |
| +} |
| + |
| +// Overload the ostream output operator to make logging work nicely. |
| +template <typename T> |
| +std::ostream& operator<<(std::ostream& os, const ClampedNumeric<T>& value) { |
| + os << static_cast<T>(value); |
| + return os; |
| +} |
| + |
| +// These implement the variadic wrapper for the math operations. |
| +template <template <typename, typename, typename> class M, |
| + typename L, |
| + typename R> |
| +ClampedNumeric<typename MathWrapper<M, L, R>::type> ClampMathOp(const L lhs, |
| + const R rhs) { |
| + using Math = typename MathWrapper<M, L, R>::math; |
| + return ClampedNumeric<typename Math::result_type>::template MathOp<M>(lhs, |
| + rhs); |
| +} |
| + |
| +// General purpose wrapper template for arithmetic operations. |
| +template <template <typename, typename, typename> class M, |
| + typename L, |
| + typename R, |
| + typename... Args> |
| +ClampedNumeric<typename ResultType<M, L, R, Args...>::type> |
| +ClampMathOp(const L lhs, const R rhs, const Args... args) { |
| + return ClampMathOp<M>(ClampMathOp<M>(lhs, rhs), args...); |
|
dcheng
2017/06/28 07:25:06
Frightening =)
jschuh
2017/06/28 12:32:37
But I figured out how to make variadic templates!
|
| +} |
| + |
| +BASE_NUMERIC_ARITHMETIC_OPERATORS(Clamped, Clamp, Add, +, +=) |
| +BASE_NUMERIC_ARITHMETIC_OPERATORS(Clamped, Clamp, Sub, -, -=) |
| +BASE_NUMERIC_ARITHMETIC_OPERATORS(Clamped, Clamp, Mul, *, *=) |
| +BASE_NUMERIC_ARITHMETIC_OPERATORS(Clamped, Clamp, Div, /, /=) |
| +BASE_NUMERIC_ARITHMETIC_OPERATORS(Clamped, Clamp, Mod, %, %=) |
| +BASE_NUMERIC_ARITHMETIC_OPERATORS(Clamped, Clamp, Lsh, <<, <<=) |
| +BASE_NUMERIC_ARITHMETIC_OPERATORS(Clamped, Clamp, Rsh, >>, >>=) |
| +BASE_NUMERIC_ARITHMETIC_OPERATORS(Clamped, Clamp, And, &, &=) |
| +BASE_NUMERIC_ARITHMETIC_OPERATORS(Clamped, Clamp, Or, |, |=) |
| +BASE_NUMERIC_ARITHMETIC_OPERATORS(Clamped, Clamp, Xor, ^, ^=) |
| +BASE_NUMERIC_ARITHMETIC_VARIADIC(Clamped, Clamp, Max) |
| +BASE_NUMERIC_ARITHMETIC_VARIADIC(Clamped, Clamp, Min) |
| +BASE_NUMERIC_COMPARISON_OPERATORS(Clamped, IsLess, <); |
| +BASE_NUMERIC_COMPARISON_OPERATORS(Clamped, IsLessOrEqual, <=); |
| +BASE_NUMERIC_COMPARISON_OPERATORS(Clamped, IsGreater, >); |
| +BASE_NUMERIC_COMPARISON_OPERATORS(Clamped, IsGreaterOrEqual, >=); |
| +BASE_NUMERIC_COMPARISON_OPERATORS(Clamped, IsEqual, ==); |
| +BASE_NUMERIC_COMPARISON_OPERATORS(Clamped, IsNotEqual, !=); |
| + |
| +} // namespace internal |
| + |
| +using internal::ClampedNumeric; |
| +using internal::MakeClampedNum; |
| +using internal::ClampMax; |
| +using internal::ClampMin; |
| +using internal::ClampAdd; |
| +using internal::ClampSub; |
| +using internal::ClampMul; |
| +using internal::ClampDiv; |
| +using internal::ClampMod; |
| +using internal::ClampLsh; |
| +using internal::ClampRsh; |
| +using internal::ClampAnd; |
| +using internal::ClampOr; |
| +using internal::ClampXor; |
| + |
| +} // namespace base |
| + |
| +#endif // BASE_NUMERICS_CLAMPED_MATH_H_ |