public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libc/31227] New: The value of cos varies with different optimization levels
@ 2024-01-10  1:20 wxzkenny at aliyun dot com
  2024-01-10  9:38 ` [Bug libc/31227] " schwab@linux-m68k.org
  2024-01-11  3:20 ` wxzkenny at aliyun dot com
  0 siblings, 2 replies; 3+ messages in thread
From: wxzkenny at aliyun dot com @ 2024-01-10  1:20 UTC (permalink / raw)
  To: glibc-bugs

https://sourceware.org/bugzilla/show_bug.cgi?id=31227

            Bug ID: 31227
           Summary: The value of cos varies with different optimization
                    levels
           Product: glibc
           Version: 2.28
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: libc
          Assignee: unassigned at sourceware dot org
          Reporter: wxzkenny at aliyun dot com
                CC: drepper.fsp at gmail dot com
  Target Milestone: ---

I wrote a test case test_cos.c, the context of file test_cos.c as following:

#include <stdio.h>
#include <math.h>

static double dcos(double arg1)
{
    return cos(arg1);
}

int main()
{
    double d = 17.14159265;
    volatile double c = dcos(d);

    printf("cos(d) = %.17f %llx\n", c, *(long long*)&c);

    return 0;
}

When we compiled with different optimization levels, we got different results: 
[root@localhost tmp]# gcc -o test_cos -lm -O test_cos.c && ./test_cos
cos(d) = -0.13673722176390943 bfc1809af3cf9a1b
[root@localhost tmp]# gcc -o test_cos -lm -O0 test_cos.c && ./test_cos
cos(d) = -0.13673722176390940 bfc1809af3cf9a1a
[root@localhost tmp]# gcc -o test_cos -lm -O1 test_cos.c && ./test_cos
cos(d) = -0.13673722176390943 bfc1809af3cf9a1b
[root@localhost tmp]# gcc -o test_cos -lm -O2 test_cos.c && ./test_cos
cos(d) = -0.13673722176390943 bfc1809af3cf9a1b
[root@localhost tmp]# gcc -o test_cos -lm -O3 test_cos.c && ./test_cos
cos(d) = -0.13673722176390943 bfc1809af3cf9a1b

When I embed cos in my large program and compile it with -O2 I get the same
result as here with -O0

-- 
You are receiving this mail because:
You are on the CC list for the bug.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug libc/31227] The value of cos varies with different optimization levels
  2024-01-10  1:20 [Bug libc/31227] New: The value of cos varies with different optimization levels wxzkenny at aliyun dot com
@ 2024-01-10  9:38 ` schwab@linux-m68k.org
  2024-01-11  3:20 ` wxzkenny at aliyun dot com
  1 sibling, 0 replies; 3+ messages in thread
From: schwab@linux-m68k.org @ 2024-01-10  9:38 UTC (permalink / raw)
  To: glibc-bugs

https://sourceware.org/bugzilla/show_bug.cgi?id=31227

Andreas Schwab <schwab@linux-m68k.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |NOTABUG

--- Comment #1 from Andreas Schwab <schwab@linux-m68k.org> ---
When compiling with optimisation the compiler expands the call to cos into a
constant, using its own implementation to compute the result.  The difference
of one ULP between different implementations is well within the expected
bounds.  You can use -fno-builtin-cos to force the use of the implementation in
libm.

-- 
You are receiving this mail because:
You are on the CC list for the bug.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug libc/31227] The value of cos varies with different optimization levels
  2024-01-10  1:20 [Bug libc/31227] New: The value of cos varies with different optimization levels wxzkenny at aliyun dot com
  2024-01-10  9:38 ` [Bug libc/31227] " schwab@linux-m68k.org
@ 2024-01-11  3:20 ` wxzkenny at aliyun dot com
  1 sibling, 0 replies; 3+ messages in thread
From: wxzkenny at aliyun dot com @ 2024-01-11  3:20 UTC (permalink / raw)
  To: glibc-bugs

https://sourceware.org/bugzilla/show_bug.cgi?id=31227

--- Comment #2 from Kenny <wxzkenny at aliyun dot com> ---
(In reply to Andreas Schwab from comment #1)
> When compiling with optimisation the compiler expands the call to cos into a
> constant, using its own implementation to compute the result.  The
> difference of one ULP between different implementations is well within the
> expected bounds.  You can use -fno-builtin-cos to force the use of the
> implementation in libm.

I added -fno-builtin-cos according to your instructions and conducted the test
again, and found that the result became stable, but the result of ARM platform
was different from that of X86 platform. Is this expected? 

the glibc version of ARM is 2.28, but the X86 is 2.17

[root@localhost tmp]# uname -a
Linux localhost.localdomain 4.19.90-23.32.v2101.ky10.aarch64 #1 SMP Fri
Feb 24 10:12:55 CST 2023 aarch64 aarch64 aarch64 GNU/Linux
[root@localhost tmp]# gcc -o test_cos -lm -g -fno-builtin-cos test_cos.c
&& ./test_cos
cos(d) = -0.13673722176390940 bfc1809af3cf9a1a
[root@localhost tmp]# gcc -o test_cos -lm -g -O -fno-builtin-cos
test_cos.c && ./test_cos
cos(d) = -0.13673722176390940 bfc1809af3cf9a1a
[root@localhost tmp]# gcc -o test_cos -lm -g -O1 -fno-builtin-cos
test_cos.c && ./test_cos
cos(d) = -0.13673722176390940 bfc1809af3cf9a1a
[root@localhost tmp]# gcc -o test_cos -lm -g -O2 -fno-builtin-cos
test_cos.c && ./test_cos
cos(d) = -0.13673722176390940 bfc1809af3cf9a1a
[root@localhost tmp]# gcc -o test_cos -lm -g -O3 -fno-builtin-cos
test_cos.c && ./test_cos
cos(d) = -0.13673722176390940 bfc1809af3cf9a1a

[root@localhost tmp]#  uname -a
Linux xiaozhong-wang 3.10.0-1160.102.1.el7.x86_64 #1 SMP Tue Oct 17
15:42:21 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost tmp]#  gcc -o test_cos -lm -g -fno-builtin-cos
test_cos.c && ./test_cos
cos(d) = -0.13673722176390943 bfc1809af3cf9a1b
[root@localhost tmp]#  gcc -o test_cos -lm -g -O -fno-builtin-cos
test_cos.c && ./test_cos
cos(d) = -0.13673722176390943 bfc1809af3cf9a1b
[root@localhost tmp]#  gcc -o test_cos -lm -g -O1 -fno-builtin-cos
test_cos.c && ./test_cos
cos(d) = -0.13673722176390943 bfc1809af3cf9a1b
[root@localhost tmp]#  gcc -o test_cos -lm -g -O2 -fno-builtin-cos
test_cos.c && ./test_cos
cos(d) = -0.13673722176390943 bfc1809af3cf9a1b
[root@localhost tmp]#  gcc -o test_cos -lm -g -O3 -fno-builtin-cos
test_cos.c && ./test_cos
cos(d) = -0.13673722176390943 bfc1809af3cf9a1b

-- 
You are receiving this mail because:
You are on the CC list for the bug.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-01-11  3:20 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-10  1:20 [Bug libc/31227] New: The value of cos varies with different optimization levels wxzkenny at aliyun dot com
2024-01-10  9:38 ` [Bug libc/31227] " schwab@linux-m68k.org
2024-01-11  3:20 ` wxzkenny at aliyun dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).