Debian Codegraph @

src/pt2_hpc.c

raw symbols SCIP
1 /*
2 ** Hardware Performance Counter delay routines.
3 **
4 ** These are by no means well written, and are made for specific
5 ** usage cases. There may be some hackish design choices here.
6 **
7 ** NOTE: hpc_SetDurationInHz() has quite a bit of overhead, so it's
8 ** recommended to have one hpcFreq_t counter for each delay value,
9 ** then call hpc_SetDurationInHz()/hpc_SetDurationInMs() on program
10 ** init instead of every time you want to delay.
11 */
12
13 #ifdef _WIN32
14 #define WIN32_MEAN_AND_LEAN
15 #include <windows.h>
16 #else
17 #include <unistd.h>
18 #endif
19 #include <SDL2/SDL.h>
20 #include <stdint.h>
21 #include <stdbool.h>
22 #include "pt2_hpc.h"cxx . . $ `<file>/src/pt2_hpc.h`/
23
24 #define DURATION_FRAC_BITScxx . . $ `src/pt2_hpc.c:24:9`! 52 /* more makes little sense */
25 #define DURATION_FRAC_SCALEcxx . . $ `src/pt2_hpc.c:25:9`! (1ULL << DURATION_FRAC_BITScxx . . $ `src/pt2_hpc.c:24:9`!)
26 #define DURATION_FRAC_MASKcxx . . $ `src/pt2_hpc.c:26:9`! (DURATION_FRAC_SCALEcxx . . $ `src/pt2_hpc.c:25:9`!-1)
27
28 hpcFreq_tcxx . . $ hpcFreq_t# hpcFreqcxx . . $ hpcFreq.;
29
30 #ifdef _WIN32 // Windows usleep() implementation
31
32 #define STATUS_SUCCESS 0
33
34 static bool canAdjustTimerResolution;
35
36 static NTSTATUS (__stdcall *NtDelayExecution)(BOOL Alertable, PLARGE_INTEGER DelayInterval);
37 static NTSTATUS (__stdcall *NtQueryTimerResolution)(PULONG MinimumResolution, PULONG MaximumResolution, PULONG ActualResolution);
38 static NTSTATUS (__stdcall *NtSetTimerResolution)(ULONG DesiredResolution, BOOLEAN SetResolution, PULONG CurrentResolution);
39
40 static void (*usleep)(int32_t usec);
41
42 static void usleepAcceptable(int32_t usec)
43 {
44 LARGE_INTEGER delayInterval;
45
46 // NtDelayExecution() delays in 100ns-units, and a negative value means to delay from current time
47 usec *= -10;
48
49 delayInterval.HighPart = 0xFFFFFFFF; // negative 64-bit value, we only set the lower dword
50 delayInterval.LowPart = usec;
51 NtDelayExecution(false, &delayInterval);
52 }
53
54 static void usleepPoor(int32_t usec) // fallback if no NtDelayExecution()
55 {
56 Sleep((usec + 500) / 1000);
57 }
58
59 static void windowsSetupUsleep(void)
60 {
61 NtDelayExecution = (NTSTATUS (__stdcall *)(BOOL, PLARGE_INTEGER))GetProcAddress(GetModuleHandle("ntdll.dll"), "NtDelayExecution");
62 usleep = (NtDelayExecution != NULL) ? usleepAcceptable : usleepPoor;
63
64 NtQueryTimerResolution = (NTSTATUS (__stdcall *)(PULONG, PULONG, PULONG))GetProcAddress(GetModuleHandle("ntdll.dll"), "NtQueryTimerResolution");
65 NtSetTimerResolution = (NTSTATUS (__stdcall *)(ULONG, BOOLEAN, PULONG))GetProcAddress(GetModuleHandle("ntdll.dll"), "NtSetTimerResolution");
66 canAdjustTimerResolution = (NtQueryTimerResolution != NULL && NtSetTimerResolution != NULL);
67 }
68 #endif
69
70 void hpc_Initcxx . . $ hpc_Init(49f6e7a06ebc5aa8).(void)
71 {
72 #ifdef _WIN32
73 windowsSetupUsleep();
74 #endif
75 hpcFreqcxx . . $ hpcFreq..freq64cxx . . $ $anonymous_type_f41cb469dd1dca50_0#freq64. = SDL_GetPerformanceFrequencycxx . . $ SDL_GetPerformanceFrequency(f708dce9a8856c70).();
76
77 double dFreq = (double)hpcFreqcxx . . $ hpcFreq..freq64cxx . . $ $anonymous_type_f41cb469dd1dca50_0#freq64.;
78
79 hpcFreqcxx . . $ hpcFreq..dFreqMulMscxx . . $ $anonymous_type_f41cb469dd1dca50_0#dFreqMulMs. = 1000.0 / dFreq;
80 hpcFreqcxx . . $ hpcFreq..dFreqMulMicrocxx . . $ $anonymous_type_f41cb469dd1dca50_0#dFreqMulMicro. = (1000.0 * 1000.0) / dFreq;
81 }
82
83 void hpc_SetDurationInHzcxx . . $ hpc_SetDurationInHz(342ecb9d325f249a).(hpc_tcxx . . $ hpc_t# *hpc, double dHz)
84 {
85 const double dTickTime = (double)hpcFreqcxx . . $ hpcFreq..freq64cxx . . $ $anonymous_type_f41cb469dd1dca50_0#freq64. / dHz;
86 double dTimeInt, dTimeFrac = modf(dTickTime, &dTimeInt);
87
88 hpc->durationIntcxx . . $ $anonymous_type_f41cb469dd1dca50_1#durationInt. = (uint32_tcxx . . $ uint32_t#)dTimeInt;
89 hpc->durationFraccxx . . $ $anonymous_type_f41cb469dd1dca50_1#durationFrac. = (uint64_tcxx . . $ uint64_t#)(dTimeFrac * DURATION_FRAC_SCALEcxx . . $ `src/pt2_hpc.c:25:9`!);
90 hpc->resetFramecxx . . $ $anonymous_type_f41cb469dd1dca50_1#resetFrame. = (uint64_tcxx . . $ uint64_t#)(dHz * (60*30)); // reset counters every half an hour
91 }
92
93 void hpc_SetDurationInMscxx . . $ hpc_SetDurationInMs(342ecb9d325f249a).(hpc_tcxx . . $ hpc_t# *hpc, double dMs)
94 {
95 hpc_SetDurationInHzcxx . . $ hpc_SetDurationInHz(342ecb9d325f249a).(hpc, 1000.0 / dMs);
96 }
97
98 void hpc_ResetCounterscxx . . $ hpc_ResetCounters(efacbc4df292ea9a).(hpc_tcxx . . $ hpc_t# *hpc)
99 {
100 hpc->endTimeIntcxx . . $ $anonymous_type_f41cb469dd1dca50_1#endTimeInt. = SDL_GetPerformanceCountercxx . . $ SDL_GetPerformanceCounter(f708dce9a8856c70).() + hpc->durationIntcxx . . $ $anonymous_type_f41cb469dd1dca50_1#durationInt.;
101 hpc->endTimeFraccxx . . $ $anonymous_type_f41cb469dd1dca50_1#endTimeFrac. = hpc->durationFraccxx . . $ $anonymous_type_f41cb469dd1dca50_1#durationFrac.;
102 }
103
104 void hpc_Waitcxx . . $ hpc_Wait(efacbc4df292ea9a).(hpc_tcxx . . $ hpc_t# *hpc)
105 {
106 #ifdef _WIN32
107 /* Make sure resolution is set to 0.5ms (safest minimum) - this is confirmed to improve
108 ** NtDelayExecution() and Sleep(). This will only be changed when needed, not per frame.
109 */
110 ULONG curRes, minRes, maxRes, junk;
111 if (canAdjustTimerResolution && NtQueryTimerResolution(&minRes, &maxRes, &curRes) == STATUS_SUCCESS)
112 {
113 if (curRes != 5000 && maxRes <= 5000)
114 NtSetTimerResolution(5000, TRUE, &junk); // 0.5ms
115 }
116 #endif
117
118 const uint64_tcxx . . $ uint64_t# currTime64 = SDL_GetPerformanceCountercxx . . $ SDL_GetPerformanceCounter(f708dce9a8856c70).();
119 if (currTime64 < hpc->endTimeIntcxx . . $ $anonymous_type_f41cb469dd1dca50_1#endTimeInt.)
120 {
121 uint64_tcxx . . $ uint64_t# timeLeft64 = hpc->endTimeIntcxx . . $ $anonymous_type_f41cb469dd1dca50_1#endTimeInt. - currTime64;
122
123 // convert to int32_t for fast SSE2 SIMD usage lateron
124 if (timeLeft64 > INT32_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:112:10`!)
125 timeLeft64 = INT32_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:112:10`!;
126
127 const int32_tcxx . . $ int32_t# timeLeft32 = (int32_tcxx . . $ int32_t#)timeLeft64;
128
129 int32_tcxx . . $ int32_t# microSecsLeft = (int32_tcxx . . $ int32_t#)((timeLeft32 * hpcFreqcxx . . $ hpcFreq..dFreqMulMicrocxx . . $ $anonymous_type_f41cb469dd1dca50_0#dFreqMulMicro.) + 0.5); // rounded
130 if (microSecsLeft > 0)
131 usleepcxx . . $ usleep(4c679297bf94b9eb).(microSecsLeft);
132 }
133
134 // set next end time
135
136 hpc->endTimeIntcxx . . $ $anonymous_type_f41cb469dd1dca50_1#endTimeInt. += hpc->durationIntcxx . . $ $anonymous_type_f41cb469dd1dca50_1#durationInt.;
137
138 // handle fractional part
139 hpc->endTimeFraccxx . . $ $anonymous_type_f41cb469dd1dca50_1#endTimeFrac. += hpc->durationFraccxx . . $ $anonymous_type_f41cb469dd1dca50_1#durationFrac.;
140 if (hpc->endTimeFraccxx . . $ $anonymous_type_f41cb469dd1dca50_1#endTimeFrac. >= DURATION_FRAC_SCALEcxx . . $ `src/pt2_hpc.c:25:9`!)
141 {
142 hpc->endTimeFraccxx . . $ $anonymous_type_f41cb469dd1dca50_1#endTimeFrac. &= DURATION_FRAC_MASKcxx . . $ `src/pt2_hpc.c:26:9`!;
143 hpc->endTimeIntcxx . . $ $anonymous_type_f41cb469dd1dca50_1#endTimeInt.++;
144 }
145
146 /* The counter ("endTimeInt") can accumulate major errors after a couple of hours,
147 ** since each frame is not happening at perfect intervals.
148 ** To fix this, reset the counter's int & frac once every half an hour. We should only
149 ** get up to one frame of jitter while they are resetting, then it's back to normal.
150 */
151 hpc->frameCountercxx . . $ $anonymous_type_f41cb469dd1dca50_1#frameCounter.++;
152 if (hpc->frameCountercxx . . $ $anonymous_type_f41cb469dd1dca50_1#frameCounter. >= hpc->resetFramecxx . . $ $anonymous_type_f41cb469dd1dca50_1#resetFrame.)
153 {
154 hpc->frameCountercxx . . $ $anonymous_type_f41cb469dd1dca50_1#frameCounter. = 0;
155 hpc_ResetCounterscxx . . $ hpc_ResetCounters(efacbc4df292ea9a).(hpc);
156 }
157 }