Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions changelog.in
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,31 @@ 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.

[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
Expand Down
14 changes: 3 additions & 11 deletions gecode/float.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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<FloatNum>
RoundingControl;

#else

/// Rounding operations that keep upward mode between calls
typedef gecode_boost::numeric::interval_lib::rounded_arith_opp<FloatNum>
RoundingControl;

#endif

/**
* \brief Floating point rounding operations without state saving
*
Expand Down
32 changes: 29 additions & 3 deletions gecode/third-party/boost/numeric/interval/rounded_arith.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,36 @@ struct rounded_arith_opp: Rounding {
# define GECODE_BOOST_UP_NEG(EXPR) return -this->force_rounding(EXPR)
template<class U> T conv_down(U const &v) { GECODE_BOOST_UP_NEG(-v); }
template<class U> 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. 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
}
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); }
Expand Down
58 changes: 58 additions & 0 deletions test/float/arithmetic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading