summaryrefslogtreecommitdiff
path: root/unittests/ihvtest1/sys_clock.cpp
blob: 4412ed887996c24af74cdc054334d908ff19b5b8 (plain) (blame)
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#include <assert.h>

#pragma optimize( "", off )

#pragma pack( push, thing )
#pragma pack( 4 )
static long g_cw, g_single_cw, g_highchop_cw, g_full_cw, g_ceil_cw, g_pushed_cw;
static struct
{
	long dummy[8];
} g_fpenv;
#pragma pack( pop, thing )

	
void __declspec ( naked ) MaskExceptions()
{
	_asm
	{
		fnstenv ds:dword ptr[g_fpenv]
		or ds:dword ptr[g_fpenv],03Fh
		fldenv ds:dword ptr[g_fpenv]
		ret
	}
}

void __declspec ( naked ) Sys_SetFPCW()
{
	_asm
	{
		fnstcw ds:word ptr[g_cw]
		mov eax,ds:dword ptr[g_cw]
		and ah,0F0h
		or ah,003h
		mov ds:dword ptr[g_full_cw],eax
		mov ds:dword ptr[g_highchop_cw],eax
		and ah,0F0h
		or ah,00Ch
		mov ds:dword ptr[g_single_cw],eax
		and ah,0F0h
		or ah,008h
		mov ds:dword ptr[g_ceil_cw],eax
		ret
	}
}

void __declspec ( naked ) Sys_PushFPCW_SetHigh()
{
	_asm
	{
		fnstcw ds:word ptr[g_pushed_cw]
		fldcw ds:word ptr[g_full_cw]
		ret
	}
}

void __declspec ( naked ) Sys_PopFPCW()
{
	_asm
	{
		fldcw ds:word ptr[g_pushed_cw]
		ret
	}
}

#pragma optimize( "", on )

//-----------------------------------------------------------------------------
// Purpose: Implements high precision clock
// TODO:  Make into an interface?
//-----------------------------------------------------------------------------
class CSysClock
{
public:
	// Construction
							CSysClock( void );

	// Initialization
	void					Init( void );
	void					SetStartTime( void );

	// Sample the clock
	double					GetTime( void );

private:
	// High performance clock frequency
	double					m_dClockFrequency;
	// Current accumulated time
	double					m_dCurrentTime;
	// How many bits to shift raw 64 bit sample count by
	int						m_nTimeSampleShift;
	// Previous 32 bit sample count
	unsigned int			m_uiPreviousTime;

	bool					m_bInitialized;
};

static CSysClock g_Clock;

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
CSysClock::CSysClock( void )
{
	m_bInitialized = false;
}

//-----------------------------------------------------------------------------
// Purpose: Initialize the clock
//-----------------------------------------------------------------------------
void CSysClock::Init( void )
{
	BOOL success;
	LARGE_INTEGER	PerformanceFreq;
	unsigned int	lowpart, highpart;

	MaskExceptions ();
	Sys_SetFPCW ();

	// Start clock at zero
	m_dCurrentTime			= 0.0;

	success = QueryPerformanceFrequency( &PerformanceFreq );
	assert( success );

	// get 32 out of the 64 time bits such that we have around
	// 1 microsecond resolution
	lowpart		= (unsigned int)PerformanceFreq.LowPart;
	highpart	= (unsigned int)PerformanceFreq.HighPart;
	
	m_nTimeSampleShift	= 0;

	while ( highpart || ( lowpart > 2000000.0 ) )
	{
		m_nTimeSampleShift++;
		lowpart >>= 1;
		lowpart |= (highpart & 1) << 31;
		highpart >>= 1;
	}
	
	m_dClockFrequency = 1.0 / (double)lowpart;

	// Get initial sample
	unsigned int		temp;
	LARGE_INTEGER		PerformanceCount;
	QueryPerformanceCounter( &PerformanceCount );
	if ( !m_nTimeSampleShift )
	{
		temp = (unsigned int)PerformanceCount.LowPart;
	}
	else
	{
		// Rotate counter to right by m_nTimeSampleShift places
		temp = ((unsigned int)PerformanceCount.LowPart >> m_nTimeSampleShift) |
			   ((unsigned int)PerformanceCount.HighPart << (32 - m_nTimeSampleShift));
	}

	// Set first time stamp
	m_uiPreviousTime = temp;

	m_bInitialized = true;

	SetStartTime();
}

void CSysClock::SetStartTime( void )
{
	GetTime();

	m_dCurrentTime = 0.0;

	m_uiPreviousTime = ( unsigned int )m_dCurrentTime;
}

double CSysClock::GetTime( void )
{
	LARGE_INTEGER		PerformanceCount;
	unsigned int		temp, t2;
	double				time;
	
	if ( !m_bInitialized )
	{
		return 0.0;
	}

	Sys_PushFPCW_SetHigh();

	// Get sample counter
	QueryPerformanceCounter( &PerformanceCount );

	if ( !m_nTimeSampleShift )
	{
		temp = (unsigned int)PerformanceCount.LowPart;
	}
	else
	{
		// Rotate counter to right by m_nTimeSampleShift places
		temp = ((unsigned int)PerformanceCount.LowPart >> m_nTimeSampleShift) |
			   ((unsigned int)PerformanceCount.HighPart << (32 - m_nTimeSampleShift));
	}

	// check for turnover or backward time
	if ( ( temp <= m_uiPreviousTime ) && 
		( ( m_uiPreviousTime - temp ) < 0x10000000) )
	{
		m_uiPreviousTime = temp;	// so we can't get stuck
	}
	else
	{
		// gap in performance clocks
		t2 = temp - m_uiPreviousTime;

		// Convert to time using frequencey of clock
		time = (double)t2 * m_dClockFrequency;

		// Remember old time
		m_uiPreviousTime = temp;

		// Increment clock
		m_dCurrentTime += time;
	}

	Sys_PopFPCW();

	// Convert to float
    return m_dCurrentTime;

}

//-----------------------------------------------------------------------------
// Purpose: Sample the high-precision clock
// Output : double
//-----------------------------------------------------------------------------
double Sys_FloatTime( void )
{
	return g_Clock.GetTime();
}

//-----------------------------------------------------------------------------
// Purpose: Initialize high-precision clock
//-----------------------------------------------------------------------------
void Sys_InitFloatTime( void )
{
	g_Clock.Init();
}