]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/multiprecision/test/test_cpp_int_left_shift.cpp
e0fe6c7f0e65e49620fa07baf26fe20b2c8149e3
[ceph.git] / ceph / src / boost / libs / multiprecision / test / test_cpp_int_left_shift.cpp
1 ///////////////////////////////////////////////////////////////
2 // Copyright 2012 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
5
6 //
7 // Compare results of truncated left shift to gmp, see:
8 // https://svn.boost.org/trac/boost/ticket/12790
9 //
10
11 #ifdef _MSC_VER
12 #define _SCL_SECURE_NO_WARNINGS
13 #endif
14
15 #include <boost/multiprecision/gmp.hpp>
16 #include <boost/multiprecision/cpp_int.hpp>
17 #include <boost/random/mersenne_twister.hpp>
18 #include <boost/random/uniform_int.hpp>
19 #include "test.hpp"
20
21 #if !defined(TEST1) && !defined(TEST2) && !defined(TEST3)
22 #define TEST1
23 #define TEST2
24 #define TEST3
25 #endif
26
27 template <class T>
28 T generate_random(unsigned bits_wanted)
29 {
30 static boost::random::mt19937 gen;
31 typedef boost::random::mt19937::result_type random_type;
32
33 T max_val;
34 unsigned digits;
35 if (std::numeric_limits<T>::is_bounded && (bits_wanted == (unsigned)std::numeric_limits<T>::digits))
36 {
37 max_val = (std::numeric_limits<T>::max)();
38 digits = std::numeric_limits<T>::digits;
39 }
40 else
41 {
42 max_val = T(1) << bits_wanted;
43 digits = bits_wanted;
44 }
45
46 unsigned bits_per_r_val = std::numeric_limits<random_type>::digits - 1;
47 while ((random_type(1) << bits_per_r_val) > (gen.max)())
48 --bits_per_r_val;
49
50 unsigned terms_needed = digits / bits_per_r_val + 1;
51
52 T val = 0;
53 for (unsigned i = 0; i < terms_needed; ++i)
54 {
55 val *= (gen.max)();
56 val += gen();
57 }
58 val %= max_val;
59 return val;
60 }
61
62 template <class T>
63 void test_value(const T& val)
64 {
65 boost::multiprecision::mpz_int z(val.str()), mask(1);
66 mask <<= std::numeric_limits<T>::digits;
67 --mask;
68
69 for (unsigned i = 0; i <= std::numeric_limits<T>::digits + 2; ++i)
70 {
71 BOOST_CHECK_EQUAL((val << i).str(), boost::multiprecision::mpz_int(((z << i) & mask)).str());
72 }
73 }
74
75 void test(const boost::mpl::int_<200>&) {}
76
77 template <int N>
78 void test(boost::mpl::int_<N> const&)
79 {
80 test(boost::mpl::int_<N + 4>());
81
82 typedef boost::multiprecision::number<boost::multiprecision::cpp_int_backend<N, N, boost::multiprecision::unsigned_magnitude>, boost::multiprecision::et_off> mp_type;
83
84 std::cout << "Running tests for precision: " << N << std::endl;
85
86 mp_type mp(-1);
87 test_value(mp);
88
89 for (unsigned i = 0; i < 1000; ++i)
90 test_value(generate_random<mp_type>(std::numeric_limits<mp_type>::digits));
91 }
92
93 int main()
94 {
95 test(boost::mpl::int_<24>());
96 return boost::report_errors();
97 }