Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(317)

Side by Side Diff: base/numerics/safe_math.h

Issue 2612443002: Convert CheckedNumeric unary operators to constexpr (Closed)
Patch Set: docs Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | base/numerics/safe_math_impl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef BASE_NUMERICS_SAFE_MATH_H_ 5 #ifndef BASE_NUMERICS_SAFE_MATH_H_
6 #define BASE_NUMERICS_SAFE_MATH_H_ 6 #define BASE_NUMERICS_SAFE_MATH_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 9
10 #include <limits> 10 #include <limits>
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 CheckedNumeric& operator<<=(const Src rhs); 197 CheckedNumeric& operator<<=(const Src rhs);
198 template <typename Src> 198 template <typename Src>
199 CheckedNumeric& operator>>=(const Src rhs); 199 CheckedNumeric& operator>>=(const Src rhs);
200 template <typename Src> 200 template <typename Src>
201 CheckedNumeric& operator&=(const Src rhs); 201 CheckedNumeric& operator&=(const Src rhs);
202 template <typename Src> 202 template <typename Src>
203 CheckedNumeric& operator|=(const Src rhs); 203 CheckedNumeric& operator|=(const Src rhs);
204 template <typename Src> 204 template <typename Src>
205 CheckedNumeric& operator^=(const Src rhs); 205 CheckedNumeric& operator^=(const Src rhs);
206 206
207 CheckedNumeric operator-() const { 207 constexpr CheckedNumeric operator-() const {
208 // Negation is always valid for floating point. 208 return CheckedNumeric<T>(
209 T value = 0; 209 NegateWrapper(state_.value()),
210 bool is_valid = (std::is_floating_point<T>::value || IsValid()) && 210 IsValid() &&
211 CheckedNeg(state_.value(), &value); 211 (!std::is_signed<T>::value || std::is_floating_point<T>::value ||
212 return CheckedNumeric<T>(value, is_valid); 212 NegateWrapper(state_.value()) !=
213 std::numeric_limits<T>::lowest()));
213 } 214 }
214 215
215 CheckedNumeric operator~() const { 216 constexpr CheckedNumeric operator~() const {
216 static_assert(!std::is_signed<T>::value, "Type must be unsigned."); 217 return CheckedNumeric<decltype(InvertWrapper(T()))>(
217 T value = 0; 218 InvertWrapper(state_.value()), IsValid());
218 bool is_valid = IsValid() && CheckedInv(state_.value(), &value);
219 return CheckedNumeric<T>(value, is_valid);
220 } 219 }
221 220
222 CheckedNumeric Abs() const { 221 constexpr CheckedNumeric Abs() const {
223 // Absolute value is always valid for floating point. 222 return CheckedNumeric<T>(
224 T value = 0; 223 AbsWrapper(state_.value()),
225 bool is_valid = (std::is_floating_point<T>::value || IsValid()) && 224 IsValid() &&
226 CheckedAbs(state_.value(), &value); 225 (!std::is_signed<T>::value || std::is_floating_point<T>::value ||
227 return CheckedNumeric<T>(value, is_valid); 226 AbsWrapper(state_.value()) != std::numeric_limits<T>::lowest()));
228 } 227 }
229 228
230 template <typename U> 229 template <typename U>
231 constexpr CheckedNumeric<typename MathWrapper<CheckedMaxOp, T, U>::type> Max( 230 constexpr CheckedNumeric<typename MathWrapper<CheckedMaxOp, T, U>::type> Max(
232 const U rhs) const { 231 const U rhs) const {
233 using R = typename UnderlyingType<U>::type; 232 using R = typename UnderlyingType<U>::type;
234 using result_type = typename MathWrapper<CheckedMaxOp, T, U>::type; 233 using result_type = typename MathWrapper<CheckedMaxOp, T, U>::type;
235 // TODO(jschuh): This can be converted to the MathOp version and remain 234 // TODO(jschuh): This can be converted to the MathOp version and remain
236 // constexpr once we have C++14 support. 235 // constexpr once we have C++14 support.
237 return CheckedNumeric<result_type>( 236 return CheckedNumeric<result_type>(
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after
500 using internal::CheckMod; 499 using internal::CheckMod;
501 using internal::CheckLsh; 500 using internal::CheckLsh;
502 using internal::CheckRsh; 501 using internal::CheckRsh;
503 using internal::CheckAnd; 502 using internal::CheckAnd;
504 using internal::CheckOr; 503 using internal::CheckOr;
505 using internal::CheckXor; 504 using internal::CheckXor;
506 505
507 } // namespace base 506 } // namespace base
508 507
509 #endif // BASE_NUMERICS_SAFE_MATH_H_ 508 #endif // BASE_NUMERICS_SAFE_MATH_H_
OLDNEW
« no previous file with comments | « no previous file | base/numerics/safe_math_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698