← All reports

[JSC] Implement Float16 ops in x64 via F16C

Component: JSC x64 JIT | 60af4b3

Source/JavaScriptCore/assembler/MacroAssemblerX86_64.h

+ m_assembler.vcvtsd2ss_rrr(src, src, fpTempRegister);
+ m_assembler.vmovd_rr(fpTempRegister, scratchRegister());
+ Jump notMidpoint = branchTest32(NonZero, scratchRegister(), TrustedImm32(0xfff));
...
+ m_assembler.vucomisd_rr(fpTempRegister, dest);
+ Jump exactOrNaN = Jump(m_assembler.je());
+ m_assembler.sbbl_ir(0, scratchRegister());
+ m_assembler.orl_ir(1, scratchRegister());
+ exactOrNaN.link(this);
+ m_assembler.vmovd_rr(scratchRegister(), fpTempRegister);
+ notMidpoint.link(this);
+ m_assembler.vcvtps2ph_i8rr(X86Assembler::RoundingType::ToNearestWithTiesToEven, fpTempRegister, dest);

Source/JavaScriptCore/assembler/MacroAssemblerX86_64.cpp

+ bool hasF16C = isTranslated || (cpuid[2] & (1 << 29));
+ s_f16cCheckState = (s_avxCheckState == CPUIDCheckState::Set && hasF16C) ? CPUIDCheckState::Set : CPUIDCheckState::Clear;

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.

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.