Skip to content

Round float multiplication lower bounds downward - #275

Merged
zayenz merged 2 commits into
mainfrom
feature/fix-float-nroot-parallel
Nov 14, 2026
Merged

zayenz merged 2 commits into
mainfrom
feature/fix-float-nroot-parallel

Conversation

@zayenz

@zayenz zayenz commented Nov 12, 2026

Copy link
Copy Markdown
Member

Problem

Parallel BAB on examples/cartesian-heart (-threads 2/4/8) aborts with

std::runtime_error: gecode_boost::interval: empty interval created

thrown from nth_root in NthRoot::propagate. On main it aborted 9 of 9 runs on an Apple Silicon Release build. -threads 1 always works.

Root cause

Boost's rounded_arith_opp keeps the FPU in upward mode and computes downward-rounded results by negation, for example mul_down(x,y) = -(x * -y). Under the default floating-point environment, clang on AArch64 compiles x * -y to fnmul, which rounds the product and then negates it. As a result, mul_down returned the upward-rounded product: Rounding::mul_down was wrong for 100% of random inexact products, at -O0 and -O2. The installed libgecodefloat contained 20 fnmul instructions.

How this produces the crash: parallel search reaches nroot on the source domain [2^-1016, 2^-1015], which sequential search prunes first. Boost's root_aux squares a Newton median near 2^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, and checking_strict throws.

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_down and div_down now pass the negated operand through negate_opaque, so the compiler cannot fuse the negation into the rounded operation. On AArch64 with GCC or clang, negate_opaque is an empty asm register barrier. Elsewhere it is boost's existing volatile force_rounding. Only mul_down was miscompiled here, but add_down and div_down rely on the same trick.

The first version routed the operand through volatile force_rounding everywhere. That cost 12.5% on descartes-folium (its search tree is unchanged, so this was pure overhead). The asm barrier brings it down to about +1%:

example (-threads 1) main volatile asm barrier
descartes-folium 1603 ms 1803 ms 1624 ms
cartesian-heart 362 ms 369 ms 363 ms
golden-spiral 157 ms 158 ms 161 ms

Test

Float::Arithmetic::DirectedRounding (check tag), next to RoundingScope, checks:

  • that mul_down/div_down/add_down and their _up counterparts give the correctly directed results for operands whose exact result lies between two doubles;
  • that nroot on [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/8 complete. -threads 1 still 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 --check passes.

Not checked: whether GCC on AArch64 or x86_64 also folds these negations. The volatile fallback covers non-AArch64 targets either way.

@zayenz
zayenz marked this pull request as ready for review November 12, 2026 08:36
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.
@zayenz
zayenz force-pushed the feature/fix-float-nroot-parallel branch from c569f66 to 32addb4 Compare November 13, 2026 05:50
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%.
@fkonvick

Copy link
Copy Markdown
Collaborator

I’ve extended this fix to MSVC and x86-64.

MSVC. Gecode has used rounded_arith_std on MSVC since 2016, switching the rounding mode with _control87 on every operation. That was the workaround for the users-list thread “Freeze with MSVC 2015 optimized build” (2015-2016). The root cause turns out to be the same as fnmul here: VS 2015 (19.00) and VS 2017 (19.16) compile x * (-y) and x / (-y) as mulsd/divsd followed by xorps, negating after rounding. VS 2019 (19.29) and later negate first. With negate_opaque, keep-upward is correct on all of them, so MSVC now uses rounded_arith_opp too and keeps the volatile round trip as its barrier.

VS 2026 x64: all Float tests pass, and cartesian-heart -threads 4 finishes.

  • Gecode 4.4.0 built with VS 2015 still hangs on the 2015 model, and solves it in 55 nodes once the volatile negation is added. This confirms that the opaque negation addresses the root cause, so the per-operation rounding is no longer needed.

  • Runtime: 4x improvement on descartes-folium, 2x improvement on cartesian-heart.

GCC/clang on x86-64. These now use an empty asm "+x" barrier, matching "+w" on AArch64. GCC 14.2 doesn’t fold at -O0 to -O3, so the barrier is only insurance there. Its runtime cost with GCC 14.2 at -O3, compared to a plain negation:

  • descartes-folium: +2.6% with the asm barrier, +5.5% with the volatile.

  • cartesian-heart: −0.8% with the asm barrier, +2.1% with the volatile.

This feels like an acceptable cost for the safety.

@zayenz zayenz changed the title Round float multiplication lower bounds downward on AArch64 Round float multiplication lower bounds downward Nov 13, 2026
@zayenz
zayenz merged commit 609a9f4 into main Nov 14, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants