Preserve the sign of a zero in the emulated Round, Ceil, Floor and Trunc #3447
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
jan-wassenberg
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,FloorandTrunccould 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), andCeil(+0)returning -0 || SCALAR |
Round(-0.5),Floor(-0), andCeil(+0)returning -0 |SSE4, AVX2 and NEON are unaffected, and C's
rint/ceil/floor/truncreturn -0 for these inputs.Why, per target
The causes are not the same, so the fixes are not either:
x86_128-inl.h): the results are computed arithmetically, andlarge + v - large,int_f - neg1andint_f + neg1are +0 for the affected inputs. These now applyCopySign, whichTruncon this path already did.emu128-inl.h/scalar-inl.h: the zero cases testv < 0, which is false for -0. Separately, emu128'sRoundwroteConvertScalarTo<T>(-0), where-0is an integer literal and therefore +0, andCeilwrote -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
-0inRoundTestCasesisConvertScalarTo<T>(-0)— the integer0, so +0 — andComputeUlpDeltareturns 0 wheneverexpected == actual, which is true for -0 vs +0.TestAllRoundingSignOfZerochecks 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/opsand 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_testpasses 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, soRound,CeilandFloornow applyCopySign.Truncis affected here too (it did not applyCopySign, unlike the x86 one), so it is fixed as well. The AArch64 path uses the nativevrnd*instructions and is untouched.rvv-inl.h:Round,Truncand theCeil/Floorthat use the native rounding modes already applyCopySign; only theCeil/Floorfallbacks for GCC < 14 and Clang < 17 did not.I have no Armv7 or RVV toolchain locally, so those two rest on CI;
TestAllRoundingSignOfZerois what exercises them.With these in, the
CopySignworkaround inModfbecomes redundant, but I have left it alone — happy to remove it in a follow-up if you want.