side_robust/overlay: GCC -ffp-contract=fast on x86-64-v3 breaks adaptive predicates (ST_SymDifference loses polygons)

Aperta
#1,523 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
57/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
cpp
Ambito
compilers

Direzione di ricerca

Begin in util/precise_math.hpp: read two_diff/two_product and confirm that only the explicit std::fma may fuse; the pragma added by PR #1453 in index/detail/algorithms/content.hpp is the precedent to mirror. First reproduce: build the ST_SymDifference WKT from st_symdifference.test with -O3 -DNDEBUG -march=x86-64-v3 and check the first geometry's area is 7.93333 rather than 76.1482. Done when that build returns the three polygons (68.2149, 5, 2.93333) with contraction left on and the geometry tests still pass.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Environment

  • Boost.Geometry 1.84.0 (bundled with MySQL 8.4.11)
  • GCC 15.2.0, Ubuntu 26.04, x86-64 (-march=x86-64-v3, FMA available)
  • Flags: -O3 -DNDEBUG
  • Compiler defaults: GCC -ffp-contract=fast (contracts across statements);
    Clang defaults to -ffp-contract=on and is not affected.
  • Affected path: strategies/relate/cartesian.hpp selects
    strategy::side::side_robust, which uses util/precise_math.hpp
    (two_diff, two_product via std::fma). strategy/cartesian/precise_area.hpp
    uses the same helpers.

Summary

With GCC's default -ffp-contract=fast, expressions inside the adaptive
predicates can be fused into FMA across statements. precise_math.hpp
implements Shewchuk-style error-free transformations that are only correct if
every +, -, * rounds exactly as written and only the explicit
std::fma is fused. Once a product or a tail is contracted, the returned
"rounding error" is no longer exact, the filter's error bound is violated, and
side_robust can return the wrong sign for near-degenerate configurations.
Overlay then selects the wrong topology and output polygons disappear.

Reproducer (deterministic, MySQL SQL)

WKT from mysql-test/suite/gis/t/st_symdifference.test:

SET @a = 'MULTIPOLYGON(((5 0,15 5,25 0,22 10,30 15,19 15,5 0)),((19 15,15 25,11 15,19 15)))';
SET @b = 'GEOMETRYCOLLECTION(MULTIPOLYGON(((5 0,15 5,25 0,22 10,30 15,19 15,5 0)),((19 15,15 25,11 15,19 15))),POLYGON((5 0,7 10,0 15,10 15,15 25,20 15,30 15,22 10,25 0,15 5,5 0),(10 6,10 10,7 12,13 13,15 18,17 13,21 13,18 10,20 6,15 8,10 6)),MULTILINESTRING((25 0,0 15,15 30,0 5)),LINESTRING(15 25,15 10),MULTIPOINT(5 0,25 0),POINT(15 25))';
SELECT ST_Area(ST_GeometryN(
         ST_SymDifference(ST_GeomFromText(@a), ST_GeomFromText(@b)), 1));

Results:

build area
-O3 -DNDEBUG -march=x86-64-v3 (FMA contracted) 7.9333333333333229
same + -ffp-contract=off 76.148226950354598 (3 polygons)
same + -mno-fma or -ffp-contract=on 76.148226950354598

The correct result is three polygons with areas 68.2149, 5, and 2.93333.
The broken value 7.93333 is exactly 5 + 2.93333: the large piece is dropped
by a wrong side/orientation decision, not by a rounding difference in the area.

Debug (-O0), ARM, and the same code compiled with Clang all pass, which is
consistent with the contraction hypothesis.

Relation to #1452 / #1453

#1452/#1453 added

#pragma GCC optimize("fp-contract=off")

only around index/detail/algorithms/content.hpp. The overlay path
(strategy/cartesian/side_robust.hpp and strategy/cartesian/precise_area.hpp
through util/precise_math.hpp) has no equivalent protection, so the same
failure mode still applies there.

Suggested fix

Disable contraction around the error-free transformations in
util/precise_math.hpp (for GCC), e.g. push/pop
#pragma GCC optimize("fp-contract=off") around the helper definitions, or
document/enforce that TUs using side_robust/precise_area must be compiled
with -ffp-contract=off. A portable #pragma STDC FP_CONTRACT OFF is not
reliably honoured for C++ by GCC, which is why #1453 used the GCC pragma.

Workaround

Compile the affected translation units / target with -ffp-contract=off
(preferred) or -mno-fma. This is what MySQL-derived builds have started doing
for their sql_gis target.

References

Lingua principale
C++
Stelle
519
Fork
241
Merge medio
4g 15h
PR unite (30g)
9

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di boostorg/geometry

Tutte le issue di boostorg/geometry

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.