MySQL  8.0.26
Source Code Documentation
add_with_saturate.h
Go to the documentation of this file.
1 #ifndef INCLUDE_ADD_WITH_SATURATE_H_
2 #define INCLUDE_ADD_WITH_SATURATE_H_
3 
4 /*
5  Copyright (c) 2018, 2021, Oracle and/or its affiliates.
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License, version 2.0,
9  as published by the Free Software Foundation.
10 
11  This program is also distributed with certain software (including
12  but not limited to OpenSSL) that is licensed under separate terms,
13  as designated in a particular file or component or in included license
14  documentation. The authors of MySQL hereby grant you an additional
15  permission to link the program and your derivative works with the
16  separately licensed software that they have included with MySQL.
17 
18  This program is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  GNU General Public License, version 2.0, for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with this program; if not, write to the Free Software
25  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
26 
27 /**
28  @file include/add_with_saturate.h
29  Some utilities for saturating add.
30  */
31 
32 #include <limits>
33 #include <type_traits>
34 
35 // Returns a + b, saturated to the range of a.
36 template <class T, class U>
37 inline T AddWithSaturate(T a, U b) {
38  static_assert(!std::is_signed<T>::value, "values must be unsigned");
39  static_assert(!std::is_signed<U>::value, "values must be unsigned");
40 
41  auto result = a + b;
42  if (result < a || result > std::numeric_limits<T>::max()) {
43  // Value wrapped around or was promoted to a larger type; saturate to the
44  // maximum value.
45  return std::numeric_limits<T>::max();
46  }
47  return result;
48 }
49 
50 // Effectively does b += a; with saturation.
51 template <class T, class U>
52 inline void AddWithSaturate(T a, U *b) {
53  *b = AddWithSaturate(*b, a);
54 }
55 
56 #endif // INCLUDE_ADD_WITH_SATURATE_H_
T AddWithSaturate(T a, U b)
Definition: add_with_saturate.h:37
const string value("\"Value\"")
struct result result
Definition: result.h:33
Definition: result.h:29
Definition: dtoa.cc:594