Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:14:00

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___RANDOM_SUBTRACT_WITH_CARRY_ENGINE_H
0010 #define _LIBCPP___RANDOM_SUBTRACT_WITH_CARRY_ENGINE_H
0011 
0012 #include <__algorithm/equal.h>
0013 #include <__algorithm/min.h>
0014 #include <__config>
0015 #include <__cstddef/size_t.h>
0016 #include <__random/is_seed_sequence.h>
0017 #include <__random/linear_congruential_engine.h>
0018 #include <__type_traits/enable_if.h>
0019 #include <cstdint>
0020 #include <iosfwd>
0021 #include <limits>
0022 
0023 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0024 #  pragma GCC system_header
0025 #endif
0026 
0027 _LIBCPP_PUSH_MACROS
0028 #include <__undef_macros>
0029 
0030 _LIBCPP_BEGIN_NAMESPACE_STD
0031 
0032 template <class _UIntType, size_t __w, size_t __s, size_t __r>
0033 class _LIBCPP_TEMPLATE_VIS subtract_with_carry_engine;
0034 
0035 template <class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
0036 _LIBCPP_HIDE_FROM_ABI bool operator==(const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x,
0037                                       const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y);
0038 
0039 template <class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
0040 _LIBCPP_HIDE_FROM_ABI bool operator!=(const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x,
0041                                       const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y);
0042 
0043 template <class _CharT, class _Traits, class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
0044 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0045 operator<<(basic_ostream<_CharT, _Traits>& __os, const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x);
0046 
0047 template <class _CharT, class _Traits, class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
0048 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
0049 operator>>(basic_istream<_CharT, _Traits>& __is, subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x);
0050 
0051 template <class _UIntType, size_t __w, size_t __s, size_t __r>
0052 class _LIBCPP_TEMPLATE_VIS subtract_with_carry_engine {
0053 public:
0054   // types
0055   typedef _UIntType result_type;
0056 
0057 private:
0058   result_type __x_[__r];
0059   result_type __c_;
0060   size_t __i_;
0061 
0062   static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;
0063   static_assert(0 < __w, "subtract_with_carry_engine invalid parameters");
0064   static_assert(__w <= _Dt, "subtract_with_carry_engine invalid parameters");
0065   static_assert(0 < __s, "subtract_with_carry_engine invalid parameters");
0066   static_assert(__s < __r, "subtract_with_carry_engine invalid parameters");
0067 
0068 public:
0069   static _LIBCPP_CONSTEXPR const result_type _Min = 0;
0070   static _LIBCPP_CONSTEXPR const result_type _Max =
0071       __w == _Dt ? result_type(~0) : (result_type(1) << __w) - result_type(1);
0072   static_assert(_Min < _Max, "subtract_with_carry_engine invalid parameters");
0073 
0074   // engine characteristics
0075   static inline _LIBCPP_CONSTEXPR const size_t word_size = __w;
0076   static inline _LIBCPP_CONSTEXPR const size_t short_lag = __s;
0077   static inline _LIBCPP_CONSTEXPR const size_t long_lag  = __r;
0078   _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
0079   _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
0080   static inline _LIBCPP_CONSTEXPR const result_type default_seed = 19780503u;
0081 
0082   // constructors and seeding functions
0083 #ifndef _LIBCPP_CXX03_LANG
0084   _LIBCPP_HIDE_FROM_ABI subtract_with_carry_engine() : subtract_with_carry_engine(default_seed) {}
0085   _LIBCPP_HIDE_FROM_ABI explicit subtract_with_carry_engine(result_type __sd) { seed(__sd); }
0086 #else
0087   _LIBCPP_HIDE_FROM_ABI explicit subtract_with_carry_engine(result_type __sd = default_seed) { seed(__sd); }
0088 #endif
0089   template <class _Sseq, __enable_if_t<__is_seed_sequence<_Sseq, subtract_with_carry_engine>::value, int> = 0>
0090   _LIBCPP_HIDE_FROM_ABI explicit subtract_with_carry_engine(_Sseq& __q) {
0091     seed(__q);
0092   }
0093   _LIBCPP_HIDE_FROM_ABI void seed(result_type __sd = default_seed) {
0094     seed(__sd, integral_constant<unsigned, 1 + (__w - 1) / 32>());
0095   }
0096   template <class _Sseq, __enable_if_t<__is_seed_sequence<_Sseq, subtract_with_carry_engine>::value, int> = 0>
0097   _LIBCPP_HIDE_FROM_ABI void seed(_Sseq& __q) {
0098     __seed(__q, integral_constant<unsigned, 1 + (__w - 1) / 32>());
0099   }
0100 
0101   // generating functions
0102   _LIBCPP_HIDE_FROM_ABI result_type operator()();
0103   _LIBCPP_HIDE_FROM_ABI void discard(unsigned long long __z) {
0104     for (; __z; --__z)
0105       operator()();
0106   }
0107 
0108   template <class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
0109   friend bool operator==(const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x,
0110                          const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y);
0111 
0112   template <class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
0113   friend bool operator!=(const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x,
0114                          const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y);
0115 
0116   template <class _CharT, class _Traits, class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
0117   friend basic_ostream<_CharT, _Traits>&
0118   operator<<(basic_ostream<_CharT, _Traits>& __os, const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x);
0119 
0120   template <class _CharT, class _Traits, class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
0121   friend basic_istream<_CharT, _Traits>&
0122   operator>>(basic_istream<_CharT, _Traits>& __is, subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x);
0123 
0124 private:
0125   _LIBCPP_HIDE_FROM_ABI void seed(result_type __sd, integral_constant<unsigned, 1>);
0126   _LIBCPP_HIDE_FROM_ABI void seed(result_type __sd, integral_constant<unsigned, 2>);
0127   template <class _Sseq>
0128   _LIBCPP_HIDE_FROM_ABI void __seed(_Sseq& __q, integral_constant<unsigned, 1>);
0129   template <class _Sseq>
0130   _LIBCPP_HIDE_FROM_ABI void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
0131 };
0132 
0133 template <class _UIntType, size_t __w, size_t __s, size_t __r>
0134 void subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd, integral_constant<unsigned, 1>) {
0135   linear_congruential_engine<result_type, 40014u, 0u, 2147483563u> __e(__sd == 0u ? default_seed : __sd);
0136   for (size_t __i = 0; __i < __r; ++__i)
0137     __x_[__i] = static_cast<result_type>(__e() & _Max);
0138   __c_ = __x_[__r - 1] == 0;
0139   __i_ = 0;
0140 }
0141 
0142 template <class _UIntType, size_t __w, size_t __s, size_t __r>
0143 void subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd, integral_constant<unsigned, 2>) {
0144   linear_congruential_engine<result_type, 40014u, 0u, 2147483563u> __e(__sd == 0u ? default_seed : __sd);
0145   for (size_t __i = 0; __i < __r; ++__i) {
0146     result_type __e0 = __e();
0147     __x_[__i]        = static_cast<result_type>((__e0 + ((uint64_t)__e() << 32)) & _Max);
0148   }
0149   __c_ = __x_[__r - 1] == 0;
0150   __i_ = 0;
0151 }
0152 
0153 template <class _UIntType, size_t __w, size_t __s, size_t __r>
0154 template <class _Sseq>
0155 void subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q, integral_constant<unsigned, 1>) {
0156   const unsigned __k = 1;
0157   uint32_t __ar[__r * __k];
0158   __q.generate(__ar, __ar + __r * __k);
0159   for (size_t __i = 0; __i < __r; ++__i)
0160     __x_[__i] = static_cast<result_type>(__ar[__i] & _Max);
0161   __c_ = __x_[__r - 1] == 0;
0162   __i_ = 0;
0163 }
0164 
0165 template <class _UIntType, size_t __w, size_t __s, size_t __r>
0166 template <class _Sseq>
0167 void subtract_with_carry_engine<_UIntType, __w, __s, __r>::__seed(_Sseq& __q, integral_constant<unsigned, 2>) {
0168   const unsigned __k = 2;
0169   uint32_t __ar[__r * __k];
0170   __q.generate(__ar, __ar + __r * __k);
0171   for (size_t __i = 0; __i < __r; ++__i)
0172     __x_[__i] = static_cast<result_type>((__ar[2 * __i] + ((uint64_t)__ar[2 * __i + 1] << 32)) & _Max);
0173   __c_ = __x_[__r - 1] == 0;
0174   __i_ = 0;
0175 }
0176 
0177 template <class _UIntType, size_t __w, size_t __s, size_t __r>
0178 _UIntType subtract_with_carry_engine<_UIntType, __w, __s, __r>::operator()() {
0179   const result_type& __xs = __x_[(__i_ + (__r - __s)) % __r];
0180   result_type& __xr       = __x_[__i_];
0181   result_type __new_c     = __c_ == 0 ? __xs < __xr : __xs != 0 ? __xs <= __xr : 1;
0182   __xr                    = (__xs - __xr - __c_) & _Max;
0183   __c_                    = __new_c;
0184   __i_                    = (__i_ + 1) % __r;
0185   return __xr;
0186 }
0187 
0188 template <class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
0189 _LIBCPP_HIDE_FROM_ABI bool operator==(const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x,
0190                                       const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y) {
0191   if (__x.__c_ != __y.__c_)
0192     return false;
0193   if (__x.__i_ == __y.__i_)
0194     return std::equal(__x.__x_, __x.__x_ + _Rp, __y.__x_);
0195   if (__x.__i_ == 0 || __y.__i_ == 0) {
0196     size_t __j = std::min(_Rp - __x.__i_, _Rp - __y.__i_);
0197     if (!std::equal(__x.__x_ + __x.__i_, __x.__x_ + __x.__i_ + __j, __y.__x_ + __y.__i_))
0198       return false;
0199     if (__x.__i_ == 0)
0200       return std::equal(__x.__x_ + __j, __x.__x_ + _Rp, __y.__x_);
0201     return std::equal(__x.__x_, __x.__x_ + (_Rp - __j), __y.__x_ + __j);
0202   }
0203   if (__x.__i_ < __y.__i_) {
0204     size_t __j = _Rp - __y.__i_;
0205     if (!std::equal(__x.__x_ + __x.__i_, __x.__x_ + (__x.__i_ + __j), __y.__x_ + __y.__i_))
0206       return false;
0207     if (!std::equal(__x.__x_ + (__x.__i_ + __j), __x.__x_ + _Rp, __y.__x_))
0208       return false;
0209     return std::equal(__x.__x_, __x.__x_ + __x.__i_, __y.__x_ + (_Rp - (__x.__i_ + __j)));
0210   }
0211   size_t __j = _Rp - __x.__i_;
0212   if (!std::equal(__y.__x_ + __y.__i_, __y.__x_ + (__y.__i_ + __j), __x.__x_ + __x.__i_))
0213     return false;
0214   if (!std::equal(__y.__x_ + (__y.__i_ + __j), __y.__x_ + _Rp, __x.__x_))
0215     return false;
0216   return std::equal(__y.__x_, __y.__x_ + __y.__i_, __x.__x_ + (_Rp - (__y.__i_ + __j)));
0217 }
0218 
0219 template <class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
0220 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x,
0221                                              const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __y) {
0222   return !(__x == __y);
0223 }
0224 
0225 template <class _CharT, class _Traits, class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
0226 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0227 operator<<(basic_ostream<_CharT, _Traits>& __os, const subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x) {
0228   __save_flags<_CharT, _Traits> __lx(__os);
0229   typedef basic_ostream<_CharT, _Traits> _Ostream;
0230   __os.flags(_Ostream::dec | _Ostream::left);
0231   _CharT __sp = __os.widen(' ');
0232   __os.fill(__sp);
0233   __os << __x.__x_[__x.__i_];
0234   for (size_t __j = __x.__i_ + 1; __j < _Rp; ++__j)
0235     __os << __sp << __x.__x_[__j];
0236   for (size_t __j = 0; __j < __x.__i_; ++__j)
0237     __os << __sp << __x.__x_[__j];
0238   __os << __sp << __x.__c_;
0239   return __os;
0240 }
0241 
0242 template <class _CharT, class _Traits, class _UInt, size_t _Wp, size_t _Sp, size_t _Rp>
0243 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
0244 operator>>(basic_istream<_CharT, _Traits>& __is, subtract_with_carry_engine<_UInt, _Wp, _Sp, _Rp>& __x) {
0245   __save_flags<_CharT, _Traits> __lx(__is);
0246   typedef basic_istream<_CharT, _Traits> _Istream;
0247   __is.flags(_Istream::dec | _Istream::skipws);
0248   _UInt __t[_Rp + 1];
0249   for (size_t __i = 0; __i < _Rp + 1; ++__i)
0250     __is >> __t[__i];
0251   if (!__is.fail()) {
0252     for (size_t __i = 0; __i < _Rp; ++__i)
0253       __x.__x_[__i] = __t[__i];
0254     __x.__c_ = __t[_Rp];
0255     __x.__i_ = 0;
0256   }
0257   return __is;
0258 }
0259 
0260 _LIBCPP_END_NAMESPACE_STD
0261 
0262 _LIBCPP_POP_MACROS
0263 
0264 #endif // _LIBCPP___RANDOM_SUBTRACT_WITH_CARRY_ENGINE_H