Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * vim: set ts=8 sts=4 et sw=4 tw=99: |
michael@0 | 3 | * |
michael@0 | 4 | * Copyright (C) 2011 Apple Inc. All rights reserved. |
michael@0 | 5 | * |
michael@0 | 6 | * Redistribution and use in source and binary forms, with or without |
michael@0 | 7 | * modification, are permitted provided that the following conditions |
michael@0 | 8 | * are met: |
michael@0 | 9 | * 1. Redistributions of source code must retain the above copyright |
michael@0 | 10 | * notice, this list of conditions and the following disclaimer. |
michael@0 | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
michael@0 | 12 | * notice, this list of conditions and the following disclaimer in the |
michael@0 | 13 | * documentation and/or other materials provided with the distribution. |
michael@0 | 14 | * |
michael@0 | 15 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
michael@0 | 16 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
michael@0 | 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
michael@0 | 18 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
michael@0 | 19 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
michael@0 | 20 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
michael@0 | 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
michael@0 | 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
michael@0 | 23 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 26 | */ |
michael@0 | 27 | |
michael@0 | 28 | #ifndef yarr_CheckedArithmetic_h |
michael@0 | 29 | #define yarr_CheckedArithmetic_h |
michael@0 | 30 | |
michael@0 | 31 | #include "assembler/wtf/Assertions.h" |
michael@0 | 32 | |
michael@0 | 33 | #include <limits> |
michael@0 | 34 | #include <stdint.h> |
michael@0 | 35 | #include "mozilla/TypeTraits.h" |
michael@0 | 36 | |
michael@0 | 37 | #ifdef _MSC_VER |
michael@0 | 38 | # undef min |
michael@0 | 39 | # undef max |
michael@0 | 40 | #endif |
michael@0 | 41 | |
michael@0 | 42 | /* Checked<T> |
michael@0 | 43 | * |
michael@0 | 44 | * This class provides a mechanism to perform overflow-safe integer arithmetic |
michael@0 | 45 | * without having to manually ensure that you have all the required bounds checks |
michael@0 | 46 | * directly in your code. |
michael@0 | 47 | * |
michael@0 | 48 | * There are two modes of operation: |
michael@0 | 49 | * - The default is Checked<T, CrashOnOverflow>, and crashes at the point |
michael@0 | 50 | * and overflow has occurred. |
michael@0 | 51 | * - The alternative is Checked<T, RecordOverflow>, which uses an additional |
michael@0 | 52 | * byte of storage to track whether an overflow has occurred, subsequent |
michael@0 | 53 | * unchecked operations will crash if an overflow has occured |
michael@0 | 54 | * |
michael@0 | 55 | * It is possible to provide a custom overflow handler, in which case you need |
michael@0 | 56 | * to support these functions: |
michael@0 | 57 | * - void overflowed(); |
michael@0 | 58 | * This function is called when an operation has produced an overflow. |
michael@0 | 59 | * - bool hasOverflowed(); |
michael@0 | 60 | * This function must return true if overflowed() has been called on an |
michael@0 | 61 | * instance and false if it has not. |
michael@0 | 62 | * - void clearOverflow(); |
michael@0 | 63 | * Used to reset overflow tracking when a value is being overwritten with |
michael@0 | 64 | * a new value. |
michael@0 | 65 | * |
michael@0 | 66 | * Checked<T> works for all integer types, with the following caveats: |
michael@0 | 67 | * - Mixing signedness of operands is only supported for types narrower than |
michael@0 | 68 | * 64bits. |
michael@0 | 69 | * - It does have a performance impact, so tight loops may want to be careful |
michael@0 | 70 | * when using it. |
michael@0 | 71 | * |
michael@0 | 72 | */ |
michael@0 | 73 | |
michael@0 | 74 | namespace WTF { |
michael@0 | 75 | |
michael@0 | 76 | class CrashOnOverflow { |
michael@0 | 77 | protected: |
michael@0 | 78 | void overflowed() |
michael@0 | 79 | { |
michael@0 | 80 | CRASH(); |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | void clearOverflow() { } |
michael@0 | 84 | |
michael@0 | 85 | public: |
michael@0 | 86 | bool hasOverflowed() const { return false; } |
michael@0 | 87 | }; |
michael@0 | 88 | |
michael@0 | 89 | class RecordOverflow { |
michael@0 | 90 | protected: |
michael@0 | 91 | RecordOverflow() |
michael@0 | 92 | : m_overflowed(false) |
michael@0 | 93 | { |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | void overflowed() |
michael@0 | 97 | { |
michael@0 | 98 | m_overflowed = true; |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | void clearOverflow() |
michael@0 | 102 | { |
michael@0 | 103 | m_overflowed = false; |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | public: |
michael@0 | 107 | bool hasOverflowed() const { return m_overflowed; } |
michael@0 | 108 | |
michael@0 | 109 | private: |
michael@0 | 110 | unsigned char m_overflowed; |
michael@0 | 111 | }; |
michael@0 | 112 | |
michael@0 | 113 | template <typename T, class OverflowHandler = RecordOverflow> class Checked; |
michael@0 | 114 | template <typename T> struct RemoveChecked; |
michael@0 | 115 | template <typename T> struct RemoveChecked<Checked<T> >; |
michael@0 | 116 | |
michael@0 | 117 | template <typename Target, typename Source, bool targetSigned = ::std::numeric_limits<Target>::is_signed, bool sourceSigned = ::std::numeric_limits<Source>::is_signed> struct BoundsChecker; |
michael@0 | 118 | template <typename Target, typename Source> struct BoundsChecker<Target, Source, false, false> { |
michael@0 | 119 | static bool inBounds(Source value) |
michael@0 | 120 | { |
michael@0 | 121 | // Same signedness so implicit type conversion will always increase precision |
michael@0 | 122 | // to widest type |
michael@0 | 123 | return value <= ::std::numeric_limits<Target>::max(); |
michael@0 | 124 | } |
michael@0 | 125 | }; |
michael@0 | 126 | |
michael@0 | 127 | template <typename Target, typename Source> struct BoundsChecker<Target, Source, true, true> { |
michael@0 | 128 | static bool inBounds(Source value) |
michael@0 | 129 | { |
michael@0 | 130 | // Same signedness so implicit type conversion will always increase precision |
michael@0 | 131 | // to widest type |
michael@0 | 132 | return ::std::numeric_limits<Target>::min() <= value && value <= ::std::numeric_limits<Target>::max(); |
michael@0 | 133 | } |
michael@0 | 134 | }; |
michael@0 | 135 | |
michael@0 | 136 | template <typename Target, typename Source> struct BoundsChecker<Target, Source, false, true> { |
michael@0 | 137 | static bool inBounds(Source value) |
michael@0 | 138 | { |
michael@0 | 139 | // Target is unsigned so any value less than zero is clearly unsafe |
michael@0 | 140 | if (value < 0) |
michael@0 | 141 | return false; |
michael@0 | 142 | // If our (unsigned) Target is the same or greater width we can |
michael@0 | 143 | // convert value to type Target without losing precision |
michael@0 | 144 | if (sizeof(Target) >= sizeof(Source)) |
michael@0 | 145 | return static_cast<Target>(value) <= ::std::numeric_limits<Target>::max(); |
michael@0 | 146 | // The signed Source type has greater precision than the target so |
michael@0 | 147 | // max(Target) -> Source will widen. |
michael@0 | 148 | return value <= static_cast<Source>(::std::numeric_limits<Target>::max()); |
michael@0 | 149 | } |
michael@0 | 150 | }; |
michael@0 | 151 | |
michael@0 | 152 | template <typename Target, typename Source> struct BoundsChecker<Target, Source, true, false> { |
michael@0 | 153 | static bool inBounds(Source value) |
michael@0 | 154 | { |
michael@0 | 155 | // Signed target with an unsigned source |
michael@0 | 156 | if (sizeof(Target) <= sizeof(Source)) |
michael@0 | 157 | return value <= static_cast<Source>(::std::numeric_limits<Target>::max()); |
michael@0 | 158 | // Target is Wider than Source so we're guaranteed to fit any value in |
michael@0 | 159 | // unsigned Source |
michael@0 | 160 | return true; |
michael@0 | 161 | } |
michael@0 | 162 | }; |
michael@0 | 163 | |
michael@0 | 164 | template <typename Target, typename Source, bool SameType = mozilla::IsSame<Target, Source>::value> struct BoundsCheckElider; |
michael@0 | 165 | template <typename Target, typename Source> struct BoundsCheckElider<Target, Source, true> { |
michael@0 | 166 | static bool inBounds(Source) { return true; } |
michael@0 | 167 | }; |
michael@0 | 168 | template <typename Target, typename Source> struct BoundsCheckElider<Target, Source, false> : public BoundsChecker<Target, Source> { |
michael@0 | 169 | }; |
michael@0 | 170 | |
michael@0 | 171 | template <typename Target, typename Source> static inline bool isInBounds(Source value) |
michael@0 | 172 | { |
michael@0 | 173 | return BoundsCheckElider<Target, Source>::inBounds(value); |
michael@0 | 174 | } |
michael@0 | 175 | |
michael@0 | 176 | template <typename T> struct RemoveChecked { |
michael@0 | 177 | typedef T CleanType; |
michael@0 | 178 | static const CleanType DefaultValue = 0; |
michael@0 | 179 | }; |
michael@0 | 180 | |
michael@0 | 181 | template <typename T> struct RemoveChecked<Checked<T, CrashOnOverflow> > { |
michael@0 | 182 | typedef typename RemoveChecked<T>::CleanType CleanType; |
michael@0 | 183 | static const CleanType DefaultValue = 0; |
michael@0 | 184 | }; |
michael@0 | 185 | |
michael@0 | 186 | template <typename T> struct RemoveChecked<Checked<T, RecordOverflow> > { |
michael@0 | 187 | typedef typename RemoveChecked<T>::CleanType CleanType; |
michael@0 | 188 | static const CleanType DefaultValue = 0; |
michael@0 | 189 | }; |
michael@0 | 190 | |
michael@0 | 191 | // The ResultBase and SignednessSelector are used to workaround typeof not being |
michael@0 | 192 | // available in MSVC |
michael@0 | 193 | template <typename U, typename V, bool uIsBigger = (sizeof(U) > sizeof(V)), bool sameSize = (sizeof(U) == sizeof(V))> struct ResultBase; |
michael@0 | 194 | template <typename U, typename V> struct ResultBase<U, V, true, false> { |
michael@0 | 195 | typedef U ResultType; |
michael@0 | 196 | }; |
michael@0 | 197 | |
michael@0 | 198 | template <typename U, typename V> struct ResultBase<U, V, false, false> { |
michael@0 | 199 | typedef V ResultType; |
michael@0 | 200 | }; |
michael@0 | 201 | |
michael@0 | 202 | template <typename U> struct ResultBase<U, U, false, true> { |
michael@0 | 203 | typedef U ResultType; |
michael@0 | 204 | }; |
michael@0 | 205 | |
michael@0 | 206 | template <typename U, typename V, bool uIsSigned = ::std::numeric_limits<U>::is_signed, bool vIsSigned = ::std::numeric_limits<V>::is_signed> struct SignednessSelector; |
michael@0 | 207 | template <typename U, typename V> struct SignednessSelector<U, V, true, true> { |
michael@0 | 208 | typedef U ResultType; |
michael@0 | 209 | }; |
michael@0 | 210 | |
michael@0 | 211 | template <typename U, typename V> struct SignednessSelector<U, V, false, false> { |
michael@0 | 212 | typedef U ResultType; |
michael@0 | 213 | }; |
michael@0 | 214 | |
michael@0 | 215 | template <typename U, typename V> struct SignednessSelector<U, V, true, false> { |
michael@0 | 216 | typedef V ResultType; |
michael@0 | 217 | }; |
michael@0 | 218 | |
michael@0 | 219 | template <typename U, typename V> struct SignednessSelector<U, V, false, true> { |
michael@0 | 220 | typedef U ResultType; |
michael@0 | 221 | }; |
michael@0 | 222 | |
michael@0 | 223 | template <typename U, typename V> struct ResultBase<U, V, false, true> { |
michael@0 | 224 | typedef typename SignednessSelector<U, V>::ResultType ResultType; |
michael@0 | 225 | }; |
michael@0 | 226 | |
michael@0 | 227 | template <typename U, typename V> struct Result : ResultBase<typename RemoveChecked<U>::CleanType, typename RemoveChecked<V>::CleanType> { |
michael@0 | 228 | }; |
michael@0 | 229 | |
michael@0 | 230 | template <typename LHS, typename RHS, typename ResultType = typename Result<LHS, RHS>::ResultType, |
michael@0 | 231 | bool lhsSigned = ::std::numeric_limits<LHS>::is_signed, bool rhsSigned = ::std::numeric_limits<RHS>::is_signed> struct ArithmeticOperations; |
michael@0 | 232 | |
michael@0 | 233 | template <typename LHS, typename RHS, typename ResultType> struct ArithmeticOperations<LHS, RHS, ResultType, true, true> { |
michael@0 | 234 | // LHS and RHS are signed types |
michael@0 | 235 | |
michael@0 | 236 | // Helper function |
michael@0 | 237 | static inline bool signsMatch(LHS lhs, RHS rhs) |
michael@0 | 238 | { |
michael@0 | 239 | return (lhs ^ rhs) >= 0; |
michael@0 | 240 | } |
michael@0 | 241 | |
michael@0 | 242 | static inline bool add(LHS lhs, RHS rhs, ResultType& result) WARN_UNUSED_RETURN |
michael@0 | 243 | { |
michael@0 | 244 | if (signsMatch(lhs, rhs)) { |
michael@0 | 245 | if (lhs >= 0) { |
michael@0 | 246 | if ((::std::numeric_limits<ResultType>::max() - rhs) < lhs) |
michael@0 | 247 | return false; |
michael@0 | 248 | } else { |
michael@0 | 249 | ResultType temp = lhs - ::std::numeric_limits<ResultType>::min(); |
michael@0 | 250 | if (rhs < -temp) |
michael@0 | 251 | return false; |
michael@0 | 252 | } |
michael@0 | 253 | } // if the signs do not match this operation can't overflow |
michael@0 | 254 | result = lhs + rhs; |
michael@0 | 255 | return true; |
michael@0 | 256 | } |
michael@0 | 257 | |
michael@0 | 258 | static inline bool sub(LHS lhs, RHS rhs, ResultType& result) WARN_UNUSED_RETURN |
michael@0 | 259 | { |
michael@0 | 260 | if (!signsMatch(lhs, rhs)) { |
michael@0 | 261 | if (lhs >= 0) { |
michael@0 | 262 | if (lhs > ::std::numeric_limits<ResultType>::max() + rhs) |
michael@0 | 263 | return false; |
michael@0 | 264 | } else { |
michael@0 | 265 | if (rhs > ::std::numeric_limits<ResultType>::max() + lhs) |
michael@0 | 266 | return false; |
michael@0 | 267 | } |
michael@0 | 268 | } // if the signs match this operation can't overflow |
michael@0 | 269 | result = lhs - rhs; |
michael@0 | 270 | return true; |
michael@0 | 271 | } |
michael@0 | 272 | |
michael@0 | 273 | static inline bool multiply(LHS lhs, RHS rhs, ResultType& result) WARN_UNUSED_RETURN |
michael@0 | 274 | { |
michael@0 | 275 | if (signsMatch(lhs, rhs)) { |
michael@0 | 276 | if (lhs >= 0) { |
michael@0 | 277 | if (lhs && (::std::numeric_limits<ResultType>::max() / lhs) < rhs) |
michael@0 | 278 | return false; |
michael@0 | 279 | } else { |
michael@0 | 280 | if (lhs == ::std::numeric_limits<ResultType>::min() || rhs == ::std::numeric_limits<ResultType>::min()) |
michael@0 | 281 | return false; |
michael@0 | 282 | if ((::std::numeric_limits<ResultType>::max() / -lhs) < -rhs) |
michael@0 | 283 | return false; |
michael@0 | 284 | } |
michael@0 | 285 | } else { |
michael@0 | 286 | if (lhs < 0) { |
michael@0 | 287 | if (rhs && lhs < (::std::numeric_limits<ResultType>::min() / rhs)) |
michael@0 | 288 | return false; |
michael@0 | 289 | } else { |
michael@0 | 290 | if (lhs && rhs < (::std::numeric_limits<ResultType>::min() / lhs)) |
michael@0 | 291 | return false; |
michael@0 | 292 | } |
michael@0 | 293 | } |
michael@0 | 294 | result = lhs * rhs; |
michael@0 | 295 | return true; |
michael@0 | 296 | } |
michael@0 | 297 | |
michael@0 | 298 | static inline bool equals(LHS lhs, RHS rhs) { return lhs == rhs; } |
michael@0 | 299 | |
michael@0 | 300 | }; |
michael@0 | 301 | |
michael@0 | 302 | template <typename LHS, typename RHS, typename ResultType> struct ArithmeticOperations<LHS, RHS, ResultType, false, false> { |
michael@0 | 303 | // LHS and RHS are unsigned types so bounds checks are nice and easy |
michael@0 | 304 | static inline bool add(LHS lhs, RHS rhs, ResultType& result) WARN_UNUSED_RETURN |
michael@0 | 305 | { |
michael@0 | 306 | ResultType temp = lhs + rhs; |
michael@0 | 307 | if (temp < lhs) |
michael@0 | 308 | return false; |
michael@0 | 309 | result = temp; |
michael@0 | 310 | return true; |
michael@0 | 311 | } |
michael@0 | 312 | |
michael@0 | 313 | static inline bool sub(LHS lhs, RHS rhs, ResultType& result) WARN_UNUSED_RETURN |
michael@0 | 314 | { |
michael@0 | 315 | ResultType temp = lhs - rhs; |
michael@0 | 316 | if (temp > lhs) |
michael@0 | 317 | return false; |
michael@0 | 318 | result = temp; |
michael@0 | 319 | return true; |
michael@0 | 320 | } |
michael@0 | 321 | |
michael@0 | 322 | static inline bool multiply(LHS lhs, RHS rhs, ResultType& result) WARN_UNUSED_RETURN |
michael@0 | 323 | { |
michael@0 | 324 | ResultType temp = lhs * rhs; |
michael@0 | 325 | if (temp < lhs) |
michael@0 | 326 | return false; |
michael@0 | 327 | result = temp; |
michael@0 | 328 | return true; |
michael@0 | 329 | } |
michael@0 | 330 | |
michael@0 | 331 | static inline bool equals(LHS lhs, RHS rhs) { return lhs == rhs; } |
michael@0 | 332 | |
michael@0 | 333 | }; |
michael@0 | 334 | |
michael@0 | 335 | template <typename ResultType> struct ArithmeticOperations<int, unsigned, ResultType, true, false> { |
michael@0 | 336 | static inline bool add(int64_t lhs, int64_t rhs, ResultType& result) |
michael@0 | 337 | { |
michael@0 | 338 | int64_t temp = lhs + rhs; |
michael@0 | 339 | if (temp < ::std::numeric_limits<ResultType>::min()) |
michael@0 | 340 | return false; |
michael@0 | 341 | if (temp > ::std::numeric_limits<ResultType>::max()) |
michael@0 | 342 | return false; |
michael@0 | 343 | result = static_cast<ResultType>(temp); |
michael@0 | 344 | return true; |
michael@0 | 345 | } |
michael@0 | 346 | |
michael@0 | 347 | static inline bool sub(int64_t lhs, int64_t rhs, ResultType& result) |
michael@0 | 348 | { |
michael@0 | 349 | int64_t temp = lhs - rhs; |
michael@0 | 350 | if (temp < ::std::numeric_limits<ResultType>::min()) |
michael@0 | 351 | return false; |
michael@0 | 352 | if (temp > ::std::numeric_limits<ResultType>::max()) |
michael@0 | 353 | return false; |
michael@0 | 354 | result = static_cast<ResultType>(temp); |
michael@0 | 355 | return true; |
michael@0 | 356 | } |
michael@0 | 357 | |
michael@0 | 358 | static inline bool multiply(int64_t lhs, int64_t rhs, ResultType& result) |
michael@0 | 359 | { |
michael@0 | 360 | int64_t temp = lhs * rhs; |
michael@0 | 361 | if (temp < ::std::numeric_limits<ResultType>::min()) |
michael@0 | 362 | return false; |
michael@0 | 363 | if (temp > ::std::numeric_limits<ResultType>::max()) |
michael@0 | 364 | return false; |
michael@0 | 365 | result = static_cast<ResultType>(temp); |
michael@0 | 366 | return true; |
michael@0 | 367 | } |
michael@0 | 368 | |
michael@0 | 369 | static inline bool equals(int lhs, unsigned rhs) |
michael@0 | 370 | { |
michael@0 | 371 | return static_cast<int64_t>(lhs) == static_cast<int64_t>(rhs); |
michael@0 | 372 | } |
michael@0 | 373 | }; |
michael@0 | 374 | |
michael@0 | 375 | template <typename ResultType> struct ArithmeticOperations<unsigned, int, ResultType, false, true> { |
michael@0 | 376 | static inline bool add(int64_t lhs, int64_t rhs, ResultType& result) |
michael@0 | 377 | { |
michael@0 | 378 | return ArithmeticOperations<int, unsigned, ResultType>::add(rhs, lhs, result); |
michael@0 | 379 | } |
michael@0 | 380 | |
michael@0 | 381 | static inline bool sub(int64_t lhs, int64_t rhs, ResultType& result) |
michael@0 | 382 | { |
michael@0 | 383 | return ArithmeticOperations<int, unsigned, ResultType>::sub(lhs, rhs, result); |
michael@0 | 384 | } |
michael@0 | 385 | |
michael@0 | 386 | static inline bool multiply(int64_t lhs, int64_t rhs, ResultType& result) |
michael@0 | 387 | { |
michael@0 | 388 | return ArithmeticOperations<int, unsigned, ResultType>::multiply(rhs, lhs, result); |
michael@0 | 389 | } |
michael@0 | 390 | |
michael@0 | 391 | static inline bool equals(unsigned lhs, int rhs) |
michael@0 | 392 | { |
michael@0 | 393 | return ArithmeticOperations<int, unsigned, ResultType>::equals(rhs, lhs); |
michael@0 | 394 | } |
michael@0 | 395 | }; |
michael@0 | 396 | |
michael@0 | 397 | template <typename U, typename V, typename R> static inline bool safeAdd(U lhs, V rhs, R& result) |
michael@0 | 398 | { |
michael@0 | 399 | return ArithmeticOperations<U, V, R>::add(lhs, rhs, result); |
michael@0 | 400 | } |
michael@0 | 401 | |
michael@0 | 402 | template <typename U, typename V, typename R> static inline bool safeSub(U lhs, V rhs, R& result) |
michael@0 | 403 | { |
michael@0 | 404 | return ArithmeticOperations<U, V, R>::sub(lhs, rhs, result); |
michael@0 | 405 | } |
michael@0 | 406 | |
michael@0 | 407 | template <typename U, typename V, typename R> static inline bool safeMultiply(U lhs, V rhs, R& result) |
michael@0 | 408 | { |
michael@0 | 409 | return ArithmeticOperations<U, V, R>::multiply(lhs, rhs, result); |
michael@0 | 410 | } |
michael@0 | 411 | |
michael@0 | 412 | template <typename U, typename V> static inline bool safeEquals(U lhs, V rhs) |
michael@0 | 413 | { |
michael@0 | 414 | return ArithmeticOperations<U, V>::equals(lhs, rhs); |
michael@0 | 415 | } |
michael@0 | 416 | |
michael@0 | 417 | enum ResultOverflowedTag { ResultOverflowed }; |
michael@0 | 418 | |
michael@0 | 419 | // FIXME: Needed to workaround http://llvm.org/bugs/show_bug.cgi?id=10801 |
michael@0 | 420 | static inline bool workAroundClangBug() { return true; } |
michael@0 | 421 | |
michael@0 | 422 | template <typename T, class OverflowHandler> class Checked : public OverflowHandler { |
michael@0 | 423 | public: |
michael@0 | 424 | template <typename _T, class _OverflowHandler> friend class Checked; |
michael@0 | 425 | Checked() |
michael@0 | 426 | : m_value(0) |
michael@0 | 427 | { |
michael@0 | 428 | } |
michael@0 | 429 | |
michael@0 | 430 | Checked(ResultOverflowedTag) |
michael@0 | 431 | : m_value(0) |
michael@0 | 432 | { |
michael@0 | 433 | // FIXME: Remove this when clang fixes http://llvm.org/bugs/show_bug.cgi?id=10801 |
michael@0 | 434 | if (workAroundClangBug()) |
michael@0 | 435 | this->overflowed(); |
michael@0 | 436 | } |
michael@0 | 437 | |
michael@0 | 438 | template <typename U> Checked(U value) |
michael@0 | 439 | { |
michael@0 | 440 | if (!isInBounds<T>(value)) |
michael@0 | 441 | this->overflowed(); |
michael@0 | 442 | m_value = static_cast<T>(value); |
michael@0 | 443 | } |
michael@0 | 444 | |
michael@0 | 445 | template <typename V> Checked(const Checked<T, V>& rhs) |
michael@0 | 446 | : m_value(rhs.m_value) |
michael@0 | 447 | { |
michael@0 | 448 | if (rhs.hasOverflowed()) |
michael@0 | 449 | this->overflowed(); |
michael@0 | 450 | } |
michael@0 | 451 | |
michael@0 | 452 | template <typename U> Checked(const Checked<U, OverflowHandler>& rhs) |
michael@0 | 453 | : OverflowHandler(rhs) |
michael@0 | 454 | { |
michael@0 | 455 | if (!isInBounds<T>(rhs.m_value)) |
michael@0 | 456 | this->overflowed(); |
michael@0 | 457 | m_value = static_cast<T>(rhs.m_value); |
michael@0 | 458 | } |
michael@0 | 459 | |
michael@0 | 460 | template <typename U, typename V> Checked(const Checked<U, V>& rhs) |
michael@0 | 461 | { |
michael@0 | 462 | if (rhs.hasOverflowed()) |
michael@0 | 463 | this->overflowed(); |
michael@0 | 464 | if (!isInBounds<T>(rhs.m_value)) |
michael@0 | 465 | this->overflowed(); |
michael@0 | 466 | m_value = static_cast<T>(rhs.m_value); |
michael@0 | 467 | } |
michael@0 | 468 | |
michael@0 | 469 | const Checked& operator=(Checked rhs) |
michael@0 | 470 | { |
michael@0 | 471 | this->clearOverflow(); |
michael@0 | 472 | if (rhs.hasOverflowed()) |
michael@0 | 473 | this->overflowed(); |
michael@0 | 474 | m_value = static_cast<T>(rhs.m_value); |
michael@0 | 475 | return *this; |
michael@0 | 476 | } |
michael@0 | 477 | |
michael@0 | 478 | template <typename U> const Checked& operator=(U value) |
michael@0 | 479 | { |
michael@0 | 480 | return *this = Checked(value); |
michael@0 | 481 | } |
michael@0 | 482 | |
michael@0 | 483 | template <typename U, typename V> const Checked& operator=(const Checked<U, V>& rhs) |
michael@0 | 484 | { |
michael@0 | 485 | return *this = Checked(rhs); |
michael@0 | 486 | } |
michael@0 | 487 | |
michael@0 | 488 | // prefix |
michael@0 | 489 | const Checked& operator++() |
michael@0 | 490 | { |
michael@0 | 491 | if (m_value == ::std::numeric_limits<T>::max()) |
michael@0 | 492 | this->overflowed(); |
michael@0 | 493 | m_value++; |
michael@0 | 494 | return *this; |
michael@0 | 495 | } |
michael@0 | 496 | |
michael@0 | 497 | const Checked& operator--() |
michael@0 | 498 | { |
michael@0 | 499 | if (m_value == ::std::numeric_limits<T>::min()) |
michael@0 | 500 | this->overflowed(); |
michael@0 | 501 | m_value--; |
michael@0 | 502 | return *this; |
michael@0 | 503 | } |
michael@0 | 504 | |
michael@0 | 505 | // postfix operators |
michael@0 | 506 | const Checked operator++(int) |
michael@0 | 507 | { |
michael@0 | 508 | if (m_value == ::std::numeric_limits<T>::max()) |
michael@0 | 509 | this->overflowed(); |
michael@0 | 510 | return Checked(m_value++); |
michael@0 | 511 | } |
michael@0 | 512 | |
michael@0 | 513 | const Checked operator--(int) |
michael@0 | 514 | { |
michael@0 | 515 | if (m_value == ::std::numeric_limits<T>::min()) |
michael@0 | 516 | this->overflowed(); |
michael@0 | 517 | return Checked(m_value--); |
michael@0 | 518 | } |
michael@0 | 519 | |
michael@0 | 520 | // Boolean operators |
michael@0 | 521 | bool operator!() const |
michael@0 | 522 | { |
michael@0 | 523 | if (this->hasOverflowed()) |
michael@0 | 524 | CRASH(); |
michael@0 | 525 | return !m_value; |
michael@0 | 526 | } |
michael@0 | 527 | |
michael@0 | 528 | typedef void* (Checked::*UnspecifiedBoolType); |
michael@0 | 529 | operator UnspecifiedBoolType*() const |
michael@0 | 530 | { |
michael@0 | 531 | if (this->hasOverflowed()) |
michael@0 | 532 | CRASH(); |
michael@0 | 533 | return (m_value) ? reinterpret_cast<UnspecifiedBoolType*>(1) : 0; |
michael@0 | 534 | } |
michael@0 | 535 | |
michael@0 | 536 | // Value accessors. unsafeGet() will crash if there's been an overflow. |
michael@0 | 537 | T unsafeGet() const |
michael@0 | 538 | { |
michael@0 | 539 | if (this->hasOverflowed()) |
michael@0 | 540 | CRASH(); |
michael@0 | 541 | return m_value; |
michael@0 | 542 | } |
michael@0 | 543 | |
michael@0 | 544 | bool safeGet(T& value) const WARN_UNUSED_RETURN |
michael@0 | 545 | { |
michael@0 | 546 | value = m_value; |
michael@0 | 547 | return this->hasOverflowed(); |
michael@0 | 548 | } |
michael@0 | 549 | |
michael@0 | 550 | // Mutating assignment |
michael@0 | 551 | template <typename U> const Checked operator+=(U rhs) |
michael@0 | 552 | { |
michael@0 | 553 | if (!safeAdd(m_value, rhs, m_value)) |
michael@0 | 554 | this->overflowed(); |
michael@0 | 555 | return *this; |
michael@0 | 556 | } |
michael@0 | 557 | |
michael@0 | 558 | template <typename U> const Checked operator-=(U rhs) |
michael@0 | 559 | { |
michael@0 | 560 | if (!safeSub(m_value, rhs, m_value)) |
michael@0 | 561 | this->overflowed(); |
michael@0 | 562 | return *this; |
michael@0 | 563 | } |
michael@0 | 564 | |
michael@0 | 565 | template <typename U> const Checked operator*=(U rhs) |
michael@0 | 566 | { |
michael@0 | 567 | if (!safeMultiply(m_value, rhs, m_value)) |
michael@0 | 568 | this->overflowed(); |
michael@0 | 569 | return *this; |
michael@0 | 570 | } |
michael@0 | 571 | |
michael@0 | 572 | const Checked operator*=(double rhs) |
michael@0 | 573 | { |
michael@0 | 574 | double result = rhs * m_value; |
michael@0 | 575 | // Handle +/- infinity and NaN |
michael@0 | 576 | if (!(::std::numeric_limits<T>::min() <= result && ::std::numeric_limits<T>::max() >= result)) |
michael@0 | 577 | this->overflowed(); |
michael@0 | 578 | m_value = (T)result; |
michael@0 | 579 | return *this; |
michael@0 | 580 | } |
michael@0 | 581 | |
michael@0 | 582 | const Checked operator*=(float rhs) |
michael@0 | 583 | { |
michael@0 | 584 | return *this *= (double)rhs; |
michael@0 | 585 | } |
michael@0 | 586 | |
michael@0 | 587 | template <typename U, typename V> const Checked operator+=(Checked<U, V> rhs) |
michael@0 | 588 | { |
michael@0 | 589 | if (rhs.hasOverflowed()) |
michael@0 | 590 | this->overflowed(); |
michael@0 | 591 | return *this += rhs.m_value; |
michael@0 | 592 | } |
michael@0 | 593 | |
michael@0 | 594 | template <typename U, typename V> const Checked operator-=(Checked<U, V> rhs) |
michael@0 | 595 | { |
michael@0 | 596 | if (rhs.hasOverflowed()) |
michael@0 | 597 | this->overflowed(); |
michael@0 | 598 | return *this -= rhs.m_value; |
michael@0 | 599 | } |
michael@0 | 600 | |
michael@0 | 601 | template <typename U, typename V> const Checked operator*=(Checked<U, V> rhs) |
michael@0 | 602 | { |
michael@0 | 603 | if (rhs.hasOverflowed()) |
michael@0 | 604 | this->overflowed(); |
michael@0 | 605 | return *this *= rhs.m_value; |
michael@0 | 606 | } |
michael@0 | 607 | |
michael@0 | 608 | // Equality comparisons |
michael@0 | 609 | template <typename V> bool operator==(Checked<T, V> rhs) |
michael@0 | 610 | { |
michael@0 | 611 | return unsafeGet() == rhs.unsafeGet(); |
michael@0 | 612 | } |
michael@0 | 613 | |
michael@0 | 614 | template <typename U> bool operator==(U rhs) |
michael@0 | 615 | { |
michael@0 | 616 | if (this->hasOverflowed()) |
michael@0 | 617 | this->overflowed(); |
michael@0 | 618 | return safeEquals(m_value, rhs); |
michael@0 | 619 | } |
michael@0 | 620 | |
michael@0 | 621 | template <typename U, typename V> const Checked operator==(Checked<U, V> rhs) |
michael@0 | 622 | { |
michael@0 | 623 | return unsafeGet() == Checked(rhs.unsafeGet()); |
michael@0 | 624 | } |
michael@0 | 625 | |
michael@0 | 626 | template <typename U> bool operator!=(U rhs) |
michael@0 | 627 | { |
michael@0 | 628 | return !(*this == rhs); |
michael@0 | 629 | } |
michael@0 | 630 | |
michael@0 | 631 | private: |
michael@0 | 632 | // Disallow implicit conversion of floating point to integer types |
michael@0 | 633 | Checked(float); |
michael@0 | 634 | Checked(double); |
michael@0 | 635 | void operator=(float); |
michael@0 | 636 | void operator=(double); |
michael@0 | 637 | void operator+=(float); |
michael@0 | 638 | void operator+=(double); |
michael@0 | 639 | void operator-=(float); |
michael@0 | 640 | void operator-=(double); |
michael@0 | 641 | T m_value; |
michael@0 | 642 | }; |
michael@0 | 643 | |
michael@0 | 644 | template <typename U, typename V, typename OverflowHandler> static inline Checked<typename Result<U, V>::ResultType, OverflowHandler> operator+(Checked<U, OverflowHandler> lhs, Checked<V, OverflowHandler> rhs) |
michael@0 | 645 | { |
michael@0 | 646 | U x = 0; |
michael@0 | 647 | V y = 0; |
michael@0 | 648 | bool overflowed = lhs.safeGet(x) || rhs.safeGet(y); |
michael@0 | 649 | typename Result<U, V>::ResultType result = 0; |
michael@0 | 650 | overflowed |= !safeAdd(x, y, result); |
michael@0 | 651 | if (overflowed) |
michael@0 | 652 | return ResultOverflowed; |
michael@0 | 653 | return result; |
michael@0 | 654 | } |
michael@0 | 655 | |
michael@0 | 656 | template <typename U, typename V, typename OverflowHandler> static inline Checked<typename Result<U, V>::ResultType, OverflowHandler> operator-(Checked<U, OverflowHandler> lhs, Checked<V, OverflowHandler> rhs) |
michael@0 | 657 | { |
michael@0 | 658 | U x = 0; |
michael@0 | 659 | V y = 0; |
michael@0 | 660 | bool overflowed = lhs.safeGet(x) || rhs.safeGet(y); |
michael@0 | 661 | typename Result<U, V>::ResultType result = 0; |
michael@0 | 662 | overflowed |= !safeSub(x, y, result); |
michael@0 | 663 | if (overflowed) |
michael@0 | 664 | return ResultOverflowed; |
michael@0 | 665 | return result; |
michael@0 | 666 | } |
michael@0 | 667 | |
michael@0 | 668 | template <typename U, typename V, typename OverflowHandler> static inline Checked<typename Result<U, V>::ResultType, OverflowHandler> operator*(Checked<U, OverflowHandler> lhs, Checked<V, OverflowHandler> rhs) |
michael@0 | 669 | { |
michael@0 | 670 | U x = 0; |
michael@0 | 671 | V y = 0; |
michael@0 | 672 | bool overflowed = lhs.safeGet(x) || rhs.safeGet(y); |
michael@0 | 673 | typename Result<U, V>::ResultType result = 0; |
michael@0 | 674 | overflowed |= !safeMultiply(x, y, result); |
michael@0 | 675 | if (overflowed) |
michael@0 | 676 | return ResultOverflowed; |
michael@0 | 677 | return result; |
michael@0 | 678 | } |
michael@0 | 679 | |
michael@0 | 680 | template <typename U, typename V, typename OverflowHandler> static inline Checked<typename Result<U, V>::ResultType, OverflowHandler> operator+(Checked<U, OverflowHandler> lhs, V rhs) |
michael@0 | 681 | { |
michael@0 | 682 | return lhs + Checked<V, OverflowHandler>(rhs); |
michael@0 | 683 | } |
michael@0 | 684 | |
michael@0 | 685 | template <typename U, typename V, typename OverflowHandler> static inline Checked<typename Result<U, V>::ResultType, OverflowHandler> operator-(Checked<U, OverflowHandler> lhs, V rhs) |
michael@0 | 686 | { |
michael@0 | 687 | return lhs - Checked<V, OverflowHandler>(rhs); |
michael@0 | 688 | } |
michael@0 | 689 | |
michael@0 | 690 | template <typename U, typename V, typename OverflowHandler> static inline Checked<typename Result<U, V>::ResultType, OverflowHandler> operator*(Checked<U, OverflowHandler> lhs, V rhs) |
michael@0 | 691 | { |
michael@0 | 692 | return lhs * Checked<V, OverflowHandler>(rhs); |
michael@0 | 693 | } |
michael@0 | 694 | |
michael@0 | 695 | template <typename U, typename V, typename OverflowHandler> static inline Checked<typename Result<U, V>::ResultType, OverflowHandler> operator+(U lhs, Checked<V, OverflowHandler> rhs) |
michael@0 | 696 | { |
michael@0 | 697 | return Checked<U, OverflowHandler>(lhs) + rhs; |
michael@0 | 698 | } |
michael@0 | 699 | |
michael@0 | 700 | template <typename U, typename V, typename OverflowHandler> static inline Checked<typename Result<U, V>::ResultType, OverflowHandler> operator-(U lhs, Checked<V, OverflowHandler> rhs) |
michael@0 | 701 | { |
michael@0 | 702 | return Checked<U, OverflowHandler>(lhs) - rhs; |
michael@0 | 703 | } |
michael@0 | 704 | |
michael@0 | 705 | template <typename U, typename V, typename OverflowHandler> static inline Checked<typename Result<U, V>::ResultType, OverflowHandler> operator*(U lhs, Checked<V, OverflowHandler> rhs) |
michael@0 | 706 | { |
michael@0 | 707 | return Checked<U, OverflowHandler>(lhs) * rhs; |
michael@0 | 708 | } |
michael@0 | 709 | |
michael@0 | 710 | } |
michael@0 | 711 | |
michael@0 | 712 | using WTF::Checked; |
michael@0 | 713 | using WTF::RecordOverflow; |
michael@0 | 714 | |
michael@0 | 715 | #endif /* yarr_CheckedArithmetic_h */ |