]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/multiprecision/test/standalone_test_convert_from_tom_int.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / multiprecision / test / standalone_test_convert_from_tom_int.cpp
CommitLineData
1e59de90
TL
1///////////////////////////////////////////////////////////////
2// Copyright 2012 John Maddock.
3// Copyright 2021 Matt Borland. Distributed under the Boost
4// Software License, Version 1.0. (See accompanying file
5// LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
6
7#ifdef _MSC_VER
8#define _SCL_SECURE_NO_WARNINGS
9#endif
10
11#define BOOST_MP_STANDALONE
12
13#ifdef HAS_TOMMATH
14
15#include <boost/multiprecision/cpp_int.hpp>
16#include <boost/random/mersenne_twister.hpp>
17#include <boost/multiprecision/tommath.hpp>
18#include <boost/multiprecision/cpp_bin_float.hpp>
19#include <boost/multiprecision/cpp_dec_float.hpp>
20#include "test.hpp"
21
22#if defined(HAS_GMP)
23#include <boost/multiprecision/gmp.hpp>
24#endif
25#if defined(HAS_MPFR)
26#include <boost/multiprecision/mpfr.hpp>
27#endif
28#if defined(HAS_MPFI)
29#include <boost/multiprecision/mpfi.hpp>
30#endif
31#ifdef HAS_FLOAT128
32#include <boost/multiprecision/float128.hpp>
33#endif
34
35using namespace boost::multiprecision;
36
37#ifdef BOOST_MSVC
38#pragma warning(disable : 4127)
39#endif
40
41template <class T>
42T generate_random(unsigned bits_wanted)
43{
44 static boost::random::mt19937 gen;
45 typedef boost::random::mt19937::result_type random_type;
46
47 T max_val;
48 unsigned digits;
49 if (std::numeric_limits<T>::is_bounded && (bits_wanted == (unsigned)std::numeric_limits<T>::digits))
50 {
51 max_val = (std::numeric_limits<T>::max)();
52 digits = std::numeric_limits<T>::digits;
53 }
54 else
55 {
56 max_val = T(1) << bits_wanted;
57 digits = bits_wanted;
58 }
59
60 unsigned bits_per_r_val = std::numeric_limits<random_type>::digits - 1;
61 while ((random_type(1) << bits_per_r_val) > (gen.max)())
62 --bits_per_r_val;
63
64 unsigned terms_needed = digits / bits_per_r_val + 1;
65
66 T val = 0;
67 for (unsigned i = 0; i < terms_needed; ++i)
68 {
69 val *= (gen.max)();
70 val += gen();
71 }
72 val %= max_val;
73 return val;
74}
75
76template <class From, class To>
77void test_convert_neg_int(From from, const std::integral_constant<bool, true>&)
78{
79 from = -from;
80 To t3(from);
81 To t4 = from.template convert_to<To>();
82 BOOST_CHECK_EQUAL(from.str(), t3.str());
83 BOOST_CHECK_EQUAL(from.str(), t4.str());
84}
85template <class From, class To>
86void test_convert_neg_int(From const&, const std::integral_constant<bool, false>&)
87{
88}
89
90template <class From, class To>
91void test_convert_imp(std::integral_constant<int, number_kind_integer> const&, std::integral_constant<int, number_kind_integer> const&)
92{
93 int bits_wanted = (std::min)((std::min)(std::numeric_limits<From>::digits, std::numeric_limits<To>::digits), 2000);
94
95 for (unsigned i = 0; i < 100; ++i)
96 {
97 From from = generate_random<From>(bits_wanted);
98 To t1(from);
99 To t2 = from.template convert_to<To>();
100 BOOST_CHECK_EQUAL(from.str(), t1.str());
101 BOOST_CHECK_EQUAL(from.str(), t2.str());
102 test_convert_neg_int<From, To>(from, std::integral_constant<bool, std::numeric_limits<From>::is_signed && std::numeric_limits<To>::is_signed > ());
103 }
104}
105
106template <class From, class To>
107void test_convert_neg_rat(From from, const std::integral_constant<bool, true>&)
108{
109 from = -from;
110 To t3(from);
111 To t4 = from.template convert_to<To>();
112 BOOST_CHECK_EQUAL(from.str(), numerator(t3).str());
113 BOOST_CHECK_EQUAL(from.str(), numerator(t4).str());
114}
115template <class From, class To>
116void test_convert_neg_rat(From const&, const std::integral_constant<bool, false>&)
117{
118}
119
120template <class From, class To>
121void test_convert_imp(std::integral_constant<int, number_kind_integer> const&, std::integral_constant<int, number_kind_rational> const&)
122{
123 int bits_wanted = (std::min)((std::min)(std::numeric_limits<From>::digits, std::numeric_limits<To>::digits), 2000);
124
125 for (unsigned i = 0; i < 100; ++i)
126 {
127 From from = generate_random<From>(bits_wanted);
128 To t1(from);
129 To t2 = from.template convert_to<To>();
130 BOOST_CHECK_EQUAL(from.str(), numerator(t1).str());
131 BOOST_CHECK_EQUAL(from.str(), numerator(t2).str());
132 test_convert_neg_rat<From, To>(from, std::integral_constant<bool, std::numeric_limits<From>::is_signed && std::numeric_limits<To>::is_signed > ());
133 }
134}
135
136template <class From, class To>
137void test_convert_neg_float(From from, const std::integral_constant<bool, true>&)
138{
139 from = -from;
140 To t3(from);
141 To t4 = from.template convert_to<To>();
142 To check(from.str() + ".0");
143 BOOST_CHECK_EQUAL(t3, check);
144 BOOST_CHECK_EQUAL(t4, check);
145}
146template <class From, class To>
147void test_convert_neg_float(From const&, const std::integral_constant<bool, false>&)
148{
149}
150
151template <class From, class To>
152void test_convert_imp(std::integral_constant<int, number_kind_integer> const&, std::integral_constant<int, number_kind_floating_point> const&)
153{
154 int bits_wanted = (std::min)((std::min)(std::numeric_limits<From>::digits, std::numeric_limits<To>::digits), 2000);
155
156 for (unsigned i = 0; i < 100; ++i)
157 {
158 From from = generate_random<From>(bits_wanted);
159 To t1(from);
160 To t2 = from.template convert_to<To>();
161 To check(from.str() + ".0");
162 BOOST_CHECK_EQUAL(t1, check);
163 BOOST_CHECK_EQUAL(t2, check);
164 test_convert_neg_float<From, To>(from, std::integral_constant<bool, std::numeric_limits<From>::is_signed && std::numeric_limits<To>::is_signed > ());
165 }
166}
167
168template <class From, class To>
169void test_convert()
170{
171 test_convert_imp<From, To>(typename number_category<From>::type(), typename number_category<To>::type());
172}
173
174template <typename From, typename To>
175void test_convert_to_builtin()
176{
177 const int bits_wanted = (std::min)((std::min)(std::numeric_limits<From>::digits, std::numeric_limits<To>::digits), 2000);
178
179 for (unsigned i = 0; i < 100; ++i)
180 {
181 From from = generate_random<From>(bits_wanted);
182 To t1 = from;
183 To t2 = from.template convert_to<To>();
184 BOOST_CHECK_EQUAL(t1, t2);
185 }
186}
187
188int main()
189{
190 test_convert_to_builtin<tom_int, unsigned long long>();
191 test_convert_to_builtin<tom_int, long long>();
192 test_convert_to_builtin<tom_int, long>();
193 test_convert_to_builtin<tom_int, unsigned>();
194
195 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
196 test_convert_to_builtin<tom_int, long double>();
197 #endif
198 test_convert_to_builtin<tom_int, double>();
199 test_convert_to_builtin<tom_int, float>();
200
201 return boost::report_errors();
202}
203
204#else
205
206int main() { return 0; }
207
208#endif