webrtc/rtc_base/numerics/math_utils.h
Yves Gerey 14696c23d9 [Cleanup] Remove our own definition of M_PI.
* This is too brittle and might clash with MSVC's M_PI. See [1].
* We only used it once (in a unit test).
* We shouldn't use PI anyway [2].

Instead, pull it from <cmath> with _USE_MATH_DEFINES,
like it's already done in the code base.

[1] https://ci.chromium.org/p/webrtc/builders/try/win_x86_msvc_rel/6844
[2] https://tauday.com/tau-manifesto

Bug: webrtc:9855
Change-Id: I7a6976240604ef367ea07478d8cb5e4020e5dfeb
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/132548
Reviewed-by: Alessio Bazzica <alessiob@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Yves Gerey <yvesg@google.com>
Cr-Commit-Position: refs/heads/master@{#27597}
2019-04-12 15:03:45 +00:00

70 lines
2.5 KiB
C++

/*
* Copyright 2005 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef RTC_BASE_NUMERICS_MATH_UTILS_H_
#define RTC_BASE_NUMERICS_MATH_UTILS_H_
#include <limits>
#include <type_traits>
#include "rtc_base/checks.h"
// Given two numbers |x| and |y| such that x >= y, computes the difference
// x - y without causing undefined behavior due to signed overflow.
template <typename T>
typename std::make_unsigned<T>::type unsigned_difference(T x, T y) {
static_assert(
std::is_signed<T>::value,
"Function unsigned_difference is only meaningful for signed types.");
RTC_DCHECK_GE(x, y);
typedef typename std::make_unsigned<T>::type unsigned_type;
// int -> unsigned conversion repeatedly adds UINT_MAX + 1 until the number
// can be represented as an unsigned. Since we know that the actual
// difference x - y can be represented as an unsigned, it is sufficient to
// compute the difference modulo UINT_MAX + 1, i.e using unsigned arithmetic.
return static_cast<unsigned_type>(x) - static_cast<unsigned_type>(y);
}
// Provide neutral element with respect to min().
// Typically used as an initial value for running minimum.
template <typename T,
typename std::enable_if<std::numeric_limits<T>::has_infinity>::type* =
nullptr>
constexpr T infinity_or_max() {
return std::numeric_limits<T>::infinity();
}
template <typename T,
typename std::enable_if<
!std::numeric_limits<T>::has_infinity>::type* = nullptr>
constexpr T infinity_or_max() {
// Fallback to max().
return std::numeric_limits<T>::max();
}
// Provide neutral element with respect to max().
// Typically used as an initial value for running maximum.
template <typename T,
typename std::enable_if<std::numeric_limits<T>::has_infinity>::type* =
nullptr>
constexpr T minus_infinity_or_min() {
static_assert(std::is_signed<T>::value, "Unsupported. Please open a bug.");
return -std::numeric_limits<T>::infinity();
}
template <typename T,
typename std::enable_if<
!std::numeric_limits<T>::has_infinity>::type* = nullptr>
constexpr T minus_infinity_or_min() {
// Fallback to min().
return std::numeric_limits<T>::min();
}
#endif // RTC_BASE_NUMERICS_MATH_UTILS_H_