]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/multiprecision/test/test_cpp_int_left_shift.cpp
3aca2e1d9a4d0e124d495269ab6f9294e7551d41
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>
19 #include <boost/timer.hpp>
22 #if !defined(TEST1) && !defined(TEST2) && !defined(TEST3)
29 T
generate_random(unsigned bits_wanted
)
31 static boost::random::mt19937 gen
;
32 typedef boost::random::mt19937::result_type random_type
;
36 if (std::numeric_limits
<T
>::is_bounded
&& (bits_wanted
== (unsigned)std::numeric_limits
<T
>::digits
))
38 max_val
= (std::numeric_limits
<T
>::max
)();
39 digits
= std::numeric_limits
<T
>::digits
;
43 max_val
= T(1) << bits_wanted
;
47 unsigned bits_per_r_val
= std::numeric_limits
<random_type
>::digits
- 1;
48 while ((random_type(1) << bits_per_r_val
) > (gen
.max
)())
51 unsigned terms_needed
= digits
/ bits_per_r_val
+ 1;
54 for (unsigned i
= 0; i
< terms_needed
; ++i
)
64 void test_value(const T
& val
)
66 boost::multiprecision::mpz_int
z(val
.str()), mask(1);
67 mask
<<= std::numeric_limits
<T
>::digits
;
70 for (unsigned i
= 0; i
<= std::numeric_limits
<T
>::digits
+ 2; ++i
)
72 BOOST_CHECK_EQUAL((val
<< i
).str(), boost::multiprecision::mpz_int(((z
<< i
) & mask
)).str());
76 void test(const boost::mpl::int_
<200>&) {}
79 void test(boost::mpl::int_
<N
> const&)
81 test(boost::mpl::int_
<N
+ 4>());
83 typedef boost::multiprecision::number
<boost::multiprecision::cpp_int_backend
<N
, N
, boost::multiprecision::unsigned_magnitude
>, boost::multiprecision::et_off
> mp_type
;
85 std::cout
<< "Running tests for precision: " << N
<< std::endl
;
90 for (unsigned i
= 0; i
< 1000; ++i
)
91 test_value(generate_random
<mp_type
>(std::numeric_limits
<mp_type
>::digits
));
96 test(boost::mpl::int_
<24>());
97 return boost::report_errors();