← Back to Pull requests

Preserve the sign of a zero in the emulated Round, Ceil, Floor and Trunc #3447

Open
unknown opened this pull request with 467 lines diff across 5 files
View on GitHub
unknown unknown commented on Oct 02, 2026 14:10 UTC
Author

Fixes #3429, which reported the bug with a reproduction and a suggested patch (thanks @rcfa).

What was wrong

On the targets that emulate float rounding, Round, Ceil, Floor and Trunc could return a zero with the wrong sign. The magnitudes were already right; only zeros were affected.

| Target | Wrong |
|---|---|
| SSE2/SSSE3 | Round(v) for v in [-0.5, -0], Ceil(v) for v in (-1, -0], Floor(-0) |
| EMU128 | Round(-0), Round(-0.5), Trunc(-0), Floor(-0), and Ceil(+0) returning -0 |
| SCALAR | Round(-0.5), Floor(-0), and Ceil(+0) returning -0 |

SSE4, AVX2 and NEON are unaffected, and C's rint/ceil/floor/trunc return -0 for these inputs.

Why, per target

The causes are not the same, so the fixes are not either:

  • x86 (x86_128-inl.h): the results are computed arithmetically, and large + v - large, int_f - neg1 and int_f + neg1 are +0 for the affected inputs. These now apply CopySign, which Trunc on this path already did.

  • emu128-inl.h / scalar-inl.h: the zero cases test v < 0, which is false for -0. Separately, emu128's Round wrote ConvertScalarTo<T>(-0), where -0 is an integer literal and therefore +0, and Ceil wrote -0 for every input that is not > 0, which includes +0. These sites now use the sign bit, and leave a zero input untouched since it already carries the correct sign.
  • This is a little more surgical than the blanket CopySign(result, orig) suggested in the issue, which would also run on the huge/NaN lanes that are returned unchanged.

    Why the tests missed it

    Both halves of the comparison are blind to it: the -0 in RoundTestCases is ConvertScalarTo<T>(-0) — the integer 0, so +0 — and ComputeUlpDelta returns 0 whenever expected == actual, which is true for -0 vs +0. TestAllRoundingSignOfZero checks the sign bit of the results that are zero instead, and prints which op and input differed.

    Testing

    g++ 13.3, Linux x86-64. EMU128 was reached with -DHWY_BROKEN_EMU128=0 (GCC < 16 otherwise selects SCALAR).

    The new test aborts on unmodified hwy/ops and passes with the fix:

    | Target | Test on master | Test with fix |
    |---|---|---|
    | SCALAR | Ceil(0) returned -zero, expected +zero | pass |
    | EMU128 | Ceil(0) returned -zero, expected +zero | pass |
    | SSE2/SSSE3 | Floor(-0) returned +zero, expected -zero | pass |

    The rest of float_test passes on SCALAR, EMU128, SSE2 and SSE4.

    I also compared all four ops bit for bit against C over 1,049,617 inputs per type (special values including both zeros, infinities, denormals and the ties, a [-8, 8] grid in steps of 1/64, and 2^20 random bit patterns):

    | Target | Mismatches before | After |
    |---|---|---|
    | SSE2 | 517,292 (f32) · 523,772 (f64) | 0 |
    | SSSE3 | 517,292 · 523,772 | 0 |
    | EMU128 | 257,606 · 261,739 | 0 |
    | SCALAR | 6 · 6 | 0 |
    | SSE4 (native, reference) | 0 · 0 | 0 |

    Every mismatch was a zero of the wrong sign.

    Armv7 and RVV

    Added in 743f0e4, as requested:

  • arm_neon-inl.h, the Armv7 fallbacks: same shape as x86, so Round, Ceil and Floor now apply CopySign. Trunc is affected here too (it did not apply CopySign, unlike the x86 one), so it is fixed as well. The AArch64 path uses the native vrnd* instructions and is untouched.

  • rvv-inl.h: Round, Trunc and the Ceil/Floor that use the native rounding modes already apply CopySign; only the Ceil/Floor fallbacks for GCC < 14 and Clang < 17 did not.
  • I have no Armv7 or RVV toolchain locally, so those two rest on CI; TestAllRoundingSignOfZero is what exercises them.

    With these in, the CopySign workaround in Modf becomes redundant, but I have left it alone — happy to remove it in a follow-up if you want.

    Diff: 467 lines (+350 / -117) across 5 files Reviewer: jan-wassenberg Review duration: 7935m
    All Vouch review confidence checks passed
    Residual risk 0.24 = Change Risk (0.46) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.46 · 467 lines diff across 5 files
    0.46
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.368
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #3447"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 45.6% (MEDIUM) · Higher = larger blast radius
    45.6%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 24.3% · Safe to merge
    24.3%
    Repository