overflowing-math.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2019 the V8 project authors. All rights reserved.
  2. // Copyright 2020 The Chromium Authors. All rights reserved.
  3. // Use of this source code is governed by a BSD-style license that can be
  4. // found in the LICENSE file.
  5. #ifndef THIRD_PARTY_FDLIBM_OVERFLOWING_MATH_H_
  6. #define THIRD_PARTY_FDLIBM_OVERFLOWING_MATH_H_
  7. #include <stdint.h>
  8. #include <cmath>
  9. #include <type_traits>
  10. namespace fdlibm {
  11. // Helpers for performing overflowing arithmetic operations without relying
  12. // on C++ undefined behavior.
  13. #define ASSERT_SIGNED_INTEGER_TYPE(Type) \
  14. static_assert(std::is_integral<Type>::value && std::is_signed<Type>::value, \
  15. "use this for signed integer types");
  16. #define OP_WITH_WRAPAROUND(Name, OP) \
  17. template <typename signed_type> \
  18. inline signed_type Name##WithWraparound(signed_type a, signed_type b) { \
  19. ASSERT_SIGNED_INTEGER_TYPE(signed_type); \
  20. using unsigned_type = typename std::make_unsigned<signed_type>::type; \
  21. unsigned_type a_unsigned = static_cast<unsigned_type>(a); \
  22. unsigned_type b_unsigned = static_cast<unsigned_type>(b); \
  23. unsigned_type result = a_unsigned OP b_unsigned; \
  24. return static_cast<signed_type>(result); \
  25. }
  26. OP_WITH_WRAPAROUND(Add, +)
  27. OP_WITH_WRAPAROUND(Sub, -)
  28. OP_WITH_WRAPAROUND(Mul, *)
  29. // 16-bit integers are special due to C++'s implicit conversion rules.
  30. // See https://bugs.llvm.org/show_bug.cgi?id=25580.
  31. template <>
  32. inline int16_t MulWithWraparound(int16_t a, int16_t b) {
  33. uint32_t a_unsigned = static_cast<uint32_t>(a);
  34. uint32_t b_unsigned = static_cast<uint32_t>(b);
  35. uint32_t result = a_unsigned * b_unsigned;
  36. return static_cast<int16_t>(static_cast<uint16_t>(result));
  37. }
  38. #undef OP_WITH_WRAPAROUND
  39. template <typename signed_type>
  40. inline signed_type NegateWithWraparound(signed_type a) {
  41. ASSERT_SIGNED_INTEGER_TYPE(signed_type);
  42. if (a == std::numeric_limits<signed_type>::min()) return a;
  43. return -a;
  44. }
  45. template <typename signed_type>
  46. inline signed_type ShlWithWraparound(signed_type a, signed_type b) {
  47. ASSERT_SIGNED_INTEGER_TYPE(signed_type);
  48. using unsigned_type = typename std::make_unsigned<signed_type>::type;
  49. const unsigned_type kMask = (sizeof(a) * 8) - 1;
  50. return static_cast<signed_type>(static_cast<unsigned_type>(a) << (b & kMask));
  51. }
  52. #undef ASSERT_SIGNED_INTEGER_TYPE
  53. // Returns the quotient x/y, avoiding C++ undefined behavior if y == 0.
  54. template <typename T>
  55. inline T Divide(T x, T y) {
  56. if (y != 0) return x / y;
  57. if (x == 0 || x != x) return std::numeric_limits<T>::quiet_NaN();
  58. if ((x >= 0) == (std::signbit(y) == 0)) {
  59. return std::numeric_limits<T>::infinity();
  60. }
  61. return -std::numeric_limits<T>::infinity();
  62. }
  63. inline float Recip(float a) { return Divide(1.0f, a); }
  64. inline float RecipSqrt(float a) {
  65. if (a != 0) return 1.0f / std::sqrt(a);
  66. if (std::signbit(a) == 0) return std::numeric_limits<float>::infinity();
  67. return -std::numeric_limits<float>::infinity();
  68. }
  69. template <typename T>
  70. inline T RoundingAverageUnsigned(T a, T b) {
  71. static_assert(std::is_unsigned<T>::value, "Only for unsiged types");
  72. static_assert(sizeof(T) < sizeof(uint64_t), "Must be smaller than uint64_t");
  73. return (static_cast<uint64_t>(a) + static_cast<uint64_t>(b) + 1) >> 1;
  74. }
  75. } // namespace fdlibm
  76. #endif // THIRD_PARTY_FDLIBM_OVERFLOWING_MATH_H_