]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/multiprecision/performance/cpp_bin_float_conversion_performance.cpp
1 #include <boost/multiprecision/cpp_bin_float.hpp>
2 #include <boost/math/special_functions.hpp>
3 #include <boost/chrono.hpp>
4 #include <boost/random/mersenne_twister.hpp>
5 #include <boost/random/uniform_int.hpp>
10 typedef typename
Clock::duration duration
;
13 m_start
= Clock::now();
17 return Clock::now() - m_start
;
21 m_start
= Clock::now();
25 typename
Clock::time_point m_start
;
32 static boost::random::mt19937 gen
;
35 while(val
!= prev_val
)
44 static boost::random::uniform_int_distribution
<e_type
> ui(-20, 20);
45 return ldexp(val
, ui(gen
));
50 double my_convert_to_double(const T
& x
)
54 if(x
.backend().exponent() >= -1023 - 52 && x
!= 0) {
55 if(x
.backend().exponent() <= 1023) {
56 int e
= x
.backend().exponent();
57 T y
= ldexp(abs(x
), 55 - e
);
59 int64_t ti
= t
.template convert_to
<int64_t>();
65 ret
= ldexp(double(ti
), e
- 55);
69 typedef boost::multiprecision::number
<boost::multiprecision::cpp_bin_float
<128, boost::multiprecision::backends::digit_base_2
> > cpp_bin_float128_t
;
70 cpp_bin_float128_t sx
= ldexp(cpp_bin_float128_t(ti
), e
- 55);
73 cpp_bin_float128_t sy
= ldexp(sx
, 55 - e
);
74 cpp_bin_float128_t st
= trunc(sy
);
75 ti
= st
.convert_to
<int64_t>();
80 ret
= ldexp(double(ti
), e
- 55) - DBL_MIN
;
95 return x
.backend().sign() ? -ret
: ret
;
100 void test_conversion_time(const char* name
)
102 std::cout
<< "Testing times for type: " << name
<< "\n";
103 std::vector
<T
> values
;
105 for(unsigned i
= 0; i
< 10000000; ++i
)
107 values
.push_back(generate_random
<T
>());
110 boost::chrono::duration
<double> time
;
111 stopwatch
<boost::chrono::high_resolution_clock
> c
;
115 for(typename
std::vector
<T
>::const_iterator i
= values
.begin(); i
!= values
.end(); ++i
)
117 total
+= my_convert_to_double(*i
);
121 std::cout
<< std::setprecision(3) << std::fixed
;
122 std::cout
<< "Reference time: " << std::setw(7) << std::right
<< time
<< " (total sum = " << total
<< ")" << std::endl
;
128 for(typename
std::vector
<T
>::const_iterator i
= values
.begin(); i
!= values
.end(); ++i
)
130 total
+= i
->template convert_to
<double>();
134 std::cout
<< "Boost time: " << std::setw(7) << std::right
<< time
<< " (total sum = " << total
<< ")" << std::endl
;
142 using namespace boost::multiprecision
;
144 test_conversion_time
<cpp_bin_float_double
>("cpp_bin_float_double");
145 test_conversion_time
<cpp_bin_float_quad
>("cpp_bin_float_quad");
146 test_conversion_time
<cpp_bin_float_50
>("cpp_bin_float_50");
147 test_conversion_time
<cpp_bin_float_100
>("cpp_bin_float_100");