From 32addb4d881cc8f06e869200ace4547bc5fcc7a8 Mon Sep 17 00:00:00 2001 From: Mikael Zayenz Lagerkvist Date: Thu, 12 Nov 2026 08:24:43 +0100 Subject: [PATCH 1/2] Round float multiplication lower bounds downward on AArch64 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. --- changelog.in | 12 ++++ .../boost/numeric/interval/rounded_arith.hpp | 24 +++++++- test/float/arithmetic.cpp | 58 +++++++++++++++++++ 3 files changed, 91 insertions(+), 3 deletions(-) diff --git a/changelog.in b/changelog.in index 28b4a63e20..e5fc81db2e 100755 --- a/changelog.in +++ b/changelog.in @@ -120,6 +120,18 @@ assigned. A new solution must now improve by at least the step, without the rounding of the step subtraction admitting a smaller improvement. With step 0, assigned costs are accepted exactly as before. +[ENTRY] +Module: float +What: bug +Rank: major +[DESCRIPTION] +Round the lower bound of float multiplication downward on AArch64. The +compiler fused the negation in the downward-rounded product into an fnmul +instruction, so the lower bound was rounded upward and could exclude the +exact product. Propagators could remove solutions, and nroot could throw an +empty-interval exception. For example, parallel search on the cartesian-heart +example aborted. Addition and division get the same protection. + [RELEASE] Version: 6.5.0 Date: 2026-10-31 diff --git a/gecode/third-party/boost/numeric/interval/rounded_arith.hpp b/gecode/third-party/boost/numeric/interval/rounded_arith.hpp index df20666f1e..40bbdd9b08 100644 --- a/gecode/third-party/boost/numeric/interval/rounded_arith.hpp +++ b/gecode/third-party/boost/numeric/interval/rounded_arith.hpp @@ -92,10 +92,28 @@ struct rounded_arith_opp: Rounding { # define GECODE_BOOST_UP_NEG(EXPR) return -this->force_rounding(EXPR) template T conv_down(U const &v) { GECODE_BOOST_UP_NEG(-v); } template T conv_up (U const &v) { GECODE_BOOST_UP(v); } - T add_down(const T& x, const T& y) { GECODE_BOOST_UP_NEG((-x) - y); } + // Gecode: the negated operand passes through negate_opaque. Otherwise + // the compiler may fuse the negation into the rounded operation, which is + // only valid under round-to-nearest. For example, clang on AArch64 turns + // x * (-y) into fnmul, which negates after rounding, so mul_down returned + // the upward-rounded product. + T negate_opaque(const T& x) { +#if defined(__GNUC__) && defined(__aarch64__) + // An empty asm hides the value without a memory round trip. + T r = -x; + __asm__("" : "+w"(r)); + return r; +#else + return this->force_rounding(-x); +#endif + } + T add_down(const T& x, const T& y) + { GECODE_BOOST_UP_NEG(negate_opaque(x) - y); } T sub_down(const T& x, const T& y) { GECODE_BOOST_UP_NEG(y - x); } - T mul_down(const T& x, const T& y) { GECODE_BOOST_UP_NEG(x * (-y)); } - T div_down(const T& x, const T& y) { GECODE_BOOST_UP_NEG(x / (-y)); } + T mul_down(const T& x, const T& y) + { GECODE_BOOST_UP_NEG(x * negate_opaque(y)); } + T div_down(const T& x, const T& y) + { GECODE_BOOST_UP_NEG(x / negate_opaque(y)); } T add_up (const T& x, const T& y) { GECODE_BOOST_UP(x + y); } T sub_up (const T& x, const T& y) { GECODE_BOOST_UP(x - y); } T mul_up (const T& x, const T& y) { GECODE_BOOST_UP(x * y); } diff --git a/test/float/arithmetic.cpp b/test/float/arithmetic.cpp index 36240ddba5..99ce781deb 100755 --- a/test/float/arithmetic.cpp +++ b/test/float/arithmetic.cpp @@ -119,6 +119,64 @@ namespace Test { namespace Float { } } rounding_scope; + /// Regression test for downward rounding of negating operations + class DirectedRounding : public Base { + protected: + /// Space posting an NthRoot constraint on a positive source + class RootSpace : public Gecode::Space { + public: + /// Source and root variables + Gecode::FloatVar source, root; + /// Post the square root of the source interval [lo,hi] + RootSpace(Gecode::FloatNum lo, Gecode::FloatNum hi) + : source(*this,lo,hi), root(*this,0.0,1.0) { + Gecode::nroot(*this,source,2,root); + } + /// Clone constructor + RootSpace(RootSpace& s) : Gecode::Space(s) { + source.update(*this,s.source); + root.update(*this,s.root); + } + /// Copy space during cloning + virtual Gecode::Space* copy(void) { + return new RootSpace(*this); + } + }; + public: + DirectedRounding(void) + : Base("Float::Arithmetic::DirectedRounding", TestTags::check()) {} + virtual bool run(void) { + bool result = true; + { + // Each exact result lies strictly between two adjacent doubles. + const volatile Gecode::FloatNum root_two = 0x1.6a09e667f3bccp+0; + const volatile Gecode::FloatNum one = 1.0; + const volatile Gecode::FloatNum three = 3.0; + const volatile Gecode::FloatNum quarter_ulp = std::ldexp(1.0,-54); + Gecode::Float::Rounding r; + if ((r.mul_down(root_two,root_two) != 0x1.ffffffffffffep+0) || + (r.mul_up(root_two,root_two) != 0x1.fffffffffffffp+0) || + (r.div_down(one,three) != 0x1.5555555555555p-2) || + (r.div_up(one,three) != 0x1.5555555555556p-2) || + (r.add_down(one,-quarter_ulp) != 0x1.fffffffffffffp-1) || + (r.add_up(one,-quarter_ulp) != 1.0)) { + olog << "Rounding operation was not directed" << std::endl; + result = false; + } + } + // Parallel search on examples/cartesian-heart reached this domain. + // A wrong mul_down made the root iteration create an empty interval. + RootSpace s(0x1p-1016,0x1p-1015); + if ((s.status() == Gecode::SS_FAILED) || + (s.root.min() > 0x1p-508) || + (s.root.max() < 0x1.6a09e667f3bcdp-508)) { + olog << "NthRoot removed the root of " << s.source << std::endl; + result = false; + } + return result; + } + } directed_rounding; + /// Median queries preserve the caller's floating-point rounding mode class MedianRoundingMode : public Base { public: From 2f6867a8d82988f9846d6605cdb02d0f26090037 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Konvi=C4=8Dka?= Date: Fri, 13 Nov 2026 09:10:43 +0100 Subject: [PATCH 2/2] Keep upward rounding between float operations also with MSVC 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%. --- changelog.in | 13 +++++++++++++ gecode/float.hh | 14 +++----------- .../boost/numeric/interval/rounded_arith.hpp | 10 +++++++++- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/changelog.in b/changelog.in index e5fc81db2e..8a4a3bbf84 100755 --- a/changelog.in +++ b/changelog.in @@ -132,6 +132,19 @@ exact product. Propagators could remove solutions, and nroot could throw an empty-interval exception. For example, parallel search on the cartesian-heart example aborted. Addition and division get the same protection. +[ENTRY] +Module: float +What: performance +Rank: minor +[DESCRIPTION] +Keep the upward rounding mode between float operations also with MSVC on +x64 and on x86 with SSE, as with all other compilers. MSVC switched the +rounding mode for every single operation, because MSVC 2015 and 2017 fused +the negation in the downward-rounded product and quotient the same way as +fnmul above. The opaque negation prevents that, and the float examples run +two to four times faster with MSVC. With GCC and clang on x86-64 the opaque +negation is an empty asm barrier instead of a volatile round trip. + [RELEASE] Version: 6.5.0 Date: 2026-10-31 diff --git a/gecode/float.hh b/gecode/float.hh index e2f0620deb..1fe8ace09a 100755 --- a/gecode/float.hh +++ b/gecode/float.hh @@ -128,24 +128,16 @@ namespace Gecode { namespace Gecode { namespace Float { -#if defined(_MSC_VER) && (defined(_M_X64) || (defined(_M_IX86_FP) && (_M_IX86_FP > 0))) - /* - * This is used for the MSVC compiler for x64 or x86 with SSE enabled. + * All compilers, MSVC included, keep upward mode between calls. The + * downward operations negate an operand, which negate_opaque in + * rounded_arith.hpp keeps from being folded into the rounded operation. * */ - /// Rounding operations with explicit direction changes (MSVC/SSE) - typedef gecode_boost::numeric::interval_lib::rounded_arith_std - RoundingControl; - -#else - /// Rounding operations that keep upward mode between calls typedef gecode_boost::numeric::interval_lib::rounded_arith_opp RoundingControl; -#endif - /** * \brief Floating point rounding operations without state saving * diff --git a/gecode/third-party/boost/numeric/interval/rounded_arith.hpp b/gecode/third-party/boost/numeric/interval/rounded_arith.hpp index 40bbdd9b08..1f61622e62 100644 --- a/gecode/third-party/boost/numeric/interval/rounded_arith.hpp +++ b/gecode/third-party/boost/numeric/interval/rounded_arith.hpp @@ -96,14 +96,22 @@ struct rounded_arith_opp: Rounding { // the compiler may fuse the negation into the rounded operation, which is // only valid under round-to-nearest. For example, clang on AArch64 turns // x * (-y) into fnmul, which negates after rounding, so mul_down returned - // the upward-rounded product. + // the upward-rounded product. MSVC 2015 and 2017 on x64 did the same + // for x * (-y) and x / (-y). T negate_opaque(const T& x) { #if defined(__GNUC__) && defined(__aarch64__) // An empty asm hides the value without a memory round trip. T r = -x; __asm__("" : "+w"(r)); return r; +#elif defined(__GNUC__) && defined(__x86_64__) + // The same barrier on an SSE register. + T r = -x; + __asm__("" : "+x"(r)); + return r; #else + // Elsewhere, MSVC included (no inline asm on x64), the volatile round + // trip is the barrier. return this->force_rounding(-x); #endif }