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 <noreply@anthropic.com>
This commit is contained in:
generatedunixname499836121andClaude Opus 5.5 authored and meta-codesync[bot] committed 2026-10-19 06:39:46 -07:00
1 parent fd5392494f
commit bf8d8c9a7a
1 file changed
+9 -1
@@ -10,6 +10,8 @@
#include <yoga/algorithm/PixelGrid.h>
#include <yoga/numeric/Comparison.h>
#include <cmath>
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`.
//