1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
/*****************************************************************************
ClockCycleCounter.hpp
Copyright (c) 2003 Laurent de Soras
Please complete the definitions according to your compiler/architecture.
It's not a big deal if it's not possible to get the clock count...
--- Legal stuff ---
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*Tab=3***********************************************************************/
#if defined (stopwatch_ClockCycleCounter_CURRENT_CODEHEADER)
#error Recursive inclusion of ClockCycleCounter code header.
#endif
#define stopwatch_ClockCycleCounter_CURRENT_CODEHEADER
#if ! defined (stopwatch_ClockCycleCounter_CODEHEADER_INCLUDED)
#define stopwatch_ClockCycleCounter_CODEHEADER_INCLUDED
/*\\\ INCLUDE FILES \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
#include "fnc.h"
#include <climits>
namespace stopwatch
{
/*\\\ PUBLIC \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
/*
==============================================================================
Name: start
Description:
Starts the counter.
Throws: Nothing
==============================================================================
*/
void ClockCycleCounter::start ()
{
_best_score = (static_cast <Int64> (1) << (sizeof (Int64) * CHAR_BIT - 2));
const Int64 start_clock = read_clock_counter ();
_start_time = start_clock;
_state = start_clock - _best_score;
}
/*
==============================================================================
Name: stop_lap
Description:
Captures the current time and updates the smallest duration between two
consecutive calls to stop_lap() or the latest start().
start() must have been called at least once before calling this function.
Throws: Nothing
==============================================================================
*/
void ClockCycleCounter::stop_lap ()
{
const Int64 end_clock = read_clock_counter ();
_best_score = min (end_clock - _state, _best_score);
_state = end_clock;
}
/*\\\ PROTECTED \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
/*\\\ PRIVATE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
Int64 ClockCycleCounter::read_clock_counter ()
{
register Int64 clock_cnt;
#if defined (_MSC_VER)
__asm
{
lea edi, clock_cnt
rdtsc
mov [edi ], eax
mov [edi + 4], edx
}
#elif defined (__GNUC__) && defined (__i386__)
__asm__ __volatile__ ("rdtsc" : "=A" (clock_cnt));
#elif (__MWERKS__) && defined (__POWERPC__)
asm
{
loop:
mftbu clock_cnt@hiword
mftb clock_cnt@loword
mftbu r5
cmpw clock_cnt@hiword,r5
bne loop
}
#endif
return (clock_cnt);
}
} // namespace stopwatch
#endif // stopwatch_ClockCycleCounter_CODEHEADER_INCLUDED
#undef stopwatch_ClockCycleCounter_CURRENT_CODEHEADER
/*\\\ EOF \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
|