startup: keep GameInput timestamps from wrapping after 21 days #1551

Merged
RenzoWit35 merged 1 commit from upstream-pr/1558 into main 2026-11-24 14:10:24 +00:00
RenzoWit35 commented 2026-11-24 13:59:57 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by RenzoWit35 on 2026-11-24T13:59:57Z.


Change

The GameInput stand-in (src/startup_stubs/gameinput.cpp, now_microseconds()) converted the performance counter to microseconds with one 64-bit product:

return static_cast<u64>(counter.QuadPart) * 1000000 / static_cast<u64>(frequency.QuadPart);

counter * 1000000 wraps once the counter passes 18,446,744,073,709 ticks. At a counter frequency of 10 MHz that is 21.35 days of counting. From then on the three timestamps the stand-in hands out (IGameInput::GetCurrentTimestamp, the reading timestamp and the device-callback timestamp) are no longer the counter in microseconds: they start again near zero and repeat every 21.35 days.

  • The conversion moves into a small header, src/startup_stubs/qpc_microseconds.h (x2w_startup::qpc_microseconds(counter, frequency)), so it can be checked without a device.
  • It converts whole seconds and the rest of a second separately: counter / frequency * 1000000 + counter % frequency * 1000000 / frequency. This is the same floor(counter * 1000000 / frequency). The rest is below frequency, so its product fits for every frequency up to 18,446,744,073,709 Hz.
  • now_microseconds() calls the helper. Nothing else in gameinput.cpp changes. Results below the old wrap point are identical.
  • New device-free check tests/startup_qpc_microseconds_check.cpp, registered next to the other header checks in CMakeLists.txt as startup_qpc_microseconds, with startup_qpc_microseconds_product_control (--mutate product, WILL_FAIL) as the negative control, following startup_storage_root and startup_plane_size.

Worked values at 10,000,000 Hz (recomputed with Python integers):

Counter Old result Correct result
18,446,744,073,709 (last one the product holds) 1,844,674,407,370 1,844,674,407,370
18,446,744,073,710 0 1,844,674,407,371
20,000,000,000,000 (about 23.1 days) 155,325,592,629 2,000,000,000,000

Verification

Environment: Windows 11 x64, MSVC 19.51 (VS Build Tools 2026), Ninja, dev preset (Debug), Python 3.14.5. Base: origin/main at bc43e665. The local counter frequency is 10,000,000 Hz ([System.Diagnostics.Stopwatch]::Frequency).

Commands (PowerShell, repository root):

. .\tools\dev.ps1
cmake --preset dev -B out/build/dev -DINTAKE_PYTHON=<repo>/out/pe-env/Scripts/python.exe
cmake --build out/build/dev --target startup_qpc_microseconds_check startup_stub_gameinput startup_gameinput_keys --parallel 3
ctest --test-dir out/build/dev --no-tests=error -R '^(startup_qpc_microseconds|startup_gameinput_keys)' -V

Observed with the fix: 100% tests passed, 0 tests failed out of 3.

  • startup_qpc_microseconds: startup_qpc_microseconds: PASS (17 conversions).
  • startup_qpc_microseconds_product_control: the check exits 1 with startup_qpc_microseconds: 10 FAILED; CTest reports Passed because of WILL_FAIL.
  • startup_gameinput_keys: PASS: startup_gameinput_keys (loads the rebuilt GameInput.dll).

The 17 values cover: 0, 9 and 10 ticks, one second, the last counter the single product holds, the first one past it, about 23 days, 30 days and one tick before it, the largest signed counter, 3,579,545 Hz with a non-zero rest and around a whole-second boundary at 70 days, 2,435,886 Hz at 400 days plus 1,234,567 ticks, 1 MHz and 1 kHz. Expected values are literal constants computed with Python integers.

Negative control: with the old one-line formula (return counter * 1000000 / frequency;) put back into the header and only startup_qpc_microseconds_check rebuilt, ctest ... -R '^startup_qpc_microseconds' -V gave 50% tests passed, 1 tests failed out of 2: startup_qpc_microseconds failed with startup_qpc_microseconds: 10 FAILED. First lines:

FAIL first counter past one product: counter 18446744073710 at 10000000 Hz gave 0, expected 1844674407371
FAIL about 23 days: counter 20000000000000 at 10000000 Hz gave 155325592629, expected 2000000000000
FAIL 3,579,545 Hz with a rest: counter 20000000000000 at 3579545 Hz gave 433925520224, expected 5587302296800

The seven values at or below 18,446,744,073,709 ticks pass with both formulas. The header was then restored and the check rebuilt; the committed tree gives the result above.

Also run: python -B tests/docs_links_check.py (Checked 16 Markdown files and 339 links.) and python -B tests/bidi_controls_check.py (Checked 1235 tracked source, script and build/CI files: no bidirectional controls.), both exit 0.

Rebased onto origin/main at a3633d4d before opening this pull request and re-run there: startup_qpc_microseconds, startup_qpc_microseconds_product_control and startup_gameinput_keys give the same results (3 of 3 passed).

Not run: the full test suite, the Release configuration, the pr preset and any hardware test. The wrap was shown with constructed counter values only; it was not observed on a machine whose counter had passed 21 days (the test machine had been up for a few hours). What a console executable does with a timestamp that jumps back is not measured.

Evidence and follow-up

No open pull request touches src/startup_stubs/gameinput.cpp or tests/startup_gameinput_keys.cpp (checked with gh pr list ... --json number,title,files right before the commit and again after it; 53 open pull requests at the last check). 31 of them touch CMakeLists.txt; none of their diffs touches the startup_plane_size / startup_derived_blend lines this registration sits between. No open issue mentions the GameInput timestamp; none is linked.

No documentation row was added: docs/startup-runtime.md has no section on the GameInput timestamps, and no list in AGENTS.md, the workflow or CMakePresets.json enumerates the sibling header checks.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6583478695 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1558). Originally posted by [RenzoWit35](https://github.com/RenzoWit35) on 2026-11-24T13:59:57Z. --- ## Change The GameInput stand-in (`src/startup_stubs/gameinput.cpp`, `now_microseconds()`) converted the performance counter to microseconds with one 64-bit product: ```cpp return static_cast<u64>(counter.QuadPart) * 1000000 / static_cast<u64>(frequency.QuadPart); ``` `counter * 1000000` wraps once the counter passes 18,446,744,073,709 ticks. At a counter frequency of 10 MHz that is 21.35 days of counting. From then on the three timestamps the stand-in hands out (`IGameInput::GetCurrentTimestamp`, the reading timestamp and the device-callback timestamp) are no longer the counter in microseconds: they start again near zero and repeat every 21.35 days. - The conversion moves into a small header, `src/startup_stubs/qpc_microseconds.h` (`x2w_startup::qpc_microseconds(counter, frequency)`), so it can be checked without a device. - It converts whole seconds and the rest of a second separately: `counter / frequency * 1000000 + counter % frequency * 1000000 / frequency`. This is the same `floor(counter * 1000000 / frequency)`. The rest is below `frequency`, so its product fits for every frequency up to 18,446,744,073,709 Hz. - `now_microseconds()` calls the helper. Nothing else in `gameinput.cpp` changes. Results below the old wrap point are identical. - New device-free check `tests/startup_qpc_microseconds_check.cpp`, registered next to the other header checks in `CMakeLists.txt` as `startup_qpc_microseconds`, with `startup_qpc_microseconds_product_control` (`--mutate product`, `WILL_FAIL`) as the negative control, following `startup_storage_root` and `startup_plane_size`. Worked values at 10,000,000 Hz (recomputed with Python integers): | Counter | Old result | Correct result | |---|---|---| | 18,446,744,073,709 (last one the product holds) | 1,844,674,407,370 | 1,844,674,407,370 | | 18,446,744,073,710 | 0 | 1,844,674,407,371 | | 20,000,000,000,000 (about 23.1 days) | 155,325,592,629 | 2,000,000,000,000 | ## Verification Environment: Windows 11 x64, MSVC 19.51 (VS Build Tools 2026), Ninja, `dev` preset (Debug), Python 3.14.5. Base: `origin/main` at `bc43e665`. The local counter frequency is 10,000,000 Hz (`[System.Diagnostics.Stopwatch]::Frequency`). Commands (PowerShell, repository root): ```powershell . .\tools\dev.ps1 cmake --preset dev -B out/build/dev -DINTAKE_PYTHON=<repo>/out/pe-env/Scripts/python.exe cmake --build out/build/dev --target startup_qpc_microseconds_check startup_stub_gameinput startup_gameinput_keys --parallel 3 ctest --test-dir out/build/dev --no-tests=error -R '^(startup_qpc_microseconds|startup_gameinput_keys)' -V ``` Observed with the fix: `100% tests passed, 0 tests failed out of 3`. - `startup_qpc_microseconds`: `startup_qpc_microseconds: PASS (17 conversions)`. - `startup_qpc_microseconds_product_control`: the check exits 1 with `startup_qpc_microseconds: 10 FAILED`; CTest reports `Passed` because of `WILL_FAIL`. - `startup_gameinput_keys`: `PASS: startup_gameinput_keys` (loads the rebuilt `GameInput.dll`). The 17 values cover: 0, 9 and 10 ticks, one second, the last counter the single product holds, the first one past it, about 23 days, 30 days and one tick before it, the largest signed counter, 3,579,545 Hz with a non-zero rest and around a whole-second boundary at 70 days, 2,435,886 Hz at 400 days plus 1,234,567 ticks, 1 MHz and 1 kHz. Expected values are literal constants computed with Python integers. Negative control: with the old one-line formula (`return counter * 1000000 / frequency;`) put back into the header and only `startup_qpc_microseconds_check` rebuilt, `ctest ... -R '^startup_qpc_microseconds' -V` gave `50% tests passed, 1 tests failed out of 2`: `startup_qpc_microseconds` failed with `startup_qpc_microseconds: 10 FAILED`. First lines: ```text FAIL first counter past one product: counter 18446744073710 at 10000000 Hz gave 0, expected 1844674407371 FAIL about 23 days: counter 20000000000000 at 10000000 Hz gave 155325592629, expected 2000000000000 FAIL 3,579,545 Hz with a rest: counter 20000000000000 at 3579545 Hz gave 433925520224, expected 5587302296800 ``` The seven values at or below 18,446,744,073,709 ticks pass with both formulas. The header was then restored and the check rebuilt; the committed tree gives the result above. Also run: `python -B tests/docs_links_check.py` (`Checked 16 Markdown files and 339 links.`) and `python -B tests/bidi_controls_check.py` (`Checked 1235 tracked source, script and build/CI files: no bidirectional controls.`), both exit 0. Rebased onto `origin/main` at `a3633d4d` before opening this pull request and re-run there: `startup_qpc_microseconds`, `startup_qpc_microseconds_product_control` and `startup_gameinput_keys` give the same results (3 of 3 passed). Not run: the full test suite, the Release configuration, the `pr` preset and any hardware test. The wrap was shown with constructed counter values only; it was not observed on a machine whose counter had passed 21 days (the test machine had been up for a few hours). What a console executable does with a timestamp that jumps back is not measured. ## Evidence and follow-up No open pull request touches `src/startup_stubs/gameinput.cpp` or `tests/startup_gameinput_keys.cpp` (checked with `gh pr list ... --json number,title,files` right before the commit and again after it; 53 open pull requests at the last check). 31 of them touch `CMakeLists.txt`; none of their diffs touches the `startup_plane_size` / `startup_derived_blend` lines this registration sits between. No open issue mentions the GameInput timestamp; none is linked. No documentation row was added: `docs/startup-runtime.md` has no section on the GameInput timestamps, and no list in `AGENTS.md`, the workflow or `CMakePresets.json` enumerates the sibling header checks. AI-assisted.
upstream-sync closed this pull request 2026-11-24 15:01:59 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!1551
No description provided.