summaryrefslogtreecommitdiff
path: root/boost/random/independent_bits.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'boost/random/independent_bits.hpp')
-rw-r--r--boost/random/independent_bits.hpp12
1 files changed, 12 insertions, 0 deletions
diff --git a/boost/random/independent_bits.hpp b/boost/random/independent_bits.hpp
index dec63b3f46..cd2dd1f225 100644
--- a/boost/random/independent_bits.hpp
+++ b/boost/random/independent_bits.hpp
@@ -158,6 +158,18 @@ public:
BOOST_ASSERT(n0*w0 + (n - n0)*(w0 + 1) == w);
+ BOOST_ASSERT((n == 1) == (w0 == w));
+
+ // special case to avoid undefined behavior from shifting
+ if(n == 1) {
+ BOOST_ASSERT(n0 == 1);
+ base_unsigned u;
+ do {
+ u = detail::subtract<base_result_type>()(_base(), (_base.min)());
+ } while(u > base_unsigned(y0 - 1));
+ return u & y0_mask;
+ }
+
result_type S = 0;
for(std::size_t k = 0; k < n0; ++k) {
base_unsigned u;