Datasets:
Download src/xireun.h from PureOne/ChronoSheaf-Input-Lag: direct link, hf CLI and curl.
- Browser
- Download file 5.01 kB
-
https://huggingface.co/datasets/PureOne/ChronoSheaf-Input-Lag/resolve/main/src/xireun.h
- Command line
-
hf download hf://datasets/PureOne/ChronoSheaf-Input-Lag/src/xireun.h
-
curl -L -o xireun.h https://huggingface.co/datasets/PureOne/ChronoSheaf-Input-Lag/resolve/main/src/xireun.h
5.01 kB
| /* C99, one instance per serial producer. All time values are microseconds. | |
| Feed actual waiting after the preceding return (zero when untreated). | |
| The function returns a delay to apply AFTER the current underlying Present. */ | |
| enum xr_mode { XR_OBSERVE, XR_PROBE, XR_ACTIVE, XR_COOLDOWN }; | |
| enum { XR_CAL = 24, XR_PROBES = 16, XR_ACTIVE_LENGTH = 240, XR_COOLDOWN_LENGTH = 180 }; | |
| typedef struct { | |
| enum xr_mode mode; | |
| unsigned count, passes, thin, short_returns; | |
| int64_t previous_end, period, target; | |
| int64_t intervals[XR_CAL], holds[XR_CAL]; | |
| int64_t contrast_hold, contrast_period, treated_wait; | |
| } xr_gate; | |
| static inline void xr_reset(xr_gate *g) { memset(g, 0, sizeof(*g)); } | |
| static inline int64_t xr_min(int64_t a, int64_t b) { return a < b ? a : b; } | |
| static inline int64_t xr_max(int64_t a, int64_t b) { return a > b ? a : b; } | |
| static inline int xr_treated(unsigned i) { | |
| unsigned j = i % 4; | |
| int middle = (j == 1 || j == 2); | |
| return ((i / 4) % 2 == 0) ? middle : !middle; | |
| } | |
| static inline void xr_sort(int64_t *v, unsigned n) { | |
| for (unsigned i = 1; i < n; ++i) { | |
| int64_t x = v[i]; unsigned j = i; | |
| while (j && v[j-1] > x) { v[j] = v[j-1]; --j; } | |
| v[j] = x; | |
| } | |
| } | |
| static inline void xr_cool(xr_gate *g) { | |
| g->mode = XR_COOLDOWN; g->count = 0; g->thin = 0; | |
| } | |
| static inline int xr_calibrate(xr_gate *g) { | |
| xr_sort(g->intervals, XR_CAL); xr_sort(g->holds, XR_CAL); | |
| g->period = g->intervals[XR_CAL / 2]; | |
| if (g->period < 1500 || g->period > 50000 || | |
| g->intervals[XR_CAL-2] - g->intervals[1] > xr_max(1200, g->period / 8)) | |
| return 0; | |
| int64_t reserve = xr_max(500, g->period / 12); | |
| g->target = xr_min(4000, xr_min(g->period/4, (g->holds[2] - reserve)/2)); | |
| return g->target >= 600; | |
| } | |
| static inline int64_t xr_after(xr_gate *g, int64_t begin, int64_t end, int64_t last_wait) { | |
| if (begin < 0 || end <= 0 || begin > end || last_wait < 0 || | |
| (g->previous_end && (begin < g->previous_end || end <= g->previous_end))) { | |
| xr_reset(g); return 0; | |
| } | |
| int64_t before = g->previous_end; | |
| g->previous_end = end; | |
| if (!before) return 0; | |
| int64_t interval = end - before, hold = end - begin; | |
| if (interval > 150000 || last_wait > interval) { | |
| xr_reset(g); g->previous_end = end; return 0; | |
| } | |
| if (g->mode == XR_COOLDOWN) { | |
| if (++g->count >= XR_COOLDOWN_LENGTH) { g->mode = XR_OBSERVE; g->count = 0; } | |
| return 0; | |
| } | |
| if (g->mode == XR_OBSERVE) { | |
| if (last_wait != 0) { g->count = 0; return 0; } | |
| g->intervals[g->count] = interval; | |
| g->holds[g->count++] = hold; | |
| if (g->count == XR_CAL) { | |
| g->count = 0; | |
| if (!xr_calibrate(g)) return 0; | |
| g->mode = XR_PROBE; g->passes = 0; | |
| g->contrast_hold = g->contrast_period = g->treated_wait = 0; | |
| } | |
| return 0; /* First trial in the first quartet is untreated. */ | |
| } | |
| int64_t spread = xr_max(1000, g->period/3); | |
| if (g->mode == XR_ACTIVE && interval < g->period - spread) { | |
| /* An early service can be an isolated clock correction. Suppress | |
| its successor's delay. A second short interval retires the mode. */ | |
| if (++g->short_returns >= 2) xr_cool(g); | |
| return 0; | |
| } | |
| if (interval > g->period + spread || interval < g->period - spread || | |
| last_wait > g->target + xr_max(600, g->target/2)) { | |
| xr_cool(g); return 0; | |
| } | |
| if (g->mode == XR_PROBE) { | |
| int treated = xr_treated(g->count); | |
| if ((treated && last_wait < g->target*3/4) || (!treated && last_wait != 0)) { | |
| xr_cool(g); return 0; | |
| } | |
| int sign = treated ? -1 : 1; | |
| g->contrast_hold += sign * hold; | |
| g->contrast_period -= sign * interval; | |
| if (treated) g->treated_wait += last_wait; | |
| ++g->count; | |
| if (g->count % 4 == 0) { | |
| int64_t waits = g->treated_wait; /* All contrasts are twice a mean. */ | |
| int64_t limit = xr_max(800, waits*35/100); | |
| if (2*g->contrast_hold >= waits && 2*g->contrast_hold <= 3*waits && | |
| g->contrast_period >= -limit && g->contrast_period <= limit) | |
| ++g->passes; | |
| g->contrast_hold = g->contrast_period = g->treated_wait = 0; | |
| } | |
| if (g->count == XR_PROBES) { | |
| if (g->passes < 3) { xr_cool(g); return 0; } | |
| g->mode = XR_ACTIVE; g->count = 0; g->thin = 0; g->short_returns = 0; | |
| return g->target; | |
| } | |
| return xr_treated(g->count) ? g->target : 0; | |
| } | |
| if (!g->short_returns && last_wait < g->target*3/4) { xr_cool(g); return 0; } | |
| g->short_returns = 0; | |
| g->thin = hold < g->target/8 ? g->thin+1 : 0; | |
| if (g->thin >= 2) { xr_cool(g); return 0; } | |
| if (++g->count >= XR_ACTIVE_LENGTH) { | |
| g->mode = XR_OBSERVE; g->count = 0; return 0; | |
| } | |
| return g->target; | |
| } | |