// 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" #include "absl/meta/type_traits.h" #include "absl/random/internal/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) { … } // 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() { … } // Computes the floor of the log. (i.e., std::floor(std::log2(N)); template <typename UIntType> constexpr UIntType IntegerLog2(UIntType n) { … } // Returns the number of bits of randomness returned through // `PowerOfTwoVariate(urbg)`. template <typename URBG> constexpr size_t NumBits() { … } // 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> constexpr UIntType MaskFromShift(size_t n) { … } // 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 { … }; template <typename UIntType> template <typename URBG> typename FastUniformBits<UIntType>::result_type FastUniformBits<UIntType>::operator()(URBG& g) { … } template <typename UIntType> template <typename URBG> typename FastUniformBits<UIntType>::result_type FastUniformBits<UIntType>::Generate(URBG& g, // NOLINT(runtime/references) SimplifiedLoopTag) { … } template <typename UIntType> template <typename URBG> typename FastUniformBits<UIntType>::result_type FastUniformBits<UIntType>::Generate(URBG& g, // NOLINT(runtime/references) RejectionLoopTag) { … } } // namespace random_internal ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_RANDOM_INTERNAL_FAST_UNIFORM_BITS_H_