pool_urbg.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef Y_ABSL_RANDOM_INTERNAL_POOL_URBG_H_
  15. #define Y_ABSL_RANDOM_INTERNAL_POOL_URBG_H_
  16. #include <cinttypes>
  17. #include <limits>
  18. #include "y_absl/random/internal/traits.h"
  19. #include "y_absl/types/span.h"
  20. namespace y_absl {
  21. Y_ABSL_NAMESPACE_BEGIN
  22. namespace random_internal {
  23. // RandenPool is a thread-safe random number generator [random.req.urbg] that
  24. // uses an underlying pool of Randen generators to generate values. Each thread
  25. // has affinity to one instance of the underlying pool generators. Concurrent
  26. // access is guarded by a spin-lock.
  27. template <typename T>
  28. class RandenPool {
  29. public:
  30. using result_type = T;
  31. static_assert(std::is_unsigned<result_type>::value,
  32. "RandenPool template argument must be a built-in unsigned "
  33. "integer type");
  34. static constexpr result_type(min)() {
  35. return (std::numeric_limits<result_type>::min)();
  36. }
  37. static constexpr result_type(max)() {
  38. return (std::numeric_limits<result_type>::max)();
  39. }
  40. RandenPool() {}
  41. // Returns a single value.
  42. inline result_type operator()() { return Generate(); }
  43. // Fill data with random values.
  44. static void Fill(y_absl::Span<result_type> data);
  45. protected:
  46. // Generate returns a single value.
  47. static result_type Generate();
  48. };
  49. extern template class RandenPool<uint8_t>;
  50. extern template class RandenPool<uint16_t>;
  51. extern template class RandenPool<uint32_t>;
  52. extern template class RandenPool<uint64_t>;
  53. // PoolURBG uses an underlying pool of random generators to implement a
  54. // thread-compatible [random.req.urbg] interface with an internal cache of
  55. // values.
  56. template <typename T, size_t kBufferSize>
  57. class PoolURBG {
  58. // Inheritance to access the protected static members of RandenPool.
  59. using unsigned_type = typename make_unsigned_bits<T>::type;
  60. using PoolType = RandenPool<unsigned_type>;
  61. using SpanType = y_absl::Span<unsigned_type>;
  62. static constexpr size_t kInitialBuffer = kBufferSize + 1;
  63. static constexpr size_t kHalfBuffer = kBufferSize / 2;
  64. public:
  65. using result_type = T;
  66. static_assert(std::is_unsigned<result_type>::value,
  67. "PoolURBG must be parameterized by an unsigned integer type");
  68. static_assert(kBufferSize > 1,
  69. "PoolURBG must be parameterized by a buffer-size > 1");
  70. static_assert(kBufferSize <= 256,
  71. "PoolURBG must be parameterized by a buffer-size <= 256");
  72. static constexpr result_type(min)() {
  73. return (std::numeric_limits<result_type>::min)();
  74. }
  75. static constexpr result_type(max)() {
  76. return (std::numeric_limits<result_type>::max)();
  77. }
  78. PoolURBG() : next_(kInitialBuffer) {}
  79. // copy-constructor does not copy cache.
  80. PoolURBG(const PoolURBG&) : next_(kInitialBuffer) {}
  81. const PoolURBG& operator=(const PoolURBG&) {
  82. next_ = kInitialBuffer;
  83. return *this;
  84. }
  85. // move-constructor does move cache.
  86. PoolURBG(PoolURBG&&) = default;
  87. PoolURBG& operator=(PoolURBG&&) = default;
  88. inline result_type operator()() {
  89. if (next_ >= kBufferSize) {
  90. next_ = (kBufferSize > 2 && next_ > kBufferSize) ? kHalfBuffer : 0;
  91. PoolType::Fill(SpanType(reinterpret_cast<unsigned_type*>(state_ + next_),
  92. kBufferSize - next_));
  93. }
  94. return state_[next_++];
  95. }
  96. private:
  97. // Buffer size.
  98. size_t next_; // index within state_
  99. result_type state_[kBufferSize];
  100. };
  101. } // namespace random_internal
  102. Y_ABSL_NAMESPACE_END
  103. } // namespace y_absl
  104. #endif // Y_ABSL_RANDOM_INTERNAL_POOL_URBG_H_