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

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

Issue 1346753002: Implement CheckedNumeric::UnsignedAbs() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: gcc build fix Created 5 years, 3 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 "base/numerics/safe_math_impl.h" 8 #include "base/numerics/safe_math_impl.h"
9 9
10 namespace base { 10 namespace base {
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 RangeConstraint validity; 138 RangeConstraint validity;
139 T value = CheckedAbs(state_.value(), &validity); 139 T value = CheckedAbs(state_.value(), &validity);
140 // Absolute value is always valid for floating point. 140 // Absolute value is always valid for floating point.
141 if (std::numeric_limits<T>::is_iec559) 141 if (std::numeric_limits<T>::is_iec559)
142 return CheckedNumeric<T>(value); 142 return CheckedNumeric<T>(value);
143 143
144 validity = GetRangeConstraint(state_.validity() | validity); 144 validity = GetRangeConstraint(state_.validity() | validity);
145 return CheckedNumeric<T>(value, validity); 145 return CheckedNumeric<T>(value, validity);
146 } 146 }
147 147
148 // This function is available only for integral types. It returns an unsigned
149 // integer of the same width as the source type, containing the absolute value
150 // of the source, and properly handling signed min.
151 CheckedNumeric<typename UnsignedOrFloatForSize<T>::type> UnsignedAbs() const {
152 return CheckedNumeric<typename UnsignedOrFloatForSize<T>::type>(
153 CheckedUnsignedAbs(state_.value()), state_.validity());
154 }
155
148 CheckedNumeric& operator++() { 156 CheckedNumeric& operator++() {
149 *this += 1; 157 *this += 1;
150 return *this; 158 return *this;
151 } 159 }
152 160
153 CheckedNumeric operator++(int) { 161 CheckedNumeric operator++(int) {
154 CheckedNumeric value = *this; 162 CheckedNumeric value = *this;
155 *this += 1; 163 *this += 1;
156 return value; 164 return value;
157 } 165 }
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 278
271 #undef BASE_NUMERIC_ARITHMETIC_OPERATORS 279 #undef BASE_NUMERIC_ARITHMETIC_OPERATORS
272 280
273 } // namespace internal 281 } // namespace internal
274 282
275 using internal::CheckedNumeric; 283 using internal::CheckedNumeric;
276 284
277 } // namespace base 285 } // namespace base
278 286
279 #endif // BASE_NUMERICS_SAFE_MATH_H_ 287 #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