-
Notifications
You must be signed in to change notification settings - Fork 0
/
constant_time_exec.hpp
85 lines (60 loc) · 2.01 KB
/
constant_time_exec.hpp
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
#ifndef CONSTANT_TIME_EXEC_HPP_INCLUDED
#define CONSTANT_TIME_EXEC_HPP_INCLUDED
#include <stdint.h>
#include <iostream>
struct constant_time_exec
{
uint64_t last_time_ms = 0;
uint64_t total_cycles = 0;
uint64_t current_cycles = 0;
uint64_t max_cycles = 0;
double cycle_budget_ms = 0;
uint64_t cycles_per_s = 1000;
void reset(uint64_t current_time_ms)
{
total_cycles = 0;
current_cycles = 0;
max_cycles = 0;
last_time_ms = current_time_ms;
cycle_budget_ms = 0;
}
void init(uint64_t _max_cycles, uint64_t current_time_ms)
{
current_cycles = 0;
max_cycles = _max_cycles;
last_time_ms = current_time_ms;
cycle_budget_ms = 0;
}
///executes a function repeatedly for however many cycles have elapsed
template<typename T>
void exec_until(uint64_t current_time_ms, T&& func)
{
if(max_cycles == current_cycles)
return;
if(cycles_per_s == 0)
return;
uint64_t diff_ms = current_time_ms - last_time_ms;
double cycles_per_ms = cycles_per_s / 1000.;
cycle_budget_ms += diff_ms;
uint64_t cycles_to_exec = cycle_budget_ms * cycles_per_ms;
cycle_budget_ms -= cycles_to_exec / cycles_per_ms;
uint64_t start_time_ms = last_time_ms;
last_time_ms = current_time_ms;
if((cycles_to_exec + current_cycles) > max_cycles)
{
cycles_to_exec = max_cycles - current_cycles;
}
current_cycles += cycles_to_exec;
total_cycles += cycles_to_exec;
assert(current_cycles <= max_cycles);
for(uint64_t i = 0; i < cycles_to_exec; i++)
{
double exec_frac = (double)i / cycles_to_exec;
double exec_diff_ms = (last_time_ms - start_time_ms) * exec_frac;
uint64_t real_time_ms = start_time_ms + exec_diff_ms;
func(i, real_time_ms);
}
//return cycles_to_exec;
}
};
#endif // CONSTANT_TIME_EXEC_HPP_INCLUDED