// Copyright 2019 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef V8_BASE_OVERFLOWING_MATH_H_ #define V8_BASE_OVERFLOWING_MATH_H_ #include #include #include #include "src/base/macros.h" namespace v8 { namespace base { // Helpers for performing overflowing arithmetic operations without relying // on C++ undefined behavior. #define ASSERT_SIGNED_INTEGER_TYPE(Type) \ static_assert(std::is_integral::value && std::is_signed::value, \ "use this for signed integer types"); #define OP_WITH_WRAPAROUND(Name, OP) \ template \ inline signed_type Name##WithWraparound(signed_type a, signed_type b) { \ ASSERT_SIGNED_INTEGER_TYPE(signed_type); \ using unsigned_type = typename std::make_unsigned::type; \ unsigned_type a_unsigned = static_cast(a); \ unsigned_type b_unsigned = static_cast(b); \ unsigned_type result = a_unsigned OP b_unsigned; \ return static_cast(result); \ } OP_WITH_WRAPAROUND(Add, +) OP_WITH_WRAPAROUND(Sub, -) OP_WITH_WRAPAROUND(Mul, *) // 16-bit integers are special due to C++'s implicit conversion rules. // See https://bugs.llvm.org/show_bug.cgi?id=25580. template <> inline int16_t MulWithWraparound(int16_t a, int16_t b) { uint32_t a_unsigned = static_cast(a); uint32_t b_unsigned = static_cast(b); uint32_t result = a_unsigned * b_unsigned; return static_cast(static_cast(result)); } #undef OP_WITH_WRAPAROUND template inline signed_type NegateWithWraparound(signed_type a) { ASSERT_SIGNED_INTEGER_TYPE(signed_type); if (a == std::numeric_limits::min()) return a; return -a; } template inline signed_type ShlWithWraparound(signed_type a, signed_type b) { ASSERT_SIGNED_INTEGER_TYPE(signed_type); using unsigned_type = typename std::make_unsigned::type; const unsigned_type kMask = (sizeof(a) * 8) - 1; return static_cast(static_cast(a) << (b & kMask)); } #undef ASSERT_SIGNED_INTEGER_TYPE // Returns the quotient x/y, avoiding C++ undefined behavior if y == 0. template inline T Divide(T x, T y) { if (y != 0) return x / y; if (x == 0 || x != x) return std::numeric_limits::quiet_NaN(); if ((x >= 0) == (std::signbit(y) == 0)) { return std::numeric_limits::infinity(); } return -std::numeric_limits::infinity(); } inline float Recip(float a) { return Divide(1.0f, a); } inline float RecipSqrt(float a) { if (a != 0) return 1.0f / std::sqrt(a); if (std::signbit(a) == 0) return std::numeric_limits::infinity(); return -std::numeric_limits::infinity(); } } // namespace base } // namespace v8 #endif // V8_BASE_OVERFLOWING_MATH_H_