| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef _LIBCPP___UTILITY_CHECKED_INT_H |
| 10 | #define _LIBCPP___UTILITY_CHECKED_INT_H |
| 11 | |
| 12 | #include <__config> |
| 13 | #include <__memory/addressof.h> |
| 14 | #include <__type_traits/enable_if.h> |
| 15 | #include <__type_traits/is_signed.h> |
| 16 | |
| 17 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 18 | # pragma GCC system_header |
| 19 | #endif |
| 20 | |
| 21 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 22 | |
| 23 | template <class _From, class _To> |
| 24 | inline const bool __is_lossless_conversion_v = |
| 25 | is_signed<_From>::value == is_signed<_To>::value |
| 26 | ? sizeof(_From) <= sizeof(_To) |
| 27 | : is_signed<_To>::value && sizeof(_From) < sizeof(_To); |
| 28 | |
| 29 | // __checked_int is an integer-like class that tracks whether any operation on it overflowed and always has modular |
| 30 | // arithmetic. Since it's unclear how this class should handle division or modulo by zero, these operations are not |
| 31 | // provided. |
| 32 | |
| 33 | // To allow the use of the modular arithmetic the operations don't use logical ors but bitwise ors instead, since they |
| 34 | // don't short-circuit. |
| 35 | |
| 36 | template <class _IntT> |
| 37 | class __checked_int { |
| 38 | _IntT __value_ = 0; |
| 39 | bool __overflowed_ = false; |
| 40 | |
| 41 | __checked_int() = default; |
| 42 | |
| 43 | public: |
| 44 | _LIBCPP_CONSTEXPR explicit __checked_int(_IntT __value) : __value_(__value) {} |
| 45 | |
| 46 | _LIBCPP_CONSTEXPR _IntT __value() const { return __value_; } |
| 47 | _LIBCPP_CONSTEXPR bool __overflowed() const { return __overflowed_; } |
| 48 | |
| 49 | _LIBCPP_CONSTEXPR_SINCE_CXX14 friend __checked_int& operator+=(__checked_int& __lhs, __checked_int __rhs) { |
| 50 | __lhs.__overflowed_ |= |
| 51 | __rhs.__overflowed_ | __builtin_add_overflow(__lhs.__value_, __rhs.__value_, std::addressof(__lhs.__value_)); |
| 52 | return __lhs; |
| 53 | } |
| 54 | |
| 55 | _LIBCPP_CONSTEXPR_SINCE_CXX14 friend __checked_int& operator-=(__checked_int& __lhs, __checked_int __rhs) { |
| 56 | __lhs.__overflowed_ |= |
| 57 | __rhs.__overflowed_ | __builtin_sub_overflow(__lhs.__value_, __rhs.__value_, std::addressof(__lhs.__value_)); |
| 58 | return __lhs; |
| 59 | } |
| 60 | |
| 61 | _LIBCPP_CONSTEXPR_SINCE_CXX14 friend __checked_int& operator*=(__checked_int& __lhs, __checked_int __rhs) { |
| 62 | __lhs.__overflowed_ |= |
| 63 | __rhs.__overflowed_ | __builtin_mul_overflow(__lhs.__value_, __rhs.__value_, std::addressof(__lhs.__value_)); |
| 64 | return __lhs; |
| 65 | } |
| 66 | |
| 67 | template <class _OtherInt, __enable_if_t<__is_lossless_conversion_v<_OtherInt, _IntT>, int> = 0> |
| 68 | _LIBCPP_CONSTEXPR_SINCE_CXX14 friend __checked_int& operator+=(__checked_int& __lhs, _OtherInt __rhs) { |
| 69 | return __lhs += __checked_int(__rhs); |
| 70 | } |
| 71 | |
| 72 | template <class _OtherInt, __enable_if_t<__is_lossless_conversion_v<_OtherInt, _IntT>, int> = 0> |
| 73 | _LIBCPP_CONSTEXPR_SINCE_CXX14 friend __checked_int& operator-=(__checked_int& __lhs, _OtherInt __rhs) { |
| 74 | return __lhs -= __checked_int(__rhs); |
| 75 | } |
| 76 | |
| 77 | template <class _OtherInt, __enable_if_t<__is_lossless_conversion_v<_OtherInt, _IntT>, int> = 0> |
| 78 | _LIBCPP_CONSTEXPR_SINCE_CXX14 friend __checked_int& operator*=(__checked_int& __lhs, _OtherInt __rhs) { |
| 79 | return __lhs *= __checked_int(__rhs); |
| 80 | } |
| 81 | |
| 82 | _LIBCPP_CONSTEXPR_SINCE_CXX14 friend __checked_int operator+(__checked_int __lhs, __checked_int __rhs) { |
| 83 | auto __copy = __lhs; |
| 84 | __copy += __rhs; |
| 85 | return __copy; |
| 86 | } |
| 87 | |
| 88 | _LIBCPP_CONSTEXPR_SINCE_CXX14 friend __checked_int operator-(__checked_int __lhs, __checked_int __rhs) { |
| 89 | auto __copy = __lhs; |
| 90 | __copy -= __rhs; |
| 91 | return __copy; |
| 92 | } |
| 93 | |
| 94 | _LIBCPP_CONSTEXPR_SINCE_CXX14 friend __checked_int operator*(__checked_int __lhs, __checked_int __rhs) { |
| 95 | auto __copy = __lhs; |
| 96 | __copy *= __rhs; |
| 97 | return __copy; |
| 98 | } |
| 99 | |
| 100 | template <class _OtherInt, __enable_if_t<__is_lossless_conversion_v<_OtherInt, _IntT>, int> = 0> |
| 101 | _LIBCPP_CONSTEXPR_SINCE_CXX14 friend __checked_int operator+(__checked_int __lhs, _OtherInt __rhs) { |
| 102 | return __lhs + __checked_int(__rhs); |
| 103 | } |
| 104 | |
| 105 | template <class _OtherInt, __enable_if_t<__is_lossless_conversion_v<_OtherInt, _IntT>, int> = 0> |
| 106 | _LIBCPP_CONSTEXPR_SINCE_CXX14 friend __checked_int operator-(__checked_int __lhs, _OtherInt __rhs) { |
| 107 | return __lhs - __checked_int(__rhs); |
| 108 | } |
| 109 | |
| 110 | template <class _OtherInt, __enable_if_t<__is_lossless_conversion_v<_OtherInt, _IntT>, int> = 0> |
| 111 | _LIBCPP_CONSTEXPR_SINCE_CXX14 friend __checked_int operator*(__checked_int __lhs, _OtherInt __rhs) { |
| 112 | return __lhs * __checked_int(__rhs); |
| 113 | } |
| 114 | }; |
| 115 | |
| 116 | _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(__checked_int); |
| 117 | |
| 118 | _LIBCPP_END_NAMESPACE_STD |
| 119 | |
| 120 | #endif // _LIBCPP___UTILITY_CHECKED_INT_H |
| 121 | |