xref: /freebsd-src/contrib/llvm-project/libcxx/include/__random/seed_seq.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
14824e7fdSDimitry Andric //===----------------------------------------------------------------------===//
24824e7fdSDimitry Andric //
34824e7fdSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44824e7fdSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
54824e7fdSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
64824e7fdSDimitry Andric //
74824e7fdSDimitry Andric //===----------------------------------------------------------------------===//
84824e7fdSDimitry Andric 
94824e7fdSDimitry Andric #ifndef _LIBCPP___RANDOM_SEED_SEQ_H
104824e7fdSDimitry Andric #define _LIBCPP___RANDOM_SEED_SEQ_H
114824e7fdSDimitry Andric 
124824e7fdSDimitry Andric #include <__algorithm/copy.h>
134824e7fdSDimitry Andric #include <__algorithm/fill.h>
144824e7fdSDimitry Andric #include <__algorithm/max.h>
154824e7fdSDimitry Andric #include <__config>
1606c3fb27SDimitry Andric #include <__iterator/iterator_traits.h>
17*0fca6ea1SDimitry Andric #include <__type_traits/is_unsigned.h>
1806c3fb27SDimitry Andric #include <cstdint>
194824e7fdSDimitry Andric #include <initializer_list>
204824e7fdSDimitry Andric #include <vector>
214824e7fdSDimitry Andric 
224824e7fdSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
234824e7fdSDimitry Andric #  pragma GCC system_header
244824e7fdSDimitry Andric #endif
254824e7fdSDimitry Andric 
264824e7fdSDimitry Andric _LIBCPP_PUSH_MACROS
274824e7fdSDimitry Andric #include <__undef_macros>
284824e7fdSDimitry Andric 
294824e7fdSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
304824e7fdSDimitry Andric 
31cb14a3feSDimitry Andric class _LIBCPP_TEMPLATE_VIS seed_seq {
324824e7fdSDimitry Andric public:
334824e7fdSDimitry Andric   // types
344824e7fdSDimitry Andric   typedef uint32_t result_type;
354824e7fdSDimitry Andric 
364824e7fdSDimitry Andric   // constructors
37cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI seed_seq() _NOEXCEPT {}
384824e7fdSDimitry Andric #ifndef _LIBCPP_CXX03_LANG
39*0fca6ea1SDimitry Andric   template <class _Tp, __enable_if_t<is_integral<_Tp>::value, int> = 0>
40cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI seed_seq(initializer_list<_Tp> __il) {
4104eeddc0SDimitry Andric     __init(__il.begin(), __il.end());
4204eeddc0SDimitry Andric   }
434824e7fdSDimitry Andric #endif // _LIBCPP_CXX03_LANG
444824e7fdSDimitry Andric 
454824e7fdSDimitry Andric   template <class _InputIterator>
46cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI seed_seq(_InputIterator __first, _InputIterator __last) {
4704eeddc0SDimitry Andric     static_assert(is_integral<typename iterator_traits<_InputIterator>::value_type>::value,
4804eeddc0SDimitry Andric                   "Mandates: iterator_traits<InputIterator>::value_type is an integer type");
4904eeddc0SDimitry Andric     __init(__first, __last);
5004eeddc0SDimitry Andric   }
514824e7fdSDimitry Andric 
524824e7fdSDimitry Andric   // generating functions
534824e7fdSDimitry Andric   template <class _RandomAccessIterator>
5406c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI void generate(_RandomAccessIterator __first, _RandomAccessIterator __last);
554824e7fdSDimitry Andric 
564824e7fdSDimitry Andric   // property functions
57cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI size_t size() const _NOEXCEPT { return __v_.size(); }
584824e7fdSDimitry Andric   template <class _OutputIterator>
59cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI void param(_OutputIterator __dest) const {
60cb14a3feSDimitry Andric     std::copy(__v_.begin(), __v_.end(), __dest);
61cb14a3feSDimitry Andric   }
624824e7fdSDimitry Andric 
630eae32dcSDimitry Andric   seed_seq(const seed_seq&)       = delete;
640eae32dcSDimitry Andric   void operator=(const seed_seq&) = delete;
654824e7fdSDimitry Andric 
66cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI static result_type _Tp(result_type __x) { return __x ^ (__x >> 27); }
6704eeddc0SDimitry Andric 
6804eeddc0SDimitry Andric private:
6904eeddc0SDimitry Andric   template <class _InputIterator>
7006c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI void __init(_InputIterator __first, _InputIterator __last);
7104eeddc0SDimitry Andric 
7204eeddc0SDimitry Andric   vector<result_type> __v_;
734824e7fdSDimitry Andric };
744824e7fdSDimitry Andric 
754824e7fdSDimitry Andric template <class _InputIterator>
76cb14a3feSDimitry Andric void seed_seq::__init(_InputIterator __first, _InputIterator __last) {
774824e7fdSDimitry Andric   for (_InputIterator __s = __first; __s != __last; ++__s)
784824e7fdSDimitry Andric     __v_.push_back(*__s & 0xFFFFFFFF);
794824e7fdSDimitry Andric }
804824e7fdSDimitry Andric 
814824e7fdSDimitry Andric template <class _RandomAccessIterator>
82cb14a3feSDimitry Andric void seed_seq::generate(_RandomAccessIterator __first, _RandomAccessIterator __last) {
83*0fca6ea1SDimitry Andric   using _ValueType = typename iterator_traits<_RandomAccessIterator>::value_type;
84*0fca6ea1SDimitry Andric   static_assert(is_unsigned<_ValueType>::value && sizeof(_ValueType) >= sizeof(uint32_t),
85*0fca6ea1SDimitry Andric                 "[rand.util.seedseq]/7 requires the value_type of the iterator to be an unsigned "
86*0fca6ea1SDimitry Andric                 "integer capable of accommodating 32-bit quantities.");
87*0fca6ea1SDimitry Andric 
88cb14a3feSDimitry Andric   if (__first != __last) {
895f757f3fSDimitry Andric     std::fill(__first, __last, 0x8b8b8b8b);
904824e7fdSDimitry Andric     const size_t __n = static_cast<size_t>(__last - __first);
914824e7fdSDimitry Andric     const size_t __s = __v_.size();
92cb14a3feSDimitry Andric     const size_t __t = (__n >= 623) ? 11 : (__n >= 68) ? 7 : (__n >= 39) ? 5 : (__n >= 7) ? 3 : (__n - 1) / 2;
934824e7fdSDimitry Andric     const size_t __p = (__n - __t) / 2;
944824e7fdSDimitry Andric     const size_t __q = __p + __t;
955f757f3fSDimitry Andric     const size_t __m = std::max(__s + 1, __n);
964824e7fdSDimitry Andric     // __k = 0;
974824e7fdSDimitry Andric     {
98cb14a3feSDimitry Andric       result_type __r = 1664525 * _Tp(__first[0] ^ __first[__p] ^ __first[__n - 1]);
994824e7fdSDimitry Andric       __first[__p] += __r;
1004824e7fdSDimitry Andric       __r += __s;
1014824e7fdSDimitry Andric       __first[__q] += __r;
1024824e7fdSDimitry Andric       __first[0] = __r;
1034824e7fdSDimitry Andric     }
10481ad6265SDimitry Andric     // Initialize indexing terms used with if statements as an optimization to
10581ad6265SDimitry Andric     // avoid calculating modulo n on every loop iteration for each term.
10681ad6265SDimitry Andric     size_t __kmodn  = 0;         // __k % __n
10781ad6265SDimitry Andric     size_t __k1modn = __n - 1;   // (__k - 1) % __n
10881ad6265SDimitry Andric     size_t __kpmodn = __p % __n; // (__k + __p) % __n
10981ad6265SDimitry Andric     size_t __kqmodn = __q % __n; // (__k + __q) % __n
11081ad6265SDimitry Andric 
111cb14a3feSDimitry Andric     for (size_t __k = 1; __k <= __s; ++__k) {
11281ad6265SDimitry Andric       if (++__kmodn == __n)
11381ad6265SDimitry Andric         __kmodn = 0;
11481ad6265SDimitry Andric       if (++__k1modn == __n)
11581ad6265SDimitry Andric         __k1modn = 0;
11681ad6265SDimitry Andric       if (++__kpmodn == __n)
11781ad6265SDimitry Andric         __kpmodn = 0;
11881ad6265SDimitry Andric       if (++__kqmodn == __n)
11981ad6265SDimitry Andric         __kqmodn = 0;
12081ad6265SDimitry Andric 
12181ad6265SDimitry Andric       result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn] ^ __first[__k1modn]);
1224824e7fdSDimitry Andric       __first[__kpmodn] += __r;
1234824e7fdSDimitry Andric       __r += __kmodn + __v_[__k - 1];
12481ad6265SDimitry Andric       __first[__kqmodn] += __r;
1254824e7fdSDimitry Andric       __first[__kmodn] = __r;
1264824e7fdSDimitry Andric     }
127cb14a3feSDimitry Andric     for (size_t __k = __s + 1; __k < __m; ++__k) {
12881ad6265SDimitry Andric       if (++__kmodn == __n)
12981ad6265SDimitry Andric         __kmodn = 0;
13081ad6265SDimitry Andric       if (++__k1modn == __n)
13181ad6265SDimitry Andric         __k1modn = 0;
13281ad6265SDimitry Andric       if (++__kpmodn == __n)
13381ad6265SDimitry Andric         __kpmodn = 0;
13481ad6265SDimitry Andric       if (++__kqmodn == __n)
13581ad6265SDimitry Andric         __kqmodn = 0;
13681ad6265SDimitry Andric 
13781ad6265SDimitry Andric       result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn] ^ __first[__k1modn]);
1384824e7fdSDimitry Andric       __first[__kpmodn] += __r;
1394824e7fdSDimitry Andric       __r += __kmodn;
14081ad6265SDimitry Andric       __first[__kqmodn] += __r;
1414824e7fdSDimitry Andric       __first[__kmodn] = __r;
1424824e7fdSDimitry Andric     }
143cb14a3feSDimitry Andric     for (size_t __k = __m; __k < __m + __n; ++__k) {
14481ad6265SDimitry Andric       if (++__kmodn == __n)
14581ad6265SDimitry Andric         __kmodn = 0;
14681ad6265SDimitry Andric       if (++__k1modn == __n)
14781ad6265SDimitry Andric         __k1modn = 0;
14881ad6265SDimitry Andric       if (++__kpmodn == __n)
14981ad6265SDimitry Andric         __kpmodn = 0;
15081ad6265SDimitry Andric       if (++__kqmodn == __n)
15181ad6265SDimitry Andric         __kqmodn = 0;
15281ad6265SDimitry Andric 
15381ad6265SDimitry Andric       result_type __r = 1566083941 * _Tp(__first[__kmodn] + __first[__kpmodn] + __first[__k1modn]);
1544824e7fdSDimitry Andric       __first[__kpmodn] ^= __r;
1554824e7fdSDimitry Andric       __r -= __kmodn;
15681ad6265SDimitry Andric       __first[__kqmodn] ^= __r;
1574824e7fdSDimitry Andric       __first[__kmodn] = __r;
1584824e7fdSDimitry Andric     }
1594824e7fdSDimitry Andric   }
1604824e7fdSDimitry Andric }
1614824e7fdSDimitry Andric 
1624824e7fdSDimitry Andric _LIBCPP_END_NAMESPACE_STD
1634824e7fdSDimitry Andric 
1644824e7fdSDimitry Andric _LIBCPP_POP_MACROS
1654824e7fdSDimitry Andric 
1664824e7fdSDimitry Andric #endif // _LIBCPP___RANDOM_SEED_SEQ_H
167