overflows.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #pragma once
  2. #include <c10/macros/Macros.h>
  3. #include <c10/util/TypeSafeSignMath.h>
  4. #include <c10/util/complex.h>
  5. #include <cmath>
  6. #include <limits>
  7. #include <type_traits>
  8. namespace c10 {
  9. // In some versions of MSVC, there will be a compiler error when building.
  10. // C4146: unary minus operator applied to unsigned type, result still unsigned
  11. // C4804: unsafe use of type 'bool' in operation
  12. // It can be addressed by disabling the following warning.
  13. #ifdef _MSC_VER
  14. #pragma warning(push)
  15. #pragma warning(disable : 4146)
  16. #pragma warning(disable : 4804)
  17. #pragma warning(disable : 4018)
  18. #endif
  19. // The overflow checks may involve float to int conversion which may
  20. // trigger precision loss warning. Re-enable the warning once the code
  21. // is fixed. See T58053069.
  22. C10_CLANG_DIAGNOSTIC_PUSH()
  23. #if C10_CLANG_HAS_WARNING("-Wimplicit-float-conversion")
  24. C10_CLANG_DIAGNOSTIC_IGNORE("-Wimplicit-float-conversion")
  25. #endif
  26. // bool can be converted to any type.
  27. // Without specializing on bool, in pytorch_linux_trusty_py2_7_9_build:
  28. // `error: comparison of constant '255' with boolean expression is always false`
  29. // for `f > limit::max()` below
  30. template <typename To, typename From>
  31. std::enable_if_t<std::is_same_v<From, bool>, bool> overflows(
  32. From /*f*/,
  33. bool strict_unsigned [[maybe_unused]] = false) {
  34. return false;
  35. }
  36. // skip isnan and isinf check for integral types
  37. template <typename To, typename From>
  38. std::enable_if_t<std::is_integral_v<From> && !std::is_same_v<From, bool>, bool>
  39. overflows(From f, bool strict_unsigned = false) {
  40. using limit = std::numeric_limits<typename scalar_value_type<To>::type>;
  41. if constexpr (!limit::is_signed && std::numeric_limits<From>::is_signed) {
  42. // allow for negative numbers to wrap using two's complement arithmetic.
  43. // For example, with uint8, this allows for `a - b` to be treated as
  44. // `a + 255 * b`.
  45. if (!strict_unsigned) {
  46. return greater_than_max<To>(f) ||
  47. (c10::is_negative(f) &&
  48. -static_cast<uint64_t>(f) > static_cast<uint64_t>(limit::max()));
  49. }
  50. }
  51. return c10::less_than_lowest<To>(f) || greater_than_max<To>(f);
  52. }
  53. template <typename To, typename From>
  54. std::enable_if_t<std::is_floating_point_v<From>, bool> overflows(
  55. From f,
  56. bool strict_unsigned [[maybe_unused]] = false) {
  57. using limit = std::numeric_limits<typename scalar_value_type<To>::type>;
  58. if (limit::has_infinity && std::isinf(static_cast<double>(f))) {
  59. return false;
  60. }
  61. if (!limit::has_quiet_NaN && (f != f)) {
  62. return true;
  63. }
  64. return f < limit::lowest() || f > limit::max();
  65. }
  66. C10_CLANG_DIAGNOSTIC_POP()
  67. #ifdef _MSC_VER
  68. #pragma warning(pop)
  69. #endif
  70. template <typename To, typename From>
  71. std::enable_if_t<is_complex<From>::value, bool> overflows(
  72. From f,
  73. bool strict_unsigned = false) {
  74. // casts from complex to real are considered to overflow if the
  75. // imaginary component is non-zero
  76. if (!is_complex<To>::value && f.imag() != 0) {
  77. return true;
  78. }
  79. // Check for overflow componentwise
  80. // (Technically, the imag overflow check is guaranteed to be false
  81. // when !is_complex<To>, but any optimizer worth its salt will be
  82. // able to figure it out.)
  83. return overflows<
  84. typename scalar_value_type<To>::type,
  85. typename From::value_type>(f.real(), strict_unsigned) ||
  86. overflows<
  87. typename scalar_value_type<To>::type,
  88. typename From::value_type>(f.imag(), strict_unsigned);
  89. }
  90. } // namespace c10