OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2005 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2005 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #ifndef WEBRTC_RTC_BASE_MATHUTILS_H_ | 11 #ifndef WEBRTC_RTC_BASE_MATHUTILS_H_ |
12 #define WEBRTC_RTC_BASE_MATHUTILS_H_ | 12 #define WEBRTC_RTC_BASE_MATHUTILS_H_ |
13 | 13 |
14 #include <math.h> | 14 #include <math.h> |
15 #include <type_traits> | 15 #include <type_traits> |
16 | 16 |
17 #include "webrtc/rtc_base/checks.h" | 17 #include "webrtc/base/checks.h" |
18 | 18 |
19 #ifndef M_PI | 19 #ifndef M_PI |
20 #define M_PI 3.14159265359f | 20 #define M_PI 3.14159265359f |
21 #endif | 21 #endif |
22 | 22 |
23 // Given two numbers |x| and |y| such that x >= y, computes the difference | 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. | 24 // x - y without causing undefined behavior due to signed overflow. |
25 template <typename T> | 25 template <typename T> |
26 typename std::make_unsigned<T>::type unsigned_difference(T x, T y) { | 26 typename std::make_unsigned<T>::type unsigned_difference(T x, T y) { |
27 static_assert( | 27 static_assert( |
28 std::is_signed<T>::value, | 28 std::is_signed<T>::value, |
29 "Function unsigned_difference is only meaningful for signed types."); | 29 "Function unsigned_difference is only meaningful for signed types."); |
30 RTC_DCHECK_GE(x, y); | 30 RTC_DCHECK_GE(x, y); |
31 typedef typename std::make_unsigned<T>::type unsigned_type; | 31 typedef typename std::make_unsigned<T>::type unsigned_type; |
32 // int -> unsigned conversion repeatedly adds UINT_MAX + 1 until the number | 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 | 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 | 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. | 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); | 36 return static_cast<unsigned_type>(x) - static_cast<unsigned_type>(y); |
37 } | 37 } |
38 | 38 |
39 #endif // WEBRTC_RTC_BASE_MATHUTILS_H_ | 39 #endif // WEBRTC_RTC_BASE_MATHUTILS_H_ |
OLD | NEW |