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

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

Issue 1995753003: Cleanup some constexpr nits in base/numerics (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: nit Created 4 years, 7 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 | « base/numerics/safe_conversions.h ('k') | 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_CONVERSIONS_IMPL_H_ 5 #ifndef BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_
6 #define BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ 6 #define BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_
7 7
8 #include <limits.h> 8 #include <limits.h>
9 #include <stdint.h> 9 #include <stdint.h>
10 10
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 }; 90 };
91 91
92 enum RangeConstraint { 92 enum RangeConstraint {
93 RANGE_VALID = 0x0, // Value can be represented by the destination type. 93 RANGE_VALID = 0x0, // Value can be represented by the destination type.
94 RANGE_UNDERFLOW = 0x1, // Value would overflow. 94 RANGE_UNDERFLOW = 0x1, // Value would overflow.
95 RANGE_OVERFLOW = 0x2, // Value would underflow. 95 RANGE_OVERFLOW = 0x2, // Value would underflow.
96 RANGE_INVALID = RANGE_UNDERFLOW | RANGE_OVERFLOW // Invalid (i.e. NaN). 96 RANGE_INVALID = RANGE_UNDERFLOW | RANGE_OVERFLOW // Invalid (i.e. NaN).
97 }; 97 };
98 98
99 // Helper function for coercing an int back to a RangeContraint. 99 // Helper function for coercing an int back to a RangeContraint.
100 inline constexpr RangeConstraint GetRangeConstraint( 100 constexpr RangeConstraint GetRangeConstraint(int integer_range_constraint) {
101 int integer_range_constraint) {
102 // TODO(jschuh): Once we get full C++14 support we want this 101 // TODO(jschuh): Once we get full C++14 support we want this
103 // assert(integer_range_constraint >= RANGE_VALID && 102 // assert(integer_range_constraint >= RANGE_VALID &&
104 // integer_range_constraint <= RANGE_INVALID) 103 // integer_range_constraint <= RANGE_INVALID)
105 return static_cast<RangeConstraint>(integer_range_constraint); 104 return static_cast<RangeConstraint>(integer_range_constraint);
106 } 105 }
107 106
108 // This function creates a RangeConstraint from an upper and lower bound 107 // This function creates a RangeConstraint from an upper and lower bound
109 // check by taking advantage of the fact that only NaN can be out of range in 108 // check by taking advantage of the fact that only NaN can be out of range in
110 // both directions at once. 109 // both directions at once.
111 constexpr inline RangeConstraint GetRangeConstraint(bool is_in_upper_bound, 110 constexpr inline RangeConstraint GetRangeConstraint(bool is_in_upper_bound,
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 static constexpr RangeConstraint Check(Src value) { 244 static constexpr RangeConstraint Check(Src value) {
246 return (MaxExponent<Dst>::value >= MaxExponent<Src>::value) 245 return (MaxExponent<Dst>::value >= MaxExponent<Src>::value)
247 ? GetRangeConstraint(true, value >= static_cast<Src>(0)) 246 ? GetRangeConstraint(true, value >= static_cast<Src>(0))
248 : GetRangeConstraint( 247 : GetRangeConstraint(
249 value <= static_cast<Src>(NarrowingRange<Dst, Src>::max()), 248 value <= static_cast<Src>(NarrowingRange<Dst, Src>::max()),
250 value >= static_cast<Src>(0)); 249 value >= static_cast<Src>(0));
251 } 250 }
252 }; 251 };
253 252
254 template <typename Dst, typename Src> 253 template <typename Dst, typename Src>
255 inline constexpr RangeConstraint DstRangeRelationToSrcRange(Src value) { 254 constexpr RangeConstraint DstRangeRelationToSrcRange(Src value) {
256 static_assert(std::numeric_limits<Src>::is_specialized, 255 static_assert(std::numeric_limits<Src>::is_specialized,
257 "Argument must be numeric."); 256 "Argument must be numeric.");
258 static_assert(std::numeric_limits<Dst>::is_specialized, 257 static_assert(std::numeric_limits<Dst>::is_specialized,
259 "Result must be numeric."); 258 "Result must be numeric.");
260 return DstRangeRelationToSrcRangeImpl<Dst, Src>::Check(value); 259 return DstRangeRelationToSrcRangeImpl<Dst, Src>::Check(value);
261 } 260 }
262 261
263 } // namespace internal 262 } // namespace internal
264 } // namespace base 263 } // namespace base
265 264
266 #endif // BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ 265 #endif // BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_
OLDNEW
« no previous file with comments | « base/numerics/safe_conversions.h ('k') | base/numerics/safe_math_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698