You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

pool_urbg.h 5.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 ABSL_RANDOM_INTERNAL_POOL_URBG_H_
  15. #define ABSL_RANDOM_INTERNAL_POOL_URBG_H_
  16. #include <cinttypes>
  17. #include <limits>
  18. #include "absl/random/internal/traits.h"
  19. #include "absl/types/span.h"
  20. namespace absl
  21. {
  22. ABSL_NAMESPACE_BEGIN
  23. namespace random_internal
  24. {
  25. // RandenPool is a thread-safe random number generator [random.req.urbg] that
  26. // uses an underlying pool of Randen generators to generate values. Each thread
  27. // has affinity to one instance of the underlying pool generators. Concurrent
  28. // access is guarded by a spin-lock.
  29. template<typename T>
  30. class RandenPool
  31. {
  32. public:
  33. using result_type = T;
  34. static_assert(std::is_unsigned<result_type>::value, "RandenPool template argument must be a built-in unsigned "
  35. "integer type");
  36. static constexpr result_type(min)()
  37. {
  38. return (std::numeric_limits<result_type>::min)();
  39. }
  40. static constexpr result_type(max)()
  41. {
  42. return (std::numeric_limits<result_type>::max)();
  43. }
  44. RandenPool()
  45. {
  46. }
  47. // Returns a single value.
  48. inline result_type operator()()
  49. {
  50. return Generate();
  51. }
  52. // Fill data with random values.
  53. static void Fill(absl::Span<result_type> data);
  54. protected:
  55. // Generate returns a single value.
  56. static result_type Generate();
  57. };
  58. extern template class RandenPool<uint8_t>;
  59. extern template class RandenPool<uint16_t>;
  60. extern template class RandenPool<uint32_t>;
  61. extern template class RandenPool<uint64_t>;
  62. // PoolURBG uses an underlying pool of random generators to implement a
  63. // thread-compatible [random.req.urbg] interface with an internal cache of
  64. // values.
  65. template<typename T, size_t kBufferSize>
  66. class PoolURBG
  67. {
  68. // Inheritance to access the protected static members of RandenPool.
  69. using unsigned_type = typename make_unsigned_bits<T>::type;
  70. using PoolType = RandenPool<unsigned_type>;
  71. using SpanType = absl::Span<unsigned_type>;
  72. static constexpr size_t kInitialBuffer = kBufferSize + 1;
  73. static constexpr size_t kHalfBuffer = kBufferSize / 2;
  74. public:
  75. using result_type = T;
  76. static_assert(std::is_unsigned<result_type>::value, "PoolURBG must be parameterized by an unsigned integer type");
  77. static_assert(kBufferSize > 1, "PoolURBG must be parameterized by a buffer-size > 1");
  78. static_assert(kBufferSize <= 256, "PoolURBG must be parameterized by a buffer-size <= 256");
  79. static constexpr result_type(min)()
  80. {
  81. return (std::numeric_limits<result_type>::min)();
  82. }
  83. static constexpr result_type(max)()
  84. {
  85. return (std::numeric_limits<result_type>::max)();
  86. }
  87. PoolURBG() :
  88. next_(kInitialBuffer)
  89. {
  90. }
  91. // copy-constructor does not copy cache.
  92. PoolURBG(const PoolURBG&) :
  93. next_(kInitialBuffer)
  94. {
  95. }
  96. const PoolURBG& operator=(const PoolURBG&)
  97. {
  98. next_ = kInitialBuffer;
  99. return *this;
  100. }
  101. // move-constructor does move cache.
  102. PoolURBG(PoolURBG&&) = default;
  103. PoolURBG& operator=(PoolURBG&&) = default;
  104. inline result_type operator()()
  105. {
  106. if (next_ >= kBufferSize)
  107. {
  108. next_ = (kBufferSize > 2 && next_ > kBufferSize) ? kHalfBuffer : 0;
  109. PoolType::Fill(SpanType(reinterpret_cast<unsigned_type*>(state_ + next_), kBufferSize - next_));
  110. }
  111. return state_[next_++];
  112. }
  113. private:
  114. // Buffer size.
  115. size_t next_; // index within state_
  116. result_type state_[kBufferSize];
  117. };
  118. } // namespace random_internal
  119. ABSL_NAMESPACE_END
  120. } // namespace absl
  121. #endif // ABSL_RANDOM_INTERNAL_POOL_URBG_H_