]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/multiprecision/test/test_convert_from_cpp_int.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / multiprecision / test / test_convert_from_cpp_int.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 http://www.boost.org/LICENSE_1_
5
6 #ifdef _MSC_VER
7 # define _SCL_SECURE_NO_WARNINGS
8 #endif
9
10 #include <boost/multiprecision/cpp_int.hpp>
11 #include <boost/random/mersenne_twister.hpp>
12 #include "test.hpp"
13
14 #if defined(HAS_GMP)
15 #include <boost/multiprecision/gmp.hpp>
16 #endif
17 #if defined(HAS_MPFR)
18 #include <boost/multiprecision/mpfr.hpp>
19 #endif
20 #if defined(HAS_MPFI)
21 #include <boost/multiprecision/mpfi.hpp>
22 #endif
23 #ifdef HAS_TOMMATH
24 #include <boost/multiprecision/tommath.hpp>
25 #endif
26 #ifdef HAS_FLOAT128
27 #include <boost/multiprecision/float128.hpp>
28 #endif
29 #include <boost/multiprecision/cpp_bin_float.hpp>
30 #include <boost/multiprecision/cpp_dec_float.hpp>
31
32
33 using namespace boost::multiprecision;
34
35 #ifdef BOOST_MSVC
36 #pragma warning(disable:4127)
37 #endif
38
39
40 template <class T>
41 T generate_random(unsigned bits_wanted)
42 {
43 static boost::random::mt19937 gen;
44 typedef boost::random::mt19937::result_type random_type;
45
46 T max_val;
47 unsigned digits;
48 if(std::numeric_limits<T>::is_bounded && (bits_wanted == (unsigned)std::numeric_limits<T>::digits))
49 {
50 max_val = (std::numeric_limits<T>::max)();
51 digits = std::numeric_limits<T>::digits;
52 }
53 else
54 {
55 max_val = T(1) << bits_wanted;
56 digits = bits_wanted;
57 }
58
59 unsigned bits_per_r_val = std::numeric_limits<random_type>::digits - 1;
60 while((random_type(1) << bits_per_r_val) > (gen.max)()) --bits_per_r_val;
61
62 unsigned terms_needed = digits / bits_per_r_val + 1;
63
64 T val = 0;
65 for(unsigned i = 0; i < terms_needed; ++i)
66 {
67 val *= (gen.max)();
68 val += gen();
69 }
70 val %= max_val;
71 return val;
72 }
73
74 template <class From, class To>
75 void test_convert_neg_int(From from, const boost::mpl::true_&)
76 {
77 from = -from;
78 To t3(from);
79 To t4 = from.template convert_to<To>();
80 BOOST_CHECK_EQUAL(from.str(), t3.str());
81 BOOST_CHECK_EQUAL(from.str(), t4.str());
82 }
83 template <class From, class To>
84 void test_convert_neg_int(From const&, const boost::mpl::false_&)
85 {
86 }
87
88 template <class From, class To>
89 void test_convert_imp(boost::mpl::int_<number_kind_integer> const&, boost::mpl::int_<number_kind_integer> const&)
90 {
91 int bits_wanted = (std::min)((std::min)(std::numeric_limits<From>::digits, std::numeric_limits<To>::digits), 2000);
92
93 for(unsigned i = 0; i < 100; ++i)
94 {
95 From from = generate_random<From>(bits_wanted);
96 To t1(from);
97 To t2 = from.template convert_to<To>();
98 BOOST_CHECK_EQUAL(from.str(), t1.str());
99 BOOST_CHECK_EQUAL(from.str(), t2.str());
100 test_convert_neg_int<From, To>(from, boost::mpl::bool_<std::numeric_limits<From>::is_signed && std::numeric_limits<To>::is_signed>());
101 }
102 }
103
104 template <class From, class To>
105 void test_convert_neg_rat(From from, const boost::mpl::true_&)
106 {
107 from = -from;
108 To t3(from);
109 To t4 = from.template convert_to<To>();
110 BOOST_CHECK_EQUAL(from.str(), numerator(t3).str());
111 BOOST_CHECK_EQUAL(from.str(), numerator(t4).str());
112 }
113 template <class From, class To>
114 void test_convert_neg_rat(From const&, const boost::mpl::false_&)
115 {
116 }
117
118 template <class From, class To>
119 void test_convert_imp(boost::mpl::int_<number_kind_integer> const&, boost::mpl::int_<number_kind_rational> const&)
120 {
121 int bits_wanted = (std::min)((std::min)(std::numeric_limits<From>::digits, std::numeric_limits<To>::digits), 2000);
122
123 for(unsigned i = 0; i < 100; ++i)
124 {
125 From from = generate_random<From>(bits_wanted);
126 To t1(from);
127 To t2 = from.template convert_to<To>();
128 BOOST_CHECK_EQUAL(from.str(), numerator(t1).str());
129 BOOST_CHECK_EQUAL(from.str(), numerator(t2).str());
130 test_convert_neg_rat<From, To>(from, boost::mpl::bool_<std::numeric_limits<From>::is_signed && std::numeric_limits<To>::is_signed>());
131 }
132 }
133
134 template <class From, class To>
135 void test_convert_neg_float(From from, const boost::mpl::true_&)
136 {
137 from = -from;
138 To t3(from);
139 To t4 = from.template convert_to<To>();
140 To check(from.str() + ".0");
141 BOOST_CHECK_EQUAL(t3, check);
142 BOOST_CHECK_EQUAL(t4, check);
143 }
144 template <class From, class To>
145 void test_convert_neg_float(From const&, const boost::mpl::false_&)
146 {
147 }
148
149 template <class From, class To>
150 void test_convert_imp(boost::mpl::int_<number_kind_integer> const&, boost::mpl::int_<number_kind_floating_point> const&)
151 {
152 int bits_wanted = (std::min)((std::min)(std::numeric_limits<From>::digits, std::numeric_limits<To>::digits), 2000);
153
154 for(unsigned i = 0; i < 100; ++i)
155 {
156 From from = generate_random<From>(bits_wanted);
157 To t1(from);
158 To t2 = from.template convert_to<To>();
159 To check(from.str() + ".0");
160 BOOST_CHECK_EQUAL(t1, check);
161 BOOST_CHECK_EQUAL(t2, check);
162 test_convert_neg_float<From, To>(from, boost::mpl::bool_<std::numeric_limits<From>::is_signed && std::numeric_limits<To>::is_signed>());
163 }
164 }
165
166
167 template <class From, class To>
168 void test_convert()
169 {
170 test_convert_imp<From, To>(typename number_category<From>::type(), typename number_category<To>::type());
171 }
172
173
174 int main()
175 {
176 test_convert<cpp_int, int128_t>();
177 test_convert<int128_t, cpp_int>();
178
179 test_convert<cpp_int, cpp_rational>();
180 test_convert<int128_t, cpp_rational>();
181 test_convert<uint128_t, cpp_rational>();
182
183 test_convert<cpp_int, cpp_bin_float_50>();
184 test_convert<int128_t, cpp_bin_float_50>();
185 test_convert<uint128_t, cpp_bin_float_50>();
186
187 test_convert<cpp_int, cpp_dec_float_50>();
188 test_convert<int128_t, cpp_dec_float_50>();
189 test_convert<uint128_t, cpp_dec_float_50>();
190
191 #if defined(HAS_GMP)
192 test_convert<cpp_int, mpz_int>();
193 test_convert<int128_t, mpz_int>();
194 test_convert<uint128_t, mpz_int>();
195
196 test_convert<cpp_int, mpq_rational>();
197 test_convert<int128_t, mpq_rational>();
198 test_convert<uint128_t, mpq_rational>();
199
200 test_convert<cpp_int, mpf_float_50>();
201 test_convert<int128_t, mpf_float_50>();
202 test_convert<uint128_t, mpf_float_50>();
203 #endif
204 #if defined(HAS_MPFR)
205 test_convert<cpp_int, mpfr_float_50>();
206 test_convert<int128_t, mpfr_float_50>();
207 test_convert<uint128_t, mpfr_float_50>();
208 #endif
209 #if defined(HAS_MPFI)
210 test_convert<cpp_int, mpfi_float_50>();
211 test_convert<int128_t, mpfi_float_50>();
212 test_convert<uint128_t, mpfi_float_50>();
213 #endif
214 #ifdef HAS_TOMMATH
215 test_convert<cpp_int, tom_int>();
216 test_convert<int128_t, tom_int>();
217 test_convert<uint128_t, tom_int>();
218
219 test_convert<cpp_int, tom_rational>();
220 test_convert<int128_t, tom_rational>();
221 test_convert<uint128_t, tom_rational>();
222 #endif
223 #ifdef HAS_FLOAT128
224 test_convert<cpp_int, float128>();
225 test_convert<int128_t, float128>();
226 test_convert<uint128_t, float128>();
227 #endif
228 return boost::report_errors();
229 }
230