xref: /aosp_15_r20/external/webrtc/rtc_base/numerics/math_utils.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright 2005 The WebRTC Project Authors. All rights reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #ifndef API_NUMERICS_MATH_UTILS_H_
12*d9f75844SAndroid Build Coastguard Worker #define API_NUMERICS_MATH_UTILS_H_
13*d9f75844SAndroid Build Coastguard Worker 
14*d9f75844SAndroid Build Coastguard Worker #include <limits>
15*d9f75844SAndroid Build Coastguard Worker #include <type_traits>
16*d9f75844SAndroid Build Coastguard Worker 
17*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/checks.h"
18*d9f75844SAndroid Build Coastguard Worker 
19*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
20*d9f75844SAndroid Build Coastguard Worker namespace webrtc_impl {
21*d9f75844SAndroid Build Coastguard Worker // Given two numbers `x` and `y` such that x >= y, computes the difference
22*d9f75844SAndroid Build Coastguard Worker // x - y without causing undefined behavior due to signed overflow.
23*d9f75844SAndroid Build Coastguard Worker template <typename T>
unsigned_difference(T x,T y)24*d9f75844SAndroid Build Coastguard Worker typename std::make_unsigned<T>::type unsigned_difference(T x, T y) {
25*d9f75844SAndroid Build Coastguard Worker   static_assert(
26*d9f75844SAndroid Build Coastguard Worker       std::is_signed<T>::value,
27*d9f75844SAndroid Build Coastguard Worker       "Function unsigned_difference is only meaningful for signed types.");
28*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_GE(x, y);
29*d9f75844SAndroid Build Coastguard Worker   typedef typename std::make_unsigned<T>::type unsigned_type;
30*d9f75844SAndroid Build Coastguard Worker   // int -> unsigned conversion repeatedly adds UINT_MAX + 1 until the number
31*d9f75844SAndroid Build Coastguard Worker   // can be represented as an unsigned. Since we know that the actual
32*d9f75844SAndroid Build Coastguard Worker   // difference x - y can be represented as an unsigned, it is sufficient to
33*d9f75844SAndroid Build Coastguard Worker   // compute the difference modulo UINT_MAX + 1, i.e using unsigned arithmetic.
34*d9f75844SAndroid Build Coastguard Worker   return static_cast<unsigned_type>(x) - static_cast<unsigned_type>(y);
35*d9f75844SAndroid Build Coastguard Worker }
36*d9f75844SAndroid Build Coastguard Worker 
37*d9f75844SAndroid Build Coastguard Worker // Provide neutral element with respect to min().
38*d9f75844SAndroid Build Coastguard Worker // Typically used as an initial value for running minimum.
39*d9f75844SAndroid Build Coastguard Worker template <typename T,
40*d9f75844SAndroid Build Coastguard Worker           typename std::enable_if<std::numeric_limits<T>::has_infinity>::type* =
41*d9f75844SAndroid Build Coastguard Worker               nullptr>
infinity_or_max()42*d9f75844SAndroid Build Coastguard Worker constexpr T infinity_or_max() {
43*d9f75844SAndroid Build Coastguard Worker   return std::numeric_limits<T>::infinity();
44*d9f75844SAndroid Build Coastguard Worker }
45*d9f75844SAndroid Build Coastguard Worker 
46*d9f75844SAndroid Build Coastguard Worker template <typename T,
47*d9f75844SAndroid Build Coastguard Worker           typename std::enable_if<
48*d9f75844SAndroid Build Coastguard Worker               !std::numeric_limits<T>::has_infinity>::type* = nullptr>
infinity_or_max()49*d9f75844SAndroid Build Coastguard Worker constexpr T infinity_or_max() {
50*d9f75844SAndroid Build Coastguard Worker   // Fallback to max().
51*d9f75844SAndroid Build Coastguard Worker   return std::numeric_limits<T>::max();
52*d9f75844SAndroid Build Coastguard Worker }
53*d9f75844SAndroid Build Coastguard Worker 
54*d9f75844SAndroid Build Coastguard Worker // Provide neutral element with respect to max().
55*d9f75844SAndroid Build Coastguard Worker // Typically used as an initial value for running maximum.
56*d9f75844SAndroid Build Coastguard Worker template <typename T,
57*d9f75844SAndroid Build Coastguard Worker           typename std::enable_if<std::numeric_limits<T>::has_infinity>::type* =
58*d9f75844SAndroid Build Coastguard Worker               nullptr>
minus_infinity_or_min()59*d9f75844SAndroid Build Coastguard Worker constexpr T minus_infinity_or_min() {
60*d9f75844SAndroid Build Coastguard Worker   static_assert(std::is_signed<T>::value, "Unsupported. Please open a bug.");
61*d9f75844SAndroid Build Coastguard Worker   return -std::numeric_limits<T>::infinity();
62*d9f75844SAndroid Build Coastguard Worker }
63*d9f75844SAndroid Build Coastguard Worker 
64*d9f75844SAndroid Build Coastguard Worker template <typename T,
65*d9f75844SAndroid Build Coastguard Worker           typename std::enable_if<
66*d9f75844SAndroid Build Coastguard Worker               !std::numeric_limits<T>::has_infinity>::type* = nullptr>
minus_infinity_or_min()67*d9f75844SAndroid Build Coastguard Worker constexpr T minus_infinity_or_min() {
68*d9f75844SAndroid Build Coastguard Worker   // Fallback to min().
69*d9f75844SAndroid Build Coastguard Worker   return std::numeric_limits<T>::min();
70*d9f75844SAndroid Build Coastguard Worker }
71*d9f75844SAndroid Build Coastguard Worker 
72*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc_impl
73*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
74*d9f75844SAndroid Build Coastguard Worker 
75*d9f75844SAndroid Build Coastguard Worker #endif  // API_NUMERICS_MATH_UTILS_H_
76