MySQL 8.3.0
Source Code Documentation
math.h
Go to the documentation of this file.
1/* Copyright (c) 2023, 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 also distributed 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 included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23/// @file math.h
24
25#ifndef MYSQL_BINLOG_EVENT_MATH_MATH_H
26#define MYSQL_BINLOG_EVENT_MATH_MATH_H
27
28#include <type_traits> // std::enable_if
29
30/// @addtogroup GroupLibsMysqlBinlogEvent
31/// @{
32
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 mysql::binlog::event::math
97
98/// @}
99
100#endif // MYSQL_BINLOG_EVENT_MATH_MATH_H
Definition: math.h:33
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 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 ceil_div(const T x, const T y)
Return ceil(x / y), where x and y are unsigned integer types.
Definition: math.h:92