runtime(cpu): emulate F16C, FMA3 and AVX2 via XSTATE_AVX on AVX1 hosts #2191
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2191"
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?
Imported from ubiquitarianism/leonida. Originally posted by robot00f on 2026-11-26T04:10:28Z.
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)
Imported from ubiquitarianism/leonida. Originally posted by ecox982745 on 2026-11-26T04:17:00Z.
I found one F16C result mismatch at
9ec6b209af7853f6f1665a76a37c56b610d73a45. TheVCVTPS2PHpath reads the saved rounding mode but does not honor the saved MXCSR denormals-are-zero (DAZ) bit on its binary32 inputs. On a Windows Ryzen 7 7800X3D, with DAZ set and rounding toward negative infinity, native F16C converts input bits0x80000001to half0x8000(negative zero). The PR returns0x8001. The same difference occurs with the largest negative binary32 subnormal,0x807fffff.I compared native
_mm_cvtps_phwithdecode+applyfor the 128-bit register form, using 12 synthetic inputs, all four rounding modes, both immediate and MXCSR-selected rounding, and both FTZ settings. With all FP exceptions masked, the PR matched native on 192/192 lanes with DAZ clear and 176/192 with DAZ set. This compares result bits only.A local edit to the conversion loop at line 762 matched native on 384/384. When
c->FltSave.MxCsr & (1u << 6)is set and the input exponent bits are zero, writeuint16_t((src.u32[k] >> 16) & 0x8000u)to preserve the sign of zero. Otherwise use the existingf32_to_f16. Keeping this check on the binary32 input preserves valid half-subnormal outputs even with FTZ set. The PR's existingavx2_emulation_testpassed all 38 checks on both versions. The two-case regression I'd add is the smallest negative binary32 subnormal with round-down, DAZ clear (0x8001) and DAZ set (0x8000).CPU-only, synthetic data. AI-assisted review (OpenAI Codex). Intel's specification covers DAZ for legacy VCVTPS2PH.
Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T21:24:30Z.
Independent Verification Report —
b6e51b7376+origin/main(aa918e5d9f)Environment: Windows 11 (
26200), AMD Ryzen 9 5950X (Zen 3 hardware oracle), MSVC19.44.35228(x64,/std:c++20 /EHsc /W4 /permissive- /utf-8, Debug/Od /MDd& Release/O2 /MD).Verdict: Core test suite & PE-loader integration PASS; 2 silicon-verified ISA edge-case mismatches noted below — clean merge with
origin/main(41c27e5d8a7b) and combined Batch 9 set (a422ed80f598).1. Build & Test Suite Verification
origin/main):bmi_emulation.h/avx2_emulation.habsent (C1083).b6e51b7376+origin/main):bmi_emulation_test(Debug & Release): PASS (65/65checks),0warnings under/W4 /permissive-.avx2_emulation_test(Debug & Release): PASS (38/38checks),0warnings under/W4 /permissive-.cpu_emulation_test(Debug & Release): PASS,0warnings.loader_test+gta6rt_tls_host.dll+main.cpp/pe_loader.cpp/W4 /permissive-compile: PASS (0new warnings in touched files;.pdata/leafLZCNT/TZCNTpatcher integration verified).2. Ryzen 9 5950X Hardware Differential Findings on
b6e51b7376VEX.L = 1, whereas x86-64 FMA3 specifiesVEX.LIG(runtime/src/avx2_emulation.h:363):VEX.LIG(VEX.Lignored) for scalar FMA3 (VFMADD*SS/SD,VFMSUB*SS/SD,VFNMADD*SS/SD,VFNMSUB*SS/SD, odd opcodes0x99..0xBF).C4 E2 75 A9 C2(vfmadd213ss xmm0, xmm1, xmm2withVEX.L = 1) on Ryzen 9 5950X executes normally without#UD(2.0f * 3.0f + 5.0f = 11.0f), whereasavx2_emulation::decodehasif (scalar && d.vex_l != 0) return {};, leaving#UDunhandled.if (scalar && d.vex_l != 0) return {};atavx2_emulation.h:363(apply_fma3already branches onscalar = (lo & 1u) != 0and zeroesYMM[dst][255:128]viawrite_xmm).VCVTPS2PHstill ignores savedMXCSR.DAZ(bit 6 = 0x0040) atb6e51b7376(runtime/src/avx2_emulation.h:817):b6e51b7376: withMXCSR = 0x3FC0(DAZ = 1, round-down), native_mm_cvtps_phconverts negative binary32 subnormals0x80000001and0x807FFFFFto0x8000(-0.0h), whereasavx2_emulation::applyreturns0x8001(matching the earlier report in#issuecomment-7182457972). Flushing input binary32 subnormals ((bits & 0x7F800000u) == 0) to signed zero when(c->FltSave.MxCsr & 0x0040u) != 0aligns all 4 lanes ([0x8000, 0x8000, 0x0000, 0x0000]).apply_fma3,avx2_emulation.h:572-670): On Windows 11 (26200), we confirmed via live#UDVEH probe thathandler_live_mxcsrmatchesctx->FltSave.MxCsr(0xBFC0) on VEH entry, capability-wise, thoughstd::fmaf/std::fmain syntheticapply(dec, ctx)calls (and softwarefmafon AVX1 hosts) uses the host CRT environment rather than consultingc->FltSave.MxCsr(RC/DAZ/FTZ).AI-assisted.
Imported from ubiquitarianism/leonida. Originally posted by TheGeogeo on 2026-11-28T22:16:34Z.
Duplicate check (diff reading only, no build): not a duplicate of #2190 but its next step: it contains #2190 (
Depends on: #2190) and adds the F16C, FMA3 and AVX2 emulation. Both #2190 and #2191 are in #2414 (git merge-base --is-ancestor), which declaresDepends on: #2190, #2191.Suggest landing the stack as one: merging #2414 with a merge commit (not a squash) also lands #2190 and #2191, and GitHub then shows them as merged. With a squash merge of #2414, these two stay open and must be closed by hand.
AI-assisted (Claude Code).