Skip to content
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

make check fails with -Ofast #1

Open
orbea opened this issue Aug 6, 2022 · 2 comments
Open

make check fails with -Ofast #1

orbea opened this issue Aug 6, 2022 · 2 comments

Comments

@orbea
Copy link
Owner

orbea commented Aug 6, 2022

compiler: gcc (GCC) 11.2.0 + clang version 13.0.0

Trio is broken with -Ofast.

CFLAGS=-Ofast ./configure
Reading symbols from ./regression...
(gdb) r
Starting program: /tmp/trio/regression 
Verifying strings
Verifying dynamic strings
Verifying special quantities
Verification failed in regression.c:1646.
  Expected "-1"
  Got      "0"
Verification failed in regression.c:1650.
  Expected "1"
  Got      "0"
Verification failed in regression.c:1652.
  Expected "1"
  Got      "0"
Verifying formatting
Verification failed in regression.c:424.
  Expected "0"
  Got      "0e-2147483648"
Verification failed in regression.c:426.
  Expected "-0"
  Got      "-0e-2147483648"
Verification failed in regression.c:477.
  Expected "0.000"
  Got      "0.000e-2147483648"

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7fa7b0e in TrioWriteDouble () from .libs/libtrio.so.2
(gdb) bt
#0  0x00007ffff7fa7b0e in TrioWriteDouble () from .libs/libtrio.so.2
#1  0x00007ffff7fa7f23 in TrioFormatProcess () from .libs/libtrio.so.2
#2  0x00007ffff7fa926f in trio_vsnprintf () from .libs/libtrio.so.2
#3  0x0000000000406eb8 in Verify ()
#4  0x000000000040411f in main ()
@orbea
Copy link
Owner Author

orbea commented Aug 8, 2022

With both gcc and clang the build passes with CFLAGS=-Ofast -fno-unsafe-math-optimizations -fno-finite-math-only.

@orbea
Copy link
Owner Author

orbea commented Aug 8, 2022

This patch hides the issue. There might be more issues hiding behind preprocessor conditionals.

trio/trio.c

Lines 1264 to 1327 in b34e724

/*************************************************************************
* TrioPower
*
* Description:
* Calculate pow(base, exponent), where number and exponent are integers.
*/
#if TRIO_FEATURE_FLOAT
TRIO_PRIVATE trio_long_double_t
TrioPower
TRIO_ARGS2((number, exponent),
int number,
int exponent)
{
trio_long_double_t result;
if (number == 10)
{
switch (exponent)
{
/* Speed up calculation of common cases */
case 0:
result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E-1);
break;
case 1:
result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+0);
break;
case 2:
result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+1);
break;
case 3:
result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+2);
break;
case 4:
result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+3);
break;
case 5:
result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+4);
break;
case 6:
result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+5);
break;
case 7:
result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+6);
break;
case 8:
result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+7);
break;
case 9:
result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+8);
break;
default:
result = trio_pow((trio_long_double_t)number,
(trio_long_double_t)exponent);
break;
}
}
else
{
return trio_pow((trio_long_double_t)number,
(trio_long_double_t)exponent);
}
return result;
}
#endif /* TRIO_FEATURE_FLOAT */

trio/trionan.c

Lines 427 to 450 in b34e724

#if defined(TRIO_FUNC_C99_FPCLASSIFY_AND_SIGNBIT)
TRIO_PRIVATE_NAN int
c99_fpclassify_and_signbit
TRIO_ARGS2((number, is_negative),
double number,
int *is_negative)
{
*is_negative = signbit(number);
switch (fpclassify(number)) {
case FP_NAN:
return TRIO_FP_NAN;
case FP_INFINITE:
return TRIO_FP_INFINITE;
case FP_SUBNORMAL:
return TRIO_FP_SUBNORMAL;
case FP_ZERO:
return TRIO_FP_ZERO;
default:
return TRIO_FP_NORMAL;
}
}
#endif /* TRIO_FUNC_C99_FPCLASSIFY_AND_SIGNBIT */

--- a/trio.c
+++ b/trio.c
@@ -1268,6 +1268,10 @@ TRIO_ARGS2((name, prev),
  *  Calculate pow(base, exponent), where number and exponent are integers.
  */
 #if TRIO_FEATURE_FLOAT
+
+#pragma GCC push_options
+#pragma GCC optimize ("no-unsafe-math-optimizations")
+
 TRIO_PRIVATE trio_long_double_t
 TrioPower
 TRIO_ARGS2((number, exponent),
@@ -1324,6 +1328,9 @@ TRIO_ARGS2((number, exponent),
     }
   return result;
 }
+
+#pragma GCC pop_options
+
 #endif /* TRIO_FEATURE_FLOAT */
 
 /*************************************************************************
--- a/trionan.c
+++ b/trionan.c
@@ -426,6 +426,10 @@ TRIO_ARGS1((number),
 
 #if defined(TRIO_FUNC_C99_FPCLASSIFY_AND_SIGNBIT)
 
+#pragma GCC push_options
+#pragma GCC optimize ("no-unsafe-math-optimizations")
+#pragma GCC optimize ("no-finite-math-only")
+
 TRIO_PRIVATE_NAN int
 c99_fpclassify_and_signbit
 TRIO_ARGS2((number, is_negative),
@@ -447,6 +451,8 @@ TRIO_ARGS2((number, is_negative),
   }
 }
 
+#pragma GCC pop_options
+
 #endif /* TRIO_FUNC_C99_FPCLASSIFY_AND_SIGNBIT */
 
 #if defined(TRIO_FUNC_DECC_FPCLASSIFY_AND_SIGNBIT)

orbea added a commit that referenced this issue Aug 9, 2022
With -Ofast which enables -funsafe-math-optimizations the value of
integerNumber becomes 0.0 instead of 1.0 which breaks the logic and
regression test program. Adding espilonCorrection which is also 0.0
prevents the compiler from optimizing the value away.

This is part of issue #1.
orbea added a commit that referenced this issue Sep 24, 2022
Workaround for issue #1.

Also strio.h is only used by example.c.
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

No branches or pull requests

1 participant