mirror of
https://github.com/ubiquitarianism/leonida.git
synced 2026-11-26 23:30:53 +01:00
[PR #2191] runtime(cpu): emulate F16C, FMA3 and AVX2 via XSTATE_AVX on AVX1 hosts #2195
Labels
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
starred/leonida#2195
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 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)
564facbruntime(cpu): emulate BMI1, BMI2, MOVBE and patch LZCNT/TZCNT on AVX1 hosts9ec6b20runtime(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.hand wire it intogta6rtso pre-HaswellAVX1 + SSE4.2hosts (Intel Sandy Bridge and Ivy Bridge) can execute the game'sF16C,FMA3, andAVX2vector instructions using the OS-saved 256-bitYMM0..YMM15registers (docs/cpu_features.md, PR #2188):XSTATE_AVXregister access:XMM0..XMM15([127:0]) fromCONTEXT::FltSave.XmmRegistersandYMM0..YMM15[255:128]viaGetXStateFeaturesMask/LocateXStateFeature(c, XSTATE_AVX, &len)/SetXStateFeaturesMask.YMM[dst][255:128] = 0) on every 128-bit VEX register write, and zero-initializes the other 15 upper lanes before enablingXSTATE_MASK_AVXwhen transitioning out of a clean (vzeroupper) state.AVX1instructions (including 256-bitvptest,vperm2f128,vpermilps,vmovups/vmovdqu, etc.) to execute directly on hardware.#UD/STATUS_ILLEGAL_INSTRUCTION):vcvtph2psandvcvtps2ph(register and memory destinations,imm8RN/RD/RU/RZandMXCSR.RCrounding, subnormals, signed zero,Inf, and quietNaNpayloads).0F38forms (132,213,231; single and double precision; 128-bit and 256-bit packed, scalarss/sdpreservingdst[127:32]/[127:64], andvfmaddsub*/vfmsubadd*) via single-roundingstd::fmaf/std::fma.C5and 3-byteC4VEX),vbroadcastss/vbroadcastsd(includingModRM.mod == 3register-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, andvpmaskmovd/vpmaskmovq(reading/writing only enabledmask < 0lanes so disabled lanes crossing into unmapped/PAGE_NOACCESSpages never fault).runtime/test/avx2_emulation_test.cpp):AVX1non-interception,InitializeContext(..., CONTEXT_XSTATE)upper-lane andvpmaskmovdpage-boundary tests, and live 256-bitYMMexecution (vmovups/vmovdqu->#UDFMA3/F16C/AVX2->vmovups/vmovdqu->vzeroupper) on hardware.Validation
ctest3x (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-bitYMMround-trips through WindowsXSTATE_AVXon real pre-Haswell hardware.python -I -m unittest discover -s tests -p "test_ctest_registration.py"andtest_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.