1 // test performance.cpp : Defines the entry point for the console application.
8 #include <boost/multiprecision/cpp_int.hpp>
9 #include <boost/multiprecision/integer.hpp>
11 #include <boost/safe_numerics/safe_integer.hpp>
13 typedef boost::safe_numerics::safe<unsigned> safe_type;
16 namespace multiprecision {
18 template <class Integer, class I2>
19 typename enable_if_c<boost::safe_numerics::is_safe<Integer>::value, Integer&>::type
20 multiply(Integer& result, const I2& a, const I2& b){
21 return result = static_cast<Integer>(a) * static_cast<Integer>(b);
24 template <class Integer>
25 typename enable_if_c<boost::safe_numerics::is_safe<Integer>::value, bool>::type
26 bit_test(const Integer& val, unsigned index){
28 if (index >= sizeof(Integer) * CHAR_BIT)
32 return val & mask ? true : false;
35 template <class I1, class I2>
36 typename enable_if_c<boost::safe_numerics::is_safe<I1>::value, I2>::type
37 integer_modulus(const I1& x, I2 val){
42 template <class T> struct double_integer;
45 struct double_integer<safe_type>{
46 using type = boost::safe_numerics::safe<std::uint64_t>;
50 template <class I1, class I2, class I3>
51 typename enable_if_c<boost::safe_numerics::is_safe<I1>::value, I1>::type
52 powm(const I1& a, I2 b, I3 c){
53 typedef typename detail::double_integer<I1>::type double_type;
60 multiply(result, x, y);
61 x = integer_modulus(result, c);
63 multiply(result, y, y);
64 y = integer_modulus(result, c);
70 template <class T, class PP, class EP>
72 lsb(const boost::safe_numerics::safe<T, PP, EP>& x){
73 return lsb(static_cast<T>(x));
78 #include <boost/multiprecision/miller_rabin.hpp>
80 template <class Clock>
83 const typename Clock::time_point m_start;
88 typename Clock::duration elapsed() const {
89 return Clock::now() - m_start;
94 void test(const char * msg){
95 const stopwatch<std::chrono::high_resolution_clock> c;
98 for (T i = 3; i < 30000000; ++i)
99 if (boost::multiprecision::miller_rabin_test(i, 25)) ++count;
101 std::chrono::duration<double> time = c.elapsed();
102 std::cout<< msg << ":\ntime = " << time.count();
103 std::cout << "\ncount = " << count << std::endl;
108 test<unsigned>("Testing type unsigned");
109 test<boost::safe_numerics::safe<unsigned>>("Testing type safe<unsigned>");