Repository navigation
Round float multiplication lower bounds downward - #275
Conversation
Boost's rounded_arith_opp keeps the FPU in upward mode and computes a downward-rounded product as -(x * -y). Under the default floating-point environment, clang on AArch64 selects fnmul for x * -y. fnmul rounds the product and then negates it, so mul_down returned the upward-rounded product and float lower bounds could exclude the exact result. This made parallel BAB on examples/cartesian-heart abort with "empty interval created". Its search reaches nroot on [2^-1016, 2^-1015]. Boost's root Newton iteration squares a median near 2^1015 there. The correct enclosure [DBL_MAX, inf] keeps the iteration sound, but the misrounded [inf, inf] turns the Newton step into [-inf, -inf]. The intersection is then empty and checking_strict throws. Sequential search never reaches that domain. The same defect made nroot([2,2],2) return a point interval that excludes sqrt(2). Pass the negated operand of add_down, mul_down and div_down through an opaque negation so that the compiler cannot fuse it into the rounded operation. On AArch64 with GCC or clang this is an empty asm register barrier. Elsewhere it is boost's volatile force_rounding. Only mul_down was miscompiled here, but add_down and div_down rely on the same negation trick. The rounding mode is set correctly in every thread. The parallel-only symptom comes from the search order, not from per-thread rounding state.
c569f66 to
32addb4
Compare
MSVC on x64, and on x86 with SSE, used rounded_arith_std, which switches the rounding mode with _control87 for every single operation. That was the fix for the 2015 users-list thread "Freeze with MSVC 2015 optimized build": with rounded_arith_opp, Float::Arithmetic::Div::Sol::A failed and a small layout model never left propagation, ignoring the search time limit. The cause was the same fold as fnmul on AArch64. VS 2015 (MSVC 19.00) and VS 2017 (19.16) compile x * (-y) and x / (-y) as mulsd or divsd followed by xorps, so the negation comes after the rounding and mul_down and div_down return the upward-rounded result. VS 2019 (19.29) and later negate first. With negate_opaque from the previous commit, keep-upward is correct on all of them, so MSVC now uses rounded_arith_opp like every other compiler. Measured with VS 2026 (19.51) x64: - All Float tests pass, including DirectedRounding and the Div tests. - descartes-folium: runtime -75%. - cartesian-heart: runtime -49%; -threads 4 finishes. - Gecode 4.4.0 built with VS 2015 hangs on the 2015 model; the same libraries with the volatile negation solve it in 55 nodes. MSVC has no inline asm on x64, so it keeps the volatile round trip. GCC and clang on x86-64 now use an empty asm "+x" barrier instead, as on AArch64. GCC 14.2 does not fold at -O0 to -O3, so this is insurance there. In isolation the barrier compiles to the same instructions as a plain negation. In whole programs, with GCC 14.2 -O3, compared to a plain negation: - descartes-folium: volatile +5.5%, asm +2.6%. - cartesian-heart: volatile +2.1%, asm -0.8%.
|
I’ve extended this fix to MSVC and x86-64. MSVC. Gecode has used VS 2026 x64: all Float tests pass, and cartesian-heart
GCC/clang on x86-64. These now use an empty asm
This feels like an acceptable cost for the safety. |
Problem
Parallel BAB on
examples/cartesian-heart(-threads 2/4/8) aborts withthrown from
nth_rootinNthRoot::propagate. Onmainit aborted 9 of 9 runs on an Apple Silicon Release build.-threads 1always works.Root cause
Boost's
rounded_arith_oppkeeps the FPU in upward mode and computes downward-rounded results by negation, for examplemul_down(x,y) = -(x * -y). Under the default floating-point environment, clang on AArch64 compilesx * -ytofnmul, which rounds the product and then negates it. As a result,mul_downreturned the upward-rounded product:Rounding::mul_downwas wrong for 100% of random inexact products, at-O0and-O2. The installedlibgecodefloatcontained 20fnmulinstructions.How this produces the crash: parallel search reaches
nrooton the source domain[2^-1016, 2^-1015], which sequential search prunes first. Boost'sroot_auxsquares a Newton median near2^1015. With correct rounding the enclosure is[DBL_MAX, inf]and the iteration stays sound. With the misrounded[inf, inf], the Newton step is[-inf, -inf], the intersection is empty, andchecking_strictthrows.The same defect is a general soundness bug. For example,
nroot([2,2],2)returned the point interval[√2↓, √2↓], which excludes √2.The rounding mode itself is set correctly in every worker thread. The parallel-only symptom comes from the search order.
Fix
add_down,mul_downanddiv_downnow pass the negated operand throughnegate_opaque, so the compiler cannot fuse the negation into the rounded operation. On AArch64 with GCC or clang,negate_opaqueis an empty asm register barrier. Elsewhere it is boost's existing volatileforce_rounding. Onlymul_downwas miscompiled here, butadd_downanddiv_downrely on the same trick.The first version routed the operand through volatile
force_roundingeverywhere. That cost 12.5% ondescartes-folium(its search tree is unchanged, so this was pure overhead). The asm barrier brings it down to about +1%:-threads 1)Test
Float::Arithmetic::DirectedRounding(check tag), next toRoundingScope, checks:mul_down/div_down/add_downand their_upcounterparts give the correctly directed results for operands whose exact result lies between two doubles;nrooton[2^-1016, 2^-1015]keeps the root.Without the fix, the test aborts with the reported exception.
Validation (Apple Silicon, CMake Release)
cartesian-heart: 20 of 20 runs across-threads 1/2/4/8complete.-threads 1still gives 572 solutions.gecode-test -test Float -tag standard: 526 of 526 passed.check: 537 of 537 passed.gecode-test -tag standard(check-standard): 6757 of 6757 passed.uv run misc/tidy.py --checkpasses.Not checked: whether GCC on AArch64 or x86_64 also folds these negations. The volatile fallback covers non-AArch64 targets either way.