forked from RandyGaul/cute_headers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcute_time.h
236 lines (191 loc) · 7.11 KB
/
cute_time.h
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
/*
------------------------------------------------------------------------------
Licensing information can be found at the end of the file.
------------------------------------------------------------------------------
cute_time.h - v1.00
To create implementation (the function definitions)
#define CUTE_TIME_IMPLEMENTATION
in *one* C/CPP file (translation unit) that includes this file
*/
#if !defined(CUTE_TIME_H)
#include <stdint.h>
typedef struct ct_timer_t ct_timer_t;
// quick and dirty elapsed time since last call
float ct_time();
// high precision timer functions found below
// Call once to setup this timer on this thread
// does a cs_record call
void ct_init_timer(ct_timer_t* timer);
// returns raw ticks in platform-specific units between now-time and last cs_record call
int64_t ct_elapsed(ct_timer_t* timer);
// ticks to seconds conversion
int64_t ct_seconds(ct_timer_t* timer, int64_t ticks);
// ticks to milliseconds conversion
int64_t ct_milliseconds(ct_timer_t* timer, int64_t ticks);
// ticks to microseconds conversion
int64_t ct_microseconds(ct_timer_t* timer, int64_t ticks);
// records the now-time in raw platform-specific units
void cs_record(ct_timer_t* timer);
#define CUTE_TIME_WINDOWS 1
#define CUTE_TIME_MAC 2
#define CUTE_TIME_UNIX 3
#if defined(_WIN32)
#define CUTE_TIME_PLATFORM CUTE_TIME_WINDOWS
#elif defined(__APPLE__)
#define CUTE_TIME_PLATFORM CUTE_TIME_MAC
#else
#define CUTE_TIME_PLATFORM CUTE_TIME_UNIX
#endif
#if CUTE_TIME_PLATFORM == CUTE_TIME_WINDOWS
struct ct_timer_t
{
LARGE_INTEGER freq;
LARGE_INTEGER prev;
};
#elif CUTE_TIME_PLATFORM == CUTE_TIME_MAC
#else
#endif
#define CUTE_TIME_H
#endif
#ifdef CUTE_TIME_IMPLEMENTATION
#ifndef CUTE_TIME_IMPLEMENTATION_ONCE
#define CUTE_TIME_IMPLEMENTATION_ONCE
// These functions are intended be called from a single thread only. In a
// multi-threaded environment make sure to call Time from the main thread only.
// Also try to set a thread affinity for the main thread to avoid core swaps.
// This can help prevent annoying bios bugs etc. from giving weird time-warp
// effects as the main thread gets passed from one core to another. This shouldn't
// be a problem, but it's a good practice to setup the affinity. Calling these
// functions on multiple threads multiple times will grant a heft performance
// loss in the form of false sharing due to CPU cache synchronization across
// multiple cores.
// More info: https://msdn.microsoft.com/en-us/library/windows/desktop/ee417693(v=vs.85).aspx
#if CUTE_TIME_PLATFORM == CUTE_TIME_WINDOWS
float ct_time()
{
static int first = 1;
static LARGE_INTEGER prev;
static double factor;
LARGE_INTEGER now;
QueryPerformanceCounter(&now);
if (first)
{
first = 0;
prev = now;
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
factor = 1.0 / (double)freq.QuadPart;
return 0;
}
float elapsed = (float)((double)(now.QuadPart - prev.QuadPart) * factor);
prev = now;
return elapsed;
}
void ct_init_timer(ct_timer_t* timer)
{
QueryPerformanceCounter(&timer->prev);
QueryPerformanceFrequency(&timer->freq);
}
int64_t ct_elapsed(ct_timer_t* timer)
{
LARGE_INTEGER now;
QueryPerformanceCounter(&now);
return (int64_t)(now.QuadPart - timer->prev.QuadPart);
}
int64_t ct_seconds(ct_timer_t* timer, int64_t ticks)
{
return ticks / timer->freq.QuadPart;
}
int64_t ct_milliseconds(ct_timer_t* timer, int64_t ticks)
{
return ticks / (timer->freq.QuadPart / 1000);
}
int64_t ct_microseconds(ct_timer_t* timer, int64_t ticks)
{
return ticks / (timer->freq.QuadPart / 1000000);
}
void cs_record(ct_timer_t* timer)
{
QueryPerformanceCounter(&timer->prev);
}
#elif CUTE_TIME_PLATFORM == CUTE_TIME_MAC
#include <mach/mach_time.h>
float ct_time()
{
static int first = 1;
static uint64_t prev = 0;
static double factor;
if (first)
{
first = 0;
mach_timebase_info_data_t info;
mach_timebase_info(&info);
factor = (double)info.numer / ((double)info.denom * 1.0e9);
prev = mach_absolute_time();
return 0;
}
uint64_t now = mach_absolute_time();
float elapsed = (float)((double)(now - prev) * factor);
prev = now;
return elapsed;
}
#else
#include <time.h>
float ct_time()
{
static int first = 1;
static struct timespec prev;
if (first)
{
first = 0;
clock_gettime(CLOCK_MONOTONIC, &prev);
return 0;
}
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
float elapsed = (float)((double)(now.tv_sec - prev.tv_sec) + ((double)(now.tv_nsec - prev.tv_nsec) * 1.0e-9));
prev = now;
return elapsed;
}
#endif
#endif // CUTE_TIME_IMPLEMENTATION_ONCE
#endif // CUTE_TIME_IMPLEMENTATION
/*
------------------------------------------------------------------------------
This software is available under 2 licenses - you may choose the one you like.
------------------------------------------------------------------------------
ALTERNATIVE A - zlib license
Copyright (c) 2017 Randy Gaul http://www.randygaul.net
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from
the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not
be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
------------------------------------------------------------------------------
ALTERNATIVE B - Public Domain (www.unlicense.org)
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this
software dedicate any and all copyright interest in the software to the public
domain. We make this dedication for the benefit of the public at large and to
the detriment of our heirs and successors. We intend this dedication to be an
overt act of relinquishment in perpetuity of all present and future rights to
this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
------------------------------------------------------------------------------
*/