Debian Codegraph @

demo/sokol/sokol_time.h

raw symbols SCIP
1 #pragma once
2 /*
3 sokol_time.h -- simple cross-platform time measurement
4
5 Project URL: https://github.com/floooh/sokol
6
7 Do this:
8 #define SOKOL_IMPL
9 before you include this file in *one* C or C++ file to create the
10 implementation.
11
12 Optionally provide the following defines with your own implementations:
13 SOKOL_ASSERT(c) - your own assert macro (default: assert(c))
14 SOKOL_API_DECL - public function declaration prefix (default: extern)
15 SOKOL_API_IMPL - public function implementation prefix (default: -)
16
17 If sokol_time.h is compiled as a DLL, define the following before
18 including the declaration or implementation:
19
20 SOKOL_DLL
21
22 On Windows, SOKOL_DLL will define SOKOL_API_DECL as __declspec(dllexport)
23 or __declspec(dllimport) as needed.
24
25 void stm_setup();
26 Call once before any other functions to initialize sokol_time
27 (this calls for instance QueryPerformanceFrequency on Windows)
28
29 uint64_t stm_now();
30 Get current point in time in unspecified 'ticks'. The value that
31 is returned has no relation to the 'wall-clock' time and is
32 not in a specific time unit, it is only useful to compute
33 time differences.
34
35 uint64_t stm_diff(uint64_t new, uint64_t old);
36 Computes the time difference between new and old. This will always
37 return a positive, non-zero value.
38
39 uint64_t stm_since(uint64_t start);
40 Takes the current time, and returns the elapsed time since start
41 (this is a shortcut for "stm_diff(stm_now(), start)")
42
43 uint64_t stm_laptime(uint64_t* last_time);
44 This is useful for measuring frame time and other recurring
45 events. It takes the current time, returns the time difference
46 to the value in last_time, and stores the current time in
47 last_time for the next call. If the value in last_time is 0,
48 the return value will be zero (this usually happens on the
49 very first call).
50
51 Use the following functions to convert a duration in ticks into
52 useful time units:
53
54 double stm_sec(uint64_t ticks);
55 double stm_ms(uint64_t ticks);
56 double stm_us(uint64_t ticks);
57 double stm_ns(uint64_t ticks);
58 Converts a tick value into seconds, milliseconds, microseconds
59 or nanoseconds. Note that not all platforms will have nanosecond
60 or even microsecond precision.
61
62 Uses the following time measurement functions under the hood:
63
64 Windows: QueryPerformanceFrequency() / QueryPerformanceCounter()
65 MacOS/iOS: mach_absolute_time()
66 emscripten: performance.now()
67 Linux+others: clock_gettime(CLOCK_MONOTONIC)
68
69 zlib/libpng license
70
71 Copyright (c) 2018 Andre Weissflog
72
73 This software is provided 'as-is', without any express or implied warranty.
74 In no event will the authors be held liable for any damages arising from the
75 use of this software.
76
77 Permission is granted to anyone to use this software for any purpose,
78 including commercial applications, and to alter it and redistribute it
79 freely, subject to the following restrictions:
80
81 1. The origin of this software must not be misrepresented; you must not
82 claim that you wrote the original software. If you use this software in a
83 product, an acknowledgment in the product documentation would be
84 appreciated but is not required.
85
86 2. Altered source versions must be plainly marked as such, and must not
87 be misrepresented as being the original software.
88
89 3. This notice may not be removed or altered from any source
90 distribution.
91 */
92 #define SOKOL_TIME_INCLUDED (1)
93 #include <stdint.h>
94
95 #ifndef SOKOL_API_DECL
96 #if defined(_WIN32) && defined(SOKOL_DLL) && defined(SOKOL_IMPL)
97 #define SOKOL_API_DECL __declspec(dllexport)
98 #elif defined(_WIN32) && defined(SOKOL_DLL)
99 #define SOKOL_API_DECL __declspec(dllimport)
100 #else
101 #define SOKOL_API_DECL extern
102 #endif
103 #endif
104
105 #ifdef __cplusplus
106 extern "C" {
107 #endif
108
109 SOKOL_API_DECL void stm_setup(void);
110 SOKOL_API_DECL uint64_t stm_now(void);
111 SOKOL_API_DECL uint64_t stm_diff(uint64_t new_ticks, uint64_t old_ticks);
112 SOKOL_API_DECL uint64_t stm_since(uint64_t start_ticks);
113 SOKOL_API_DECL uint64_t stm_laptime(uint64_t* last_time);
114 SOKOL_API_DECL double stm_sec(uint64_t ticks);
115 SOKOL_API_DECL double stm_ms(uint64_t ticks);
116 SOKOL_API_DECL double stm_us(uint64_t ticks);
117 SOKOL_API_DECL double stm_ns(uint64_t ticks);
118
119 #ifdef __cplusplus
120 } /* extern "C" */
121 #endif
122
123 /*-- IMPLEMENTATION ----------------------------------------------------------*/
124 #ifdef SOKOL_IMPL
125 #define SOKOL_TIME_IMPL_INCLUDED (1)
126 #include <string.h> /* memset */
127
128 #ifndef SOKOL_API_IMPL
129 #define SOKOL_API_IMPL
130 #endif
131 #ifndef SOKOL_ASSERT
132 #include <assert.h>
133 #define SOKOL_ASSERT(c) assert(c)
134 #endif
135 #ifndef _SOKOL_PRIVATE
136 #if defined(__GNUC__)
137 #define _SOKOL_PRIVATE __attribute__((unused)) static
138 #else
139 #define _SOKOL_PRIVATE static
140 #endif
141 #endif
142
143 #if defined(_WIN32)
144 #ifndef WIN32_LEAN_AND_MEAN
145 #define WIN32_LEAN_AND_MEAN
146 #endif
147 #include <windows.h>
148 typedef struct {
149 uint32_t initialized;
150 LARGE_INTEGER freq;
151 LARGE_INTEGER start;
152 } _stm_state_t;
153 #elif defined(__APPLE__) && defined(__MACH__)
154 #include <mach/mach_time.h>
155 typedef struct {
156 uint32_t initialized;
157 mach_timebase_info_data_t timebase;
158 uint64_t start;
159 } _stm_state_t;
160 #elif defined(__EMSCRIPTEN__)
161 #include <emscripten/emscripten.h>
162 typedef struct {
163 uint32_t initialized;
164 double start;
165 } _stm_state_t;
166 #else /* anything else, this will need more care for non-Linux platforms */
167 #include <time.h>
168 typedef struct {
169 uint32_t initialized;
170 uint64_t start;
171 } _stm_state_t;
172 #endif
173 static _stm_state_t _stm;
174
175 /* prevent 64-bit overflow when computing relative timestamp
176 see https://gist.github.com/jspohr/3dc4f00033d79ec5bdaf67bc46c813e3
177 */
178 #if defined(_WIN32) || (defined(__APPLE__) && defined(__MACH__))
179 _SOKOL_PRIVATE int64_t int64_muldiv(int64_t value, int64_t numer, int64_t denom) {
180 int64_t q = value / denom;
181 int64_t r = value % denom;
182 return q * numer + r * numer / denom;
183 }
184 #endif
185
186 #if defined(__EMSCRIPTEN__)
187 EM_JS(double, stm_js_perfnow, (void), {
188 return performance.now();
189 });
190 #endif
191
192 SOKOL_API_IMPL void stm_setup(void) {
193 memset(&_stm, 0, sizeof(_stm));
194 _stm.initialized = 0xABCDABCD;
195 #if defined(_WIN32)
196 QueryPerformanceFrequency(&_stm.freq);
197 QueryPerformanceCounter(&_stm.start);
198 #elif defined(__APPLE__) && defined(__MACH__)
199 mach_timebase_info(&_stm.timebase);
200 _stm.start = mach_absolute_time();
201 #elif defined(__EMSCRIPTEN__)
202 _stm.start = stm_js_perfnow();
203 #else
204 struct timespec ts;
205 clock_gettime(CLOCK_MONOTONIC, &ts);
206 _stm.start = (uint64_t)ts.tv_sec*1000000000 + (uint64_t)ts.tv_nsec;
207 #endif
208 }
209
210 SOKOL_API_IMPL uint64_t stm_now(void) {
211 SOKOL_ASSERT(_stm.initialized == 0xABCDABCD);
212 uint64_t now;
213 #if defined(_WIN32)
214 LARGE_INTEGER qpc_t;
215 QueryPerformanceCounter(&qpc_t);
216 now = int64_muldiv(qpc_t.QuadPart - _stm.start.QuadPart, 1000000000, _stm.freq.QuadPart);
217 #elif defined(__APPLE__) && defined(__MACH__)
218 const uint64_t mach_now = mach_absolute_time() - _stm.start;
219 now = int64_muldiv(mach_now, _stm.timebase.numer, _stm.timebase.denom);
220 #elif defined(__EMSCRIPTEN__)
221 double js_now = stm_js_perfnow() - _stm.start;
222 SOKOL_ASSERT(js_now >= 0.0);
223 now = (uint64_t) (js_now * 1000000.0);
224 #else
225 struct timespec ts;
226 clock_gettime(CLOCK_MONOTONIC, &ts);
227 now = ((uint64_t)ts.tv_sec*1000000000 + (uint64_t)ts.tv_nsec) - _stm.start;
228 #endif
229 return now;
230 }
231
232 SOKOL_API_IMPL uint64_t stm_diff(uint64_t new_ticks, uint64_t old_ticks) {
233 if (new_ticks > old_ticks) {
234 return new_ticks - old_ticks;
235 }
236 else {
237 return 1;
238 }
239 }
240
241 SOKOL_API_IMPL uint64_t stm_since(uint64_t start_ticks) {
242 return stm_diff(stm_now(), start_ticks);
243 }
244
245 SOKOL_API_IMPL uint64_t stm_laptime(uint64_t* last_time) {
246 SOKOL_ASSERT(last_time);
247 uint64_t dt = 0;
248 uint64_t now = stm_now();
249 if (0 != *last_time) {
250 dt = stm_diff(now, *last_time);
251 }
252 *last_time = now;
253 return dt;
254 }
255
256 SOKOL_API_IMPL double stm_sec(uint64_t ticks) {
257 return (double)ticks / 1000000000.0;
258 }
259
260 SOKOL_API_IMPL double stm_ms(uint64_t ticks) {
261 return (double)ticks / 1000000.0;
262 }
263
264 SOKOL_API_IMPL double stm_us(uint64_t ticks) {
265 return (double)ticks / 1000.0;
266 }
267
268 SOKOL_API_IMPL double stm_ns(uint64_t ticks) {
269 return (double)ticks;
270 }
271 #endif /* SOKOL_IMPL */
272