1 ///////////////////////////////////////////////////////////////
2 // Copyright 2017 John Maddock. Distributed under the Boost
3 // Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
7 // Check results of truncated overflow.
11 #define _SCL_SECURE_NO_WARNINGS
14 #include <boost/multiprecision/cpp_int.hpp>
16 #include <boost/random/mersenne_twister.hpp>
17 #include <boost/random/uniform_int.hpp>
20 T generate_random(unsigned bits_wanted)
22 static boost::random::mt19937 gen;
23 typedef boost::random::mt19937::result_type random_type;
27 if (std::numeric_limits<T>::is_bounded && (bits_wanted == (unsigned)std::numeric_limits<T>::digits))
29 max_val = (std::numeric_limits<T>::max)();
30 digits = std::numeric_limits<T>::digits;
34 max_val = T(1) << bits_wanted;
38 unsigned bits_per_r_val = std::numeric_limits<random_type>::digits - 1;
39 while ((random_type(1) << bits_per_r_val) > (gen.max)())
42 unsigned terms_needed = digits / bits_per_r_val + 1;
45 for (unsigned i = 0; i < terms_needed; ++i)
54 template <class Number>
57 using namespace boost::multiprecision;
58 typedef Number test_type;
60 for (unsigned i = 30; i < std::numeric_limits<test_type>::digits; ++i)
62 for (unsigned j = std::numeric_limits<test_type>::digits - i - 1; j < std::numeric_limits<test_type>::digits; ++j)
64 for (unsigned k = 0; k < 10; ++k)
66 test_type a = static_cast<test_type>(generate_random<cpp_int>(i));
67 test_type b = static_cast<test_type>(generate_random<cpp_int>(j));
68 test_type c = static_cast<test_type>(cpp_int(a) * cpp_int(b));
70 BOOST_CHECK_EQUAL(c, d);
72 if ((k == 0) && (j == 0))
74 for (unsigned s = 1; s < std::numeric_limits<test_type>::digits; ++s)
75 BOOST_CHECK_EQUAL(a << s, test_type(cpp_int(a) << s));
84 using namespace boost::multiprecision;
90 // We also need to test type with "odd" bit counts in order to ensure full code coverage:
92 test<number<cpp_int_backend<528, 528, signed_magnitude, unchecked, void> > >();
93 test<number<cpp_int_backend<528, 528, unsigned_magnitude, unchecked, void> > >();
94 test<number<cpp_int_backend<48, 48, signed_magnitude, unchecked, void> > >();
95 test<number<cpp_int_backend<48, 48, unsigned_magnitude, unchecked, void> > >();
96 return boost::report_errors();