MySQL 8.0.37
Source Code Documentation
math.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/// @addtogroup Replication
25/// @{
26/// @file math.h
27
28#ifndef MYSQL_MATH_MATH_H_
29#define MYSQL_MATH_MATH_H_
30
31#include <type_traits> // std::enable_if
32
33namespace mysqlns::math {
34
35/// Return x+y, limited to the given maximum.
36///
37/// @note This works even when x+y would exceed the maximum for the
38/// datatype.
39///
40/// @tparam T Data type. Must be an unsigned integral datatype.
41///
42/// @param x The first term.
43///
44/// @param y The second term.
45///
46/// @param maximum The maximum allowed value.
47///
48/// @return The smallest of (x + y) and (maximum), computed as if
49/// using infinite precision arithmetic.
50template <typename T, std::enable_if_t<std::is_integral<T>::value &&
51 std::is_unsigned<T>::value,
52 bool> = true>
53constexpr T add_bounded(const T x, const T y, const T maximum) {
54 if (y >= maximum || maximum - y < x) return maximum;
55 return x + y;
56}
57
58/// Return x*y, limited to the given maximum.
59///
60/// @note This works even when x * y would exceed the maximum for any of the
61/// data type.
62///
63/// @tparam T Data type for the first factor, the maximum, and the
64/// result. This must be an unsigned integral.
65///
66/// @tparam T2 datatype for the second factor. This can be any
67/// arithmetic type, including floating point types.
68///
69/// @param x The first factor.
70///
71/// @param y The second factor.
72///
73/// @param maximum The maximum allowed value.
74///
75/// @return The smallest of (x + y) and (maximum), computed as if
76/// using infinite precision arithmetic; or 0 if y is negative.
77template <
78 typename T, typename T2,
79 std::enable_if_t<std::is_integral<T>::value && std::is_unsigned<T>::value &&
80 std::is_arithmetic<T2>::value,
81 bool> = true>
82constexpr T multiply_bounded(const T x, const T2 y, const T maximum) {
83 if (y <= 0) return 0;
84 if (y > 1 && static_cast<T>(maximum / y) < x) return maximum;
85 return static_cast<T>(x * y);
86}
87
88/// Return ceil(x / y), where x and y are unsigned integer types
89template <typename T, std::enable_if_t<std::is_integral<T>::value &&
90 std::is_unsigned<T>::value,
91 bool> = true>
92constexpr T ceil_div(const T x, const T y) {
93 return (x + y - 1) / y;
94}
95
96} // namespace mysqlns::math
97
98/// @} (end of group Replication)
99
100#endif // MYSQL_MATH_MATH_H_
Definition: math.h:33
constexpr T add_bounded(const T x, const T y, const T maximum)
Return x+y, limited to the given maximum.
Definition: math.h:53
constexpr T multiply_bounded(const T x, const T2 y, const T maximum)
Return x*y, limited to the given maximum.
Definition: math.h:82
constexpr T ceil_div(const T x, const T y)
Return ceil(x / y), where x and y are unsigned integer types.
Definition: math.h:92