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