]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/multiprecision/test/test_convert_from_float128.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / multiprecision / test / test_convert_from_float128.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 #ifdef HAS_FLOAT128
11
12 #include <boost/multiprecision/cpp_int.hpp>
13 #include <boost/random/mersenne_twister.hpp>
14 #include <boost/random/uniform_int.hpp>
15 #include "test.hpp"
16
17 #if defined(HAS_GMP)
18 #include <boost/multiprecision/gmp.hpp>
19 #endif
20 #if defined(HAS_MPFR)
21 #include <boost/multiprecision/mpfr.hpp>
22 #endif
23 #if defined(HAS_MPFI)
24 #include <boost/multiprecision/mpfi.hpp>
25 #endif
26 #ifdef HAS_TOMMATH
27 #include <boost/multiprecision/tommath.hpp>
28 #endif
29 #ifdef HAS_FLOAT128
30 #include <boost/multiprecision/float128.hpp>
31 #endif
32 #include <boost/multiprecision/cpp_bin_float.hpp>
33 #include <boost/multiprecision/cpp_dec_float.hpp>
34
35
36 using namespace boost::multiprecision;
37
38 #ifdef BOOST_MSVC
39 #pragma warning(disable:4127)
40 #endif
41
42
43 template <class T>
44 T generate_random()
45 {
46 typedef int e_type;
47 static boost::random::mt19937 gen;
48 T val = gen();
49 T prev_val = -1;
50 while(val != prev_val)
51 {
52 val *= (gen.max)();
53 prev_val = val;
54 val += gen();
55 }
56 e_type e;
57 val = frexp(val, &e);
58
59 static boost::random::uniform_int_distribution<e_type> ui(-20, 20);
60 return ldexp(val, ui(gen));
61 }
62
63 template <class From, class To>
64 void test_convert_neg_int(From from, const boost::mpl::true_&)
65 {
66 from = -from;
67 To t3(from);
68 To t4 = from.template convert_to<To>();
69 BOOST_CHECK_EQUAL(From(trunc(from)), From(t3));
70 BOOST_CHECK_EQUAL(From(trunc(from)), From(t4));
71 }
72 template <class From, class To>
73 void test_convert_neg_int(From const&, const boost::mpl::false_&)
74 {
75 }
76
77 template <class From, class To>
78 void test_convert_imp(boost::mpl::int_<number_kind_floating_point> const&, boost::mpl::int_<number_kind_integer> const&)
79 {
80 for(unsigned i = 0; i < 100; ++i)
81 {
82 From from = generate_random<From>();
83 To t1(from);
84 To t2 = from.template convert_to<To>();
85 BOOST_CHECK_EQUAL(From(trunc(from)), From(t1));
86 BOOST_CHECK_EQUAL(From(trunc(from)), From(t2));
87 test_convert_neg_int<From, To>(from, boost::mpl::bool_<std::numeric_limits<From>::is_signed && std::numeric_limits<To>::is_signed>());
88 }
89 }
90
91 template <class From, class To>
92 void test_convert_neg_rat(From from, const boost::mpl::true_&)
93 {
94 from = -from;
95 To t3(from);
96 To t4 = from.template convert_to<To>();
97 BOOST_CHECK_EQUAL(From(t3), from);
98 BOOST_CHECK_EQUAL(From(t4), from);
99 }
100 template <class From, class To>
101 void test_convert_rat_int(From const&, const boost::mpl::false_&)
102 {
103 }
104
105 template <class From, class To>
106 void test_convert_imp(boost::mpl::int_<number_kind_floating_point> const&, boost::mpl::int_<number_kind_rational> const&)
107 {
108 for(unsigned i = 0; i < 100; ++i)
109 {
110 From from = generate_random<From>();
111 To t1(from);
112 To t2 = from.template convert_to<To>();
113 BOOST_CHECK_EQUAL(From(t1), from);
114 BOOST_CHECK_EQUAL(From(t2), from);
115 test_convert_neg_rat<From, To>(from, boost::mpl::bool_<std::numeric_limits<From>::is_signed && std::numeric_limits<To>::is_signed>());
116 }
117 }
118
119 template <class From, class To>
120 void test_convert_neg_float(From from, const boost::mpl::true_&)
121 {
122 from = -from;
123 To t3(from);
124 To t4 = from.template convert_to<To>();
125 To answer(from.str());
126 To tol = (std::max)(std::numeric_limits<To>::epsilon(), To(std::numeric_limits<From>::epsilon())) * 2;
127 BOOST_CHECK_CLOSE_FRACTION(t3, answer, tol);
128 BOOST_CHECK_CLOSE_FRACTION(t4, answer, tol);
129 }
130 template <class From, class To>
131 void test_convert_neg_float(From const&, const boost::mpl::false_&)
132 {
133 }
134
135 template <class From, class To>
136 void test_convert_imp(boost::mpl::int_<number_kind_floating_point> const&, boost::mpl::int_<number_kind_floating_point> const&)
137 {
138 for(unsigned i = 0; i < 100; ++i)
139 {
140 From from = generate_random<From>();
141 To t1(from);
142 To t2 = from.template convert_to<To>();
143 To answer(from.str());
144 To tol = (std::max)(std::numeric_limits<To>::epsilon(), To(std::numeric_limits<From>::epsilon())) * 2;
145 BOOST_CHECK_CLOSE_FRACTION(t1, answer, tol);
146 BOOST_CHECK_CLOSE_FRACTION(t2, answer, tol);
147 test_convert_neg_float<From, To>(from, boost::mpl::bool_<std::numeric_limits<From>::is_signed && std::numeric_limits<To>::is_signed>());
148 }
149 }
150
151 template <class From, class To>
152 void test_convert()
153 {
154 test_convert_imp<From, To>(typename number_category<From>::type(), typename number_category<To>::type());
155 }
156
157
158 int main()
159 {
160 test_convert<float128, cpp_int>();
161 test_convert<float128, int128_t>();
162 test_convert<float128, uint128_t>();
163
164
165 test_convert<float128, cpp_rational>();
166
167 test_convert<float128, cpp_dec_float_50>();
168
169 #if defined(HAS_GMP)
170 test_convert<float128, mpz_int>();
171 test_convert<float128, mpq_rational>();
172 test_convert<float128, mpf_float_50>();
173 #endif
174 #if defined(HAS_MPFR)
175 test_convert<float128, mpfr_float_50>();
176 #endif
177 #if defined(HAS_MPFI)
178 test_convert<float128, mpfi_float_50>();
179 #endif
180 #ifdef HAS_TOMMATH
181 test_convert<float128, tom_int>();
182 test_convert<float128, tom_rational>();
183 #endif
184 return boost::report_errors();
185 }
186
187 #else
188
189 int main() { return 0; }
190
191 #endif