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> { static const size_t value = 0; };
90
91template <size_t _Rp> struct __log2_imp<0, _Rp> { static const size_t value = _Rp + 1; };
92
93template <class _UIntType, _UIntType _Xp> struct __log2 {
94 static const size_t value = __log2_imp<_Xp, sizeof(_UIntType) * __CHAR_BIT__ - 1>::value;
95};
96
97template <class _Engine, class _UIntType> class __independent_bits_engine {
98public:
99 // types
100 using result_type = _UIntType;
101
102private:
103 using _Engine_result_type = typename _Engine::result_type;
104 typedef typename std::conditional<sizeof(_Engine_result_type) <= sizeof(result_type), result_type,
105 _Engine_result_type>::type _Working_result_type;
106
107 _Engine &__e_;
108 size_t __w_;
109 size_t __w0_;
110 size_t __n_;
111 size_t __n0_;
112 _Working_result_type __y0_;
113 _Working_result_type __y1_;
114 _Engine_result_type __mask0_;
115 _Engine_result_type __mask1_;
116
117#ifdef _MSC_VER
118#pragma warning(push)
119#pragma warning(disable : 4307)
120#endif
121 static constexpr const _Working_result_type _Rp = _Engine::max() - _Engine::min() + _Working_result_type(1);
122#ifdef _MSC_VER
123#pragma warning(pop)
124#endif
125 static constexpr const size_t __m = __log2<_Working_result_type, _Rp>::value;
126 static constexpr const size_t _WDt = std::numeric_limits<_Working_result_type>::digits;
127 static constexpr const size_t _EDt = std::numeric_limits<_Engine_result_type>::digits;
128
129public:
130 // constructors and seeding functions
131 __independent_bits_engine(_Engine &__e, size_t __w);
132
133 // generating functions
134 result_type operator()() { return __eval(std::integral_constant<bool, _Rp != 0>()); }
135
136private:
137 result_type __eval(std::false_type);
138 result_type __eval(std::true_type);
139};
140
141template <class _Engine, class _UIntType>
142__independent_bits_engine<_Engine, _UIntType>::__independent_bits_engine(_Engine &__e, size_t __w)
143 : __e_(__e), __w_(__w) {
144 __n_ = __w_ / __m + (__w_ % __m != 0);
145 __w0_ = __w_ / __n_;
146 if (_Rp == 0)
147 __y0_ = _Rp;
148 else if (__w0_ < _WDt)
149 __y0_ = (_Rp >> __w0_) << __w0_;
150 else
151 __y0_ = 0;
152 if (_Rp - __y0_ > __y0_ / __n_) {
153 ++__n_;
154 __w0_ = __w_ / __n_;
155 if (__w0_ < _WDt)
156 __y0_ = (_Rp >> __w0_) << __w0_;
157 else
158 __y0_ = 0;
159 }
160 __n0_ = __n_ - __w_ % __n_;
161 if (__w0_ < _WDt - 1)
162 __y1_ = (_Rp >> (__w0_ + 1)) << (__w0_ + 1);
163 else
164 __y1_ = 0;
165 __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) : _Engine_result_type(0);
166 __mask1_ = __w0_ < _EDt - 1 ? _Engine_result_type(~0) >> (_EDt - (__w0_ + 1)) : _Engine_result_type(~0);
167}
168
169template <class _Engine, class _UIntType>
170inline _UIntType __independent_bits_engine<_Engine, _UIntType>::__eval(std::false_type) {
171 return static_cast<result_type>(__e_() & __mask0_);
172}
173
174template <class _Engine, class _UIntType>
175_UIntType __independent_bits_engine<_Engine, _UIntType>::__eval(std::true_type) {
176 const size_t _WRt = std::numeric_limits<result_type>::digits;
177 result_type _Sp = 0;
178 for (size_t __k = 0; __k < __n0_; ++__k) {
179 _Engine_result_type __u;
180 do {
181 __u = __e_() - _Engine::min();
182 } while (__u >= __y0_);
183 if (__w0_ < _WRt)
184 _Sp <<= __w0_;
185 else
186 _Sp = 0;
187 _Sp += static_cast<result_type>(__u & __mask0_);
188 }
189 for (size_t __k = __n0_; __k < __n_; ++__k) {
190 _Engine_result_type __u;
191 do {
192 __u = __e_() - _Engine::min();
193 } while (__u >= __y1_);
194 if (__w0_ < _WRt - 1)
195 _Sp <<= __w0_ + 1;
196 else
197 _Sp = 0;
198 _Sp += static_cast<result_type>(__u & __mask1_);
199 }
200 return _Sp;
201}
202
203//@endcond
204
205// uniform_int_distribution
206
207template <class _IntType = int> class uniform_int_distribution {
208public:
209 // types
210 using result_type = _IntType;
211
215
216 public:
218
219 explicit param_type(result_type __a = 0, result_type __b = std::numeric_limits<result_type>::max())
220 : __a_(__a), __b_(__b) {}
221
222 result_type a() const { return __a_; }
223 result_type b() const { return __b_; }
224
225 friend bool operator==(const param_type &__x, const param_type &__y) {
226 return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;
227 }
228 friend bool operator!=(const param_type &__x, const param_type &__y) { return !(__x == __y); }
229 };
230
231private:
233
234public:
235 // constructors and reset functions
236 explicit uniform_int_distribution(result_type __a = 0, result_type __b = std::numeric_limits<result_type>::max())
237 : __p_(param_type(__a, __b)) {}
238 explicit uniform_int_distribution(const param_type &__p) : __p_(__p) {}
239 void reset() {}
240
241 // generating functions
242 template <class _URNG> result_type operator()(_URNG &__g) { return (*this)(__g, __p_); }
243 template <class _URNG> result_type operator()(_URNG &__g, const param_type &__p);
244
245 // property functions
246 result_type a() const { return __p_.a(); }
247 result_type b() const { return __p_.b(); }
248
249 param_type param() const { return __p_; }
250 void param(const param_type &__p) { __p_ = __p; }
251
252 result_type min() const { return a(); }
253 result_type max() const { return b(); }
254
256 return __x.__p_ == __y.__p_;
257 }
259 return !(__x == __y);
260 }
261};
262
263template <class _IntType>
264template <class _URNG>
267 using _UIntType = typename std::conditional<sizeof(result_type) <= sizeof(uint32_t), uint32_t, uint64_t>::type;
268 const _UIntType _Rp = __p.b() - __p.a() + _UIntType(1);
269 if (_Rp == 1)
270 return __p.a();
271 const size_t _Dt = std::numeric_limits<_UIntType>::digits;
272 using _Eng = __independent_bits_engine<_URNG, _UIntType>;
273 if (_Rp == 0)
274 return static_cast<result_type>(_Eng(__g, _Dt)());
275 size_t __w = _Dt - __clz(_Rp) - 1;
276 if ((_Rp & (std::numeric_limits<_UIntType>::max() >> (_Dt - __w))) != 0)
277 ++__w;
278 _Eng __e(__g, __w);
279 _UIntType __u;
280 do {
281 __u = __e();
282 } while (__u >= _Rp);
283 return static_cast<result_type>(__u + __p.a());
284}
285
286} // namespace Kernel
287} // namespace Mantid
288
289// Clean up macros
290#undef INLINE_VISIBILITY
291#ifdef _MANTID_CHAR_BIT_DEFINED_HERE
292#undef __CHAR_BIT__
293#undef _MANTID_CHAR_BIT_DEFINED_HERE
294#endif
double value
The value of the point.
Definition: FitMW.cpp:51
bool result_type
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