Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:38

0001 //===----------------------------------------------------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef _LIBCPP___CXX03___RANDOM_INDEPENDENT_BITS_ENGINE_H
0010 #define _LIBCPP___CXX03___RANDOM_INDEPENDENT_BITS_ENGINE_H
0011 
0012 #include <__cxx03/__config>
0013 #include <__cxx03/__fwd/istream.h>
0014 #include <__cxx03/__fwd/ostream.h>
0015 #include <__cxx03/__random/is_seed_sequence.h>
0016 #include <__cxx03/__random/log2.h>
0017 #include <__cxx03/__type_traits/conditional.h>
0018 #include <__cxx03/__type_traits/enable_if.h>
0019 #include <__cxx03/__type_traits/is_convertible.h>
0020 #include <__cxx03/__utility/move.h>
0021 #include <__cxx03/cstddef>
0022 #include <__cxx03/limits>
0023 
0024 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0025 #  pragma GCC system_header
0026 #endif
0027 
0028 _LIBCPP_PUSH_MACROS
0029 #include <__cxx03/__undef_macros>
0030 
0031 _LIBCPP_BEGIN_NAMESPACE_STD
0032 
0033 template <class _Engine, size_t __w, class _UIntType>
0034 class _LIBCPP_TEMPLATE_VIS independent_bits_engine {
0035   template <class _UInt, _UInt _R0, size_t _Wp, size_t _Mp>
0036   class __get_n {
0037     static _LIBCPP_CONSTEXPR const size_t _Dt = numeric_limits<_UInt>::digits;
0038     static _LIBCPP_CONSTEXPR const size_t _Np = _Wp / _Mp + (_Wp % _Mp != 0);
0039     static _LIBCPP_CONSTEXPR const size_t _W0 = _Wp / _Np;
0040     static _LIBCPP_CONSTEXPR const _UInt _Y0  = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0;
0041 
0042   public:
0043     static _LIBCPP_CONSTEXPR const size_t value = _R0 - _Y0 > _Y0 / _Np ? _Np + 1 : _Np;
0044   };
0045 
0046 public:
0047   // types
0048   typedef _UIntType result_type;
0049 
0050 private:
0051   _Engine __e_;
0052 
0053   static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;
0054   static_assert(0 < __w, "independent_bits_engine invalid parameters");
0055   static_assert(__w <= _Dt, "independent_bits_engine invalid parameters");
0056 
0057   typedef typename _Engine::result_type _Engine_result_type;
0058   typedef __conditional_t<sizeof(_Engine_result_type) <= sizeof(result_type), result_type, _Engine_result_type>
0059       _Working_result_type;
0060 #ifdef _LIBCPP_CXX03_LANG
0061   static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min + _Working_result_type(1);
0062 #else
0063   static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min() + _Working_result_type(1);
0064 #endif
0065   static _LIBCPP_CONSTEXPR const size_t __m                = __log2<_Working_result_type, _Rp>::value;
0066   static _LIBCPP_CONSTEXPR const size_t __n                = __get_n<_Working_result_type, _Rp, __w, __m>::value;
0067   static _LIBCPP_CONSTEXPR const size_t __w0               = __w / __n;
0068   static _LIBCPP_CONSTEXPR const size_t __n0               = __n - __w % __n;
0069   static _LIBCPP_CONSTEXPR const size_t _WDt               = numeric_limits<_Working_result_type>::digits;
0070   static _LIBCPP_CONSTEXPR const size_t _EDt               = numeric_limits<_Engine_result_type>::digits;
0071   static _LIBCPP_CONSTEXPR const _Working_result_type __y0 = __w0 >= _WDt ? 0 : (_Rp >> __w0) << __w0;
0072   static _LIBCPP_CONSTEXPR const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 : (_Rp >> (__w0 + 1)) << (__w0 + 1);
0073   static _LIBCPP_CONSTEXPR const
0074       _Engine_result_type __mask0 = __w0 > 0 ? _Engine_result_type(~0) >> (_EDt - __w0) : _Engine_result_type(0);
0075   static _LIBCPP_CONSTEXPR const _Engine_result_type __mask1 =
0076       __w0 < _EDt - 1 ? _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) : _Engine_result_type(~0);
0077 
0078 public:
0079   static _LIBCPP_CONSTEXPR const result_type _Min = 0;
0080   static _LIBCPP_CONSTEXPR const result_type _Max =
0081       __w == _Dt ? result_type(~0) : (result_type(1) << __w) - result_type(1);
0082   static_assert(_Min < _Max, "independent_bits_engine invalid parameters");
0083 
0084   // engine characteristics
0085   _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
0086   _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
0087 
0088   // constructors and seeding functions
0089   _LIBCPP_HIDE_FROM_ABI independent_bits_engine() {}
0090   _LIBCPP_HIDE_FROM_ABI explicit independent_bits_engine(const _Engine& __e) : __e_(__e) {}
0091 #ifndef _LIBCPP_CXX03_LANG
0092   _LIBCPP_HIDE_FROM_ABI explicit independent_bits_engine(_Engine&& __e) : __e_(std::move(__e)) {}
0093 #endif // _LIBCPP_CXX03_LANG
0094   _LIBCPP_HIDE_FROM_ABI explicit independent_bits_engine(result_type __sd) : __e_(__sd) {}
0095   template <
0096       class _Sseq,
0097       __enable_if_t<__is_seed_sequence<_Sseq, independent_bits_engine>::value && !is_convertible<_Sseq, _Engine>::value,
0098                     int> = 0>
0099   _LIBCPP_HIDE_FROM_ABI explicit independent_bits_engine(_Sseq& __q) : __e_(__q) {}
0100   _LIBCPP_HIDE_FROM_ABI void seed() { __e_.seed(); }
0101   _LIBCPP_HIDE_FROM_ABI void seed(result_type __sd) { __e_.seed(__sd); }
0102   template <class _Sseq, __enable_if_t<__is_seed_sequence<_Sseq, independent_bits_engine>::value, int> = 0>
0103   _LIBCPP_HIDE_FROM_ABI void seed(_Sseq& __q) {
0104     __e_.seed(__q);
0105   }
0106 
0107   // generating functions
0108   _LIBCPP_HIDE_FROM_ABI result_type operator()() { return __eval(integral_constant<bool, _Rp != 0>()); }
0109   _LIBCPP_HIDE_FROM_ABI void discard(unsigned long long __z) {
0110     for (; __z; --__z)
0111       operator()();
0112   }
0113 
0114   // property functions
0115   _LIBCPP_HIDE_FROM_ABI const _Engine& base() const _NOEXCEPT { return __e_; }
0116 
0117   template <class _Eng, size_t _Wp, class _UInt>
0118   friend bool operator==(const independent_bits_engine<_Eng, _Wp, _UInt>& __x,
0119                          const independent_bits_engine<_Eng, _Wp, _UInt>& __y);
0120 
0121   template <class _Eng, size_t _Wp, class _UInt>
0122   friend bool operator!=(const independent_bits_engine<_Eng, _Wp, _UInt>& __x,
0123                          const independent_bits_engine<_Eng, _Wp, _UInt>& __y);
0124 
0125   template <class _CharT, class _Traits, class _Eng, size_t _Wp, class _UInt>
0126   friend basic_ostream<_CharT, _Traits>&
0127   operator<<(basic_ostream<_CharT, _Traits>& __os, const independent_bits_engine<_Eng, _Wp, _UInt>& __x);
0128 
0129   template <class _CharT, class _Traits, class _Eng, size_t _Wp, class _UInt>
0130   friend basic_istream<_CharT, _Traits>&
0131   operator>>(basic_istream<_CharT, _Traits>& __is, independent_bits_engine<_Eng, _Wp, _UInt>& __x);
0132 
0133 private:
0134   _LIBCPP_HIDE_FROM_ABI result_type __eval(false_type);
0135   _LIBCPP_HIDE_FROM_ABI result_type __eval(true_type);
0136 
0137   template <size_t __count,
0138             __enable_if_t<__count< _Dt, int> = 0> _LIBCPP_HIDE_FROM_ABI static result_type __lshift(result_type __x) {
0139     return __x << __count;
0140   }
0141 
0142   template <size_t __count, __enable_if_t<(__count >= _Dt), int> = 0>
0143   _LIBCPP_HIDE_FROM_ABI static result_type __lshift(result_type) {
0144     return result_type(0);
0145   }
0146 };
0147 
0148 template <class _Engine, size_t __w, class _UIntType>
0149 inline _UIntType independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type) {
0150   return static_cast<result_type>(__e_() & __mask0);
0151 }
0152 
0153 template <class _Engine, size_t __w, class _UIntType>
0154 _UIntType independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type) {
0155   result_type __sp = 0;
0156   for (size_t __k = 0; __k < __n0; ++__k) {
0157     _Engine_result_type __u;
0158     do {
0159       __u = __e_() - _Engine::min();
0160     } while (__u >= __y0);
0161     __sp = static_cast<result_type>(__lshift<__w0>(__sp) + (__u & __mask0));
0162   }
0163   for (size_t __k = __n0; __k < __n; ++__k) {
0164     _Engine_result_type __u;
0165     do {
0166       __u = __e_() - _Engine::min();
0167     } while (__u >= __y1);
0168     __sp = static_cast<result_type>(__lshift<__w0 + 1>(__sp) + (__u & __mask1));
0169   }
0170   return __sp;
0171 }
0172 
0173 template <class _Eng, size_t _Wp, class _UInt>
0174 inline _LIBCPP_HIDE_FROM_ABI bool
0175 operator==(const independent_bits_engine<_Eng, _Wp, _UInt>& __x, const independent_bits_engine<_Eng, _Wp, _UInt>& __y) {
0176   return __x.base() == __y.base();
0177 }
0178 
0179 template <class _Eng, size_t _Wp, class _UInt>
0180 inline _LIBCPP_HIDE_FROM_ABI bool
0181 operator!=(const independent_bits_engine<_Eng, _Wp, _UInt>& __x, const independent_bits_engine<_Eng, _Wp, _UInt>& __y) {
0182   return !(__x == __y);
0183 }
0184 
0185 template <class _CharT, class _Traits, class _Eng, size_t _Wp, class _UInt>
0186 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0187 operator<<(basic_ostream<_CharT, _Traits>& __os, const independent_bits_engine<_Eng, _Wp, _UInt>& __x) {
0188   return __os << __x.base();
0189 }
0190 
0191 template <class _CharT, class _Traits, class _Eng, size_t _Wp, class _UInt>
0192 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
0193 operator>>(basic_istream<_CharT, _Traits>& __is, independent_bits_engine<_Eng, _Wp, _UInt>& __x) {
0194   _Eng __e;
0195   __is >> __e;
0196   if (!__is.fail())
0197     __x.__e_ = __e;
0198   return __is;
0199 }
0200 
0201 _LIBCPP_END_NAMESPACE_STD
0202 
0203 _LIBCPP_POP_MACROS
0204 
0205 #endif // _LIBCPP___CXX03___RANDOM_INDEPENDENT_BITS_ENGINE_H