MySQL 9.1.0
Source Code Documentation
bounded_arithmetic.h
Go to the documentation of this file.
1/* Copyright (c) 2023, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24/// @file
25
26#ifndef MYSQL_MATH_BOUNDED_ARITHMETIC_H
27#define MYSQL_MATH_BOUNDED_ARITHMETIC_H
28
29#include <type_traits> // std::enable_if
30
31/// @addtogroup GroupLibsMysqlMath
32/// @{
33
34namespace mysql::math {
35
36/// Return x+y, limited to the given maximum.
37///
38/// @note This works even when x+y would exceed the maximum for the
39/// datatype.
40///
41/// @tparam T Data type. Must be an unsigned integral datatype.
42///
43/// @param x The first term.
44///
45/// @param y The second term.
46///
47/// @param maximum The maximum allowed value.
48///
49/// @return The smallest of (x + y) and (maximum), computed as if
50/// using infinite precision arithmetic.
51template <typename T, std::enable_if_t<std::is_integral<T>::value &&
52 std::is_unsigned<T>::value,
53 bool> = true>
54constexpr T add_bounded(const T x, const T y, const T maximum) {
55 if (y >= maximum || maximum - y < x) return maximum;
56 return x + y;
57}
58
59/// Return x*y, limited to the given maximum.
60///
61/// @note This works even when x * y would exceed the maximum for any of the
62/// data type.
63///
64/// @tparam T Data type for the first factor, the maximum, and the
65/// result. This must be an unsigned integral.
66///
67/// @tparam T2 datatype for the second factor. This can be any
68/// arithmetic type, including floating point types.
69///
70/// @param x The first factor.
71///
72/// @param y The second factor.
73///
74/// @param maximum The maximum allowed value.
75///
76/// @return The smallest of (x + y) and (maximum), computed as if
77/// using infinite precision arithmetic; or 0 if y is negative.
78template <
79 typename T, typename T2,
80 std::enable_if_t<std::is_integral<T>::value && std::is_unsigned<T>::value &&
81 std::is_arithmetic<T2>::value,
82 bool> = true>
83constexpr T multiply_bounded(const T x, const T2 y, const T maximum) {
84 if (y <= 0) return 0;
85 if (y > 1 && static_cast<T>(maximum / y) < x) return maximum;
86 return static_cast<T>(x * y);
87}
88
89/// Return ceil(x / y), where x and y are unsigned integer types
90template <typename T, std::enable_if_t<std::is_integral<T>::value &&
91 std::is_unsigned<T>::value,
92 bool> = true>
93constexpr T ceil_div(const T x, const T y) {
94 return (x + y - 1) / y;
95}
96
97} // namespace mysql::math
98
99/// @}
100
101#endif // MYSQL_MATH_BOUNDED_ARITHMETIC_H
Definition: bounded_arithmetic.h:34
constexpr T multiply_bounded(const T x, const T2 y, const T maximum)
Return x*y, limited to the given maximum.
Definition: bounded_arithmetic.h:83
constexpr T ceil_div(const T x, const T y)
Return ceil(x / y), where x and y are unsigned integer types.
Definition: bounded_arithmetic.h:93
constexpr T add_bounded(const T x, const T y, const T maximum)
Return x+y, limited to the given maximum.
Definition: bounded_arithmetic.h:54