summaryrefslogtreecommitdiffstats
path: root/demos/spectrum/3rdparty/fftreal/stopwatch/ClockCycleCounter.cpp
blob: fe1d4245f4acb96035743e874b41bd23c638d9d7 (plain)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*****************************************************************************

        ClockCycleCounter.cpp
        Copyright (c) 2003 Laurent de Soras

--- 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 (_MSC_VER)
	#pragma warning (1 : 4130) // "'operator' : logical operation on address of string constant"
	#pragma warning (1 : 4223) // "nonstandard extension used : non-lvalue array converted to pointer"
	#pragma warning (1 : 4705) // "statement has no effect"
	#pragma warning (1 : 4706) // "assignment within conditional expression"
	#pragma warning (4 : 4786) // "identifier was truncated to '255' characters in the debug information"
	#pragma warning (4 : 4800) // "forcing value to bool 'true' or 'false' (performance warning)"
	#pragma warning (4 : 4355) // "'this' : used in base member initializer list"
#endif



/*\\\ INCLUDE FILES \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/

#include	"ClockCycleCounter.h"

#include	<cassert>



namespace stopwatch
{



/*\\\ PUBLIC \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/



/*
==============================================================================
Name: ctor
Description:
	The first object constructed initialise global data. This first
	construction may be a bit slow.
Throws: Nothing
==============================================================================
*/

ClockCycleCounter::ClockCycleCounter ()
:	_start_time (0)
,	_state (0)
,	_best_score (-1)
{
	if (! _init_flag)
	{
		// Should be executed in this order
		compute_clk_mul ();
		compute_measure_time_total ();
		compute_measure_time_lap ();

		// Restores object state
		_start_time = 0;
		_state      = 0;
		_best_score = -1;

		_init_flag = true;
	}
}



/*
==============================================================================
Name: get_time_total
Description:
	Gives the time elapsed between the latest stop_lap() and start() calls.
Returns:
	The duration, in clock cycles.
Throws: Nothing
==============================================================================
*/

Int64	ClockCycleCounter::get_time_total () const
{
	const Int64		duration = _state - _start_time;
	assert (duration >= 0);

	const Int64		t = max (
		duration - _measure_time_total,
		static_cast <Int64> (0)
	);

	return (t * _clk_mul);
}



/*
==============================================================================
Name: get_time_best_lap
Description:
	Gives the smallest time between two consecutive stop_lap() or between
	the stop_lap() and start(). The value is reset by a call to start().
	Call this function only after a stop_lap().
	The time is amputed from the duration of the stop_lap() call itself.
Returns:
	The smallest duration, in clock cycles.
Throws: Nothing
==============================================================================
*/

Int64	ClockCycleCounter::get_time_best_lap () const
{
	assert (_best_score >= 0);

	const Int64		t1 = max (
		_best_score - _measure_time_lap,
		static_cast <Int64> (0)
	);
	const Int64		t = min (t1, get_time_total ());

	return (t * _clk_mul);
}



/*\\\ PROTECTED \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/



/*\\\ PRIVATE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/



#if defined (__MACOS__)

static inline double	stopwatch_ClockCycleCounter_get_time_s ()
{
	const Nanoseconds	ns = AbsoluteToNanoseconds (UpTime ());

	return (ns.hi * 4294967296e-9 + ns.lo * 1e-9);
}

#endif	// __MACOS__



/*
==============================================================================
Name: compute_clk_mul
Description:
	This function, only for PowerPC/MacOS computers, computes the multiplier
	required to deduce clock cycles from the internal counter.
Throws: Nothing
==============================================================================
*/

void	ClockCycleCounter::compute_clk_mul ()
{
	assert (! _init_flag);

#if defined (__MACOS__)

	long				clk_speed_mhz = CurrentProcessorSpeed ();
	const Int64		clk_speed =
		static_cast <Int64> (clk_speed_mhz) * (1000L*1000L);

	const double	start_time_s = stopwatch_ClockCycleCounter_get_time_s ();
	start ();

	const double	duration = 0.01;	// Seconds
	while (stopwatch_ClockCycleCounter_get_time_s () - start_time_s < duration)
	{
		continue;
	}

	const double	stop_time_s = stopwatch_ClockCycleCounter_get_time_s ();
	stop ();

	const double	diff_time_s = stop_time_s - start_time_s;
	const double	nbr_cycles = diff_time_s * static_cast <double> (clk_speed);

	const Int64		diff_time_c = _state - _start_time;
	const double	clk_mul = nbr_cycles / static_cast <double> (diff_time_c);

	_clk_mul = round_int (clk_mul);

#endif	// __MACOS__
}



void	ClockCycleCounter::compute_measure_time_total ()
{
	start ();
	spend_time ();

	Int64				best_result = 0x7FFFFFFFL;	// Should be enough
	long				nbr_tests = 100;
	for (long cnt = 0; cnt < nbr_tests; ++cnt)
	{
		start ();
		stop_lap ();
		const Int64		duration = _state - _start_time;
		best_result = min (best_result, duration);
	}

	_measure_time_total = best_result;
}



/*
==============================================================================
Name: compute_measure_time_lap
Description:
	Computes the duration of one stop_lap() call and store it. It will be used
	later to get the real duration of the measured operation (by substracting
	the measurement duration).
Throws: Nothing
==============================================================================
*/

void	ClockCycleCounter::compute_measure_time_lap ()
{
	start ();
	spend_time ();

	long				nbr_tests = 10;
	for (long cnt = 0; cnt < nbr_tests; ++cnt)
	{
		stop_lap ();
		stop_lap ();
		stop_lap ();
		stop_lap ();
	}

	_measure_time_lap = _best_score;
}



void	ClockCycleCounter::spend_time ()
{
	const Int64		nbr_clocks = 500;	// Number of clock cycles to spend

	const Int64		start = read_clock_counter ();
	Int64				current;

	do
	{
		current = read_clock_counter ();
	}
	while ((current - start) * _clk_mul < nbr_clocks);
}



Int64	ClockCycleCounter::_measure_time_total = 0;
Int64	ClockCycleCounter::_measure_time_lap = 0;
int	ClockCycleCounter::_clk_mul = 1;
bool	ClockCycleCounter::_init_flag = false;


}	// namespace stopwatch



/*\\\ EOF \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/