[JSC] Implement Float16 ops in x64 via F16C
Component: JSC x64 JIT | 60af4b3
Source/JavaScriptCore/assembler/MacroAssemblerX86_64.h
Source/JavaScriptCore/assembler/MacroAssemblerX86_64.cpp
Float16Array and DataView.{get,set}Float16 store IEEE-754 binary16 values, but JS arithmetic runs in double, so every access needs a double/half conversion with correct round-to-nearest-even, overflow to Infinity at 65520, and subnormal handling. Widening a half NaN preserves sign and payload, so the result can land in the impure NaN range and must be purified before boxing. JSC emits these accesses through Baseline/DFG/FTL as inline code or through by-val inline caches, runtime-generated stubs that convert elements through the macro assembler's FP temp register. F16C is an optional x86 extension, so the new path is selected by a CPU-feature probe.
The commit adds X86Assembler encodings (VCVTPH2PS, VCVTPS2PH, memory-operand VPINSRW/VPEXTRW, SBB, and a three-byte VEX helper) and implements the previously UNREACHABLE MacroAssemblerX86_64 helpers convertDoubleToFloat16, convertFloat16ToDouble, loadFloat16, storeFloat16, move16ToFloat16 and moveFloat16To16, gated by a new s_f16cCheckState. FTL lowering for PutByVal, EnumeratorPutByVal and DataViewSet, and the FTL::Output helpers storeDoubleAsFloat16 and f16round, now use the x64 path. Because double to float to half rounds twice, the store sequence detects a possible half midpoint in the low 12 float bits and, when the first rounding was inexact, nudges the float with round-to-odd (sbb/or 1) so the final vcvtps2ph matches a direct double-to-half rounding.
Significance
Float16Array stores and loads, DataView get/setFloat16 and Math.f16round now compile to native instructions on x64 instead of calling software conversion. The cost is a new hardware-feature-dependent codegen path in Baseline and FTL, and a new FTL test checks that no live FP value sits in fpTempRegister across by-val ICs, which clobber it.
Audit directions
- FP temp register clobbering. The by-val ICs and
convertDoubleToFloat16both convert throughfpTempRegister, so FTL must never keep a live value there across them (ftl-by-val-ic-preserves-fp-temp-register.jsexercises this under high FP pressure and randomized allocation). Check every other path that emits these sequences (Baseline, DFG, FTL put-by-val, enumerator put-by-val,DataViewSet,getByValWithThis); a clobbered register can turn into a wrong value being written or boxed. The appendix entryc57b5cebelow is the same register-clobber class onGetByVal. - Encoding correctness. Review
vexNdsLigThreeByteOp,vcvtps2ph_i8rr, the memory forms ofvpinsrw/vpextrw, andsbbl_irfor operand order, VEX.L/pp/mmmmm bits, REX-extended registers (xmm8+/r8+) and addressing modes; a bad encoding on high registers misbehaves only under certain allocations. - Feature gating.
s_f16cCheckStaterequires AVX and F16C; under Rosetta the CPUID bit test is bypassed because Rosetta translates F16C without advertising it. Confirm every caller checkssupportsFloat16, and that hypervisor masking (AVX masked while F16C or AVX2 still reported) cannot enable the path on hardware lacking it. - Semantic divergence across tiers. Differentially compare JIT and LLInt results for NaN payloads, widened half NaNs in the impure range (which must be purified before boxing), ties, and values near 65520, plus address computation on the 16-bit load/store and DataView byte-swapping combined with
move16ToFloat16/moveFloat16To16.