← All reports

Inline double modulo by power-of-two constants in DFG and Baseline

Component: JSC JIT | 98571be

Source/JavaScriptCore/runtime/MathCommon.h

+inline std::optional<double> powerOfTwoDivisorForDoubleMod(double constant)
+{
+ double divisor = std::abs(constant);
+ if (!std::isfinite(divisor) || divisor < 1)
+ return std::nullopt;
+ int exponent;
+ if (std::frexp(divisor, &exponent) != 0.5)
+ return std::nullopt;
+ return divisor;
+}

Source/JavaScriptCore/jit/AssemblyHelpers.cpp

+ move64ToDouble(TrustedImm64(std::bit_cast<int64_t>(1 / divisor)), scratchFPR);
+ mulDouble(dividendFPR, scratchFPR, resultFPR);
+ roundTowardZeroDouble(resultFPR, resultFPR);
+ move64ToDouble(TrustedImm64(std::bit_cast<int64_t>(divisor)), scratchFPR);
+ mulDouble(resultFPR, scratchFPR, resultFPR);
+ subDouble(dividendFPR, resultFPR, resultFPR);

JS % on doubles follows C fmod: the result takes the dividend's sign, a zero result from a negative dividend is -0, and NaN or Infinity dividends yield NaN. For a power-of-two divisor, x - trunc(x * (1/C)) * C is exact because scaling by a power of two loses no bits. However, that sequence yields +0 where fmod yields -0, so the dividend's sign bit has to be ORed back in. DFG and Baseline emit machine code through AssemblyHelpers/MacroAssembler, while FTL goes through B3 IR, so each tier carries its own copy of these semantics.

When C is a constant ±2^k with |C| ≥ 1 and the target supports FP rounding, three tiers now inline the operation. B3 gets lowerDoubleModByPowerOfTwo, DFG gets compileArithModDoubleByPowerOfTwo (calling emitDoubleModByPowerOfTwo), and Baseline gets emitModByPowerOfTwoConstant. Baseline calls only the IgnoringSignOfZero variant and restores the sign with and64/or64. It also adds an int32 bias-and-mask fast path and re-boxes integral results as int32 to keep int32 predictions at use sites. Every other case keeps the existing path.

Double x % 2^k now compiles to a handful of FP ops instead of the generic fmod path in B3, DFG and Baseline. The cost is that these hand-written sequences must match fmod bit for bit, including -0, in three separate implementations.

A wrong result here is a miscompilation that later range and type reasoning may trust. Check sign restoration in each tier: Baseline uses and64/or64 on unboxed bits, while DFG and B3 work in FP or integer-cast form. In Baseline's int32 path, exercise the bias-and-mask sequence and the xor32 + moveConditionallyTest32 -0 detection with mask == 0 (divisor 1), the mask == 1 urshift32 special case, divisor 2^31 (mask INT32_MAX), divisors above 2^31, and INT32_MIN dividends. The int32 re-box check must reject -0, NaN, and out-of-range values, and sub64(regT3, numberTagRegister, regT0) must produce the correct double box otherwise. testb3_2.cpp now accepts any NaN, so check that a non-canonical NaN cannot reach NaN-boxing or purification-sensitive consumers. Confirm powerOfTwoDivisorForDoubleMod's exactness for 2^1023 (where 1/divisor is subnormal) and for dividends near 2^52/2^53. The DFG divisor comes from m_state.forNode(child2).value(), so abstract-interpreter soundness is load-bearing, and noOverlap is only an ASSERT. The most efficient approach is a differential fuzz of interpreter vs Baseline vs DFG vs FTL over the new stress test's edge-case dividends, with int32 and double operands and negative divisors. The same differential approach applies to any future constant-specialized arithmetic lowering.