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