Mantid
Loading...
Searching...
No Matches
uniform_int_distribution.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7#pragma once
8
9#include <limits>
10#include <random>
11#include <type_traits>
12
13// Implementation of uniform_int_distribution taken from llvm/libcxx at
14// https://github.com/llvm-mirror/libcxx/blob/master/include/algorithm#L2965
15//
16// Each of our supported platforms has the header but minor differences in the
17// results yield problems consistency of our tests across platorms.
18//
19// Modifications to enable compilation:
20// * define required macros just for this header
21// * prefix some structures with std:: now they are not in std:: namespace
22// * applied clang format
23
24#ifdef _MSC_VER
25#define __CHAR_BIT__ CHAR_BIT
26#define INLINE_VISIBILITY __forceinline
27#else
28#define INLINE_VISIBILITY __attribute__((__always_inline__))
29#endif
30
31namespace Mantid {
32namespace Kernel {
33
34//@cond
35
36// Precondition: __x != 0
37inline INLINE_VISIBILITY unsigned __clz(unsigned __x) {
38#ifndef _MSC_VER
39 return static_cast<unsigned>(__builtin_clz(__x));
40#else
41 static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
42 static_assert(sizeof(unsigned long) == 4, "");
43 unsigned long where;
44 // Search from LSB to MSB for first set bit.
45 // Returns zero if no set bit is found.
46 if (_BitScanReverse(&where, __x))
47 return 31 - where;
48 return 32; // Undefined Behavior.
49#endif
50}
51
52inline INLINE_VISIBILITY unsigned long __clz(unsigned long __x) {
53#ifndef _MSC_VER
54 return static_cast<unsigned long>(__builtin_clzl(__x));
55#else
56 static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
57 return __clz(static_cast<unsigned>(__x));
58#endif
59}
60
61inline INLINE_VISIBILITY unsigned long long __clz(unsigned long long __x) {
62#ifndef _MSC_VER
63 return static_cast<unsigned long long>(__builtin_clzll(__x));
64#else
65 unsigned long where;
66// BitScanReverse scans from MSB to LSB for first set bit.
67// Returns 0 if no set bit is found.
68#if defined(_LIBCPP_HAS_BITSCAN64)
69 if (_BitScanReverse64(&where, __x))
70 return static_cast<int>(63 - where);
71#else
72 // Scan the high 32 bits.
73 if (_BitScanReverse(&where, static_cast<unsigned long>(__x >> 32)))
74 return 63 - (where + 32); // Create a bit offset from the MSB.
75 // Scan the low 32 bits.
76 if (_BitScanReverse(&where, static_cast<unsigned long>(__x)))
77 return 63 - where;
78#endif
79 return 64; // Undefined Behavior.
80#endif // _MSC_VER
81}
82
83// __independent_bits_engine
84
85template <unsigned long long _Xp, size_t _Rp> struct __log2_imp {
86 static const size_t value = _Xp & ((unsigned long long)(1) << _Rp) ? _Rp : __log2_imp<_Xp, _Rp - 1>::value;
87};
88
89template <unsigned long long _Xp> struct __log2_imp<_Xp, 0> {
90 static const size_t value = 0;
91};
92
93template <size_t _Rp> struct __log2_imp<0, _Rp> {
94 static const size_t value = _Rp + 1;
95};
96
97template <class _UIntType, _UIntType _Xp> struct __log2 {
98 static const size_t value = __log2_imp<_Xp, sizeof(_UIntType) * __CHAR_BIT__ - 1>::value;
99};
100
101template <class _Engine, class _UIntType> class __independent_bits_engine {
102public:
103 // types
104 using result_type = _UIntType;
105
106private:
107 using _Engine_result_type = typename _Engine::result_type;
108 typedef typename std::conditional<sizeof(_Engine_result_type) <= sizeof(result_type), result_type,
109 _Engine_result_type>::type _Working_result_type;
110
111 _Engine &__e_;
112 size_t __w_;
113 size_t __w0_;
114 size_t __n_;
115 size_t __n0_;
116 _Working_result_type __y0_;
117 _Working_result_type __y1_;
118 _Engine_result_type __mask0_;
119 _Engine_result_type __mask1_;
120
121#ifdef _MSC_VER
122#pragma warning(push)
123#pragma warning(disable : 4307)
124#endif
125 static constexpr const _Working_result_type _Rp = _Engine::max() - _Engine::min() + _Working_result_type(1);
126#ifdef _MSC_VER
127#pragma warning(pop)
128#endif
129 static constexpr const size_t __m = __log2<_Working_result_type, _Rp>::value;
130 static constexpr const size_t _WDt = std::numeric_limits<_Working_result_type>::digits;
131 static constexpr const size_t _EDt = std::numeric_limits<_Engine_result_type>::digits;
132
133public:
134 // constructors and seeding functions
135 __independent_bits_engine(_Engine &__e, size_t __w);
136
137 // generating functions
138 result_type operator()() { return __eval(std::integral_constant<bool, _Rp != 0>()); }
139
140private:
141 result_type __eval(std::false_type);
142 result_type __eval(std::true_type);
143};
144
145template <class _Engine, class _UIntType>
146__independent_bits_engine<_Engine, _UIntType>::__independent_bits_engine(_Engine &__e, size_t __w)
147 : __e_(__e), __w_(__w) {
148 __n_ = __w_ / __m + (__w_ % __m != 0);
149 __w0_ = __w_ / __n_;
150 if (_Rp == 0)
151 __y0_ = _Rp;
152 else if (__w0_ < _WDt)
153 __y0_ = (_Rp >> __w0_) << __w0_;
154 else
155 __y0_ = 0;
156 if (_Rp - __y0_ > __y0_ / __n_) {
157 ++__n_;
158 __w0_ = __w_ / __n_;
159 if (__w0_ < _WDt)
160 __y0_ = (_Rp >> __w0_) << __w0_;
161 else
162 __y0_ = 0;
163 }
164 __n0_ = __n_ - __w_ % __n_;
165 if (__w0_ < _WDt - 1)
166 __y1_ = (_Rp >> (__w0_ + 1)) << (__w0_ + 1);
167 else
168 __y1_ = 0;
169 __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) : _Engine_result_type(0);
170 __mask1_ = __w0_ < _EDt - 1 ? _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) : _Engine_result_type(~0);
171}
172
173template <class _Engine, class _UIntType>
174inline _UIntType __independent_bits_engine<_Engine, _UIntType>::__eval(std::false_type) {
175 return static_cast<result_type>(__e_() & __mask0_);
176}
177
178template <class _Engine, class _UIntType>
179_UIntType __independent_bits_engine<_Engine, _UIntType>::__eval(std::true_type) {
180 const size_t _WRt = std::numeric_limits<result_type>::digits;
181 result_type _Sp = 0;
182 for (size_t __k = 0; __k < __n0_; ++__k) {
183 _Engine_result_type __u;
184 do {
185 __u = __e_() - _Engine::min();
186 } while (__u >= __y0_);
187 if (__w0_ < _WRt)
188 _Sp <<= __w0_;
189 else
190 _Sp = 0;
191 _Sp += static_cast<result_type>(__u & __mask0_);
192 }
193 for (size_t __k = __n0_; __k < __n_; ++__k) {
194 _Engine_result_type __u;
195 do {
196 __u = __e_() - _Engine::min();
197 } while (__u >= __y1_);
198 if (__w0_ < _WRt - 1)
199 _Sp <<= __w0_ + 1;
200 else
201 _Sp = 0;
202 _Sp += static_cast<result_type>(__u & __mask1_);
203 }
204 return _Sp;
205}
206
207//@endcond
208
209// uniform_int_distribution
210
211template <class _IntType = int> class uniform_int_distribution {
212public:
213 // types
214 using result_type = _IntType;
215
219
220 public:
222
223 explicit param_type(result_type __a = 0, result_type __b = std::numeric_limits<result_type>::max())
224 : __a_(__a), __b_(__b) {}
225
226 result_type a() const { return __a_; }
227 result_type b() const { return __b_; }
228
229 friend bool operator==(const param_type &__x, const param_type &__y) {
230 return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;
231 }
232 friend bool operator!=(const param_type &__x, const param_type &__y) { return !(__x == __y); }
233 };
234
235private:
237
238public:
239 // constructors and reset functions
240 explicit uniform_int_distribution(result_type __a = 0, result_type __b = std::numeric_limits<result_type>::max())
241 : __p_(param_type(__a, __b)) {}
242 explicit uniform_int_distribution(const param_type &__p) : __p_(__p) {}
243 void reset() {}
244
245 // generating functions
246 template <class _URNG> result_type operator()(_URNG &__g) { return (*this)(__g, __p_); }
247 template <class _URNG> result_type operator()(_URNG &__g, const param_type &__p);
248
249 // property functions
250 result_type a() const { return __p_.a(); }
251 result_type b() const { return __p_.b(); }
252
253 param_type param() const { return __p_; }
254 void param(const param_type &__p) { __p_ = __p; }
255
256 result_type min() const { return a(); }
257 result_type max() const { return b(); }
258
260 return __x.__p_ == __y.__p_;
261 }
263 return !(__x == __y);
264 }
265};
266
267template <class _IntType>
268template <class _URNG>
271 using _UIntType = typename std::conditional<sizeof(result_type) <= sizeof(uint32_t), uint32_t, uint64_t>::type;
272 const _UIntType _Rp = __p.b() - __p.a() + _UIntType(1);
273 if (_Rp == 1)
274 return __p.a();
275 const size_t _Dt = std::numeric_limits<_UIntType>::digits;
276 using _Eng = __independent_bits_engine<_URNG, _UIntType>;
277 if (_Rp == 0)
278 return static_cast<result_type>(_Eng(__g, _Dt)());
279 size_t __w = _Dt - __clz(_Rp) - 1;
280 if ((_Rp & (std::numeric_limits<_UIntType>::max() >> (_Dt - __w))) != 0)
281 ++__w;
282 _Eng __e(__g, __w);
283 _UIntType __u;
284 do {
285 __u = __e();
286 } while (__u >= _Rp);
287 return static_cast<result_type>(__u + __p.a());
288}
289
290} // namespace Kernel
291} // namespace Mantid
292
293// Clean up macros
294#undef INLINE_VISIBILITY
295#ifdef _MANTID_CHAR_BIT_DEFINED_HERE
296#undef __CHAR_BIT__
297#undef _MANTID_CHAR_BIT_DEFINED_HERE
298#endif
double value
The value of the point.
Definition FitMW.cpp:51
friend bool operator!=(const param_type &__x, const param_type &__y)
param_type(result_type __a=0, result_type __b=std::numeric_limits< result_type >::max())
friend bool operator==(const param_type &__x, const param_type &__y)
uniform_int_distribution(result_type __a=0, result_type __b=std::numeric_limits< result_type >::max())
friend bool operator==(const uniform_int_distribution &__x, const uniform_int_distribution &__y)
friend bool operator!=(const uniform_int_distribution &__x, const uniform_int_distribution &__y)
result_type operator()(_URNG &__g, const param_type &__p)
Helper class which provides the Collimation Length for SANS instruments.
#define INLINE_VISIBILITY