[PR #2191] runtime(cpu): emulate F16C, FMA3 and AVX2 via XSTATE_AVX on AVX1 hosts #2195

Open
opened 2026-11-26 21:20:22 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/2191
Author: @robot00f
Created: 11/26/2026
Status: 🔄 Open

Base: main ← Head: feat/rt-cpu-fma-f16c-avx2


📝 Commits (2)

  • 564facb runtime(cpu): emulate BMI1, BMI2, MOVBE and patch LZCNT/TZCNT on AVX1 hosts
  • 9ec6b20 runtime(cpu): emulate F16C, FMA3 and AVX2 via XSTATE_AVX on AVX1 hosts

📊 Changes

7 files changed (+2822 additions, -0 deletions)

View changed files

📝 runtime/CMakeLists.txt (+18 -0)
➕ runtime/src/avx2_emulation.h (+1063 -0)
➕ runtime/src/bmi_emulation.h (+1148 -0)
📝 runtime/src/main.cpp (+15 -0)
📝 runtime/src/pe_loader.cpp (+27 -0)
➕ runtime/test/avx2_emulation_test.cpp (+235 -0)
➕ runtime/test/bmi_emulation_test.cpp (+316 -0)

📄 Description

Summary

Add runtime/src/avx2_emulation.h and wire it into gta6rt so pre-Haswell AVX1 + SSE4.2 hosts (Intel Sandy Bridge and Ivy Bridge) can execute the game's F16C, FMA3, and AVX2 vector instructions using the OS-saved 256-bit YMM0..YMM15 registers (docs/cpu_features.md, PR #2188):

  • Full 256-bit XSTATE_AVX register access:
    • Reads and writes XMM0..XMM15 ([127:0]) from CONTEXT::FltSave.XmmRegisters and YMM0..YMM15[255:128] via GetXStateFeaturesMask / LocateXStateFeature(c, XSTATE_AVX, &len) / SetXStateFeaturesMask.
    • Enforces the x86-64 VEX upper-128 zeroing rule (YMM[dst][255:128] = 0) on every 128-bit VEX register write, and zero-initializes the other 15 upper lanes before enabling XSTATE_MASK_AVX when transitioning out of a clean (vzeroupper) state.
    • Leaves native AVX1 instructions (including 256-bit vptest, vperm2f128, vpermilps, vmovups/vmovdqu, etc.) to execute directly on hardware.
  • Instruction coverage (#UD / STATUS_ILLEGAL_INSTRUCTION):
    • F16C: vcvtph2ps and vcvtps2ph (register and memory destinations, imm8 RN/RD/RU/RZ and MXCSR.RC rounding, subnormals, signed zero, Inf, and quiet NaN payloads).
    • FMA3: all 60 0F38 forms (132, 213, 231; single and double precision; 128-bit and 256-bit packed, scalar ss/sd preserving dst[127:32]/[127:64], and vfmaddsub*/vfmsubadd*) via single-rounding std::fmaf / std::fma.
    • AVX2: 256-bit integer SIMD promotions (2-byte C5 and 3-byte C4 VEX), vbroadcastss / vbroadcastsd (including ModRM.mod == 3 register-source forms), vbroadcasti128, vpbroadcastb/w/d/q, vinserti128, vextracti128, vperm2i128, vpermq, vpermpd, vpermd, vpermps, vpblendd (128/256), vpblendw, vpblendvb, vpsllvd/q, vpsrlvd/q, vpsravd, vpmovsx* / vpmovzx*, vpmovmskb, and vpmaskmovd / vpmaskmovq (reading/writing only enabled mask < 0 lanes so disabled lanes crossing into unmapped/PAGE_NOACCESS pages never fault).
  • Tests (runtime/test/avx2_emulation_test.cpp):
    • IEEE-754 half/float edge cases, 2-byte/3-byte VEX decoding + AVX1 non-interception, InitializeContext(..., CONTEXT_XSTATE) upper-lane and vpmaskmovd page-boundary tests, and live 256-bit YMM execution (vmovups/vmovdqu -> #UD FMA3 / F16C / AVX2 -> vmovups/vmovdqu -> vzeroupper) on hardware.

Validation

  • Built and executed ctest 3x (avx2_emulation, bmi_emulation, cpu_emulation, clzero_patch, run_tiny_game) on an Intel Core i7-2600 (Sandy Bridge, AVX1 + SSE4.2, AVX2=0 FMA=0 F16C=0), verifying live 256-bit YMM round-trips through Windows XSTATE_AVX on real pre-Haswell hardware.
  • python -I -m unittest discover -s tests -p "test_ctest_registration.py" and test_text_write_line_endings.py (OK).
  • python -I scripts/check_no_game_data.py (clean).

Depends on: #2190
Claim: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7181466437
AI-assisted: yes (Antigravity, Gemini 4 Carbon)


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/2191 **Author:** [@robot00f](https://github.com/robot00f) **Created:** 11/26/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `feat/rt-cpu-fma-f16c-avx2` --- ### 📝 Commits (2) - [`564facb`](https://github.com/ubiquitarianism/leonida/commit/564facb61258c30c7cf764940ce8d138a91dab75) runtime(cpu): emulate BMI1, BMI2, MOVBE and patch LZCNT/TZCNT on AVX1 hosts - [`9ec6b20`](https://github.com/ubiquitarianism/leonida/commit/9ec6b209af7853f6f1665a76a37c56b610d73a45) runtime(cpu): emulate F16C, FMA3 and AVX2 via XSTATE_AVX on AVX1 hosts ### 📊 Changes **7 files changed** (+2822 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `runtime/CMakeLists.txt` (+18 -0) ➕ `runtime/src/avx2_emulation.h` (+1063 -0) ➕ `runtime/src/bmi_emulation.h` (+1148 -0) 📝 `runtime/src/main.cpp` (+15 -0) 📝 `runtime/src/pe_loader.cpp` (+27 -0) ➕ `runtime/test/avx2_emulation_test.cpp` (+235 -0) ➕ `runtime/test/bmi_emulation_test.cpp` (+316 -0) </details> ### 📄 Description ## Summary Add `runtime/src/avx2_emulation.h` and wire it into `gta6rt` so pre-Haswell `AVX1 + SSE4.2` hosts (Intel Sandy Bridge and Ivy Bridge) can execute the game's `F16C`, `FMA3`, and `AVX2` vector instructions using the OS-saved 256-bit `YMM0..YMM15` registers (`docs/cpu_features.md`, PR #2188): - **Full 256-bit `XSTATE_AVX` register access:** - Reads and writes `XMM0..XMM15` (`[127:0]`) from `CONTEXT::FltSave.XmmRegisters` and `YMM0..YMM15[255:128]` via `GetXStateFeaturesMask` / `LocateXStateFeature(c, XSTATE_AVX, &len)` / `SetXStateFeaturesMask`. - Enforces the x86-64 VEX upper-128 zeroing rule (`YMM[dst][255:128] = 0`) on every 128-bit VEX register write, and zero-initializes the other 15 upper lanes before enabling `XSTATE_MASK_AVX` when transitioning out of a clean (`vzeroupper`) state. - Leaves native `AVX1` instructions (including 256-bit `vptest`, `vperm2f128`, `vpermilps`, `vmovups`/`vmovdqu`, etc.) to execute directly on hardware. - **Instruction coverage (`#UD` / `STATUS_ILLEGAL_INSTRUCTION`):** - **F16C:** `vcvtph2ps` and `vcvtps2ph` (register and memory destinations, `imm8` `RN`/`RD`/`RU`/`RZ` and `MXCSR.RC` rounding, subnormals, signed zero, `Inf`, and quiet `NaN` payloads). - **FMA3:** all 60 `0F38` forms (`132`, `213`, `231`; single and double precision; 128-bit and 256-bit packed, scalar `ss`/`sd` preserving `dst[127:32]`/`[127:64]`, and `vfmaddsub*`/`vfmsubadd*`) via single-rounding `std::fmaf` / `std::fma`. - **AVX2:** 256-bit integer SIMD promotions (2-byte `C5` and 3-byte `C4` VEX), `vbroadcastss` / `vbroadcastsd` (including `ModRM.mod == 3` register-source forms), `vbroadcasti128`, `vpbroadcastb/w/d/q`, `vinserti128`, `vextracti128`, `vperm2i128`, `vpermq`, `vpermpd`, `vpermd`, `vpermps`, `vpblendd` (128/256), `vpblendw`, `vpblendvb`, `vpsllvd/q`, `vpsrlvd/q`, `vpsravd`, `vpmovsx*` / `vpmovzx*`, `vpmovmskb`, and `vpmaskmovd` / `vpmaskmovq` (reading/writing only enabled `mask < 0` lanes so disabled lanes crossing into unmapped/`PAGE_NOACCESS` pages never fault). - **Tests (`runtime/test/avx2_emulation_test.cpp`):** - IEEE-754 half/float edge cases, 2-byte/3-byte VEX decoding + `AVX1` non-interception, `InitializeContext(..., CONTEXT_XSTATE)` upper-lane and `vpmaskmovd` page-boundary tests, and live 256-bit `YMM` execution (`vmovups`/`vmovdqu` -> `#UD` `FMA3` / `F16C` / `AVX2` -> `vmovups`/`vmovdqu` -> `vzeroupper`) on hardware. ## Validation - Built and executed `ctest` 3x (`avx2_emulation`, `bmi_emulation`, `cpu_emulation`, `clzero_patch`, `run_tiny_game`) on an **Intel Core i7-2600** (Sandy Bridge, `AVX1 + SSE4.2`, `AVX2=0 FMA=0 F16C=0`), verifying live 256-bit `YMM` round-trips through Windows `XSTATE_AVX` on real pre-Haswell hardware. - `python -I -m unittest discover -s tests -p "test_ctest_registration.py"` and `test_text_write_line_endings.py` (OK). - `python -I scripts/check_no_game_data.py` (clean). Depends on: #2190 Claim: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7181466437 AI-assisted: yes (Antigravity, Gemini 4 Carbon) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#2195
No description provided.