OLD | NEW |
---|---|
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> | 8 #include <limits> |
9 | 9 |
10 #include "base/template_util.h" | 10 #include "base/template_util.h" |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
141 // Signed to signed narrowing: Both the upper and lower boundaries may be | 141 // Signed to signed narrowing: Both the upper and lower boundaries may be |
142 // exceeded. | 142 // exceeded. |
143 template <typename Dst, typename Src> | 143 template <typename Dst, typename Src> |
144 struct DstRangeRelationToSrcRangeImpl<Dst, | 144 struct DstRangeRelationToSrcRangeImpl<Dst, |
145 Src, | 145 Src, |
146 INTEGER_REPRESENTATION_SIGNED, | 146 INTEGER_REPRESENTATION_SIGNED, |
147 INTEGER_REPRESENTATION_SIGNED, | 147 INTEGER_REPRESENTATION_SIGNED, |
148 NUMERIC_RANGE_NOT_CONTAINED> { | 148 NUMERIC_RANGE_NOT_CONTAINED> { |
149 static RangeConstraint Check(Src value) { | 149 static RangeConstraint Check(Src value) { |
150 return std::numeric_limits<Dst>::is_iec559 | 150 return std::numeric_limits<Dst>::is_iec559 |
151 ? GetRangeConstraint(value <= std::numeric_limits<Dst>::max(), | 151 ? GetRangeConstraint( |
152 value >= -std::numeric_limits<Dst>::max()) | 152 value<std::numeric_limits<Dst>::max(), value> - |
danakj
2015/06/05 00:19:59
This is some good clang-format mistake here, filin
| |
153 : GetRangeConstraint(value <= std::numeric_limits<Dst>::max(), | 153 std::numeric_limits<Dst>::max()) |
154 value >= std::numeric_limits<Dst>::min()); | 154 : GetRangeConstraint( |
155 value<std::numeric_limits<Dst>::max(), value> | |
156 std::numeric_limits<Dst>::min()); | |
155 } | 157 } |
156 }; | 158 }; |
157 | 159 |
158 // Unsigned to unsigned narrowing: Only the upper boundary can be exceeded. | 160 // Unsigned to unsigned narrowing: Only the upper boundary can be exceeded. |
159 template <typename Dst, typename Src> | 161 template <typename Dst, typename Src> |
160 struct DstRangeRelationToSrcRangeImpl<Dst, | 162 struct DstRangeRelationToSrcRangeImpl<Dst, |
161 Src, | 163 Src, |
162 INTEGER_REPRESENTATION_UNSIGNED, | 164 INTEGER_REPRESENTATION_UNSIGNED, |
163 INTEGER_REPRESENTATION_UNSIGNED, | 165 INTEGER_REPRESENTATION_UNSIGNED, |
164 NUMERIC_RANGE_NOT_CONTAINED> { | 166 NUMERIC_RANGE_NOT_CONTAINED> { |
165 static RangeConstraint Check(Src value) { | 167 static RangeConstraint Check(Src value) { |
166 return GetRangeConstraint(value <= std::numeric_limits<Dst>::max(), true); | 168 return GetRangeConstraint(value < std::numeric_limits<Dst>::max(), true); |
167 } | 169 } |
168 }; | 170 }; |
169 | 171 |
170 // Unsigned to signed: The upper boundary may be exceeded. | 172 // Unsigned to signed: The upper boundary may be exceeded. |
171 template <typename Dst, typename Src> | 173 template <typename Dst, typename Src> |
172 struct DstRangeRelationToSrcRangeImpl<Dst, | 174 struct DstRangeRelationToSrcRangeImpl<Dst, |
173 Src, | 175 Src, |
174 INTEGER_REPRESENTATION_SIGNED, | 176 INTEGER_REPRESENTATION_SIGNED, |
175 INTEGER_REPRESENTATION_UNSIGNED, | 177 INTEGER_REPRESENTATION_UNSIGNED, |
176 NUMERIC_RANGE_NOT_CONTAINED> { | 178 NUMERIC_RANGE_NOT_CONTAINED> { |
177 static RangeConstraint Check(Src value) { | 179 static RangeConstraint Check(Src value) { |
178 return sizeof(Dst) > sizeof(Src) | 180 return sizeof(Dst) > sizeof(Src) |
179 ? RANGE_VALID | 181 ? RANGE_VALID |
180 : GetRangeConstraint( | 182 : GetRangeConstraint( |
181 value <= static_cast<Src>(std::numeric_limits<Dst>::max()), | 183 value < static_cast<Src>(std::numeric_limits<Dst>::max()), |
182 true); | 184 true); |
183 } | 185 } |
184 }; | 186 }; |
185 | 187 |
186 // Signed to unsigned: The upper boundary may be exceeded for a narrower Dst, | 188 // Signed to unsigned: The upper boundary may be exceeded for a narrower Dst, |
187 // and any negative value exceeds the lower boundary. | 189 // and any negative value exceeds the lower boundary. |
188 template <typename Dst, typename Src> | 190 template <typename Dst, typename Src> |
189 struct DstRangeRelationToSrcRangeImpl<Dst, | 191 struct DstRangeRelationToSrcRangeImpl<Dst, |
190 Src, | 192 Src, |
191 INTEGER_REPRESENTATION_UNSIGNED, | 193 INTEGER_REPRESENTATION_UNSIGNED, |
192 INTEGER_REPRESENTATION_SIGNED, | 194 INTEGER_REPRESENTATION_SIGNED, |
193 NUMERIC_RANGE_NOT_CONTAINED> { | 195 NUMERIC_RANGE_NOT_CONTAINED> { |
194 static RangeConstraint Check(Src value) { | 196 static RangeConstraint Check(Src value) { |
195 return (MaxExponent<Dst>::value >= MaxExponent<Src>::value) | 197 return (MaxExponent<Dst>::value >= MaxExponent<Src>::value) |
196 ? GetRangeConstraint(true, value >= static_cast<Src>(0)) | 198 ? GetRangeConstraint(true, value >= static_cast<Src>(0)) |
197 : GetRangeConstraint( | 199 : GetRangeConstraint( |
198 value <= static_cast<Src>(std::numeric_limits<Dst>::max()), | 200 value < static_cast<Src>(std::numeric_limits<Dst>::max()), |
199 value >= static_cast<Src>(0)); | 201 value >= static_cast<Src>(0)); |
200 } | 202 } |
201 }; | 203 }; |
202 | 204 |
203 template <typename Dst, typename Src> | 205 template <typename Dst, typename Src> |
204 inline RangeConstraint DstRangeRelationToSrcRange(Src value) { | 206 inline RangeConstraint DstRangeRelationToSrcRange(Src value) { |
205 static_assert(std::numeric_limits<Src>::is_specialized, | 207 static_assert(std::numeric_limits<Src>::is_specialized, |
206 "Argument must be numeric."); | 208 "Argument must be numeric."); |
207 static_assert(std::numeric_limits<Dst>::is_specialized, | 209 static_assert(std::numeric_limits<Dst>::is_specialized, |
208 "Result must be numeric."); | 210 "Result must be numeric."); |
209 return DstRangeRelationToSrcRangeImpl<Dst, Src>::Check(value); | 211 return DstRangeRelationToSrcRangeImpl<Dst, Src>::Check(value); |
210 } | 212 } |
211 | 213 |
212 } // namespace internal | 214 } // namespace internal |
213 } // namespace base | 215 } // namespace base |
214 | 216 |
215 #endif // BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ | 217 #endif // BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ |
OLD | NEW |