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

Side by Side Diff: base/safe_numerics_impl.h

Issue 141113003: Refactor base/safe_numerics.h (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 6 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 | Annotate | Revision Log
« no previous file with comments | « base/safe_numerics.h ('k') | base/safe_numerics_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef BASE_SAFE_NUMERICS_IMPL_H_
6 #define BASE_SAFE_NUMERICS_IMPL_H_
7
8 #include <limits>
9
10 #include "base/macros.h"
11
12 namespace base {
13 namespace internal {
14
15 enum DstSign {
16 DST_UNSIGNED,
17 DST_SIGNED
18 };
19
20 enum SrcSign {
21 SRC_UNSIGNED,
22 SRC_SIGNED
23 };
24
25 enum DstRange {
26 OVERLAPS_RANGE,
27 CONTAINS_RANGE
28 };
29
30 // Helper templates to statically determine if our destination type can contain
31 // all values represented by the source type.
32
33 template <typename Dst, typename Src,
34 DstSign IsDstSigned = std::numeric_limits<Dst>::is_signed ?
35 DST_SIGNED : DST_UNSIGNED,
36 SrcSign IsSrcSigned = std::numeric_limits<Src>::is_signed ?
37 SRC_SIGNED : SRC_UNSIGNED>
38 struct StaticRangeCheck {};
39
40 template <typename Dst, typename Src>
41 struct StaticRangeCheck<Dst, Src, DST_SIGNED, SRC_SIGNED> {
42 typedef std::numeric_limits<Dst> DstLimits;
43 typedef std::numeric_limits<Src> SrcLimits;
44 // Compare based on max_exponent, which we must compute for integrals.
45 static const size_t kDstMaxExponent = DstLimits::is_iec559 ?
46 DstLimits::max_exponent :
47 (sizeof(Dst) * 8 - 1);
48 static const size_t kSrcMaxExponent = SrcLimits::is_iec559 ?
49 SrcLimits::max_exponent :
50 (sizeof(Src) * 8 - 1);
51 static const DstRange value = kDstMaxExponent >= kSrcMaxExponent ?
52 CONTAINS_RANGE : OVERLAPS_RANGE;
53 };
54
55 template <typename Dst, typename Src>
56 struct StaticRangeCheck<Dst, Src, DST_UNSIGNED, SRC_UNSIGNED> {
57 static const DstRange value = sizeof(Dst) >= sizeof(Src) ?
58 CONTAINS_RANGE : OVERLAPS_RANGE;
59 };
60
61 template <typename Dst, typename Src>
62 struct StaticRangeCheck<Dst, Src, DST_SIGNED, SRC_UNSIGNED> {
63 typedef std::numeric_limits<Dst> DstLimits;
64 typedef std::numeric_limits<Src> SrcLimits;
65 // Compare based on max_exponent, which we must compute for integrals.
66 static const size_t kDstMaxExponent = DstLimits::is_iec559 ?
67 DstLimits::max_exponent :
68 (sizeof(Dst) * 8 - 1);
69 static const size_t kSrcMaxExponent = sizeof(Src) * 8;
70 static const DstRange value = kDstMaxExponent >= kSrcMaxExponent ?
71 CONTAINS_RANGE : OVERLAPS_RANGE;
72 };
73
74 template <typename Dst, typename Src>
75 struct StaticRangeCheck<Dst, Src, DST_UNSIGNED, SRC_SIGNED> {
76 static const DstRange value = OVERLAPS_RANGE;
77 };
78
79
80 enum RangeCheckResult {
81 TYPE_VALID = 0, // Value can be represented by the destination type.
82 TYPE_UNDERFLOW = 1, // Value would overflow.
83 TYPE_OVERFLOW = 2, // Value would underflow.
84 TYPE_INVALID = 3 // Source value is invalid (i.e. NaN).
85 };
86
87 // This macro creates a RangeCheckResult from an upper and lower bound
88 // check by taking advantage of the fact that only NaN can be out of range in
89 // both directions at once.
90 #define BASE_NUMERIC_RANGE_CHECK_RESULT(is_in_upper_bound, is_in_lower_bound) \
91 RangeCheckResult(((is_in_upper_bound) ? 0 : TYPE_OVERFLOW) | \
92 ((is_in_lower_bound) ? 0 : TYPE_UNDERFLOW))
93
94 template <typename Dst,
95 typename Src,
96 DstSign IsDstSigned = std::numeric_limits<Dst>::is_signed ?
97 DST_SIGNED : DST_UNSIGNED,
98 SrcSign IsSrcSigned = std::numeric_limits<Src>::is_signed ?
99 SRC_SIGNED : SRC_UNSIGNED,
100 DstRange IsSrcRangeContained = StaticRangeCheck<Dst, Src>::value>
101 struct RangeCheckImpl {};
102
103 // The following templates are for ranges that must be verified at runtime. We
104 // split it into checks based on signedness to avoid confusing casts and
105 // compiler warnings on signed an unsigned comparisons.
106
107 // Dst range always contains the result: nothing to check.
108 template <typename Dst, typename Src, DstSign IsDstSigned, SrcSign IsSrcSigned>
109 struct RangeCheckImpl<Dst, Src, IsDstSigned, IsSrcSigned, CONTAINS_RANGE> {
110 static RangeCheckResult Check(Src value) {
111 return TYPE_VALID;
112 }
113 };
114
115 // Signed to signed narrowing.
116 template <typename Dst, typename Src>
117 struct RangeCheckImpl<Dst, Src, DST_SIGNED, SRC_SIGNED, OVERLAPS_RANGE> {
118 static RangeCheckResult Check(Src value) {
119 typedef std::numeric_limits<Dst> DstLimits;
120 return DstLimits::is_iec559 ?
121 BASE_NUMERIC_RANGE_CHECK_RESULT(
122 value <= static_cast<Src>(DstLimits::max()),
123 value >= static_cast<Src>(DstLimits::max() * -1)) :
124 BASE_NUMERIC_RANGE_CHECK_RESULT(
125 value <= static_cast<Src>(DstLimits::max()),
126 value >= static_cast<Src>(DstLimits::min()));
127 }
128 };
129
130 // Unsigned to unsigned narrowing.
131 template <typename Dst, typename Src>
132 struct RangeCheckImpl<Dst, Src, DST_UNSIGNED, SRC_UNSIGNED, OVERLAPS_RANGE> {
133 static RangeCheckResult Check(Src value) {
134 typedef std::numeric_limits<Dst> DstLimits;
135 return BASE_NUMERIC_RANGE_CHECK_RESULT(
136 value <= static_cast<Src>(DstLimits::max()), true);
137 }
138 };
139
140 // Unsigned to signed.
141 template <typename Dst, typename Src>
142 struct RangeCheckImpl<Dst, Src, DST_SIGNED, SRC_UNSIGNED, OVERLAPS_RANGE> {
143 static RangeCheckResult Check(Src value) {
144 typedef std::numeric_limits<Dst> DstLimits;
145 return sizeof(Dst) > sizeof(Src) ? TYPE_VALID :
146 BASE_NUMERIC_RANGE_CHECK_RESULT(
147 value <= static_cast<Src>(DstLimits::max()), true);
148 }
149 };
150
151 // Signed to unsigned.
152 template <typename Dst, typename Src>
153 struct RangeCheckImpl<Dst, Src, DST_UNSIGNED, SRC_SIGNED, OVERLAPS_RANGE> {
154 static RangeCheckResult Check(Src value) {
155 typedef std::numeric_limits<Dst> DstLimits;
156 typedef std::numeric_limits<Src> SrcLimits;
157 // Compare based on max_exponent, which we must compute for integrals.
158 static const size_t kDstMaxExponent = sizeof(Dst) * 8;
159 static const size_t kSrcMaxExponent = SrcLimits::is_iec559 ?
160 SrcLimits::max_exponent :
161 (sizeof(Src) * 8 - 1);
162 return (kDstMaxExponent >= kSrcMaxExponent) ?
163 BASE_NUMERIC_RANGE_CHECK_RESULT(true, value >= static_cast<Src>(0)) :
164 BASE_NUMERIC_RANGE_CHECK_RESULT(
165 value <= static_cast<Src>(DstLimits::max()),
166 value >= static_cast<Src>(0));
167 }
168 };
169
170 template <typename Dst, typename Src>
171 inline RangeCheckResult RangeCheck(Src value) {
172 COMPILE_ASSERT(std::numeric_limits<Src>::is_specialized,
173 argument_must_be_numeric);
174 COMPILE_ASSERT(std::numeric_limits<Dst>::is_specialized,
175 result_must_be_numeric);
176 return RangeCheckImpl<Dst, Src>::Check(value);
177 }
178
179 } // namespace internal
180 } // namespace base
181
182 #endif // BASE_SAFE_NUMERICS_IMPL_H_
183
OLDNEW
« no previous file with comments | « base/safe_numerics.h ('k') | base/safe_numerics_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698