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

Side by Side Diff: base/rand_util.cc

Issue 1467003002: Switch to static_assert in base/. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: / Created 5 years 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 | « base/observer_list_threadsafe.h ('k') | base/strings/safe_sprintf.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 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/rand_util.h" 5 #include "base/rand_util.h"
6 6
7 #include <math.h> 7 #include <math.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <algorithm> 10 #include <algorithm>
(...skipping 20 matching lines...) Expand all
31 double RandDouble() { 31 double RandDouble() {
32 return BitsToOpenEndedUnitInterval(base::RandUint64()); 32 return BitsToOpenEndedUnitInterval(base::RandUint64());
33 } 33 }
34 34
35 double BitsToOpenEndedUnitInterval(uint64_t bits) { 35 double BitsToOpenEndedUnitInterval(uint64_t bits) {
36 // We try to get maximum precision by masking out as many bits as will fit 36 // We try to get maximum precision by masking out as many bits as will fit
37 // in the target type's mantissa, and raising it to an appropriate power to 37 // in the target type's mantissa, and raising it to an appropriate power to
38 // produce output in the range [0, 1). For IEEE 754 doubles, the mantissa 38 // produce output in the range [0, 1). For IEEE 754 doubles, the mantissa
39 // is expected to accommodate 53 bits. 39 // is expected to accommodate 53 bits.
40 40
41 COMPILE_ASSERT(std::numeric_limits<double>::radix == 2, otherwise_use_scalbn); 41 static_assert(std::numeric_limits<double>::radix == 2,
42 "otherwise use scalbn");
42 static const int kBits = std::numeric_limits<double>::digits; 43 static const int kBits = std::numeric_limits<double>::digits;
43 uint64_t random_bits = bits & ((UINT64_C(1) << kBits) - 1); 44 uint64_t random_bits = bits & ((UINT64_C(1) << kBits) - 1);
44 double result = ldexp(static_cast<double>(random_bits), -1 * kBits); 45 double result = ldexp(static_cast<double>(random_bits), -1 * kBits);
45 DCHECK_GE(result, 0.0); 46 DCHECK_GE(result, 0.0);
46 DCHECK_LT(result, 1.0); 47 DCHECK_LT(result, 1.0);
47 return result; 48 return result;
48 } 49 }
49 50
50 uint64_t RandGenerator(uint64_t range) { 51 uint64_t RandGenerator(uint64_t range) {
51 DCHECK_GT(range, 0u); 52 DCHECK_GT(range, 0u);
(...skipping 13 matching lines...) Expand all
65 } 66 }
66 67
67 std::string RandBytesAsString(size_t length) { 68 std::string RandBytesAsString(size_t length) {
68 DCHECK_GT(length, 0u); 69 DCHECK_GT(length, 0u);
69 std::string result; 70 std::string result;
70 RandBytes(WriteInto(&result, length + 1), length); 71 RandBytes(WriteInto(&result, length + 1), length);
71 return result; 72 return result;
72 } 73 }
73 74
74 } // namespace base 75 } // namespace base
OLDNEW
« no previous file with comments | « base/observer_list_threadsafe.h ('k') | base/strings/safe_sprintf.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698