libstdc++
sat_arith.h
Go to the documentation of this file.
1// Saturation arithmetic -*- C++ -*-
2
3// Copyright The GNU Toolchain Authors.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file include/bits/sat_arith.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{numeric}
28 */
29
30#ifndef _GLIBCXX_SAT_ARITH_H
31#define _GLIBCXX_SAT_ARITH_H 1
32
33#pragma GCC system_header
34
35#include <bits/version.h>
36
37#ifdef __glibcxx_saturation_arithmetic // C++ >= 26
38
39#include <concepts>
40#include <ext/numeric_traits.h>
41
42namespace std _GLIBCXX_VISIBILITY(default)
43{
44_GLIBCXX_BEGIN_NAMESPACE_VERSION
45
46 /// Add two integers, with saturation in case of overflow.
47 template<typename _Tp> requires __is_standard_integer<_Tp>::value
48 constexpr _Tp
49 add_sat(_Tp __x, _Tp __y) noexcept
50 {
51 _Tp __z;
52 if (!__builtin_add_overflow(__x, __y, &__z))
53 return __z;
54 if constexpr (is_unsigned_v<_Tp>)
56 else if (__x < 0)
58 else
60 }
61
62 /// Subtract one integer from another, with saturation in case of overflow.
63 template<typename _Tp> requires __is_standard_integer<_Tp>::value
64 constexpr _Tp
65 sub_sat(_Tp __x, _Tp __y) noexcept
66 {
67 _Tp __z;
68 if (!__builtin_sub_overflow(__x, __y, &__z))
69 return __z;
70 if constexpr (is_unsigned_v<_Tp>)
72 else if (__x < 0)
74 else
76 }
77
78 /// Multiply two integers, with saturation in case of overflow.
79 template<typename _Tp> requires __is_standard_integer<_Tp>::value
80 constexpr _Tp
81 mul_sat(_Tp __x, _Tp __y) noexcept
82 {
83 _Tp __z;
84 if (!__builtin_mul_overflow(__x, __y, &__z))
85 return __z;
86 if constexpr (is_unsigned_v<_Tp>)
88 else if (__x < 0 != __y < 0)
90 else
92 }
93
94 /// Divide one integer by another, with saturation in case of overflow.
95 template<typename _Tp> requires __is_standard_integer<_Tp>::value
96 constexpr _Tp
97 div_sat(_Tp __x, _Tp __y) noexcept
98 {
99 __glibcxx_assert(__y != 0);
100 if constexpr (is_signed_v<_Tp>)
101 if (__x == __gnu_cxx::__int_traits<_Tp>::__min && __y == _Tp(-1))
103 return __x / __y;
104 }
105
106 /// Divide one integer by another, with saturation in case of overflow.
107 template<typename _Res, typename _Tp>
108 requires __is_standard_integer<_Res>::value
109 && __is_standard_integer<_Tp>::value
110 constexpr _Res
111 saturate_cast(_Tp __x) noexcept
112 {
113 constexpr int __digits_R = __gnu_cxx::__int_traits<_Res>::__digits;
114 constexpr int __digits_T = __gnu_cxx::__int_traits<_Tp>::__digits;
115 constexpr _Res __max_Res = __gnu_cxx::__int_traits<_Res>::__max;
116
117 if constexpr (is_signed_v<_Res> == is_signed_v<_Tp>)
118 {
119 if constexpr (__digits_R < __digits_T)
120 {
121 constexpr _Res __min_Res = __gnu_cxx::__int_traits<_Res>::__min;
122
123 if (__x < static_cast<_Tp>(__min_Res))
124 return __min_Res;
125 else if (__x > static_cast<_Tp>(__max_Res))
126 return __max_Res;
127 }
128 }
129 else if constexpr (is_signed_v<_Tp>) // Res is unsigned
130 {
131 if (__x < 0)
132 return 0;
133 else if (make_unsigned_t<_Tp>(__x) > __max_Res)
135 }
136 else // Tp is unsigned, Res is signed
137 {
138 if (__x > make_unsigned_t<_Res>(__max_Res))
139 return __max_Res;
140 }
141 return static_cast<_Res>(__x);
142 }
143
144_GLIBCXX_END_NAMESPACE_VERSION
145} // namespace
146
147#endif // __glibcxx_saturation_arithmetic
148#endif /* _GLIBCXX_SAT_ARITH_H */
ISO C++ entities toplevel namespace is std.
__numeric_traits_integer< _Tp > __int_traits
Convenience alias for __numeric_traits<integer-type>.