8000 Fix overlaps.sql test fail on 32 bit Debian due to gcc bug 323 by vitcpp · Pull Request #62 · postgrespro/pgsphere · GitHub
[go: up one dir, main page]

Skip to content

Fix overlaps.sql test fail on 32 bit Debian due to gcc bug 323 #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 8, 2023
Merged
Changes from 1 commit
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
Next Next commit
Fix test fails on Debian 32 bit system due to gcc bug 323
On some 32 bit platforms, there is a gcc bug that makes floating point
calculations and comparisons unstable (see the link below). The problem
originates in FPU 80 bits registers where double values are not truncated
to 64 bit values. When gcc compiles some code with enabled optimizations,
the intermediate results may be kept in the FPU registers without truncation
to 64 bit values. Extra bits may produce unstable results when comparing
the numbers.

The generic solution is to save the intermediate results in the memory where
the values are truncated to 64 bit values. It affects the performance but
makes the tests stable on all platforms.

PGSPHERE_FLOAT_STORE macro enables storing of intermediate results for FPxx
operations in the memory. It is enabled by default for 32 bit platforms.
It can be explicitly enabled or disabled in CFLAGS. To enable it for all
code the gcc option -ffloat-store may be used as well.

Link to gcc bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=323
  • Loading branch information
Vitaly Davydov committed Sep 7, 2023
commit cce07bb9baa567ec86ce95f3ed2de65f790fefea
77 changes: 77 additions & 0 deletions src/pg_sphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,85 @@

#include "pgs_util.h"

/* On some 32 bit platforms, there is a gcc bug that makes floating point
* calculations and comparisons unstable (see the link below). The problem
* originates in FPU 80 bits registers where double values are not truncated
* to 64 bit values. When gcc compiles some code with enabled optimizations,
* the intermediate results may be kept in the FPU registers without truncation
* to 64 bit values. Extra bits may produce unstable results when comparing
* the numbers.
*
* The generic solution is to save the intermediate results in the memory where
* the values are truncated to 64 bit values. It affects the performance but
* makes the tests stable on all platforms.
*
* PGSPHERE_FLOAT_STORE macro enables storing of intermediate results for FPxx
* operations in the memory. It is enabled by default for 32 bit platforms.
* It can be explicitly enabled or disabled in CFLAGS. To enable it for all
* code the gcc option -ffloat-store may be used as well.
*
* Link to gcc bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=323
*/
#if !defined(PGSPHERE_FLOAT_STORE)
#if _WIN64 || (__GNUC__ && __x86_64__)
#define PGSPHERE_FLOAT_STORE 0
#elif _WIN32 || __GNUC__
#define PGSPHERE_FLOAT_STORE 1
#else
#define PGSPHERE_FLOAT_STORE 0
#endif
#endif // PGSPHERE_FLOAT_STORE

#define EPSILON 1.0E-09

#define FPzero(A) (fabs(A) <= EPSILON)

#if PGSPHERE_FLOAT_STORE

static inline bool
FPeq(double A, double B)
{
const volatile double AB = A - B;
return A == B || fabs(AB) <= EPSILON;
}

static inline bool
FPne(double A, double B)
{
const volatile double AB = A - B;
return A != B && fabs(AB) > EPSILON;
}

static inline bool
FPlt(double A, double B)
{
const volatile double AE = A + EPSILON;
return AE < B;
}

static inline bool
FPle(double A, double B)
{
const volatile double BE = B + EPSILON;
return A <= BE;
}

static inline bool
FPgt(double A, double B)
{
const volatile double BE = B + EPSILON;
return A > BE;
}

static inline bool
FPge(double A, double B)
{
const volatile double AE = A + EPSILON;
return AE >= B;
}

#else

static inline bool
FPeq(double A, double B)
{
Expand Down Expand Up @@ -85,6 +160,8 @@ FPge(double A, double B)
return A + EPSILON >= B;
}

#endif // PGSPHERE_FLOAT_STORE

/*---------------------------------------------------------------------
* Point - (x,y)
*-------------------------------------------------------------------*/
Expand Down
0