Bug 28713

Summary: GCC 12 miscompiles libm
Product: glibc Reporter: H.J. Lu <hjl.tools>
Component: mathAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal CC: jwakely.gcc
Priority: P2    
Version: 2.35   
Target Milestone: 2.35   
See Also: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103735
Host: Target:
Build: Last reconfirmed:

Description H.J. Lu 2021-12-18 23:10:27 UTC
GCC 12 commit:

https://gcc.gnu.org/git/gitweb.cgi?p=gcc.git;h=a84b9d5373c7e67fd0ab2a412c22162cdf969c91

changed the -frounding-math behavior which caused:

FAIL: math/test-float-cos
FAIL: math/test-float-j0
FAIL: math/test-float-jn
FAIL: math/test-float-sin
FAIL: math/test-float-sincos
FAIL: math/test-float-y0
FAIL: math/test-float-y1
FAIL: math/test-float-yn
FAIL: math/test-float32-cos
FAIL: math/test-float32-j0
FAIL: math/test-float32-jn
FAIL: math/test-float32-sin
FAIL: math/test-float32-sincos
FAIL: math/test-float32-y0
FAIL: math/test-float32-y1
FAIL: math/test-float32-yn

on x86-64.  The failures look like

testing float (without inline functions)
Failure: cos (qNaN): Exception "Inexact" set
Failure: cos (-qNaN): Exception "Inexact" set
Failure: cos_downward (qNaN): Exception "Inexact" set
Failure: cos_downward (-qNaN): Exception "Inexact" set
Failure: cos_towardzero (qNaN): Exception "Inexact" set
Failure: cos_towardzero (-qNaN): Exception "Inexact" set
Failure: cos_upward (qNaN): Exception "Inexact" set
Failure: cos_upward (-qNaN): Exception "Inexact" set
Comment 1 H.J. Lu 2021-12-23 15:43:37 UTC
Fixed by

commit 6e30181b4a3ab6c56da0378b65f4d60504982300
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Mon Dec 20 15:00:24 2021 -0800

    math: Properly cast X_TLOSS to float [BZ #28713]
    
    Add
    
     #define AS_FLOAT_CONSTANT_1(x) x##f
     #define AS_FLOAT_CONSTANT(x) AS_FLOAT_CONSTANT_1(x)
    
    to cast X_TLOSS to float at compile-time to fix:
    
    FAIL: math/test-float-j0
    FAIL: math/test-float-jn
    FAIL: math/test-float-y0
    FAIL: math/test-float-y1
    FAIL: math/test-float-yn
    FAIL: math/test-float32-j0
    FAIL: math/test-float32-jn
    FAIL: math/test-float32-y0
    FAIL: math/test-float32-y1
    FAIL: math/test-float32-yn
    
    when compiling with GCC 12.
    
    Reviewed-by: Paul Zimmermann <Paul.Zimmermann@inria.fr>
Comment 2 H.J. Lu 2021-12-27 13:39:57 UTC
And by

commit d3e4f5a1014db09ff1c62c6506f92cba469e193d
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Mon Dec 20 14:37:26 2021 -0800

    s_sincosf.h: Change pio4 type to float [BZ #28713]
    
    s_cosf.c and s_sinf.c have
    
      if (abstop12 (y) < abstop12 (pio4))
    
    where abstop12 takes a float argument, but pio4 is static const double.
    pio4 is used only in calls to abstop12 and never in arithmetic.  Apply
    
    -static const double pio4 = 0x1.921FB54442D18p-1;
    +static const float pio4 = 0x1.921FB6p-1f;
    
    to fix:
    
    FAIL: math/test-float-cos
    FAIL: math/test-float-sin
    FAIL: math/test-float-sincos
    FAIL: math/test-float32-cos
    FAIL: math/test-float32-sin
    FAIL: math/test-float32-sincos
    
    when compiling with GCC 12.
    
    Reviewed-by: Paul Zimmermann <Paul.Zimmermann@inria.fr>
Comment 3 Sourceware Commits 2022-01-10 14:28:53 UTC
The master branch has been updated by Szabolcs Nagy <nsz@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=347a5b592c91f0230d52a5117fd83e1f75028207

commit 347a5b592c91f0230d52a5117fd83e1f75028207
Author: Szabolcs Nagy <szabolcs.nagy@arm.com>
Date:   Fri Dec 31 09:50:50 2021 +0000

    math: Fix float conversion regressions with gcc-12 [BZ #28713]
    
    Converting double precision constants to float is now affected by the
    runtime dynamic rounding mode instead of being evaluated at compile
    time with default rounding mode (except static object initializers).
    
    This can change the computed result and cause performance regression.
    The known correctness issues (increased ulp errors) are already fixed,
    this patch fixes remaining cases of unnecessary runtime conversions.
    
    Add float M_* macros to math.h as new GNU extension API.  To avoid
    conversions the new M_* macros are used and instead of casting double
    literals to float, use float literals (only required if the conversion
    is inexact).
    
    The patch was tested on aarch64 where the following symbols had new
    spurious conversion instructions that got fixed:
    
      __clog10f
      __gammaf_r_finite@GLIBC_2.17
      __j0f_finite@GLIBC_2.17
      __j1f_finite@GLIBC_2.17
      __jnf_finite@GLIBC_2.17
      __kernel_casinhf
      __lgamma_negf
      __log1pf
      __y0f_finite@GLIBC_2.17
      __y1f_finite@GLIBC_2.17
      cacosf
      cacoshf
      casinhf
      catanf
      catanhf
      clogf
      gammaf_positive
    
    Fixes bug 28713.
    
    Reviewed-by: Paul Zimmermann <Paul.Zimmermann@inria.fr>
Comment 4 Jonathan Wakely 2022-03-14 12:20:52 UTC
This breaks any C++ program using those names, e.g. see
https://bugzilla.redhat.com/show_bug.cgi?id=2045149#c6

This is because G++ always defines _GNU_SOURCE.

Was this considered?
Comment 5 Jonathan Wakely 2022-03-14 13:02:22 UTC
(In reply to Jonathan Wakely from comment #4)
> This breaks any C++ program using those names, e.g. see
> https://bugzilla.redhat.com/show_bug.cgi?id=2045149#c6
> 
> This is because G++ always defines _GNU_SOURCE.
> 
> Was this considered?

Filed as PR 28963