public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/109294] New: Inconsistent exp function implementations for compile time and run time evaluation
@ 2023-03-27  9:54 jdavidpeter at gmail dot com
  2023-03-27 13:38 ` [Bug c++/109294] " xry111 at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: jdavidpeter at gmail dot com @ 2023-03-27  9:54 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109294

            Bug ID: 109294
           Summary: Inconsistent exp function implementations for compile
                    time and run time evaluation
           Product: gcc
           Version: 12.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jdavidpeter at gmail dot com
  Target Milestone: ---

Created attachment 54764
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=54764&action=edit
Raw and preprocessed source file reproducing the problem on WSL Ubuntu 20.04.5
LTS

Implementation for exp function provides different results for certain inputs
based on whether the call is evaluated compile time (e.g. constexpr) or
runtime.

Refer to the GodBolt link in PS as well as the attached, raw and preprocessed
source.

# System info:
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.5 LTS
Release:        20.04
Codename:       focal

$ g++ --version
g++ (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

# Repro steps:
g++ bug.cpp -O0 -save-temps -o O0
g++ bug.cpp -O3 -save-temps -o O3

$ ./O0
d9bd1a4d
d9bd1a4d
d9bd1a4e

$ ./O3
d9bd1a4d
d9bd1a4e
d9bd1a4e

In the example, there are 3 calls to exp with the same input.

1. The first will always use the runtime implementation due to the volatile
keyword on the variable.
2. The second uses runtime implementation in -O0 and compile time in -O3.
3. The third always uses compile time implementation based on the GodBolt
assembly.

Based on the calls, the same binary will provide different results for use case
1 vs 3 which is a bug.
Also, the result will be different for use case 2 in -O0 and -O3, which is also
a bug.

I also question if the third call should be always optimized to a registry
operation instead of calling the actual runtime exp.
However, I didn't find any documentation on optimizations done in -O0, so this
might be intentional.
Would be less of a problem if the compile time and runtime implementation would
yield the same input.

This bug also manifests in gcc12.
This isn't an issue for clang 16.0.

PS:
https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1DIApACYAQuYukl9ZATwDKjdAGFUtAK4sGIAGwAnKSuADJ4DJgAcj4ARpjEIGYAHKQADqgKhE4MHt6%2BAcEZWY4C4ZExLPGJKbaY9qUMQgRMxAR5Pn5BdQ05za0E5dFxCUmpCi1tHQXdEwNDldVjAJS2qF7EyOwcAPQ7ANQAKgCeaZj7x%2BvE%2B2hY%2BwgJmKT7JPu0qEzo%2B4b7mKqsaXoADoTBoAILmADMEWQ3juJkhbicE2ImFYCOwoIhZmhDFhXnhiOQLCYBAQGKxUJhcPOCKRmQp4KxXiyRn2zDYCjSTC2%2Bwm6ARViZTJxWBokX2AEkoso5Id9gBaDSqLhAzBcSGQqjJJhmTAafxmSRpBWSSnggBuqDwXzSxAiBAAEn8IOh1rF6PtVMt9iYAOxCsH7YM3dYEX2Iun3P4RtxR1EOhJ2zAEAD6ogmdK8DCywEiArM/gxEG9sajrlogspfoAIuawQ79iSIhBltXA1iQ/srQZHJ63V4Pece6S6JhJQw0l5wwia1KZXKq%2BCuwOh/sIlOZ5C59LZYclxDlyHV56/mlUyO%2B%2BPJ9OI3OzxBL2OJ5u25DAyv3afVOeN7fZ78P4QH%2BBBvh%2Bx5fucZ6prucp3oBaQQLBhxgfWXZ2g6zqqBA0FPvQL7TmB%2Bx7PsbhiLQCj7EQ%2BzENmjhsOuLCApgbCCKOAidiGGGCFhOE/qmIFESRZG0BRVGoKGTFjlReAMbJz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22c3YhDSvnJYvtMj%2B1yJ0oOAyQ6pzDgneorSmiZ0DhHKZUcZnzHGQXBOszhm52WRUCZXAi4lwkGXDgCDeFV0wU/HBr98Gd27l/PuOJdlkOHqPcRkiJHSK2rwGec8NBmD9GYTeyRkjsI6pCP00gTkKMOoI06yj4BqJAGfacF8NE3zvpwB%2BWDn64Lfh/W5P8/6yDyUA9WRStYQNKdApgsCPGHOObtFBaDbyoHyhi85L88EEIgEQ3RCRfQ4jMAPRRs1SCUKwIkGhnM6GLVVIEP5fpl5cC4KvSQhoNoQv4bYA%2BQjhViMhMtV560J7NPVY84Rhz36IPpQo7VIiGbECyM4SQQA%3D%3D%3D

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

* [Bug c++/109294] Inconsistent exp function implementations for compile time and run time evaluation
  2023-03-27  9:54 [Bug c++/109294] New: Inconsistent exp function implementations for compile time and run time evaluation jdavidpeter at gmail dot com
@ 2023-03-27 13:38 ` xry111 at gcc dot gnu.org
  2023-03-27 13:55 ` redi at gcc dot gnu.org
  2023-03-27 15:21 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: xry111 at gcc dot gnu.org @ 2023-03-27 13:38 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109294

Xi Ruoyao <xry111 at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |xry111 at gcc dot gnu.org

--- Comment #1 from Xi Ruoyao <xry111 at gcc dot gnu.org> ---
Here GCC produces more precise value at compile time (as MPFR is used for
floating evaluation), while Glibc produces less precise value at runtime.

The C standard allows exp to be not correctly rounded (N3054 F.5 para 1), i. e.
several ULPs of error is allowed.  So there is no way to predict what Glibc (or
another Glibc) will produce at runtime.

I see nothing we can do here.

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

* [Bug c++/109294] Inconsistent exp function implementations for compile time and run time evaluation
  2023-03-27  9:54 [Bug c++/109294] New: Inconsistent exp function implementations for compile time and run time evaluation jdavidpeter at gmail dot com
  2023-03-27 13:38 ` [Bug c++/109294] " xry111 at gcc dot gnu.org
@ 2023-03-27 13:55 ` redi at gcc dot gnu.org
  2023-03-27 15:21 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: redi at gcc dot gnu.org @ 2023-03-27 13:55 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109294

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Dávid Péter Jánosa from comment #0)
> Based on the calls, the same binary will provide different results for use
> case 1 vs 3 which is a bug.
> Also, the result will be different for use case 2 in -O0 and -O3, which is
> also a bug.

No, those are not bugs, see the discussion in
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p1383r1.pdf

It's not reasonable to expect the exact same results for floating-point maths
under all conditions.

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

* [Bug c++/109294] Inconsistent exp function implementations for compile time and run time evaluation
  2023-03-27  9:54 [Bug c++/109294] New: Inconsistent exp function implementations for compile time and run time evaluation jdavidpeter at gmail dot com
  2023-03-27 13:38 ` [Bug c++/109294] " xry111 at gcc dot gnu.org
  2023-03-27 13:55 ` redi at gcc dot gnu.org
@ 2023-03-27 15:21 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-03-27 15:21 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109294

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=66736
         Resolution|---                         |INVALID
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
.

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

end of thread, other threads:[~2023-03-27 15:21 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-27  9:54 [Bug c++/109294] New: Inconsistent exp function implementations for compile time and run time evaluation jdavidpeter at gmail dot com
2023-03-27 13:38 ` [Bug c++/109294] " xry111 at gcc dot gnu.org
2023-03-27 13:55 ` redi at gcc dot gnu.org
2023-03-27 15:21 ` pinskia at gcc dot gnu.org

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).