blob: d5f3ee40734a252a1d1bdcb52fc73c2a1dd88f65 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2005 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef RTC_BASE_NUMERICS_MATH_UTILS_H_
12#define RTC_BASE_NUMERICS_MATH_UTILS_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <math.h>
15#include <type_traits>
tereliusd802b5b2016-03-01 11:07:34 -080016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/checks.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020018
19#ifndef M_PI
20#define M_PI 3.14159265359f
21#endif
22
23// Given two numbers |x| and |y| such that x >= y, computes the difference
24// x - y without causing undefined behavior due to signed overflow.
25template <typename T>
26typename std::make_unsigned<T>::type unsigned_difference(T x, T y) {
27 static_assert(
28 std::is_signed<T>::value,
29 "Function unsigned_difference is only meaningful for signed types.");
30 RTC_DCHECK_GE(x, y);
31 typedef typename std::make_unsigned<T>::type unsigned_type;
32 // int -> unsigned conversion repeatedly adds UINT_MAX + 1 until the number
33 // can be represented as an unsigned. Since we know that the actual
34 // difference x - y can be represented as an unsigned, it is sufficient to
35 // compute the difference modulo UINT_MAX + 1, i.e using unsigned arithmetic.
36 return static_cast<unsigned_type>(x) - static_cast<unsigned_type>(y);
37}
tereliusd802b5b2016-03-01 11:07:34 -080038
Yves Gerey890f62b2019-04-10 17:18:48 +020039// Provide neutral element with respect to min().
40// Typically used as an initial value for running minimum.
41template <typename T,
42 typename std::enable_if<std::numeric_limits<T>::has_infinity>::type* =
43 nullptr>
44constexpr T infinity_or_max() {
45 return std::numeric_limits<T>::infinity();
46}
47
48template <typename T,
49 typename std::enable_if<
50 !std::numeric_limits<T>::has_infinity>::type* = nullptr>
51constexpr T infinity_or_max() {
52 // Fallback to max().
53 return std::numeric_limits<T>::max();
54}
55
56// Provide neutral element with respect to max().
57// Typically used as an initial value for running maximum.
58template <typename T,
59 typename std::enable_if<std::numeric_limits<T>::has_infinity>::type* =
60 nullptr>
61constexpr T minus_infinity_or_min() {
62 static_assert(std::is_signed<T>::value, "Unsupported. Please open a bug.");
63 return -std::numeric_limits<T>::infinity();
64}
65
66template <typename T,
67 typename std::enable_if<
68 !std::numeric_limits<T>::has_infinity>::type* = nullptr>
69constexpr T minus_infinity_or_min() {
70 // Fallback to min().
71 return std::numeric_limits<T>::min();
72}
73
Steve Anton10542f22019-01-11 09:11:00 -080074#endif // RTC_BASE_NUMERICS_MATH_UTILS_H_