Changeset dcd7804 in mainline
- Timestamp:
- 2018-07-05T21:41:22Z (6 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- ffd94b9
- Parents:
- 87f625f
- git-author:
- Dzejrou <dzejrou@…> (2018-05-03 15:26:38)
- git-committer:
- Dzejrou <dzejrou@…> (2018-07-05 21:41:22)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/cpp/include/impl/random.hpp
r87f625f rdcd7804 30 30 #define LIBCPP_RANDOM 31 31 32 #include <cstdint> 32 33 #include <cstdlib> 33 34 #include <ctime> … … 122 123 void seed(result_type s = default_seed) 123 124 { 124 if (c % modulus_ == 0 && s == 0)125 state_ = 0;125 if (c % modulus_ == 0 && s % modulus_ == 0) 126 state_ = 1; 126 127 else 127 state_ = s ;128 state_ = s % modulus_; 128 129 } 129 130 … … 133 134 ) 134 135 { 135 q.generate(arr_, arr_ + k_ + 3); 136 auto k = static_cast<size_t>(aux::ceil(aux::log2(modulus_) / 32)); 137 auto arr = new result_type[k + 3]; 138 139 q.generate(arr, arr + k + 3); 136 140 137 141 result_type s{}; 138 for (size_t j = 0; j < k _; ++j)139 s += arr _[j + 3] * aux::pow2(32U * j);142 for (size_t j = 0; j < k; ++j) 143 s += arr[j + 3] * aux::pow2(32U * j); 140 144 s = s % modulus_; 141 145 142 seed(s); 146 if (c % modulus_ == 0 && s == 0) 147 state_ = 1; 148 else 149 state_ = s % modulus_; 150 delete[] arr; 143 151 } 144 152 … … 197 205 static constexpr result_type modulus_ = 198 206 (m == 0) ? (numeric_limits<result_type>::max() + 1) : m; 199 200 /**201 * We use constexpr builtins to keep this array202 * between calls to seed(Seq&), which means we don't203 * have to keep allocating and deleting it.204 */205 static constexpr size_t k_ = static_cast<size_t>(aux::ceil(aux::log2(modulus_) / 32));206 result_type arr_[k_ + 3];207 207 208 208 void transition_() … … 303 303 ) 304 304 { 305 q.generate(arr_, arr_ + n * k_); 305 auto k = static_cast<size_t>(w / 32); 306 auto arr = new result_type[n * k]; 307 q.generate(arr, arr + n * k); 306 308 307 309 for (long long i = -n; i <= -1; ++i) 308 310 { 309 311 state_[idx_(i)] = result_type{}; 310 for (long long j = 0; j < k _; ++j)311 state_[idx_(i)] += arr _[k_* (i + n) + j] * aux::pow2(32 * j);312 for (long long j = 0; j < k; ++j) 313 state_[idx_(i)] += arr[k * (i + n) + j] * aux::pow2(32 * j); 312 314 state_[idx_(i)] %= aux::pow2(w); 313 315 } 316 317 delete[] arr; 314 318 } 315 319 … … 384 388 result_type state_[n]; 385 389 size_t i_; 386 387 static constexpr size_t k_ = static_cast<size_t>(w / 32);388 result_type arr_[n * k_];389 390 390 391 void transition_() … … 1069 1070 { 1070 1071 while (first != last) 1071 vec_.push_back( *first++ % aux::pow2(32));1072 vec_.push_back((*first++) % aux::pow2u(32)); 1072 1073 } 1073 1074 … … 1079 1080 return; 1080 1081 1081 // TODO: research this 1082 auto s = vec_.size(); 1083 size_t n = last - first; 1084 result_type t = (n >= 623) ? 11 : (n >= 68) ? 7 : (n >= 39) ? 5 : (n >= 7) ? 3 : (n - 1) / 2; 1085 result_type p = (n - t) / 2; 1086 result_type q = p + t; 1087 1088 auto current = first; 1089 while (current != last) 1090 *current++ = 0x8b8b8b8b; 1091 1092 auto m = (s + 1 > n) ? (s + 1) : n; 1093 decltype(m) k{}; 1094 for (; k < m; ++k) 1095 { 1096 auto r1 = 1664525 * t_(first[k % n] ^ first[(k + p) % n] ^ first[(k - 1) % n]); 1097 auto r2 = r1; 1098 1099 if (k == 0) 1100 r2 += s; 1101 else if (k > 0 && k <= s) 1102 r2 += (k % n) + vec_[(k - 1) % n]; 1103 else if (s < k) 1104 r2 += (k % n); 1105 1106 first[(k + p) % n] += r1; 1107 first[(k + q) % n] += r2; 1108 first[k % n] = r2; 1109 } 1110 1111 for (; k < m + n - 1; ++k) 1112 { 1113 auto r3 = 1566083941 * t_(first[k % n] + first[(k + p) % n] + first[(k - 1) % n]); 1114 auto r4 = r3 - (k % n); 1115 1116 first[(k + p) % n] ^= r3; 1117 first[(k + q) % n] ^= r4; 1118 first[k % n] = r4; 1119 } 1082 1120 } 1083 1121 … … 1099 1137 private: 1100 1138 vector<result_type> vec_; 1139 1140 result_type t_(result_type val) const 1141 { 1142 return val ^ (val >> 27); 1143 } 1101 1144 }; 1102 1145
Note:
See TracChangeset
for help on using the changeset viewer.