saturation_arithmetic.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___NUMERIC_SATURATION_ARITHMETIC_H
  10. #define _LIBCPP___NUMERIC_SATURATION_ARITHMETIC_H
  11. #include <__assert>
  12. #include <__concepts/arithmetic.h>
  13. #include <__config>
  14. #include <__utility/cmp.h>
  15. #include <limits>
  16. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  17. # pragma GCC system_header
  18. #endif
  19. _LIBCPP_PUSH_MACROS
  20. #include <__undef_macros>
  21. _LIBCPP_BEGIN_NAMESPACE_STD
  22. #if _LIBCPP_STD_VER >= 26
  23. template <__libcpp_integer _Tp>
  24. _LIBCPP_HIDE_FROM_ABI constexpr _Tp add_sat(_Tp __x, _Tp __y) noexcept {
  25. if (_Tp __sum; !__builtin_add_overflow(__x, __y, &__sum))
  26. return __sum;
  27. // Handle overflow
  28. if constexpr (__libcpp_unsigned_integer<_Tp>) {
  29. return std::numeric_limits<_Tp>::max();
  30. } else {
  31. // Signed addition overflow
  32. if (__x > 0)
  33. // Overflows if (x > 0 && y > 0)
  34. return std::numeric_limits<_Tp>::max();
  35. else
  36. // Overflows if (x < 0 && y < 0)
  37. return std::numeric_limits<_Tp>::min();
  38. }
  39. }
  40. template <__libcpp_integer _Tp>
  41. _LIBCPP_HIDE_FROM_ABI constexpr _Tp sub_sat(_Tp __x, _Tp __y) noexcept {
  42. if (_Tp __sub; !__builtin_sub_overflow(__x, __y, &__sub))
  43. return __sub;
  44. // Handle overflow
  45. if constexpr (__libcpp_unsigned_integer<_Tp>) {
  46. // Overflows if (x < y)
  47. return std::numeric_limits<_Tp>::min();
  48. } else {
  49. // Signed subtration overflow
  50. if (__x >= 0)
  51. // Overflows if (x >= 0 && y < 0)
  52. return std::numeric_limits<_Tp>::max();
  53. else
  54. // Overflows if (x < 0 && y > 0)
  55. return std::numeric_limits<_Tp>::min();
  56. }
  57. }
  58. template <__libcpp_integer _Tp>
  59. _LIBCPP_HIDE_FROM_ABI constexpr _Tp mul_sat(_Tp __x, _Tp __y) noexcept {
  60. if (_Tp __mul; !__builtin_mul_overflow(__x, __y, &__mul))
  61. return __mul;
  62. // Handle overflow
  63. if constexpr (__libcpp_unsigned_integer<_Tp>) {
  64. return std::numeric_limits<_Tp>::max();
  65. } else {
  66. // Signed multiplication overflow
  67. if ((__x > 0 && __y > 0) || (__x < 0 && __y < 0))
  68. return std::numeric_limits<_Tp>::max();
  69. // Overflows if (x < 0 && y > 0) || (x > 0 && y < 0)
  70. return std::numeric_limits<_Tp>::min();
  71. }
  72. }
  73. template <__libcpp_integer _Tp>
  74. _LIBCPP_HIDE_FROM_ABI constexpr _Tp div_sat(_Tp __x, _Tp __y) noexcept {
  75. _LIBCPP_ASSERT_UNCATEGORIZED(__y != 0, "Division by 0 is undefined");
  76. if constexpr (__libcpp_unsigned_integer<_Tp>) {
  77. return __x / __y;
  78. } else {
  79. // Handle signed division overflow
  80. if (__x == std::numeric_limits<_Tp>::min() && __y == _Tp{-1})
  81. return std::numeric_limits<_Tp>::max();
  82. return __x / __y;
  83. }
  84. }
  85. template <__libcpp_integer _Rp, __libcpp_integer _Tp>
  86. _LIBCPP_HIDE_FROM_ABI constexpr _Rp saturate_cast(_Tp __x) noexcept {
  87. // Saturation is impossible edge case when ((min _Rp) < (min _Tp) && (max _Rp) > (max _Tp)) and it is expected to be
  88. // optimized out by the compiler.
  89. // Handle overflow
  90. if (std::cmp_less(__x, std::numeric_limits<_Rp>::min()))
  91. return std::numeric_limits<_Rp>::min();
  92. if (std::cmp_greater(__x, std::numeric_limits<_Rp>::max()))
  93. return std::numeric_limits<_Rp>::max();
  94. // No overflow
  95. return static_cast<_Rp>(__x);
  96. }
  97. #endif // _LIBCPP_STD_VER >= 26
  98. _LIBCPP_END_NAMESPACE_STD
  99. _LIBCPP_POP_MACROS
  100. #endif // _LIBCPP___NUMERIC_SATURATION_ARITHMETIC_H