Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(84)

Side by Side Diff: webrtc/rtc_base/random.cc

Issue 3009123002: Move UBSan warnings from a blacklist to the source (Closed)
Patch Set: Address review comments Created 3 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/rtc_base/ipaddress_unittest.cc ('k') | webrtc/rtc_base/random_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2015 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 #include "webrtc/rtc_base/random.h" 10 #include "webrtc/rtc_base/random.h"
11 11
12 #include <math.h> 12 #include <math.h>
13 13
14 #include "webrtc/rtc_base/checks.h" 14 #include "webrtc/rtc_base/checks.h"
15 #include "webrtc/rtc_base/safe_conversions.h"
15 16
16 namespace webrtc { 17 namespace webrtc {
17 18
18 Random::Random(uint64_t seed) { 19 Random::Random(uint64_t seed) {
19 RTC_DCHECK(seed != 0x0ull); 20 RTC_DCHECK(seed != 0x0ull);
20 state_ = seed; 21 state_ = seed;
21 } 22 }
22 23
23 uint32_t Random::Rand(uint32_t t) { 24 uint32_t Random::Rand(uint32_t t) {
24 // Casting the output to 32 bits will give an almost uniform number. 25 // Casting the output to 32 bits will give an almost uniform number.
25 // Pr[x=0] = (2^32-1) / (2^64-1) 26 // Pr[x=0] = (2^32-1) / (2^64-1)
26 // Pr[x=k] = 2^32 / (2^64-1) for k!=0 27 // Pr[x=k] = 2^32 / (2^64-1) for k!=0
27 // Uniform would be Pr[x=k] = 2^32 / 2^64 for all 32-bit integers k. 28 // Uniform would be Pr[x=k] = 2^32 / 2^64 for all 32-bit integers k.
28 uint32_t x = NextOutput(); 29 uint32_t x = NextOutput();
29 // If x / 2^32 is uniform on [0,1), then x / 2^32 * (t+1) is uniform on 30 // If x / 2^32 is uniform on [0,1), then x / 2^32 * (t+1) is uniform on
30 // the interval [0,t+1), so the integer part is uniform on [0,t]. 31 // the interval [0,t+1), so the integer part is uniform on [0,t].
31 uint64_t result = x * (static_cast<uint64_t>(t) + 1); 32 uint64_t result = x * (static_cast<uint64_t>(t) + 1);
32 result >>= 32; 33 result >>= 32;
33 return result; 34 return result;
34 } 35 }
35 36
36 uint32_t Random::Rand(uint32_t low, uint32_t high) { 37 uint32_t Random::Rand(uint32_t low, uint32_t high) {
37 RTC_DCHECK(low <= high); 38 RTC_DCHECK(low <= high);
38 return Rand(high - low) + low; 39 return Rand(high - low) + low;
39 } 40 }
40 41
41 int32_t Random::Rand(int32_t low, int32_t high) { 42 int32_t Random::Rand(int32_t low, int32_t high) {
42 RTC_DCHECK(low <= high); 43 RTC_DCHECK(low <= high);
43 // We rely on subtraction (and addition) to be the same for signed and 44 const int64_t low_i64{low};
44 // unsigned numbers in two-complement representation. Thus, although 45 return rtc::dchecked_cast<int32_t>(
45 // high - low might be negative as an int, it is the correct difference 46 Rand(rtc::dchecked_cast<uint32_t>(high - low_i64)) + low_i64);
46 // when interpreted as an unsigned.
47 return Rand(high - low) + low;
48 } 47 }
49 48
50 template <> 49 template <>
51 float Random::Rand<float>() { 50 float Random::Rand<float>() {
52 double result = NextOutput() - 1; 51 double result = NextOutput() - 1;
53 result = result / 0xFFFFFFFFFFFFFFFEull; 52 result = result / 0xFFFFFFFFFFFFFFFEull;
54 return static_cast<float>(result); 53 return static_cast<float>(result);
55 } 54 }
56 55
57 template <> 56 template <>
(...skipping 19 matching lines...) Expand all
77 double u2 = static_cast<double>(NextOutput()) / 0xFFFFFFFFFFFFFFFFull; 76 double u2 = static_cast<double>(NextOutput()) / 0xFFFFFFFFFFFFFFFFull;
78 return mean + standard_deviation * sqrt(-2 * log(u1)) * cos(2 * kPi * u2); 77 return mean + standard_deviation * sqrt(-2 * log(u1)) * cos(2 * kPi * u2);
79 } 78 }
80 79
81 double Random::Exponential(double lambda) { 80 double Random::Exponential(double lambda) {
82 double uniform = Rand<double>(); 81 double uniform = Rand<double>();
83 return -log(uniform) / lambda; 82 return -log(uniform) / lambda;
84 } 83 }
85 84
86 } // namespace webrtc 85 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/rtc_base/ipaddress_unittest.cc ('k') | webrtc/rtc_base/random_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698