seed_seq.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef _LIBCPP___RANDOM_SEED_SEQ_H
  9. #define _LIBCPP___RANDOM_SEED_SEQ_H
  10. #include <__algorithm/copy.h>
  11. #include <__algorithm/fill.h>
  12. #include <__algorithm/max.h>
  13. #include <__config>
  14. #include <__iterator/iterator_traits.h>
  15. #include <cstdint>
  16. #include <initializer_list>
  17. #include <vector>
  18. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  19. # pragma GCC system_header
  20. #endif
  21. _LIBCPP_PUSH_MACROS
  22. #include <__undef_macros>
  23. _LIBCPP_BEGIN_NAMESPACE_STD
  24. class _LIBCPP_TEMPLATE_VIS seed_seq
  25. {
  26. public:
  27. // types
  28. typedef uint32_t result_type;
  29. // constructors
  30. _LIBCPP_INLINE_VISIBILITY
  31. seed_seq() _NOEXCEPT {}
  32. #ifndef _LIBCPP_CXX03_LANG
  33. template<class _Tp, __enable_if_t<is_integral<_Tp>::value>* = nullptr>
  34. _LIBCPP_INLINE_VISIBILITY
  35. seed_seq(initializer_list<_Tp> __il) {
  36. __init(__il.begin(), __il.end());
  37. }
  38. #endif // _LIBCPP_CXX03_LANG
  39. template<class _InputIterator>
  40. _LIBCPP_INLINE_VISIBILITY
  41. seed_seq(_InputIterator __first, _InputIterator __last) {
  42. static_assert(is_integral<typename iterator_traits<_InputIterator>::value_type>::value,
  43. "Mandates: iterator_traits<InputIterator>::value_type is an integer type");
  44. __init(__first, __last);
  45. }
  46. // generating functions
  47. template<class _RandomAccessIterator>
  48. _LIBCPP_HIDE_FROM_ABI void generate(_RandomAccessIterator __first, _RandomAccessIterator __last);
  49. // property functions
  50. _LIBCPP_INLINE_VISIBILITY
  51. size_t size() const _NOEXCEPT {return __v_.size();}
  52. template<class _OutputIterator>
  53. _LIBCPP_INLINE_VISIBILITY
  54. void param(_OutputIterator __dest) const
  55. {_VSTD::copy(__v_.begin(), __v_.end(), __dest);}
  56. seed_seq(const seed_seq&) = delete;
  57. void operator=(const seed_seq&) = delete;
  58. _LIBCPP_INLINE_VISIBILITY
  59. static result_type _Tp(result_type __x) {return __x ^ (__x >> 27);}
  60. private:
  61. template<class _InputIterator>
  62. _LIBCPP_HIDE_FROM_ABI void __init(_InputIterator __first, _InputIterator __last);
  63. vector<result_type> __v_;
  64. };
  65. template<class _InputIterator>
  66. void
  67. seed_seq::__init(_InputIterator __first, _InputIterator __last)
  68. {
  69. for (_InputIterator __s = __first; __s != __last; ++__s)
  70. __v_.push_back(*__s & 0xFFFFFFFF);
  71. }
  72. template<class _RandomAccessIterator>
  73. void
  74. seed_seq::generate(_RandomAccessIterator __first, _RandomAccessIterator __last)
  75. {
  76. if (__first != __last)
  77. {
  78. _VSTD::fill(__first, __last, 0x8b8b8b8b);
  79. const size_t __n = static_cast<size_t>(__last - __first);
  80. const size_t __s = __v_.size();
  81. const size_t __t = (__n >= 623) ? 11
  82. : (__n >= 68) ? 7
  83. : (__n >= 39) ? 5
  84. : (__n >= 7) ? 3
  85. : (__n - 1) / 2;
  86. const size_t __p = (__n - __t) / 2;
  87. const size_t __q = __p + __t;
  88. const size_t __m = _VSTD::max(__s + 1, __n);
  89. // __k = 0;
  90. {
  91. result_type __r = 1664525 * _Tp(__first[0] ^ __first[__p]
  92. ^ __first[__n - 1]);
  93. __first[__p] += __r;
  94. __r += __s;
  95. __first[__q] += __r;
  96. __first[0] = __r;
  97. }
  98. // Initialize indexing terms used with if statements as an optimization to
  99. // avoid calculating modulo n on every loop iteration for each term.
  100. size_t __kmodn = 0; // __k % __n
  101. size_t __k1modn = __n - 1; // (__k - 1) % __n
  102. size_t __kpmodn = __p % __n; // (__k + __p) % __n
  103. size_t __kqmodn = __q % __n; // (__k + __q) % __n
  104. for (size_t __k = 1; __k <= __s; ++__k)
  105. {
  106. if (++__kmodn == __n)
  107. __kmodn = 0;
  108. if (++__k1modn == __n)
  109. __k1modn = 0;
  110. if (++__kpmodn == __n)
  111. __kpmodn = 0;
  112. if (++__kqmodn == __n)
  113. __kqmodn = 0;
  114. result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn] ^ __first[__k1modn]);
  115. __first[__kpmodn] += __r;
  116. __r += __kmodn + __v_[__k - 1];
  117. __first[__kqmodn] += __r;
  118. __first[__kmodn] = __r;
  119. }
  120. for (size_t __k = __s + 1; __k < __m; ++__k)
  121. {
  122. if (++__kmodn == __n)
  123. __kmodn = 0;
  124. if (++__k1modn == __n)
  125. __k1modn = 0;
  126. if (++__kpmodn == __n)
  127. __kpmodn = 0;
  128. if (++__kqmodn == __n)
  129. __kqmodn = 0;
  130. result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn] ^ __first[__k1modn]);
  131. __first[__kpmodn] += __r;
  132. __r += __kmodn;
  133. __first[__kqmodn] += __r;
  134. __first[__kmodn] = __r;
  135. }
  136. for (size_t __k = __m; __k < __m + __n; ++__k)
  137. {
  138. if (++__kmodn == __n)
  139. __kmodn = 0;
  140. if (++__k1modn == __n)
  141. __k1modn = 0;
  142. if (++__kpmodn == __n)
  143. __kpmodn = 0;
  144. if (++__kqmodn == __n)
  145. __kqmodn = 0;
  146. result_type __r = 1566083941 * _Tp(__first[__kmodn] + __first[__kpmodn] + __first[__k1modn]);
  147. __first[__kpmodn] ^= __r;
  148. __r -= __kmodn;
  149. __first[__kqmodn] ^= __r;
  150. __first[__kmodn] = __r;
  151. }
  152. }
  153. }
  154. _LIBCPP_END_NAMESPACE_STD
  155. _LIBCPP_POP_MACROS
  156. #endif // _LIBCPP___RANDOM_SEED_SEQ_H