1 #include "HalideRuntime.h"
2 
3 extern "C" {
4 
5 #ifdef BITS_64
6 extern bool QueryPerformanceCounter(int64_t *);
7 extern bool QueryPerformanceFrequency(int64_t *);
8 extern void Sleep(int);
9 #else
10 extern __stdcall bool QueryPerformanceCounter(int64_t *);
11 extern __stdcall bool QueryPerformanceFrequency(int64_t *);
12 extern __stdcall void Sleep(int);
13 #endif
14 
15 WEAK bool halide_reference_clock_inited = false;
16 WEAK int64_t halide_reference_clock = 0;
17 WEAK int64_t halide_clock_frequency = 1;
18 
halide_start_clock(void * user_context)19 WEAK int halide_start_clock(void *user_context) {
20     // Guard against multiple calls
21     if (!halide_reference_clock_inited) {
22         QueryPerformanceCounter(&halide_reference_clock);
23         QueryPerformanceFrequency(&halide_clock_frequency);
24         halide_reference_clock_inited = true;
25     }
26     return 0;
27 }
28 
halide_current_time_ns(void * user_context)29 WEAK int64_t halide_current_time_ns(void *user_context) {
30     int64_t clock;
31     QueryPerformanceCounter(&clock);
32     clock -= halide_reference_clock;
33     double ns_per_tick = 1000000000.0 / halide_clock_frequency;
34     return (int64_t)(ns_per_tick * clock);
35 }
36 
halide_sleep_ms(void * user_context,int ms)37 WEAK void halide_sleep_ms(void *user_context, int ms) {
38     Sleep(ms);
39 }
40 }
41