1 // Copyright 2010 Vicente J. Botet Escriba 2 // Distributed under the Boost Software License, Version 1.0. 3 // See http://www.boost.org/LICENSE_1_0.txt 4 5 #ifndef BOOST_CHRONO_TEST_CYCLE_COUNT_HPP 6 #define BOOST_CHRONO_TEST_CYCLE_COUNT_HPP 7 8 #include <boost/chrono/ceil.hpp> 9 #include <boost/chrono/duration.hpp> 10 #include <boost/chrono/time_point.hpp> 11 #include <boost/chrono/stopwatches/reporters/stopwatch_reporter_default_formatter.hpp> 12 #include <boost/chrono/stopwatches/formatters/elapsed_formatter.hpp> 13 #include <boost/chrono/stopwatches/strict_stopwatch.hpp> 14 15 namespace ex 16 { 17 template <long long speed> 18 struct cycle_count 19 { 20 typedef typename boost::ratio_multiply<boost::ratio<speed>, boost::mega>::type frequency; // Mhz 21 typedef typename boost::ratio_divide<boost::ratio<1>, frequency>::type period; 22 typedef long long rep; 23 typedef boost::chrono::duration<rep, period> duration; 24 typedef boost::chrono::time_point<cycle_count> time_point; 25 BOOST_STATIC_CONSTEXPR bool is_steady = true; 26 static long long ticks_; 27 nowex::cycle_count28 static time_point now() 29 { 30 // return exact cycle count 31 return time_point(duration(ticks_)); 32 } nowex::cycle_count33 static time_point now(boost::system::error_code & ) 34 { 35 // return exact cycle count 36 return time_point(duration(ticks_)); 37 } advanceex::cycle_count38 static void advance(std::size_t ticks) 39 { 40 ticks_ += ticks; 41 } 42 template <typename D> advanceex::cycle_count43 static void advance(D const& d) 44 { 45 ticks_ += boost::chrono::ceil<duration>(d).count(); 46 } 47 }; 48 template <long long speed> 49 long long cycle_count<speed>::ticks_ = 0; 50 51 52 template<class Clock, class Rep, class Period> sleep_for(const boost::chrono::duration<Rep,Period> & d)53 void sleep_for(const boost::chrono::duration<Rep, Period>& d) 54 { 55 Clock::advance(d); 56 } 57 58 } 59 60 61 namespace boost 62 { 63 namespace chrono 64 { 65 66 template <typename CharT, long long speed> 67 struct basic_stopwatch_reporter_default_formatter<CharT, strict_stopwatch<ex::cycle_count<speed> > > 68 { 69 typedef basic_elapsed_formatter<milli, CharT> type; 70 }; 71 72 // template <long long speed> 73 // struct wstopwatch_reporter_default_formatter<strict_stopwatch<ex::cycle_count<speed> > > 74 // { 75 // typedef welapsed_formatter type; 76 // }; 77 78 } // namespace chrono 79 } // namespace boost 80 81 82 #endif 83