From bf8d8c9a7a388264304075a3e184a253de12d09f Mon Sep 17 00:00:00 2001 From: generatedunixname499836121 Date: Mon, 19 Oct 2026 06:39:46 -0700 Subject: [PATCH] Avoid fmod when rounding layout results to the pixel grid (#2041) Summary: copysign(x - trunc(x), x) is bit-identical to fmod(x, 1.0) for every double but avoids the libm fmod loop, which is slow on Android's bionic. roundValueToPixelGridDouble runs six times per node on every layout pass. Changelog: [Internal] Reviewed By: cortinico Differential Revision: D125600548 fbshipit-source-id: 7866bfb8619f7a6ec63bf799821a8c2270340a57 Co-authored-by: Claude Opus 5.5 --- .../ReactCommon/yoga/yoga/algorithm/PixelGrid.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/react-native/ReactCommon/yoga/yoga/algorithm/PixelGrid.cpp b/packages/react-native/ReactCommon/yoga/yoga/algorithm/PixelGrid.cpp index 529dedf4ddb..73f74821437 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/algorithm/PixelGrid.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/algorithm/PixelGrid.cpp @@ -10,6 +10,8 @@ #include #include +#include + namespace facebook::yoga { namespace { @@ -23,7 +25,13 @@ double roundValueToPixelGridDouble( // We want to calculate `fractional` such that `floor(scaledValue) = // scaledValue // - fractional`. - double fractional = fmod(scaledValue, 1.0); + // + // `copysign(x - trunc(x), x)` is bit-identical to `fmod(x, 1.0)` for every + // double (including -0.0, infinities and NaN) but avoids the libm `fmod` + // loop, which is slow on some platforms (e.g. Android's bionic). This + // function runs six times per node on every layout pass. + double fractional = + std::copysign(scaledValue - std::trunc(scaledValue), scaledValue); if (fractional < 0) { // This branch is for handling negative numbers for `value`. //