1 // Copyright 2018 John Maddock. Distributed under the Boost
2 // Software License, Version 1.0. (See accompanying file
3 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 #include <boost/multiprecision/cpp_bin_float.hpp>
6 #include <boost/math/special_functions.hpp>
7 #include <boost/chrono.hpp>
8 #include <boost/random/mersenne_twister.hpp>
9 #include <boost/random/uniform_int.hpp>
11 template <class Clock>
14 typedef typename Clock::duration duration;
17 m_start = Clock::now();
21 return Clock::now() - m_start;
25 m_start = Clock::now();
29 typename Clock::time_point m_start;
36 static boost::random::mt19937 gen;
39 while (val != prev_val)
48 static boost::random::uniform_int_distribution<e_type> ui(-20, 20);
49 return ldexp(val, ui(gen));
53 double my_convert_to_double(const T& x)
58 if (x.backend().exponent() >= -1023 - 52 && x != 0)
60 if (x.backend().exponent() <= 1023)
62 int e = x.backend().exponent();
63 T y = ldexp(abs(x), 55 - e);
65 int64_t ti = t.template convert_to<int64_t>();
73 ret = ldexp(double(ti), e - 55);
78 typedef boost::multiprecision::number<boost::multiprecision::cpp_bin_float<128, boost::multiprecision::backends::digit_base_2> > cpp_bin_float128_t;
79 cpp_bin_float128_t sx = ldexp(cpp_bin_float128_t(ti), e - 55);
82 cpp_bin_float128_t sy = ldexp(sx, 55 - e);
83 cpp_bin_float128_t st = trunc(sy);
84 ti = st.convert_to<int64_t>();
90 ret = ldexp(double(ti), e - 55) - DBL_MIN;
107 return x.backend().sign() ? -ret : ret;
111 void test_conversion_time(const char* name)
113 std::cout << "Testing times for type: " << name << "\n";
114 std::vector<T> values;
116 for (unsigned i = 0; i < 10000000; ++i)
118 values.push_back(generate_random<T>());
121 boost::chrono::duration<double> time;
122 stopwatch<boost::chrono::high_resolution_clock> c;
126 for (typename std::vector<T>::const_iterator i = values.begin(); i != values.end(); ++i)
128 total += my_convert_to_double(*i);
132 std::cout << std::setprecision(3) << std::fixed;
133 std::cout << "Reference time: " << std::setw(7) << std::right << time << " (total sum = " << total << ")" << std::endl;
139 for (typename std::vector<T>::const_iterator i = values.begin(); i != values.end(); ++i)
141 total += i->template convert_to<double>();
145 std::cout << "Boost time: " << std::setw(7) << std::right << time << " (total sum = " << total << ")" << std::endl;
150 using namespace boost::multiprecision;
152 test_conversion_time<cpp_bin_float_double>("cpp_bin_float_double");
153 test_conversion_time<cpp_bin_float_quad>("cpp_bin_float_quad");
154 test_conversion_time<cpp_bin_float_oct>("cpp_bin_float_oct");
155 test_conversion_time<cpp_bin_float_50>("cpp_bin_float_50");
156 test_conversion_time<cpp_bin_float_100>("cpp_bin_float_100");