2
0

sokol_time.h 8.6 KB

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