OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license | |
5 * that can be found in the LICENSE file in the root of the source | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 * | |
10 */ | |
11 | |
12 // Borrowed from Chromium's src/base/numerics/safe_conversions_impl.h. | |
13 // - Modified to work in WebRTC (paths, namespace, use of webrtc/base). | |
14 // Based on 'chromium_revision': 'ee311243eae6aef9c907543663754ff38f1f4f40'. | |
15 | |
16 #ifndef WEBRTC_BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ | |
17 #define WEBRTC_BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ | |
18 | |
19 #include <stdint.h> | |
20 | |
21 #include <limits.h> | |
kwiberg-webrtc
2016/03/05 00:47:26
The original includes these two in the other order
hbos
2016/03/07 09:18:02
Done. (I think after the presubmit made me use lim
| |
22 | |
23 #include "webrtc/base/checks.h" | |
24 | |
25 namespace rtc { | |
26 namespace internal { | |
27 | |
28 // The std library doesn't provide a binary max_exponent for integers, however | |
29 // we can compute one by adding one to the number of non-sign bits. This allows | |
30 // for accurate range comparisons between floating point and integer types. | |
31 template <typename NumericType> | |
32 struct MaxExponent { | |
33 static const int value = std::numeric_limits<NumericType>::is_iec559 | |
34 ? std::numeric_limits<NumericType>::max_exponent | |
35 : (sizeof(NumericType) * 8 + 1 - | |
36 std::numeric_limits<NumericType>::is_signed); | |
37 }; | |
38 | |
39 enum IntegerRepresentation { | |
40 INTEGER_REPRESENTATION_UNSIGNED, | |
41 INTEGER_REPRESENTATION_SIGNED | |
42 }; | |
43 | |
44 // A range for a given nunmeric Src type is contained for a given numeric Dst | |
45 // type if both numeric_limits<Src>::max() <= numeric_limits<Dst>::max() and | |
46 // numeric_limits<Src>::min() >= numeric_limits<Dst>::min() are true. | |
47 // We implement this as template specializations rather than simple static | |
48 // comparisons to ensure type correctness in our comparisons. | |
49 enum NumericRangeRepresentation { | |
50 NUMERIC_RANGE_NOT_CONTAINED, | |
51 NUMERIC_RANGE_CONTAINED | |
52 }; | |
53 | |
54 // Helper templates to statically determine if our destination type can contain | |
55 // maximum and minimum values represented by the source type. | |
56 | |
57 template < | |
58 typename Dst, | |
59 typename Src, | |
60 IntegerRepresentation DstSign = std::numeric_limits<Dst>::is_signed | |
61 ? INTEGER_REPRESENTATION_SIGNED | |
62 : INTEGER_REPRESENTATION_UNSIGNED, | |
63 IntegerRepresentation SrcSign = | |
64 std::numeric_limits<Src>::is_signed | |
65 ? INTEGER_REPRESENTATION_SIGNED | |
66 : INTEGER_REPRESENTATION_UNSIGNED > | |
67 struct StaticDstRangeRelationToSrcRange; | |
68 | |
69 // Same sign: Dst is guaranteed to contain Src only if its range is equal or | |
70 // larger. | |
71 template <typename Dst, typename Src, IntegerRepresentation Sign> | |
72 struct StaticDstRangeRelationToSrcRange<Dst, Src, Sign, Sign> { | |
73 static const NumericRangeRepresentation value = | |
74 MaxExponent<Dst>::value >= MaxExponent<Src>::value | |
75 ? NUMERIC_RANGE_CONTAINED | |
76 : NUMERIC_RANGE_NOT_CONTAINED; | |
77 }; | |
78 | |
79 // Unsigned to signed: Dst is guaranteed to contain source only if its range is | |
80 // larger. | |
81 template <typename Dst, typename Src> | |
82 struct StaticDstRangeRelationToSrcRange<Dst, | |
83 Src, | |
84 INTEGER_REPRESENTATION_SIGNED, | |
85 INTEGER_REPRESENTATION_UNSIGNED> { | |
86 static const NumericRangeRepresentation value = | |
87 MaxExponent<Dst>::value > MaxExponent<Src>::value | |
88 ? NUMERIC_RANGE_CONTAINED | |
89 : NUMERIC_RANGE_NOT_CONTAINED; | |
90 }; | |
91 | |
92 // Signed to unsigned: Dst cannot be statically determined to contain Src. | |
93 template <typename Dst, typename Src> | |
94 struct StaticDstRangeRelationToSrcRange<Dst, | |
95 Src, | |
96 INTEGER_REPRESENTATION_UNSIGNED, | |
97 INTEGER_REPRESENTATION_SIGNED> { | |
98 static const NumericRangeRepresentation value = NUMERIC_RANGE_NOT_CONTAINED; | |
99 }; | |
100 | |
101 enum RangeConstraint { | |
102 RANGE_VALID = 0x0, // Value can be represented by the destination type. | |
103 RANGE_UNDERFLOW = 0x1, // Value would overflow. | |
104 RANGE_OVERFLOW = 0x2, // Value would underflow. | |
105 RANGE_INVALID = RANGE_UNDERFLOW | RANGE_OVERFLOW // Invalid (i.e. NaN). | |
106 }; | |
107 | |
108 // Helper function for coercing an int back to a RangeContraint. | |
109 inline RangeConstraint GetRangeConstraint(int integer_range_constraint) { | |
110 RTC_DCHECK(integer_range_constraint >= RANGE_VALID && | |
111 integer_range_constraint <= RANGE_INVALID); | |
112 return static_cast<RangeConstraint>(integer_range_constraint); | |
113 } | |
114 | |
115 // This function creates a RangeConstraint from an upper and lower bound | |
116 // check by taking advantage of the fact that only NaN can be out of range in | |
117 // both directions at once. | |
118 inline RangeConstraint GetRangeConstraint(bool is_in_upper_bound, | |
119 bool is_in_lower_bound) { | |
120 return GetRangeConstraint((is_in_upper_bound ? 0 : RANGE_OVERFLOW) | | |
121 (is_in_lower_bound ? 0 : RANGE_UNDERFLOW)); | |
122 } | |
123 | |
124 // The following helper template addresses a corner case in range checks for | |
125 // conversion from a floating-point type to an integral type of smaller range | |
126 // but larger precision (e.g. float -> unsigned). The problem is as follows: | |
127 // 1. Integral maximum is always one less than a power of two, so it must be | |
128 // truncated to fit the mantissa of the floating point. The direction of | |
129 // rounding is implementation defined, but by default it's always IEEE | |
130 // floats, which round to nearest and thus result in a value of larger | |
131 // magnitude than the integral value. | |
132 // Example: float f = UINT_MAX; // f is 4294967296f but UINT_MAX | |
133 // // is 4294967295u. | |
134 // 2. If the floating point value is equal to the promoted integral maximum | |
135 // value, a range check will erroneously pass. | |
136 // Example: (4294967296f <= 4294967295u) // This is true due to a precision | |
137 // // loss in rounding up to float. | |
138 // 3. When the floating point value is then converted to an integral, the | |
139 // resulting value is out of range for the target integral type and | |
140 // thus is implementation defined. | |
141 // Example: unsigned u = (float)INT_MAX; // u will typically overflow to 0. | |
142 // To fix this bug we manually truncate the maximum value when the destination | |
143 // type is an integral of larger precision than the source floating-point type, | |
144 // such that the resulting maximum is represented exactly as a floating point. | |
145 template <typename Dst, typename Src> | |
146 struct NarrowingRange { | |
147 typedef typename std::numeric_limits<Src> SrcLimits; | |
148 typedef typename std::numeric_limits<Dst> DstLimits; | |
149 | |
150 static Dst max() { | |
151 // The following logic avoids warnings where the max function is | |
152 // instantiated with invalid values for a bit shift (even though | |
153 // such a function can never be called). | |
154 static const int shift = | |
155 (MaxExponent<Src>::value > MaxExponent<Dst>::value && | |
156 SrcLimits::digits < DstLimits::digits && SrcLimits::is_iec559 && | |
157 DstLimits::is_integer) | |
158 ? (DstLimits::digits - SrcLimits::digits) | |
159 : 0; | |
160 | |
161 // We use UINTMAX_C below to avoid compiler warnings about shifting floating | |
162 // points. Since it's a compile time calculation, it shouldn't have any | |
163 // performance impact. | |
164 return DstLimits::max() - static_cast<Dst>((UINTMAX_C(1) << shift) - 1); | |
165 } | |
166 | |
167 static Dst min() { | |
168 return std::numeric_limits<Dst>::is_iec559 ? -DstLimits::max() | |
169 : DstLimits::min(); | |
170 } | |
171 }; | |
172 | |
173 template < | |
174 typename Dst, | |
175 typename Src, | |
176 IntegerRepresentation DstSign = std::numeric_limits<Dst>::is_signed | |
177 ? INTEGER_REPRESENTATION_SIGNED | |
178 : INTEGER_REPRESENTATION_UNSIGNED, | |
179 IntegerRepresentation SrcSign = std::numeric_limits<Src>::is_signed | |
180 ? INTEGER_REPRESENTATION_SIGNED | |
181 : INTEGER_REPRESENTATION_UNSIGNED, | |
182 NumericRangeRepresentation DstRange = | |
183 StaticDstRangeRelationToSrcRange<Dst, Src>::value > | |
184 struct DstRangeRelationToSrcRangeImpl; | |
185 | |
186 // The following templates are for ranges that must be verified at runtime. We | |
187 // split it into checks based on signedness to avoid confusing casts and | |
188 // compiler warnings on signed an unsigned comparisons. | |
189 | |
190 // Dst range is statically determined to contain Src: Nothing to check. | |
191 template <typename Dst, | |
192 typename Src, | |
193 IntegerRepresentation DstSign, | |
194 IntegerRepresentation SrcSign> | |
195 struct DstRangeRelationToSrcRangeImpl<Dst, | |
196 Src, | |
197 DstSign, | |
198 SrcSign, | |
199 NUMERIC_RANGE_CONTAINED> { | |
200 static RangeConstraint Check(Src value) { return RANGE_VALID; } | |
201 }; | |
202 | |
203 // Signed to signed narrowing: Both the upper and lower boundaries may be | |
204 // exceeded. | |
205 template <typename Dst, typename Src> | |
206 struct DstRangeRelationToSrcRangeImpl<Dst, | |
207 Src, | |
208 INTEGER_REPRESENTATION_SIGNED, | |
209 INTEGER_REPRESENTATION_SIGNED, | |
210 NUMERIC_RANGE_NOT_CONTAINED> { | |
211 static RangeConstraint Check(Src value) { | |
212 return GetRangeConstraint((value <= NarrowingRange<Dst, Src>::max()), | |
213 (value >= NarrowingRange<Dst, Src>::min())); | |
214 } | |
215 }; | |
216 | |
217 // Unsigned to unsigned narrowing: Only the upper boundary can be exceeded. | |
218 template <typename Dst, typename Src> | |
219 struct DstRangeRelationToSrcRangeImpl<Dst, | |
220 Src, | |
221 INTEGER_REPRESENTATION_UNSIGNED, | |
222 INTEGER_REPRESENTATION_UNSIGNED, | |
223 NUMERIC_RANGE_NOT_CONTAINED> { | |
224 static RangeConstraint Check(Src value) { | |
225 return GetRangeConstraint(value <= NarrowingRange<Dst, Src>::max(), true); | |
226 } | |
227 }; | |
228 | |
229 // Unsigned to signed: The upper boundary may be exceeded. | |
230 template <typename Dst, typename Src> | |
231 struct DstRangeRelationToSrcRangeImpl<Dst, | |
232 Src, | |
233 INTEGER_REPRESENTATION_SIGNED, | |
234 INTEGER_REPRESENTATION_UNSIGNED, | |
235 NUMERIC_RANGE_NOT_CONTAINED> { | |
236 static RangeConstraint Check(Src value) { | |
237 return sizeof(Dst) > sizeof(Src) | |
238 ? RANGE_VALID | |
239 : GetRangeConstraint( | |
240 value <= static_cast<Src>(NarrowingRange<Dst, Src>::max()), | |
241 true); | |
242 } | |
243 }; | |
244 | |
245 // Signed to unsigned: The upper boundary may be exceeded for a narrower Dst, | |
246 // and any negative value exceeds the lower boundary. | |
247 template <typename Dst, typename Src> | |
248 struct DstRangeRelationToSrcRangeImpl<Dst, | |
249 Src, | |
250 INTEGER_REPRESENTATION_UNSIGNED, | |
251 INTEGER_REPRESENTATION_SIGNED, | |
252 NUMERIC_RANGE_NOT_CONTAINED> { | |
253 static RangeConstraint Check(Src value) { | |
254 return (MaxExponent<Dst>::value >= MaxExponent<Src>::value) | |
255 ? GetRangeConstraint(true, value >= static_cast<Src>(0)) | |
256 : GetRangeConstraint( | |
257 value <= static_cast<Src>(NarrowingRange<Dst, Src>::max()), | |
258 value >= static_cast<Src>(0)); | |
259 } | |
260 }; | |
261 | |
262 template <typename Dst, typename Src> | |
263 inline RangeConstraint DstRangeRelationToSrcRange(Src value) { | |
264 static_assert(std::numeric_limits<Src>::is_specialized, | |
265 "Argument must be numeric."); | |
266 static_assert(std::numeric_limits<Dst>::is_specialized, | |
267 "Result must be numeric."); | |
268 return DstRangeRelationToSrcRangeImpl<Dst, Src>::Check(value); | |
269 } | |
270 | |
271 } // namespace internal | |
272 } // namespace rtc | |
273 | |
274 #endif // WEBRTC_BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ | |
OLD | NEW |