]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/multiprecision/test/test_cpp_int_left_shift.cpp
e0fe6c7f0e65e49620fa07baf26fe20b2c8149e3
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
7 // Compare results of truncated left shift to gmp, see:
8 // https://svn.boost.org/trac/boost/ticket/12790
12 #define _SCL_SECURE_NO_WARNINGS
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>
21 #if !defined(TEST1) && !defined(TEST2) && !defined(TEST3)
28 T
generate_random(unsigned bits_wanted
)
30 static boost::random::mt19937 gen
;
31 typedef boost::random::mt19937::result_type random_type
;
35 if (std::numeric_limits
<T
>::is_bounded
&& (bits_wanted
== (unsigned)std::numeric_limits
<T
>::digits
))
37 max_val
= (std::numeric_limits
<T
>::max
)();
38 digits
= std::numeric_limits
<T
>::digits
;
42 max_val
= T(1) << bits_wanted
;
46 unsigned bits_per_r_val
= std::numeric_limits
<random_type
>::digits
- 1;
47 while ((random_type(1) << bits_per_r_val
) > (gen
.max
)())
50 unsigned terms_needed
= digits
/ bits_per_r_val
+ 1;
53 for (unsigned i
= 0; i
< terms_needed
; ++i
)
63 void test_value(const T
& val
)
65 boost::multiprecision::mpz_int
z(val
.str()), mask(1);
66 mask
<<= std::numeric_limits
<T
>::digits
;
69 for (unsigned i
= 0; i
<= std::numeric_limits
<T
>::digits
+ 2; ++i
)
71 BOOST_CHECK_EQUAL((val
<< i
).str(), boost::multiprecision::mpz_int(((z
<< i
) & mask
)).str());
75 void test(const boost::mpl::int_
<200>&) {}
78 void test(boost::mpl::int_
<N
> const&)
80 test(boost::mpl::int_
<N
+ 4>());
82 typedef boost::multiprecision::number
<boost::multiprecision::cpp_int_backend
<N
, N
, boost::multiprecision::unsigned_magnitude
>, boost::multiprecision::et_off
> mp_type
;
84 std::cout
<< "Running tests for precision: " << N
<< std::endl
;
89 for (unsigned i
= 0; i
< 1000; ++i
)
90 test_value(generate_random
<mp_type
>(std::numeric_limits
<mp_type
>::digits
));
95 test(boost::mpl::int_
<24>());
96 return boost::report_errors();