|
|
|
// Copyright 2017 The Abseil Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#ifndef ABSL_RANDOM_INTERNAL_FAST_UNIFORM_BITS_H_
|
|
|
|
#define ABSL_RANDOM_INTERNAL_FAST_UNIFORM_BITS_H_
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <limits>
|
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
#include "absl/base/config.h"
|
Export of internal Abseil changes
--
4833151c207fac9f57a735efe6d5db4c83368415 by Gennadiy Rozental <rogeeff@google.com>:
Import of CCTZ from GitHub.
PiperOrigin-RevId: 320398694
--
a1becb36b223230f0a45f204a5fb33b83d2deffe by Gennadiy Rozental <rogeeff@google.com>:
Update CMakeLists.txt
Import of https://github.com/abseil/abseil-cpp/pull/737
PiperOrigin-RevId: 320391906
--
b529c45856fe7a3447f1f3259286d57e13b1f292 by Abseil Team <absl-team@google.com>:
Improves a comment about use of absl::Condition.
PiperOrigin-RevId: 320384329
--
c7b1dacda2739c10dc1ccbfb56b07ed7fe2464a4 by Laramie Leavitt <lar@google.com>:
Improve FastUniformBits performance for std::minstd_rand.
The rejection algorithm was too pessimistic before, and not in line with the [rand.adapt.ibits]. Specifically, when sampling from an URBG with a non power of 2 range, FastUniformBits constructed a rejection threshold with a power-of-2 range that was too restrictive.
For example, minstd_rand has a range of
[1, 2147483646], which has a range of 2145386495, or about 30.999 bits.
Before FastUniformBits rejected values between 1<<30 and 2145386495, which includes approximately 50% of the generated values. However, since a minimum of 3 calls are required to generate a full 64-bit value from an entropy pool of 30.9 bits, the correct value for rejection sampling is the range value which masks 21 (0x7fe00000) or 22 bits and rejects values greater than that. This reduces the probability of rejecting a sample to about 0.1%
NOTE: Abseil random does not guarantee sequence stability over time, and this is expected to change sequences in some cases.
PiperOrigin-RevId: 320285836
--
15800a39557a07dd52e0add66a0ab67aed00590b by Gennadiy Rozental <rogeeff@google.com>:
Internal change.
PiperOrigin-RevId: 320220913
--
ef39348360873f6d19669755fe0b5d09a945a501 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320181729
--
4f9f6ef8034a24da1832e4c838c72f80fc2ea062 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176084
--
6bfc8008462801657d231585bd5c37fc18bb25b6 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176070
--
b35b055ab1f41e6056031ff0641cabab23530027 by Abseil Team <absl-team@google.com>:
Disabling using header module as well as building one for randen_hwaes_impl
PiperOrigin-RevId: 320024299
GitOrigin-RevId: 4833151c207fac9f57a735efe6d5db4c83368415
Change-Id: I9cf102dbf46ed07752a508b7cda3ab3858857d0d
4 years ago
|
|
|
#include "absl/meta/type_traits.h"
|
|
|
|
|
|
|
|
namespace absl {
|
|
|
|
ABSL_NAMESPACE_BEGIN
|
|
|
|
namespace random_internal {
|
|
|
|
// Returns true if the input value is zero or a power of two. Useful for
|
|
|
|
// determining if the range of output values in a URBG
|
|
|
|
template <typename UIntType>
|
|
|
|
constexpr bool IsPowerOfTwoOrZero(UIntType n) {
|
|
|
|
return (n == 0) || ((n & (n - 1)) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Computes the length of the range of values producible by the URBG, or returns
|
|
|
|
// zero if that would encompass the entire range of representable values in
|
|
|
|
// URBG::result_type.
|
|
|
|
template <typename URBG>
|
|
|
|
constexpr typename URBG::result_type RangeSize() {
|
|
|
|
using result_type = typename URBG::result_type;
|
Export of internal Abseil changes
--
4833151c207fac9f57a735efe6d5db4c83368415 by Gennadiy Rozental <rogeeff@google.com>:
Import of CCTZ from GitHub.
PiperOrigin-RevId: 320398694
--
a1becb36b223230f0a45f204a5fb33b83d2deffe by Gennadiy Rozental <rogeeff@google.com>:
Update CMakeLists.txt
Import of https://github.com/abseil/abseil-cpp/pull/737
PiperOrigin-RevId: 320391906
--
b529c45856fe7a3447f1f3259286d57e13b1f292 by Abseil Team <absl-team@google.com>:
Improves a comment about use of absl::Condition.
PiperOrigin-RevId: 320384329
--
c7b1dacda2739c10dc1ccbfb56b07ed7fe2464a4 by Laramie Leavitt <lar@google.com>:
Improve FastUniformBits performance for std::minstd_rand.
The rejection algorithm was too pessimistic before, and not in line with the [rand.adapt.ibits]. Specifically, when sampling from an URBG with a non power of 2 range, FastUniformBits constructed a rejection threshold with a power-of-2 range that was too restrictive.
For example, minstd_rand has a range of
[1, 2147483646], which has a range of 2145386495, or about 30.999 bits.
Before FastUniformBits rejected values between 1<<30 and 2145386495, which includes approximately 50% of the generated values. However, since a minimum of 3 calls are required to generate a full 64-bit value from an entropy pool of 30.9 bits, the correct value for rejection sampling is the range value which masks 21 (0x7fe00000) or 22 bits and rejects values greater than that. This reduces the probability of rejecting a sample to about 0.1%
NOTE: Abseil random does not guarantee sequence stability over time, and this is expected to change sequences in some cases.
PiperOrigin-RevId: 320285836
--
15800a39557a07dd52e0add66a0ab67aed00590b by Gennadiy Rozental <rogeeff@google.com>:
Internal change.
PiperOrigin-RevId: 320220913
--
ef39348360873f6d19669755fe0b5d09a945a501 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320181729
--
4f9f6ef8034a24da1832e4c838c72f80fc2ea062 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176084
--
6bfc8008462801657d231585bd5c37fc18bb25b6 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176070
--
b35b055ab1f41e6056031ff0641cabab23530027 by Abseil Team <absl-team@google.com>:
Disabling using header module as well as building one for randen_hwaes_impl
PiperOrigin-RevId: 320024299
GitOrigin-RevId: 4833151c207fac9f57a735efe6d5db4c83368415
Change-Id: I9cf102dbf46ed07752a508b7cda3ab3858857d0d
4 years ago
|
|
|
static_assert((URBG::max)() != (URBG::min)(), "URBG range cannot be 0.");
|
|
|
|
return ((URBG::max)() == (std::numeric_limits<result_type>::max)() &&
|
|
|
|
(URBG::min)() == std::numeric_limits<result_type>::lowest())
|
|
|
|
? result_type{0}
|
Export of internal Abseil changes
--
4833151c207fac9f57a735efe6d5db4c83368415 by Gennadiy Rozental <rogeeff@google.com>:
Import of CCTZ from GitHub.
PiperOrigin-RevId: 320398694
--
a1becb36b223230f0a45f204a5fb33b83d2deffe by Gennadiy Rozental <rogeeff@google.com>:
Update CMakeLists.txt
Import of https://github.com/abseil/abseil-cpp/pull/737
PiperOrigin-RevId: 320391906
--
b529c45856fe7a3447f1f3259286d57e13b1f292 by Abseil Team <absl-team@google.com>:
Improves a comment about use of absl::Condition.
PiperOrigin-RevId: 320384329
--
c7b1dacda2739c10dc1ccbfb56b07ed7fe2464a4 by Laramie Leavitt <lar@google.com>:
Improve FastUniformBits performance for std::minstd_rand.
The rejection algorithm was too pessimistic before, and not in line with the [rand.adapt.ibits]. Specifically, when sampling from an URBG with a non power of 2 range, FastUniformBits constructed a rejection threshold with a power-of-2 range that was too restrictive.
For example, minstd_rand has a range of
[1, 2147483646], which has a range of 2145386495, or about 30.999 bits.
Before FastUniformBits rejected values between 1<<30 and 2145386495, which includes approximately 50% of the generated values. However, since a minimum of 3 calls are required to generate a full 64-bit value from an entropy pool of 30.9 bits, the correct value for rejection sampling is the range value which masks 21 (0x7fe00000) or 22 bits and rejects values greater than that. This reduces the probability of rejecting a sample to about 0.1%
NOTE: Abseil random does not guarantee sequence stability over time, and this is expected to change sequences in some cases.
PiperOrigin-RevId: 320285836
--
15800a39557a07dd52e0add66a0ab67aed00590b by Gennadiy Rozental <rogeeff@google.com>:
Internal change.
PiperOrigin-RevId: 320220913
--
ef39348360873f6d19669755fe0b5d09a945a501 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320181729
--
4f9f6ef8034a24da1832e4c838c72f80fc2ea062 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176084
--
6bfc8008462801657d231585bd5c37fc18bb25b6 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176070
--
b35b055ab1f41e6056031ff0641cabab23530027 by Abseil Team <absl-team@google.com>:
Disabling using header module as well as building one for randen_hwaes_impl
PiperOrigin-RevId: 320024299
GitOrigin-RevId: 4833151c207fac9f57a735efe6d5db4c83368415
Change-Id: I9cf102dbf46ed07752a508b7cda3ab3858857d0d
4 years ago
|
|
|
: ((URBG::max)() - (URBG::min)() + result_type{1});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Computes the floor of the log. (i.e., std::floor(std::log2(N));
|
|
|
|
template <typename UIntType>
|
|
|
|
constexpr UIntType IntegerLog2(UIntType n) {
|
Export of internal Abseil changes
--
4833151c207fac9f57a735efe6d5db4c83368415 by Gennadiy Rozental <rogeeff@google.com>:
Import of CCTZ from GitHub.
PiperOrigin-RevId: 320398694
--
a1becb36b223230f0a45f204a5fb33b83d2deffe by Gennadiy Rozental <rogeeff@google.com>:
Update CMakeLists.txt
Import of https://github.com/abseil/abseil-cpp/pull/737
PiperOrigin-RevId: 320391906
--
b529c45856fe7a3447f1f3259286d57e13b1f292 by Abseil Team <absl-team@google.com>:
Improves a comment about use of absl::Condition.
PiperOrigin-RevId: 320384329
--
c7b1dacda2739c10dc1ccbfb56b07ed7fe2464a4 by Laramie Leavitt <lar@google.com>:
Improve FastUniformBits performance for std::minstd_rand.
The rejection algorithm was too pessimistic before, and not in line with the [rand.adapt.ibits]. Specifically, when sampling from an URBG with a non power of 2 range, FastUniformBits constructed a rejection threshold with a power-of-2 range that was too restrictive.
For example, minstd_rand has a range of
[1, 2147483646], which has a range of 2145386495, or about 30.999 bits.
Before FastUniformBits rejected values between 1<<30 and 2145386495, which includes approximately 50% of the generated values. However, since a minimum of 3 calls are required to generate a full 64-bit value from an entropy pool of 30.9 bits, the correct value for rejection sampling is the range value which masks 21 (0x7fe00000) or 22 bits and rejects values greater than that. This reduces the probability of rejecting a sample to about 0.1%
NOTE: Abseil random does not guarantee sequence stability over time, and this is expected to change sequences in some cases.
PiperOrigin-RevId: 320285836
--
15800a39557a07dd52e0add66a0ab67aed00590b by Gennadiy Rozental <rogeeff@google.com>:
Internal change.
PiperOrigin-RevId: 320220913
--
ef39348360873f6d19669755fe0b5d09a945a501 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320181729
--
4f9f6ef8034a24da1832e4c838c72f80fc2ea062 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176084
--
6bfc8008462801657d231585bd5c37fc18bb25b6 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176070
--
b35b055ab1f41e6056031ff0641cabab23530027 by Abseil Team <absl-team@google.com>:
Disabling using header module as well as building one for randen_hwaes_impl
PiperOrigin-RevId: 320024299
GitOrigin-RevId: 4833151c207fac9f57a735efe6d5db4c83368415
Change-Id: I9cf102dbf46ed07752a508b7cda3ab3858857d0d
4 years ago
|
|
|
return (n <= 1) ? 0 : 1 + IntegerLog2(n >> 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the number of bits of randomness returned through
|
|
|
|
// `PowerOfTwoVariate(urbg)`.
|
|
|
|
template <typename URBG>
|
|
|
|
constexpr size_t NumBits() {
|
|
|
|
return RangeSize<URBG>() == 0
|
|
|
|
? std::numeric_limits<typename URBG::result_type>::digits
|
Export of internal Abseil changes
--
4833151c207fac9f57a735efe6d5db4c83368415 by Gennadiy Rozental <rogeeff@google.com>:
Import of CCTZ from GitHub.
PiperOrigin-RevId: 320398694
--
a1becb36b223230f0a45f204a5fb33b83d2deffe by Gennadiy Rozental <rogeeff@google.com>:
Update CMakeLists.txt
Import of https://github.com/abseil/abseil-cpp/pull/737
PiperOrigin-RevId: 320391906
--
b529c45856fe7a3447f1f3259286d57e13b1f292 by Abseil Team <absl-team@google.com>:
Improves a comment about use of absl::Condition.
PiperOrigin-RevId: 320384329
--
c7b1dacda2739c10dc1ccbfb56b07ed7fe2464a4 by Laramie Leavitt <lar@google.com>:
Improve FastUniformBits performance for std::minstd_rand.
The rejection algorithm was too pessimistic before, and not in line with the [rand.adapt.ibits]. Specifically, when sampling from an URBG with a non power of 2 range, FastUniformBits constructed a rejection threshold with a power-of-2 range that was too restrictive.
For example, minstd_rand has a range of
[1, 2147483646], which has a range of 2145386495, or about 30.999 bits.
Before FastUniformBits rejected values between 1<<30 and 2145386495, which includes approximately 50% of the generated values. However, since a minimum of 3 calls are required to generate a full 64-bit value from an entropy pool of 30.9 bits, the correct value for rejection sampling is the range value which masks 21 (0x7fe00000) or 22 bits and rejects values greater than that. This reduces the probability of rejecting a sample to about 0.1%
NOTE: Abseil random does not guarantee sequence stability over time, and this is expected to change sequences in some cases.
PiperOrigin-RevId: 320285836
--
15800a39557a07dd52e0add66a0ab67aed00590b by Gennadiy Rozental <rogeeff@google.com>:
Internal change.
PiperOrigin-RevId: 320220913
--
ef39348360873f6d19669755fe0b5d09a945a501 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320181729
--
4f9f6ef8034a24da1832e4c838c72f80fc2ea062 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176084
--
6bfc8008462801657d231585bd5c37fc18bb25b6 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176070
--
b35b055ab1f41e6056031ff0641cabab23530027 by Abseil Team <absl-team@google.com>:
Disabling using header module as well as building one for randen_hwaes_impl
PiperOrigin-RevId: 320024299
GitOrigin-RevId: 4833151c207fac9f57a735efe6d5db4c83368415
Change-Id: I9cf102dbf46ed07752a508b7cda3ab3858857d0d
4 years ago
|
|
|
: IntegerLog2(RangeSize<URBG>());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Given a shift value `n`, constructs a mask with exactly the low `n` bits set.
|
|
|
|
// If `n == 0`, all bits are set.
|
|
|
|
template <typename UIntType>
|
Export of internal Abseil changes
--
4833151c207fac9f57a735efe6d5db4c83368415 by Gennadiy Rozental <rogeeff@google.com>:
Import of CCTZ from GitHub.
PiperOrigin-RevId: 320398694
--
a1becb36b223230f0a45f204a5fb33b83d2deffe by Gennadiy Rozental <rogeeff@google.com>:
Update CMakeLists.txt
Import of https://github.com/abseil/abseil-cpp/pull/737
PiperOrigin-RevId: 320391906
--
b529c45856fe7a3447f1f3259286d57e13b1f292 by Abseil Team <absl-team@google.com>:
Improves a comment about use of absl::Condition.
PiperOrigin-RevId: 320384329
--
c7b1dacda2739c10dc1ccbfb56b07ed7fe2464a4 by Laramie Leavitt <lar@google.com>:
Improve FastUniformBits performance for std::minstd_rand.
The rejection algorithm was too pessimistic before, and not in line with the [rand.adapt.ibits]. Specifically, when sampling from an URBG with a non power of 2 range, FastUniformBits constructed a rejection threshold with a power-of-2 range that was too restrictive.
For example, minstd_rand has a range of
[1, 2147483646], which has a range of 2145386495, or about 30.999 bits.
Before FastUniformBits rejected values between 1<<30 and 2145386495, which includes approximately 50% of the generated values. However, since a minimum of 3 calls are required to generate a full 64-bit value from an entropy pool of 30.9 bits, the correct value for rejection sampling is the range value which masks 21 (0x7fe00000) or 22 bits and rejects values greater than that. This reduces the probability of rejecting a sample to about 0.1%
NOTE: Abseil random does not guarantee sequence stability over time, and this is expected to change sequences in some cases.
PiperOrigin-RevId: 320285836
--
15800a39557a07dd52e0add66a0ab67aed00590b by Gennadiy Rozental <rogeeff@google.com>:
Internal change.
PiperOrigin-RevId: 320220913
--
ef39348360873f6d19669755fe0b5d09a945a501 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320181729
--
4f9f6ef8034a24da1832e4c838c72f80fc2ea062 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176084
--
6bfc8008462801657d231585bd5c37fc18bb25b6 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176070
--
b35b055ab1f41e6056031ff0641cabab23530027 by Abseil Team <absl-team@google.com>:
Disabling using header module as well as building one for randen_hwaes_impl
PiperOrigin-RevId: 320024299
GitOrigin-RevId: 4833151c207fac9f57a735efe6d5db4c83368415
Change-Id: I9cf102dbf46ed07752a508b7cda3ab3858857d0d
4 years ago
|
|
|
constexpr UIntType MaskFromShift(size_t n) {
|
|
|
|
return ((n % std::numeric_limits<UIntType>::digits) == 0)
|
|
|
|
? ~UIntType{0}
|
|
|
|
: (UIntType{1} << n) - UIntType{1};
|
|
|
|
}
|
|
|
|
|
Export of internal Abseil changes
--
4833151c207fac9f57a735efe6d5db4c83368415 by Gennadiy Rozental <rogeeff@google.com>:
Import of CCTZ from GitHub.
PiperOrigin-RevId: 320398694
--
a1becb36b223230f0a45f204a5fb33b83d2deffe by Gennadiy Rozental <rogeeff@google.com>:
Update CMakeLists.txt
Import of https://github.com/abseil/abseil-cpp/pull/737
PiperOrigin-RevId: 320391906
--
b529c45856fe7a3447f1f3259286d57e13b1f292 by Abseil Team <absl-team@google.com>:
Improves a comment about use of absl::Condition.
PiperOrigin-RevId: 320384329
--
c7b1dacda2739c10dc1ccbfb56b07ed7fe2464a4 by Laramie Leavitt <lar@google.com>:
Improve FastUniformBits performance for std::minstd_rand.
The rejection algorithm was too pessimistic before, and not in line with the [rand.adapt.ibits]. Specifically, when sampling from an URBG with a non power of 2 range, FastUniformBits constructed a rejection threshold with a power-of-2 range that was too restrictive.
For example, minstd_rand has a range of
[1, 2147483646], which has a range of 2145386495, or about 30.999 bits.
Before FastUniformBits rejected values between 1<<30 and 2145386495, which includes approximately 50% of the generated values. However, since a minimum of 3 calls are required to generate a full 64-bit value from an entropy pool of 30.9 bits, the correct value for rejection sampling is the range value which masks 21 (0x7fe00000) or 22 bits and rejects values greater than that. This reduces the probability of rejecting a sample to about 0.1%
NOTE: Abseil random does not guarantee sequence stability over time, and this is expected to change sequences in some cases.
PiperOrigin-RevId: 320285836
--
15800a39557a07dd52e0add66a0ab67aed00590b by Gennadiy Rozental <rogeeff@google.com>:
Internal change.
PiperOrigin-RevId: 320220913
--
ef39348360873f6d19669755fe0b5d09a945a501 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320181729
--
4f9f6ef8034a24da1832e4c838c72f80fc2ea062 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176084
--
6bfc8008462801657d231585bd5c37fc18bb25b6 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176070
--
b35b055ab1f41e6056031ff0641cabab23530027 by Abseil Team <absl-team@google.com>:
Disabling using header module as well as building one for randen_hwaes_impl
PiperOrigin-RevId: 320024299
GitOrigin-RevId: 4833151c207fac9f57a735efe6d5db4c83368415
Change-Id: I9cf102dbf46ed07752a508b7cda3ab3858857d0d
4 years ago
|
|
|
// Tags used to dispatch FastUniformBits::generate to the simple or more complex
|
|
|
|
// entropy extraction algorithm.
|
|
|
|
struct SimplifiedLoopTag {};
|
|
|
|
struct RejectionLoopTag {};
|
|
|
|
|
|
|
|
// FastUniformBits implements a fast path to acquire uniform independent bits
|
|
|
|
// from a type which conforms to the [rand.req.urbg] concept.
|
|
|
|
// Parameterized by:
|
|
|
|
// `UIntType`: the result (output) type
|
|
|
|
//
|
|
|
|
// The std::independent_bits_engine [rand.adapt.ibits] adaptor can be
|
|
|
|
// instantiated from an existing generator through a copy or a move. It does
|
|
|
|
// not, however, facilitate the production of pseudorandom bits from an un-owned
|
|
|
|
// generator that will outlive the std::independent_bits_engine instance.
|
|
|
|
template <typename UIntType = uint64_t>
|
|
|
|
class FastUniformBits {
|
|
|
|
public:
|
|
|
|
using result_type = UIntType;
|
|
|
|
|
|
|
|
static constexpr result_type(min)() { return 0; }
|
|
|
|
static constexpr result_type(max)() {
|
|
|
|
return (std::numeric_limits<result_type>::max)();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename URBG>
|
|
|
|
result_type operator()(URBG& g); // NOLINT(runtime/references)
|
|
|
|
|
|
|
|
private:
|
|
|
|
static_assert(std::is_unsigned<UIntType>::value,
|
|
|
|
"Class-template FastUniformBits<> must be parameterized using "
|
|
|
|
"an unsigned type.");
|
|
|
|
|
|
|
|
// Generate() generates a random value, dispatched on whether
|
Export of internal Abseil changes
--
4833151c207fac9f57a735efe6d5db4c83368415 by Gennadiy Rozental <rogeeff@google.com>:
Import of CCTZ from GitHub.
PiperOrigin-RevId: 320398694
--
a1becb36b223230f0a45f204a5fb33b83d2deffe by Gennadiy Rozental <rogeeff@google.com>:
Update CMakeLists.txt
Import of https://github.com/abseil/abseil-cpp/pull/737
PiperOrigin-RevId: 320391906
--
b529c45856fe7a3447f1f3259286d57e13b1f292 by Abseil Team <absl-team@google.com>:
Improves a comment about use of absl::Condition.
PiperOrigin-RevId: 320384329
--
c7b1dacda2739c10dc1ccbfb56b07ed7fe2464a4 by Laramie Leavitt <lar@google.com>:
Improve FastUniformBits performance for std::minstd_rand.
The rejection algorithm was too pessimistic before, and not in line with the [rand.adapt.ibits]. Specifically, when sampling from an URBG with a non power of 2 range, FastUniformBits constructed a rejection threshold with a power-of-2 range that was too restrictive.
For example, minstd_rand has a range of
[1, 2147483646], which has a range of 2145386495, or about 30.999 bits.
Before FastUniformBits rejected values between 1<<30 and 2145386495, which includes approximately 50% of the generated values. However, since a minimum of 3 calls are required to generate a full 64-bit value from an entropy pool of 30.9 bits, the correct value for rejection sampling is the range value which masks 21 (0x7fe00000) or 22 bits and rejects values greater than that. This reduces the probability of rejecting a sample to about 0.1%
NOTE: Abseil random does not guarantee sequence stability over time, and this is expected to change sequences in some cases.
PiperOrigin-RevId: 320285836
--
15800a39557a07dd52e0add66a0ab67aed00590b by Gennadiy Rozental <rogeeff@google.com>:
Internal change.
PiperOrigin-RevId: 320220913
--
ef39348360873f6d19669755fe0b5d09a945a501 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320181729
--
4f9f6ef8034a24da1832e4c838c72f80fc2ea062 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176084
--
6bfc8008462801657d231585bd5c37fc18bb25b6 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176070
--
b35b055ab1f41e6056031ff0641cabab23530027 by Abseil Team <absl-team@google.com>:
Disabling using header module as well as building one for randen_hwaes_impl
PiperOrigin-RevId: 320024299
GitOrigin-RevId: 4833151c207fac9f57a735efe6d5db4c83368415
Change-Id: I9cf102dbf46ed07752a508b7cda3ab3858857d0d
4 years ago
|
|
|
// the underlying URBG must use rejection sampling to generate a value,
|
|
|
|
// or whether a simplified loop will suffice.
|
|
|
|
template <typename URBG>
|
|
|
|
result_type Generate(URBG& g, // NOLINT(runtime/references)
|
Export of internal Abseil changes
--
4833151c207fac9f57a735efe6d5db4c83368415 by Gennadiy Rozental <rogeeff@google.com>:
Import of CCTZ from GitHub.
PiperOrigin-RevId: 320398694
--
a1becb36b223230f0a45f204a5fb33b83d2deffe by Gennadiy Rozental <rogeeff@google.com>:
Update CMakeLists.txt
Import of https://github.com/abseil/abseil-cpp/pull/737
PiperOrigin-RevId: 320391906
--
b529c45856fe7a3447f1f3259286d57e13b1f292 by Abseil Team <absl-team@google.com>:
Improves a comment about use of absl::Condition.
PiperOrigin-RevId: 320384329
--
c7b1dacda2739c10dc1ccbfb56b07ed7fe2464a4 by Laramie Leavitt <lar@google.com>:
Improve FastUniformBits performance for std::minstd_rand.
The rejection algorithm was too pessimistic before, and not in line with the [rand.adapt.ibits]. Specifically, when sampling from an URBG with a non power of 2 range, FastUniformBits constructed a rejection threshold with a power-of-2 range that was too restrictive.
For example, minstd_rand has a range of
[1, 2147483646], which has a range of 2145386495, or about 30.999 bits.
Before FastUniformBits rejected values between 1<<30 and 2145386495, which includes approximately 50% of the generated values. However, since a minimum of 3 calls are required to generate a full 64-bit value from an entropy pool of 30.9 bits, the correct value for rejection sampling is the range value which masks 21 (0x7fe00000) or 22 bits and rejects values greater than that. This reduces the probability of rejecting a sample to about 0.1%
NOTE: Abseil random does not guarantee sequence stability over time, and this is expected to change sequences in some cases.
PiperOrigin-RevId: 320285836
--
15800a39557a07dd52e0add66a0ab67aed00590b by Gennadiy Rozental <rogeeff@google.com>:
Internal change.
PiperOrigin-RevId: 320220913
--
ef39348360873f6d19669755fe0b5d09a945a501 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320181729
--
4f9f6ef8034a24da1832e4c838c72f80fc2ea062 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176084
--
6bfc8008462801657d231585bd5c37fc18bb25b6 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176070
--
b35b055ab1f41e6056031ff0641cabab23530027 by Abseil Team <absl-team@google.com>:
Disabling using header module as well as building one for randen_hwaes_impl
PiperOrigin-RevId: 320024299
GitOrigin-RevId: 4833151c207fac9f57a735efe6d5db4c83368415
Change-Id: I9cf102dbf46ed07752a508b7cda3ab3858857d0d
4 years ago
|
|
|
SimplifiedLoopTag);
|
|
|
|
|
|
|
|
template <typename URBG>
|
|
|
|
result_type Generate(URBG& g, // NOLINT(runtime/references)
|
Export of internal Abseil changes
--
4833151c207fac9f57a735efe6d5db4c83368415 by Gennadiy Rozental <rogeeff@google.com>:
Import of CCTZ from GitHub.
PiperOrigin-RevId: 320398694
--
a1becb36b223230f0a45f204a5fb33b83d2deffe by Gennadiy Rozental <rogeeff@google.com>:
Update CMakeLists.txt
Import of https://github.com/abseil/abseil-cpp/pull/737
PiperOrigin-RevId: 320391906
--
b529c45856fe7a3447f1f3259286d57e13b1f292 by Abseil Team <absl-team@google.com>:
Improves a comment about use of absl::Condition.
PiperOrigin-RevId: 320384329
--
c7b1dacda2739c10dc1ccbfb56b07ed7fe2464a4 by Laramie Leavitt <lar@google.com>:
Improve FastUniformBits performance for std::minstd_rand.
The rejection algorithm was too pessimistic before, and not in line with the [rand.adapt.ibits]. Specifically, when sampling from an URBG with a non power of 2 range, FastUniformBits constructed a rejection threshold with a power-of-2 range that was too restrictive.
For example, minstd_rand has a range of
[1, 2147483646], which has a range of 2145386495, or about 30.999 bits.
Before FastUniformBits rejected values between 1<<30 and 2145386495, which includes approximately 50% of the generated values. However, since a minimum of 3 calls are required to generate a full 64-bit value from an entropy pool of 30.9 bits, the correct value for rejection sampling is the range value which masks 21 (0x7fe00000) or 22 bits and rejects values greater than that. This reduces the probability of rejecting a sample to about 0.1%
NOTE: Abseil random does not guarantee sequence stability over time, and this is expected to change sequences in some cases.
PiperOrigin-RevId: 320285836
--
15800a39557a07dd52e0add66a0ab67aed00590b by Gennadiy Rozental <rogeeff@google.com>:
Internal change.
PiperOrigin-RevId: 320220913
--
ef39348360873f6d19669755fe0b5d09a945a501 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320181729
--
4f9f6ef8034a24da1832e4c838c72f80fc2ea062 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176084
--
6bfc8008462801657d231585bd5c37fc18bb25b6 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176070
--
b35b055ab1f41e6056031ff0641cabab23530027 by Abseil Team <absl-team@google.com>:
Disabling using header module as well as building one for randen_hwaes_impl
PiperOrigin-RevId: 320024299
GitOrigin-RevId: 4833151c207fac9f57a735efe6d5db4c83368415
Change-Id: I9cf102dbf46ed07752a508b7cda3ab3858857d0d
4 years ago
|
|
|
RejectionLoopTag);
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename UIntType>
|
|
|
|
template <typename URBG>
|
|
|
|
typename FastUniformBits<UIntType>::result_type
|
|
|
|
FastUniformBits<UIntType>::operator()(URBG& g) { // NOLINT(runtime/references)
|
|
|
|
// kRangeMask is the mask used when sampling variates from the URBG when the
|
|
|
|
// width of the URBG range is not a power of 2.
|
|
|
|
// Y = (2 ^ kRange) - 1
|
|
|
|
static_assert((URBG::max)() > (URBG::min)(),
|
|
|
|
"URBG::max and URBG::min may not be equal.");
|
Export of internal Abseil changes
--
4833151c207fac9f57a735efe6d5db4c83368415 by Gennadiy Rozental <rogeeff@google.com>:
Import of CCTZ from GitHub.
PiperOrigin-RevId: 320398694
--
a1becb36b223230f0a45f204a5fb33b83d2deffe by Gennadiy Rozental <rogeeff@google.com>:
Update CMakeLists.txt
Import of https://github.com/abseil/abseil-cpp/pull/737
PiperOrigin-RevId: 320391906
--
b529c45856fe7a3447f1f3259286d57e13b1f292 by Abseil Team <absl-team@google.com>:
Improves a comment about use of absl::Condition.
PiperOrigin-RevId: 320384329
--
c7b1dacda2739c10dc1ccbfb56b07ed7fe2464a4 by Laramie Leavitt <lar@google.com>:
Improve FastUniformBits performance for std::minstd_rand.
The rejection algorithm was too pessimistic before, and not in line with the [rand.adapt.ibits]. Specifically, when sampling from an URBG with a non power of 2 range, FastUniformBits constructed a rejection threshold with a power-of-2 range that was too restrictive.
For example, minstd_rand has a range of
[1, 2147483646], which has a range of 2145386495, or about 30.999 bits.
Before FastUniformBits rejected values between 1<<30 and 2145386495, which includes approximately 50% of the generated values. However, since a minimum of 3 calls are required to generate a full 64-bit value from an entropy pool of 30.9 bits, the correct value for rejection sampling is the range value which masks 21 (0x7fe00000) or 22 bits and rejects values greater than that. This reduces the probability of rejecting a sample to about 0.1%
NOTE: Abseil random does not guarantee sequence stability over time, and this is expected to change sequences in some cases.
PiperOrigin-RevId: 320285836
--
15800a39557a07dd52e0add66a0ab67aed00590b by Gennadiy Rozental <rogeeff@google.com>:
Internal change.
PiperOrigin-RevId: 320220913
--
ef39348360873f6d19669755fe0b5d09a945a501 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320181729
--
4f9f6ef8034a24da1832e4c838c72f80fc2ea062 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176084
--
6bfc8008462801657d231585bd5c37fc18bb25b6 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176070
--
b35b055ab1f41e6056031ff0641cabab23530027 by Abseil Team <absl-team@google.com>:
Disabling using header module as well as building one for randen_hwaes_impl
PiperOrigin-RevId: 320024299
GitOrigin-RevId: 4833151c207fac9f57a735efe6d5db4c83368415
Change-Id: I9cf102dbf46ed07752a508b7cda3ab3858857d0d
4 years ago
|
|
|
|
|
|
|
using tag = absl::conditional_t<IsPowerOfTwoOrZero(RangeSize<URBG>()),
|
|
|
|
SimplifiedLoopTag, RejectionLoopTag>;
|
|
|
|
return Generate(g, tag{});
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename UIntType>
|
|
|
|
template <typename URBG>
|
|
|
|
typename FastUniformBits<UIntType>::result_type
|
|
|
|
FastUniformBits<UIntType>::Generate(URBG& g, // NOLINT(runtime/references)
|
Export of internal Abseil changes
--
4833151c207fac9f57a735efe6d5db4c83368415 by Gennadiy Rozental <rogeeff@google.com>:
Import of CCTZ from GitHub.
PiperOrigin-RevId: 320398694
--
a1becb36b223230f0a45f204a5fb33b83d2deffe by Gennadiy Rozental <rogeeff@google.com>:
Update CMakeLists.txt
Import of https://github.com/abseil/abseil-cpp/pull/737
PiperOrigin-RevId: 320391906
--
b529c45856fe7a3447f1f3259286d57e13b1f292 by Abseil Team <absl-team@google.com>:
Improves a comment about use of absl::Condition.
PiperOrigin-RevId: 320384329
--
c7b1dacda2739c10dc1ccbfb56b07ed7fe2464a4 by Laramie Leavitt <lar@google.com>:
Improve FastUniformBits performance for std::minstd_rand.
The rejection algorithm was too pessimistic before, and not in line with the [rand.adapt.ibits]. Specifically, when sampling from an URBG with a non power of 2 range, FastUniformBits constructed a rejection threshold with a power-of-2 range that was too restrictive.
For example, minstd_rand has a range of
[1, 2147483646], which has a range of 2145386495, or about 30.999 bits.
Before FastUniformBits rejected values between 1<<30 and 2145386495, which includes approximately 50% of the generated values. However, since a minimum of 3 calls are required to generate a full 64-bit value from an entropy pool of 30.9 bits, the correct value for rejection sampling is the range value which masks 21 (0x7fe00000) or 22 bits and rejects values greater than that. This reduces the probability of rejecting a sample to about 0.1%
NOTE: Abseil random does not guarantee sequence stability over time, and this is expected to change sequences in some cases.
PiperOrigin-RevId: 320285836
--
15800a39557a07dd52e0add66a0ab67aed00590b by Gennadiy Rozental <rogeeff@google.com>:
Internal change.
PiperOrigin-RevId: 320220913
--
ef39348360873f6d19669755fe0b5d09a945a501 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320181729
--
4f9f6ef8034a24da1832e4c838c72f80fc2ea062 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176084
--
6bfc8008462801657d231585bd5c37fc18bb25b6 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176070
--
b35b055ab1f41e6056031ff0641cabab23530027 by Abseil Team <absl-team@google.com>:
Disabling using header module as well as building one for randen_hwaes_impl
PiperOrigin-RevId: 320024299
GitOrigin-RevId: 4833151c207fac9f57a735efe6d5db4c83368415
Change-Id: I9cf102dbf46ed07752a508b7cda3ab3858857d0d
4 years ago
|
|
|
SimplifiedLoopTag) {
|
|
|
|
// The simplified version of FastUniformBits works only on URBGs that have
|
|
|
|
// a range that is a power of 2. In this case we simply loop and shift without
|
|
|
|
// attempting to balance the bits across calls.
|
|
|
|
static_assert(IsPowerOfTwoOrZero(RangeSize<URBG>()),
|
|
|
|
"incorrect Generate tag for URBG instance");
|
|
|
|
|
|
|
|
static constexpr size_t kResultBits =
|
|
|
|
std::numeric_limits<result_type>::digits;
|
|
|
|
static constexpr size_t kUrbgBits = NumBits<URBG>();
|
|
|
|
static constexpr size_t kIters =
|
|
|
|
(kResultBits / kUrbgBits) + (kResultBits % kUrbgBits != 0);
|
|
|
|
static constexpr size_t kShift = (kIters == 1) ? 0 : kUrbgBits;
|
|
|
|
static constexpr auto kMin = (URBG::min)();
|
|
|
|
|
Export of internal Abseil changes
--
4833151c207fac9f57a735efe6d5db4c83368415 by Gennadiy Rozental <rogeeff@google.com>:
Import of CCTZ from GitHub.
PiperOrigin-RevId: 320398694
--
a1becb36b223230f0a45f204a5fb33b83d2deffe by Gennadiy Rozental <rogeeff@google.com>:
Update CMakeLists.txt
Import of https://github.com/abseil/abseil-cpp/pull/737
PiperOrigin-RevId: 320391906
--
b529c45856fe7a3447f1f3259286d57e13b1f292 by Abseil Team <absl-team@google.com>:
Improves a comment about use of absl::Condition.
PiperOrigin-RevId: 320384329
--
c7b1dacda2739c10dc1ccbfb56b07ed7fe2464a4 by Laramie Leavitt <lar@google.com>:
Improve FastUniformBits performance for std::minstd_rand.
The rejection algorithm was too pessimistic before, and not in line with the [rand.adapt.ibits]. Specifically, when sampling from an URBG with a non power of 2 range, FastUniformBits constructed a rejection threshold with a power-of-2 range that was too restrictive.
For example, minstd_rand has a range of
[1, 2147483646], which has a range of 2145386495, or about 30.999 bits.
Before FastUniformBits rejected values between 1<<30 and 2145386495, which includes approximately 50% of the generated values. However, since a minimum of 3 calls are required to generate a full 64-bit value from an entropy pool of 30.9 bits, the correct value for rejection sampling is the range value which masks 21 (0x7fe00000) or 22 bits and rejects values greater than that. This reduces the probability of rejecting a sample to about 0.1%
NOTE: Abseil random does not guarantee sequence stability over time, and this is expected to change sequences in some cases.
PiperOrigin-RevId: 320285836
--
15800a39557a07dd52e0add66a0ab67aed00590b by Gennadiy Rozental <rogeeff@google.com>:
Internal change.
PiperOrigin-RevId: 320220913
--
ef39348360873f6d19669755fe0b5d09a945a501 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320181729
--
4f9f6ef8034a24da1832e4c838c72f80fc2ea062 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176084
--
6bfc8008462801657d231585bd5c37fc18bb25b6 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176070
--
b35b055ab1f41e6056031ff0641cabab23530027 by Abseil Team <absl-team@google.com>:
Disabling using header module as well as building one for randen_hwaes_impl
PiperOrigin-RevId: 320024299
GitOrigin-RevId: 4833151c207fac9f57a735efe6d5db4c83368415
Change-Id: I9cf102dbf46ed07752a508b7cda3ab3858857d0d
4 years ago
|
|
|
result_type r = static_cast<result_type>(g() - kMin);
|
|
|
|
for (size_t n = 1; n < kIters; ++n) {
|
|
|
|
r = (r << kShift) + static_cast<result_type>(g() - kMin);
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename UIntType>
|
|
|
|
template <typename URBG>
|
|
|
|
typename FastUniformBits<UIntType>::result_type
|
|
|
|
FastUniformBits<UIntType>::Generate(URBG& g, // NOLINT(runtime/references)
|
Export of internal Abseil changes
--
4833151c207fac9f57a735efe6d5db4c83368415 by Gennadiy Rozental <rogeeff@google.com>:
Import of CCTZ from GitHub.
PiperOrigin-RevId: 320398694
--
a1becb36b223230f0a45f204a5fb33b83d2deffe by Gennadiy Rozental <rogeeff@google.com>:
Update CMakeLists.txt
Import of https://github.com/abseil/abseil-cpp/pull/737
PiperOrigin-RevId: 320391906
--
b529c45856fe7a3447f1f3259286d57e13b1f292 by Abseil Team <absl-team@google.com>:
Improves a comment about use of absl::Condition.
PiperOrigin-RevId: 320384329
--
c7b1dacda2739c10dc1ccbfb56b07ed7fe2464a4 by Laramie Leavitt <lar@google.com>:
Improve FastUniformBits performance for std::minstd_rand.
The rejection algorithm was too pessimistic before, and not in line with the [rand.adapt.ibits]. Specifically, when sampling from an URBG with a non power of 2 range, FastUniformBits constructed a rejection threshold with a power-of-2 range that was too restrictive.
For example, minstd_rand has a range of
[1, 2147483646], which has a range of 2145386495, or about 30.999 bits.
Before FastUniformBits rejected values between 1<<30 and 2145386495, which includes approximately 50% of the generated values. However, since a minimum of 3 calls are required to generate a full 64-bit value from an entropy pool of 30.9 bits, the correct value for rejection sampling is the range value which masks 21 (0x7fe00000) or 22 bits and rejects values greater than that. This reduces the probability of rejecting a sample to about 0.1%
NOTE: Abseil random does not guarantee sequence stability over time, and this is expected to change sequences in some cases.
PiperOrigin-RevId: 320285836
--
15800a39557a07dd52e0add66a0ab67aed00590b by Gennadiy Rozental <rogeeff@google.com>:
Internal change.
PiperOrigin-RevId: 320220913
--
ef39348360873f6d19669755fe0b5d09a945a501 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320181729
--
4f9f6ef8034a24da1832e4c838c72f80fc2ea062 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176084
--
6bfc8008462801657d231585bd5c37fc18bb25b6 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176070
--
b35b055ab1f41e6056031ff0641cabab23530027 by Abseil Team <absl-team@google.com>:
Disabling using header module as well as building one for randen_hwaes_impl
PiperOrigin-RevId: 320024299
GitOrigin-RevId: 4833151c207fac9f57a735efe6d5db4c83368415
Change-Id: I9cf102dbf46ed07752a508b7cda3ab3858857d0d
4 years ago
|
|
|
RejectionLoopTag) {
|
|
|
|
static_assert(!IsPowerOfTwoOrZero(RangeSize<URBG>()),
|
|
|
|
"incorrect Generate tag for URBG instance");
|
|
|
|
using urbg_result_type = typename URBG::result_type;
|
|
|
|
|
|
|
|
// See [rand.adapt.ibits] for more details on the constants calculated below.
|
|
|
|
//
|
|
|
|
// It is preferable to use roughly the same number of bits from each generator
|
|
|
|
// call, however this is only possible when the number of bits provided by the
|
|
|
|
// URBG is a divisor of the number of bits in `result_type`. In all other
|
|
|
|
// cases, the number of bits used cannot always be the same, but it can be
|
|
|
|
// guaranteed to be off by at most 1. Thus we run two loops, one with a
|
|
|
|
// smaller bit-width size (`kSmallWidth`) and one with a larger width size
|
|
|
|
// (satisfying `kLargeWidth == kSmallWidth + 1`). The loops are run
|
|
|
|
// `kSmallIters` and `kLargeIters` times respectively such
|
|
|
|
// that
|
|
|
|
//
|
Export of internal Abseil changes
--
4833151c207fac9f57a735efe6d5db4c83368415 by Gennadiy Rozental <rogeeff@google.com>:
Import of CCTZ from GitHub.
PiperOrigin-RevId: 320398694
--
a1becb36b223230f0a45f204a5fb33b83d2deffe by Gennadiy Rozental <rogeeff@google.com>:
Update CMakeLists.txt
Import of https://github.com/abseil/abseil-cpp/pull/737
PiperOrigin-RevId: 320391906
--
b529c45856fe7a3447f1f3259286d57e13b1f292 by Abseil Team <absl-team@google.com>:
Improves a comment about use of absl::Condition.
PiperOrigin-RevId: 320384329
--
c7b1dacda2739c10dc1ccbfb56b07ed7fe2464a4 by Laramie Leavitt <lar@google.com>:
Improve FastUniformBits performance for std::minstd_rand.
The rejection algorithm was too pessimistic before, and not in line with the [rand.adapt.ibits]. Specifically, when sampling from an URBG with a non power of 2 range, FastUniformBits constructed a rejection threshold with a power-of-2 range that was too restrictive.
For example, minstd_rand has a range of
[1, 2147483646], which has a range of 2145386495, or about 30.999 bits.
Before FastUniformBits rejected values between 1<<30 and 2145386495, which includes approximately 50% of the generated values. However, since a minimum of 3 calls are required to generate a full 64-bit value from an entropy pool of 30.9 bits, the correct value for rejection sampling is the range value which masks 21 (0x7fe00000) or 22 bits and rejects values greater than that. This reduces the probability of rejecting a sample to about 0.1%
NOTE: Abseil random does not guarantee sequence stability over time, and this is expected to change sequences in some cases.
PiperOrigin-RevId: 320285836
--
15800a39557a07dd52e0add66a0ab67aed00590b by Gennadiy Rozental <rogeeff@google.com>:
Internal change.
PiperOrigin-RevId: 320220913
--
ef39348360873f6d19669755fe0b5d09a945a501 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320181729
--
4f9f6ef8034a24da1832e4c838c72f80fc2ea062 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176084
--
6bfc8008462801657d231585bd5c37fc18bb25b6 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176070
--
b35b055ab1f41e6056031ff0641cabab23530027 by Abseil Team <absl-team@google.com>:
Disabling using header module as well as building one for randen_hwaes_impl
PiperOrigin-RevId: 320024299
GitOrigin-RevId: 4833151c207fac9f57a735efe6d5db4c83368415
Change-Id: I9cf102dbf46ed07752a508b7cda3ab3858857d0d
4 years ago
|
|
|
// `kResultBits == kSmallIters * kSmallBits
|
|
|
|
// + kLargeIters * kLargeBits`
|
|
|
|
//
|
Export of internal Abseil changes
--
4833151c207fac9f57a735efe6d5db4c83368415 by Gennadiy Rozental <rogeeff@google.com>:
Import of CCTZ from GitHub.
PiperOrigin-RevId: 320398694
--
a1becb36b223230f0a45f204a5fb33b83d2deffe by Gennadiy Rozental <rogeeff@google.com>:
Update CMakeLists.txt
Import of https://github.com/abseil/abseil-cpp/pull/737
PiperOrigin-RevId: 320391906
--
b529c45856fe7a3447f1f3259286d57e13b1f292 by Abseil Team <absl-team@google.com>:
Improves a comment about use of absl::Condition.
PiperOrigin-RevId: 320384329
--
c7b1dacda2739c10dc1ccbfb56b07ed7fe2464a4 by Laramie Leavitt <lar@google.com>:
Improve FastUniformBits performance for std::minstd_rand.
The rejection algorithm was too pessimistic before, and not in line with the [rand.adapt.ibits]. Specifically, when sampling from an URBG with a non power of 2 range, FastUniformBits constructed a rejection threshold with a power-of-2 range that was too restrictive.
For example, minstd_rand has a range of
[1, 2147483646], which has a range of 2145386495, or about 30.999 bits.
Before FastUniformBits rejected values between 1<<30 and 2145386495, which includes approximately 50% of the generated values. However, since a minimum of 3 calls are required to generate a full 64-bit value from an entropy pool of 30.9 bits, the correct value for rejection sampling is the range value which masks 21 (0x7fe00000) or 22 bits and rejects values greater than that. This reduces the probability of rejecting a sample to about 0.1%
NOTE: Abseil random does not guarantee sequence stability over time, and this is expected to change sequences in some cases.
PiperOrigin-RevId: 320285836
--
15800a39557a07dd52e0add66a0ab67aed00590b by Gennadiy Rozental <rogeeff@google.com>:
Internal change.
PiperOrigin-RevId: 320220913
--
ef39348360873f6d19669755fe0b5d09a945a501 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320181729
--
4f9f6ef8034a24da1832e4c838c72f80fc2ea062 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176084
--
6bfc8008462801657d231585bd5c37fc18bb25b6 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176070
--
b35b055ab1f41e6056031ff0641cabab23530027 by Abseil Team <absl-team@google.com>:
Disabling using header module as well as building one for randen_hwaes_impl
PiperOrigin-RevId: 320024299
GitOrigin-RevId: 4833151c207fac9f57a735efe6d5db4c83368415
Change-Id: I9cf102dbf46ed07752a508b7cda3ab3858857d0d
4 years ago
|
|
|
// where `kResultBits` is the total number of bits in `result_type`.
|
|
|
|
//
|
Export of internal Abseil changes
--
4833151c207fac9f57a735efe6d5db4c83368415 by Gennadiy Rozental <rogeeff@google.com>:
Import of CCTZ from GitHub.
PiperOrigin-RevId: 320398694
--
a1becb36b223230f0a45f204a5fb33b83d2deffe by Gennadiy Rozental <rogeeff@google.com>:
Update CMakeLists.txt
Import of https://github.com/abseil/abseil-cpp/pull/737
PiperOrigin-RevId: 320391906
--
b529c45856fe7a3447f1f3259286d57e13b1f292 by Abseil Team <absl-team@google.com>:
Improves a comment about use of absl::Condition.
PiperOrigin-RevId: 320384329
--
c7b1dacda2739c10dc1ccbfb56b07ed7fe2464a4 by Laramie Leavitt <lar@google.com>:
Improve FastUniformBits performance for std::minstd_rand.
The rejection algorithm was too pessimistic before, and not in line with the [rand.adapt.ibits]. Specifically, when sampling from an URBG with a non power of 2 range, FastUniformBits constructed a rejection threshold with a power-of-2 range that was too restrictive.
For example, minstd_rand has a range of
[1, 2147483646], which has a range of 2145386495, or about 30.999 bits.
Before FastUniformBits rejected values between 1<<30 and 2145386495, which includes approximately 50% of the generated values. However, since a minimum of 3 calls are required to generate a full 64-bit value from an entropy pool of 30.9 bits, the correct value for rejection sampling is the range value which masks 21 (0x7fe00000) or 22 bits and rejects values greater than that. This reduces the probability of rejecting a sample to about 0.1%
NOTE: Abseil random does not guarantee sequence stability over time, and this is expected to change sequences in some cases.
PiperOrigin-RevId: 320285836
--
15800a39557a07dd52e0add66a0ab67aed00590b by Gennadiy Rozental <rogeeff@google.com>:
Internal change.
PiperOrigin-RevId: 320220913
--
ef39348360873f6d19669755fe0b5d09a945a501 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320181729
--
4f9f6ef8034a24da1832e4c838c72f80fc2ea062 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176084
--
6bfc8008462801657d231585bd5c37fc18bb25b6 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176070
--
b35b055ab1f41e6056031ff0641cabab23530027 by Abseil Team <absl-team@google.com>:
Disabling using header module as well as building one for randen_hwaes_impl
PiperOrigin-RevId: 320024299
GitOrigin-RevId: 4833151c207fac9f57a735efe6d5db4c83368415
Change-Id: I9cf102dbf46ed07752a508b7cda3ab3858857d0d
4 years ago
|
|
|
static constexpr size_t kResultBits =
|
|
|
|
std::numeric_limits<result_type>::digits; // w
|
|
|
|
static constexpr urbg_result_type kUrbgRange = RangeSize<URBG>(); // R
|
|
|
|
static constexpr size_t kUrbgBits = NumBits<URBG>(); // m
|
|
|
|
|
|
|
|
// compute the initial estimate of the bits used.
|
|
|
|
// [rand.adapt.ibits] 2 (c)
|
|
|
|
static constexpr size_t kA = // ceil(w/m)
|
|
|
|
(kResultBits / kUrbgBits) + ((kResultBits % kUrbgBits) != 0); // n'
|
|
|
|
|
|
|
|
static constexpr size_t kABits = kResultBits / kA; // w0'
|
|
|
|
static constexpr urbg_result_type kARejection =
|
|
|
|
((kUrbgRange >> kABits) << kABits); // y0'
|
|
|
|
|
|
|
|
// refine the selection to reduce the rejection frequency.
|
|
|
|
static constexpr size_t kTotalIters =
|
|
|
|
((kUrbgRange - kARejection) <= (kARejection / kA)) ? kA : (kA + 1); // n
|
|
|
|
|
|
|
|
// [rand.adapt.ibits] 2 (b)
|
|
|
|
static constexpr size_t kSmallIters =
|
|
|
|
kTotalIters - (kResultBits % kTotalIters); // n0
|
|
|
|
static constexpr size_t kSmallBits = kResultBits / kTotalIters; // w0
|
|
|
|
static constexpr urbg_result_type kSmallRejection =
|
|
|
|
((kUrbgRange >> kSmallBits) << kSmallBits); // y0
|
|
|
|
|
|
|
|
static constexpr size_t kLargeBits = kSmallBits + 1; // w0+1
|
|
|
|
static constexpr urbg_result_type kLargeRejection =
|
|
|
|
((kUrbgRange >> kLargeBits) << kLargeBits); // y1
|
|
|
|
|
|
|
|
//
|
Export of internal Abseil changes
--
4833151c207fac9f57a735efe6d5db4c83368415 by Gennadiy Rozental <rogeeff@google.com>:
Import of CCTZ from GitHub.
PiperOrigin-RevId: 320398694
--
a1becb36b223230f0a45f204a5fb33b83d2deffe by Gennadiy Rozental <rogeeff@google.com>:
Update CMakeLists.txt
Import of https://github.com/abseil/abseil-cpp/pull/737
PiperOrigin-RevId: 320391906
--
b529c45856fe7a3447f1f3259286d57e13b1f292 by Abseil Team <absl-team@google.com>:
Improves a comment about use of absl::Condition.
PiperOrigin-RevId: 320384329
--
c7b1dacda2739c10dc1ccbfb56b07ed7fe2464a4 by Laramie Leavitt <lar@google.com>:
Improve FastUniformBits performance for std::minstd_rand.
The rejection algorithm was too pessimistic before, and not in line with the [rand.adapt.ibits]. Specifically, when sampling from an URBG with a non power of 2 range, FastUniformBits constructed a rejection threshold with a power-of-2 range that was too restrictive.
For example, minstd_rand has a range of
[1, 2147483646], which has a range of 2145386495, or about 30.999 bits.
Before FastUniformBits rejected values between 1<<30 and 2145386495, which includes approximately 50% of the generated values. However, since a minimum of 3 calls are required to generate a full 64-bit value from an entropy pool of 30.9 bits, the correct value for rejection sampling is the range value which masks 21 (0x7fe00000) or 22 bits and rejects values greater than that. This reduces the probability of rejecting a sample to about 0.1%
NOTE: Abseil random does not guarantee sequence stability over time, and this is expected to change sequences in some cases.
PiperOrigin-RevId: 320285836
--
15800a39557a07dd52e0add66a0ab67aed00590b by Gennadiy Rozental <rogeeff@google.com>:
Internal change.
PiperOrigin-RevId: 320220913
--
ef39348360873f6d19669755fe0b5d09a945a501 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320181729
--
4f9f6ef8034a24da1832e4c838c72f80fc2ea062 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176084
--
6bfc8008462801657d231585bd5c37fc18bb25b6 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176070
--
b35b055ab1f41e6056031ff0641cabab23530027 by Abseil Team <absl-team@google.com>:
Disabling using header module as well as building one for randen_hwaes_impl
PiperOrigin-RevId: 320024299
GitOrigin-RevId: 4833151c207fac9f57a735efe6d5db4c83368415
Change-Id: I9cf102dbf46ed07752a508b7cda3ab3858857d0d
4 years ago
|
|
|
// Because `kLargeBits == kSmallBits + 1`, it follows that
|
|
|
|
//
|
Export of internal Abseil changes
--
4833151c207fac9f57a735efe6d5db4c83368415 by Gennadiy Rozental <rogeeff@google.com>:
Import of CCTZ from GitHub.
PiperOrigin-RevId: 320398694
--
a1becb36b223230f0a45f204a5fb33b83d2deffe by Gennadiy Rozental <rogeeff@google.com>:
Update CMakeLists.txt
Import of https://github.com/abseil/abseil-cpp/pull/737
PiperOrigin-RevId: 320391906
--
b529c45856fe7a3447f1f3259286d57e13b1f292 by Abseil Team <absl-team@google.com>:
Improves a comment about use of absl::Condition.
PiperOrigin-RevId: 320384329
--
c7b1dacda2739c10dc1ccbfb56b07ed7fe2464a4 by Laramie Leavitt <lar@google.com>:
Improve FastUniformBits performance for std::minstd_rand.
The rejection algorithm was too pessimistic before, and not in line with the [rand.adapt.ibits]. Specifically, when sampling from an URBG with a non power of 2 range, FastUniformBits constructed a rejection threshold with a power-of-2 range that was too restrictive.
For example, minstd_rand has a range of
[1, 2147483646], which has a range of 2145386495, or about 30.999 bits.
Before FastUniformBits rejected values between 1<<30 and 2145386495, which includes approximately 50% of the generated values. However, since a minimum of 3 calls are required to generate a full 64-bit value from an entropy pool of 30.9 bits, the correct value for rejection sampling is the range value which masks 21 (0x7fe00000) or 22 bits and rejects values greater than that. This reduces the probability of rejecting a sample to about 0.1%
NOTE: Abseil random does not guarantee sequence stability over time, and this is expected to change sequences in some cases.
PiperOrigin-RevId: 320285836
--
15800a39557a07dd52e0add66a0ab67aed00590b by Gennadiy Rozental <rogeeff@google.com>:
Internal change.
PiperOrigin-RevId: 320220913
--
ef39348360873f6d19669755fe0b5d09a945a501 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320181729
--
4f9f6ef8034a24da1832e4c838c72f80fc2ea062 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176084
--
6bfc8008462801657d231585bd5c37fc18bb25b6 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176070
--
b35b055ab1f41e6056031ff0641cabab23530027 by Abseil Team <absl-team@google.com>:
Disabling using header module as well as building one for randen_hwaes_impl
PiperOrigin-RevId: 320024299
GitOrigin-RevId: 4833151c207fac9f57a735efe6d5db4c83368415
Change-Id: I9cf102dbf46ed07752a508b7cda3ab3858857d0d
4 years ago
|
|
|
// `kResultBits == kSmallIters * kSmallBits + kLargeIters`
|
|
|
|
//
|
|
|
|
// and therefore
|
|
|
|
//
|
|
|
|
// `kLargeIters == kTotalWidth % kSmallWidth`
|
|
|
|
//
|
|
|
|
// Intuitively, each iteration with the large width accounts for one unit
|
|
|
|
// of the remainder when `kTotalWidth` is divided by `kSmallWidth`. As
|
|
|
|
// mentioned above, if the URBG width is a divisor of `kTotalWidth`, then
|
|
|
|
// there would be no need for any large iterations (i.e., one loop would
|
|
|
|
// suffice), and indeed, in this case, `kLargeIters` would be zero.
|
Export of internal Abseil changes
--
4833151c207fac9f57a735efe6d5db4c83368415 by Gennadiy Rozental <rogeeff@google.com>:
Import of CCTZ from GitHub.
PiperOrigin-RevId: 320398694
--
a1becb36b223230f0a45f204a5fb33b83d2deffe by Gennadiy Rozental <rogeeff@google.com>:
Update CMakeLists.txt
Import of https://github.com/abseil/abseil-cpp/pull/737
PiperOrigin-RevId: 320391906
--
b529c45856fe7a3447f1f3259286d57e13b1f292 by Abseil Team <absl-team@google.com>:
Improves a comment about use of absl::Condition.
PiperOrigin-RevId: 320384329
--
c7b1dacda2739c10dc1ccbfb56b07ed7fe2464a4 by Laramie Leavitt <lar@google.com>:
Improve FastUniformBits performance for std::minstd_rand.
The rejection algorithm was too pessimistic before, and not in line with the [rand.adapt.ibits]. Specifically, when sampling from an URBG with a non power of 2 range, FastUniformBits constructed a rejection threshold with a power-of-2 range that was too restrictive.
For example, minstd_rand has a range of
[1, 2147483646], which has a range of 2145386495, or about 30.999 bits.
Before FastUniformBits rejected values between 1<<30 and 2145386495, which includes approximately 50% of the generated values. However, since a minimum of 3 calls are required to generate a full 64-bit value from an entropy pool of 30.9 bits, the correct value for rejection sampling is the range value which masks 21 (0x7fe00000) or 22 bits and rejects values greater than that. This reduces the probability of rejecting a sample to about 0.1%
NOTE: Abseil random does not guarantee sequence stability over time, and this is expected to change sequences in some cases.
PiperOrigin-RevId: 320285836
--
15800a39557a07dd52e0add66a0ab67aed00590b by Gennadiy Rozental <rogeeff@google.com>:
Internal change.
PiperOrigin-RevId: 320220913
--
ef39348360873f6d19669755fe0b5d09a945a501 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320181729
--
4f9f6ef8034a24da1832e4c838c72f80fc2ea062 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176084
--
6bfc8008462801657d231585bd5c37fc18bb25b6 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176070
--
b35b055ab1f41e6056031ff0641cabab23530027 by Abseil Team <absl-team@google.com>:
Disabling using header module as well as building one for randen_hwaes_impl
PiperOrigin-RevId: 320024299
GitOrigin-RevId: 4833151c207fac9f57a735efe6d5db4c83368415
Change-Id: I9cf102dbf46ed07752a508b7cda3ab3858857d0d
4 years ago
|
|
|
static_assert(kResultBits == kSmallIters * kSmallBits +
|
|
|
|
(kTotalIters - kSmallIters) * kLargeBits,
|
|
|
|
"Error in looping constant calculations.");
|
|
|
|
|
Export of internal Abseil changes
--
4833151c207fac9f57a735efe6d5db4c83368415 by Gennadiy Rozental <rogeeff@google.com>:
Import of CCTZ from GitHub.
PiperOrigin-RevId: 320398694
--
a1becb36b223230f0a45f204a5fb33b83d2deffe by Gennadiy Rozental <rogeeff@google.com>:
Update CMakeLists.txt
Import of https://github.com/abseil/abseil-cpp/pull/737
PiperOrigin-RevId: 320391906
--
b529c45856fe7a3447f1f3259286d57e13b1f292 by Abseil Team <absl-team@google.com>:
Improves a comment about use of absl::Condition.
PiperOrigin-RevId: 320384329
--
c7b1dacda2739c10dc1ccbfb56b07ed7fe2464a4 by Laramie Leavitt <lar@google.com>:
Improve FastUniformBits performance for std::minstd_rand.
The rejection algorithm was too pessimistic before, and not in line with the [rand.adapt.ibits]. Specifically, when sampling from an URBG with a non power of 2 range, FastUniformBits constructed a rejection threshold with a power-of-2 range that was too restrictive.
For example, minstd_rand has a range of
[1, 2147483646], which has a range of 2145386495, or about 30.999 bits.
Before FastUniformBits rejected values between 1<<30 and 2145386495, which includes approximately 50% of the generated values. However, since a minimum of 3 calls are required to generate a full 64-bit value from an entropy pool of 30.9 bits, the correct value for rejection sampling is the range value which masks 21 (0x7fe00000) or 22 bits and rejects values greater than that. This reduces the probability of rejecting a sample to about 0.1%
NOTE: Abseil random does not guarantee sequence stability over time, and this is expected to change sequences in some cases.
PiperOrigin-RevId: 320285836
--
15800a39557a07dd52e0add66a0ab67aed00590b by Gennadiy Rozental <rogeeff@google.com>:
Internal change.
PiperOrigin-RevId: 320220913
--
ef39348360873f6d19669755fe0b5d09a945a501 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320181729
--
4f9f6ef8034a24da1832e4c838c72f80fc2ea062 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176084
--
6bfc8008462801657d231585bd5c37fc18bb25b6 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176070
--
b35b055ab1f41e6056031ff0641cabab23530027 by Abseil Team <absl-team@google.com>:
Disabling using header module as well as building one for randen_hwaes_impl
PiperOrigin-RevId: 320024299
GitOrigin-RevId: 4833151c207fac9f57a735efe6d5db4c83368415
Change-Id: I9cf102dbf46ed07752a508b7cda3ab3858857d0d
4 years ago
|
|
|
// The small shift is essentially small bits, but due to the potential
|
|
|
|
// of generating a smaller result_type from a larger urbg type, the actual
|
|
|
|
// shift might be 0.
|
|
|
|
static constexpr size_t kSmallShift = kSmallBits % kResultBits;
|
|
|
|
static constexpr auto kSmallMask =
|
|
|
|
MaskFromShift<urbg_result_type>(kSmallShift);
|
|
|
|
static constexpr size_t kLargeShift = kLargeBits % kResultBits;
|
|
|
|
static constexpr auto kLargeMask =
|
|
|
|
MaskFromShift<urbg_result_type>(kLargeShift);
|
|
|
|
|
Export of internal Abseil changes
--
4833151c207fac9f57a735efe6d5db4c83368415 by Gennadiy Rozental <rogeeff@google.com>:
Import of CCTZ from GitHub.
PiperOrigin-RevId: 320398694
--
a1becb36b223230f0a45f204a5fb33b83d2deffe by Gennadiy Rozental <rogeeff@google.com>:
Update CMakeLists.txt
Import of https://github.com/abseil/abseil-cpp/pull/737
PiperOrigin-RevId: 320391906
--
b529c45856fe7a3447f1f3259286d57e13b1f292 by Abseil Team <absl-team@google.com>:
Improves a comment about use of absl::Condition.
PiperOrigin-RevId: 320384329
--
c7b1dacda2739c10dc1ccbfb56b07ed7fe2464a4 by Laramie Leavitt <lar@google.com>:
Improve FastUniformBits performance for std::minstd_rand.
The rejection algorithm was too pessimistic before, and not in line with the [rand.adapt.ibits]. Specifically, when sampling from an URBG with a non power of 2 range, FastUniformBits constructed a rejection threshold with a power-of-2 range that was too restrictive.
For example, minstd_rand has a range of
[1, 2147483646], which has a range of 2145386495, or about 30.999 bits.
Before FastUniformBits rejected values between 1<<30 and 2145386495, which includes approximately 50% of the generated values. However, since a minimum of 3 calls are required to generate a full 64-bit value from an entropy pool of 30.9 bits, the correct value for rejection sampling is the range value which masks 21 (0x7fe00000) or 22 bits and rejects values greater than that. This reduces the probability of rejecting a sample to about 0.1%
NOTE: Abseil random does not guarantee sequence stability over time, and this is expected to change sequences in some cases.
PiperOrigin-RevId: 320285836
--
15800a39557a07dd52e0add66a0ab67aed00590b by Gennadiy Rozental <rogeeff@google.com>:
Internal change.
PiperOrigin-RevId: 320220913
--
ef39348360873f6d19669755fe0b5d09a945a501 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320181729
--
4f9f6ef8034a24da1832e4c838c72f80fc2ea062 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176084
--
6bfc8008462801657d231585bd5c37fc18bb25b6 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176070
--
b35b055ab1f41e6056031ff0641cabab23530027 by Abseil Team <absl-team@google.com>:
Disabling using header module as well as building one for randen_hwaes_impl
PiperOrigin-RevId: 320024299
GitOrigin-RevId: 4833151c207fac9f57a735efe6d5db4c83368415
Change-Id: I9cf102dbf46ed07752a508b7cda3ab3858857d0d
4 years ago
|
|
|
static constexpr auto kMin = (URBG::min)();
|
|
|
|
|
Export of internal Abseil changes
--
4833151c207fac9f57a735efe6d5db4c83368415 by Gennadiy Rozental <rogeeff@google.com>:
Import of CCTZ from GitHub.
PiperOrigin-RevId: 320398694
--
a1becb36b223230f0a45f204a5fb33b83d2deffe by Gennadiy Rozental <rogeeff@google.com>:
Update CMakeLists.txt
Import of https://github.com/abseil/abseil-cpp/pull/737
PiperOrigin-RevId: 320391906
--
b529c45856fe7a3447f1f3259286d57e13b1f292 by Abseil Team <absl-team@google.com>:
Improves a comment about use of absl::Condition.
PiperOrigin-RevId: 320384329
--
c7b1dacda2739c10dc1ccbfb56b07ed7fe2464a4 by Laramie Leavitt <lar@google.com>:
Improve FastUniformBits performance for std::minstd_rand.
The rejection algorithm was too pessimistic before, and not in line with the [rand.adapt.ibits]. Specifically, when sampling from an URBG with a non power of 2 range, FastUniformBits constructed a rejection threshold with a power-of-2 range that was too restrictive.
For example, minstd_rand has a range of
[1, 2147483646], which has a range of 2145386495, or about 30.999 bits.
Before FastUniformBits rejected values between 1<<30 and 2145386495, which includes approximately 50% of the generated values. However, since a minimum of 3 calls are required to generate a full 64-bit value from an entropy pool of 30.9 bits, the correct value for rejection sampling is the range value which masks 21 (0x7fe00000) or 22 bits and rejects values greater than that. This reduces the probability of rejecting a sample to about 0.1%
NOTE: Abseil random does not guarantee sequence stability over time, and this is expected to change sequences in some cases.
PiperOrigin-RevId: 320285836
--
15800a39557a07dd52e0add66a0ab67aed00590b by Gennadiy Rozental <rogeeff@google.com>:
Internal change.
PiperOrigin-RevId: 320220913
--
ef39348360873f6d19669755fe0b5d09a945a501 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320181729
--
4f9f6ef8034a24da1832e4c838c72f80fc2ea062 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176084
--
6bfc8008462801657d231585bd5c37fc18bb25b6 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176070
--
b35b055ab1f41e6056031ff0641cabab23530027 by Abseil Team <absl-team@google.com>:
Disabling using header module as well as building one for randen_hwaes_impl
PiperOrigin-RevId: 320024299
GitOrigin-RevId: 4833151c207fac9f57a735efe6d5db4c83368415
Change-Id: I9cf102dbf46ed07752a508b7cda3ab3858857d0d
4 years ago
|
|
|
result_type s = 0;
|
|
|
|
for (size_t n = 0; n < kSmallIters; ++n) {
|
Export of internal Abseil changes
--
4833151c207fac9f57a735efe6d5db4c83368415 by Gennadiy Rozental <rogeeff@google.com>:
Import of CCTZ from GitHub.
PiperOrigin-RevId: 320398694
--
a1becb36b223230f0a45f204a5fb33b83d2deffe by Gennadiy Rozental <rogeeff@google.com>:
Update CMakeLists.txt
Import of https://github.com/abseil/abseil-cpp/pull/737
PiperOrigin-RevId: 320391906
--
b529c45856fe7a3447f1f3259286d57e13b1f292 by Abseil Team <absl-team@google.com>:
Improves a comment about use of absl::Condition.
PiperOrigin-RevId: 320384329
--
c7b1dacda2739c10dc1ccbfb56b07ed7fe2464a4 by Laramie Leavitt <lar@google.com>:
Improve FastUniformBits performance for std::minstd_rand.
The rejection algorithm was too pessimistic before, and not in line with the [rand.adapt.ibits]. Specifically, when sampling from an URBG with a non power of 2 range, FastUniformBits constructed a rejection threshold with a power-of-2 range that was too restrictive.
For example, minstd_rand has a range of
[1, 2147483646], which has a range of 2145386495, or about 30.999 bits.
Before FastUniformBits rejected values between 1<<30 and 2145386495, which includes approximately 50% of the generated values. However, since a minimum of 3 calls are required to generate a full 64-bit value from an entropy pool of 30.9 bits, the correct value for rejection sampling is the range value which masks 21 (0x7fe00000) or 22 bits and rejects values greater than that. This reduces the probability of rejecting a sample to about 0.1%
NOTE: Abseil random does not guarantee sequence stability over time, and this is expected to change sequences in some cases.
PiperOrigin-RevId: 320285836
--
15800a39557a07dd52e0add66a0ab67aed00590b by Gennadiy Rozental <rogeeff@google.com>:
Internal change.
PiperOrigin-RevId: 320220913
--
ef39348360873f6d19669755fe0b5d09a945a501 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320181729
--
4f9f6ef8034a24da1832e4c838c72f80fc2ea062 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176084
--
6bfc8008462801657d231585bd5c37fc18bb25b6 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176070
--
b35b055ab1f41e6056031ff0641cabab23530027 by Abseil Team <absl-team@google.com>:
Disabling using header module as well as building one for randen_hwaes_impl
PiperOrigin-RevId: 320024299
GitOrigin-RevId: 4833151c207fac9f57a735efe6d5db4c83368415
Change-Id: I9cf102dbf46ed07752a508b7cda3ab3858857d0d
4 years ago
|
|
|
urbg_result_type v;
|
|
|
|
do {
|
|
|
|
v = g() - kMin;
|
|
|
|
} while (v >= kSmallRejection);
|
|
|
|
|
Export of internal Abseil changes
--
4833151c207fac9f57a735efe6d5db4c83368415 by Gennadiy Rozental <rogeeff@google.com>:
Import of CCTZ from GitHub.
PiperOrigin-RevId: 320398694
--
a1becb36b223230f0a45f204a5fb33b83d2deffe by Gennadiy Rozental <rogeeff@google.com>:
Update CMakeLists.txt
Import of https://github.com/abseil/abseil-cpp/pull/737
PiperOrigin-RevId: 320391906
--
b529c45856fe7a3447f1f3259286d57e13b1f292 by Abseil Team <absl-team@google.com>:
Improves a comment about use of absl::Condition.
PiperOrigin-RevId: 320384329
--
c7b1dacda2739c10dc1ccbfb56b07ed7fe2464a4 by Laramie Leavitt <lar@google.com>:
Improve FastUniformBits performance for std::minstd_rand.
The rejection algorithm was too pessimistic before, and not in line with the [rand.adapt.ibits]. Specifically, when sampling from an URBG with a non power of 2 range, FastUniformBits constructed a rejection threshold with a power-of-2 range that was too restrictive.
For example, minstd_rand has a range of
[1, 2147483646], which has a range of 2145386495, or about 30.999 bits.
Before FastUniformBits rejected values between 1<<30 and 2145386495, which includes approximately 50% of the generated values. However, since a minimum of 3 calls are required to generate a full 64-bit value from an entropy pool of 30.9 bits, the correct value for rejection sampling is the range value which masks 21 (0x7fe00000) or 22 bits and rejects values greater than that. This reduces the probability of rejecting a sample to about 0.1%
NOTE: Abseil random does not guarantee sequence stability over time, and this is expected to change sequences in some cases.
PiperOrigin-RevId: 320285836
--
15800a39557a07dd52e0add66a0ab67aed00590b by Gennadiy Rozental <rogeeff@google.com>:
Internal change.
PiperOrigin-RevId: 320220913
--
ef39348360873f6d19669755fe0b5d09a945a501 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320181729
--
4f9f6ef8034a24da1832e4c838c72f80fc2ea062 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176084
--
6bfc8008462801657d231585bd5c37fc18bb25b6 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176070
--
b35b055ab1f41e6056031ff0641cabab23530027 by Abseil Team <absl-team@google.com>:
Disabling using header module as well as building one for randen_hwaes_impl
PiperOrigin-RevId: 320024299
GitOrigin-RevId: 4833151c207fac9f57a735efe6d5db4c83368415
Change-Id: I9cf102dbf46ed07752a508b7cda3ab3858857d0d
4 years ago
|
|
|
s = (s << kSmallShift) + static_cast<result_type>(v & kSmallMask);
|
|
|
|
}
|
|
|
|
|
Export of internal Abseil changes
--
4833151c207fac9f57a735efe6d5db4c83368415 by Gennadiy Rozental <rogeeff@google.com>:
Import of CCTZ from GitHub.
PiperOrigin-RevId: 320398694
--
a1becb36b223230f0a45f204a5fb33b83d2deffe by Gennadiy Rozental <rogeeff@google.com>:
Update CMakeLists.txt
Import of https://github.com/abseil/abseil-cpp/pull/737
PiperOrigin-RevId: 320391906
--
b529c45856fe7a3447f1f3259286d57e13b1f292 by Abseil Team <absl-team@google.com>:
Improves a comment about use of absl::Condition.
PiperOrigin-RevId: 320384329
--
c7b1dacda2739c10dc1ccbfb56b07ed7fe2464a4 by Laramie Leavitt <lar@google.com>:
Improve FastUniformBits performance for std::minstd_rand.
The rejection algorithm was too pessimistic before, and not in line with the [rand.adapt.ibits]. Specifically, when sampling from an URBG with a non power of 2 range, FastUniformBits constructed a rejection threshold with a power-of-2 range that was too restrictive.
For example, minstd_rand has a range of
[1, 2147483646], which has a range of 2145386495, or about 30.999 bits.
Before FastUniformBits rejected values between 1<<30 and 2145386495, which includes approximately 50% of the generated values. However, since a minimum of 3 calls are required to generate a full 64-bit value from an entropy pool of 30.9 bits, the correct value for rejection sampling is the range value which masks 21 (0x7fe00000) or 22 bits and rejects values greater than that. This reduces the probability of rejecting a sample to about 0.1%
NOTE: Abseil random does not guarantee sequence stability over time, and this is expected to change sequences in some cases.
PiperOrigin-RevId: 320285836
--
15800a39557a07dd52e0add66a0ab67aed00590b by Gennadiy Rozental <rogeeff@google.com>:
Internal change.
PiperOrigin-RevId: 320220913
--
ef39348360873f6d19669755fe0b5d09a945a501 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320181729
--
4f9f6ef8034a24da1832e4c838c72f80fc2ea062 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176084
--
6bfc8008462801657d231585bd5c37fc18bb25b6 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176070
--
b35b055ab1f41e6056031ff0641cabab23530027 by Abseil Team <absl-team@google.com>:
Disabling using header module as well as building one for randen_hwaes_impl
PiperOrigin-RevId: 320024299
GitOrigin-RevId: 4833151c207fac9f57a735efe6d5db4c83368415
Change-Id: I9cf102dbf46ed07752a508b7cda3ab3858857d0d
4 years ago
|
|
|
for (size_t n = kSmallIters; n < kTotalIters; ++n) {
|
|
|
|
urbg_result_type v;
|
|
|
|
do {
|
|
|
|
v = g() - kMin;
|
|
|
|
} while (v >= kLargeRejection);
|
|
|
|
|
Export of internal Abseil changes
--
4833151c207fac9f57a735efe6d5db4c83368415 by Gennadiy Rozental <rogeeff@google.com>:
Import of CCTZ from GitHub.
PiperOrigin-RevId: 320398694
--
a1becb36b223230f0a45f204a5fb33b83d2deffe by Gennadiy Rozental <rogeeff@google.com>:
Update CMakeLists.txt
Import of https://github.com/abseil/abseil-cpp/pull/737
PiperOrigin-RevId: 320391906
--
b529c45856fe7a3447f1f3259286d57e13b1f292 by Abseil Team <absl-team@google.com>:
Improves a comment about use of absl::Condition.
PiperOrigin-RevId: 320384329
--
c7b1dacda2739c10dc1ccbfb56b07ed7fe2464a4 by Laramie Leavitt <lar@google.com>:
Improve FastUniformBits performance for std::minstd_rand.
The rejection algorithm was too pessimistic before, and not in line with the [rand.adapt.ibits]. Specifically, when sampling from an URBG with a non power of 2 range, FastUniformBits constructed a rejection threshold with a power-of-2 range that was too restrictive.
For example, minstd_rand has a range of
[1, 2147483646], which has a range of 2145386495, or about 30.999 bits.
Before FastUniformBits rejected values between 1<<30 and 2145386495, which includes approximately 50% of the generated values. However, since a minimum of 3 calls are required to generate a full 64-bit value from an entropy pool of 30.9 bits, the correct value for rejection sampling is the range value which masks 21 (0x7fe00000) or 22 bits and rejects values greater than that. This reduces the probability of rejecting a sample to about 0.1%
NOTE: Abseil random does not guarantee sequence stability over time, and this is expected to change sequences in some cases.
PiperOrigin-RevId: 320285836
--
15800a39557a07dd52e0add66a0ab67aed00590b by Gennadiy Rozental <rogeeff@google.com>:
Internal change.
PiperOrigin-RevId: 320220913
--
ef39348360873f6d19669755fe0b5d09a945a501 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320181729
--
4f9f6ef8034a24da1832e4c838c72f80fc2ea062 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176084
--
6bfc8008462801657d231585bd5c37fc18bb25b6 by Gennadiy Rozental <rogeeff@google.com>:
Internal change
PiperOrigin-RevId: 320176070
--
b35b055ab1f41e6056031ff0641cabab23530027 by Abseil Team <absl-team@google.com>:
Disabling using header module as well as building one for randen_hwaes_impl
PiperOrigin-RevId: 320024299
GitOrigin-RevId: 4833151c207fac9f57a735efe6d5db4c83368415
Change-Id: I9cf102dbf46ed07752a508b7cda3ab3858857d0d
4 years ago
|
|
|
s = (s << kLargeShift) + static_cast<result_type>(v & kLargeMask);
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace random_internal
|
|
|
|
ABSL_NAMESPACE_END
|
|
|
|
} // namespace absl
|
|
|
|
|
|
|
|
#endif // ABSL_RANDOM_INTERNAL_FAST_UNIFORM_BITS_H_
|