henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
Artem Titov | 9d77762 | 2020-09-18 18:23:08 +0200 | [diff] [blame] | 11 | #ifndef API_NUMERICS_MATH_UTILS_H_ |
| 12 | #define API_NUMERICS_MATH_UTILS_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
Yves Gerey | 14696c2 | 2019-04-11 13:51:10 +0200 | [diff] [blame] | 14 | #include <limits> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 15 | #include <type_traits> |
terelius | d802b5b | 2016-03-01 11:07:34 -0800 | [diff] [blame] | 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "rtc_base/checks.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 18 | |
Artem Titov | 9d77762 | 2020-09-18 18:23:08 +0200 | [diff] [blame] | 19 | namespace webrtc { |
| 20 | namespace webrtc_impl { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 21 | // Given two numbers |x| and |y| such that x >= y, computes the difference |
| 22 | // x - y without causing undefined behavior due to signed overflow. |
| 23 | template <typename T> |
| 24 | typename std::make_unsigned<T>::type unsigned_difference(T x, T y) { |
| 25 | static_assert( |
| 26 | std::is_signed<T>::value, |
| 27 | "Function unsigned_difference is only meaningful for signed types."); |
| 28 | RTC_DCHECK_GE(x, y); |
| 29 | typedef typename std::make_unsigned<T>::type unsigned_type; |
| 30 | // int -> unsigned conversion repeatedly adds UINT_MAX + 1 until the number |
| 31 | // can be represented as an unsigned. Since we know that the actual |
| 32 | // difference x - y can be represented as an unsigned, it is sufficient to |
| 33 | // compute the difference modulo UINT_MAX + 1, i.e using unsigned arithmetic. |
| 34 | return static_cast<unsigned_type>(x) - static_cast<unsigned_type>(y); |
| 35 | } |
terelius | d802b5b | 2016-03-01 11:07:34 -0800 | [diff] [blame] | 36 | |
Yves Gerey | 890f62b | 2019-04-10 17:18:48 +0200 | [diff] [blame] | 37 | // Provide neutral element with respect to min(). |
| 38 | // Typically used as an initial value for running minimum. |
| 39 | template <typename T, |
| 40 | typename std::enable_if<std::numeric_limits<T>::has_infinity>::type* = |
| 41 | nullptr> |
| 42 | constexpr T infinity_or_max() { |
| 43 | return std::numeric_limits<T>::infinity(); |
| 44 | } |
| 45 | |
| 46 | template <typename T, |
| 47 | typename std::enable_if< |
| 48 | !std::numeric_limits<T>::has_infinity>::type* = nullptr> |
| 49 | constexpr T infinity_or_max() { |
| 50 | // Fallback to max(). |
| 51 | return std::numeric_limits<T>::max(); |
| 52 | } |
| 53 | |
| 54 | // Provide neutral element with respect to max(). |
| 55 | // Typically used as an initial value for running maximum. |
| 56 | template <typename T, |
| 57 | typename std::enable_if<std::numeric_limits<T>::has_infinity>::type* = |
| 58 | nullptr> |
| 59 | constexpr T minus_infinity_or_min() { |
| 60 | static_assert(std::is_signed<T>::value, "Unsupported. Please open a bug."); |
| 61 | return -std::numeric_limits<T>::infinity(); |
| 62 | } |
| 63 | |
| 64 | template <typename T, |
| 65 | typename std::enable_if< |
| 66 | !std::numeric_limits<T>::has_infinity>::type* = nullptr> |
| 67 | constexpr T minus_infinity_or_min() { |
| 68 | // Fallback to min(). |
| 69 | return std::numeric_limits<T>::min(); |
| 70 | } |
| 71 | |
Artem Titov | 9d77762 | 2020-09-18 18:23:08 +0200 | [diff] [blame] | 72 | } // namespace webrtc_impl |
| 73 | } // namespace webrtc |
| 74 | |
| 75 | #endif // API_NUMERICS_MATH_UTILS_H_ |