public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/110615] New: std::abs converts integers to floats and back
@ 2023-07-10 15:21 julien.jorge@stuff-o-matic.com
  2023-07-10 15:53 ` [Bug libstdc++/110615] " redi at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: julien.jorge@stuff-o-matic.com @ 2023-07-10 15:21 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 110615
           Summary: std::abs converts integers to floats and back
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: julien.jorge@stuff-o-matic.com
  Target Milestone: ---

The implementation of std::abs in current HEAD
(a3ad2301d2f4aab2deeb286fa5bd0282260bfd0a) is as follows (in
libstdc++-v3/include/c_std/cmath):

    template<typename _Tp>
    inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value, 
                                           double>::__type
    abs(_Tp __x)
    { return __builtin_fabs(__x); }

The function is only used when _Tp is an integer (due to the __enable_if part),
yet it calls __builtin_fabs where, I believe, __builtin_llabs should have been
used. Unless the intent was to negate the condition in __enable_if?

I observed a lot of int <-> float conversions in the assembly of my program on
an older version of GCC with this implementation, unless I included cstdlib
which defines abs(long long) and co. The int <-> float conversions do not
happen with current HEAD but I believe it is due to a side effect of cmath
transitively including stdlib.h.

I first observed this with GCC 4.8.5. Compiler explorer shows int <-> float
conversions when cstdlib is missing with GCC up to 6.4:
https://godbolt.org/z/bWfEv1jxP

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

* [Bug libstdc++/110615] std::abs converts integers to floats and back
  2023-07-10 15:21 [Bug libstdc++/110615] New: std::abs converts integers to floats and back julien.jorge@stuff-o-matic.com
@ 2023-07-10 15:53 ` redi at gcc dot gnu.org
  2023-07-10 15:56 ` redi at gcc dot gnu.org
  2023-07-10 16:44 ` julien.jorge@stuff-o-matic.com
  2 siblings, 0 replies; 4+ messages in thread
From: redi at gcc dot gnu.org @ 2023-07-10 15:53 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |INVALID
             Status|UNCONFIRMED                 |RESOLVED
            Version|unknown                     |6.5.0

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
This is the expected behaviour for those old releases (which are no longer
supported or maintained, and so there's no point reporting bugs in them).

Prior to the resolutions of https://wg21.link/lwg2192 and
https://wg21.link/lwg2294 the integer overloads of std::abs were only declared
in <cstdlib>, so if you only include <cmath> then you only get the
floating-point overloads.

GCC 7.0 implements the resolutions to those issues, in r7-3447-g37b204de605563

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

* [Bug libstdc++/110615] std::abs converts integers to floats and back
  2023-07-10 15:21 [Bug libstdc++/110615] New: std::abs converts integers to floats and back julien.jorge@stuff-o-matic.com
  2023-07-10 15:53 ` [Bug libstdc++/110615] " redi at gcc dot gnu.org
@ 2023-07-10 15:56 ` redi at gcc dot gnu.org
  2023-07-10 16:44 ` julien.jorge@stuff-o-matic.com
  2 siblings, 0 replies; 4+ messages in thread
From: redi at gcc dot gnu.org @ 2023-07-10 15:56 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Julien Jorge from comment #0)
> The int <-> float conversions
> do not happen with current HEAD but I believe it is due to a side effect of
> cmath transitively including stdlib.h.

No, it's due to the resolution of LWG 2192.

There is no transitive include of <stdlib.h> in <cmath>, instead both <cstdlib>
and <cmath> include <bits/std_abs.h> which defines all overloads for integers
and floating-point types.

This is by design, not a side effect.

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

* [Bug libstdc++/110615] std::abs converts integers to floats and back
  2023-07-10 15:21 [Bug libstdc++/110615] New: std::abs converts integers to floats and back julien.jorge@stuff-o-matic.com
  2023-07-10 15:53 ` [Bug libstdc++/110615] " redi at gcc dot gnu.org
  2023-07-10 15:56 ` redi at gcc dot gnu.org
@ 2023-07-10 16:44 ` julien.jorge@stuff-o-matic.com
  2 siblings, 0 replies; 4+ messages in thread
From: julien.jorge@stuff-o-matic.com @ 2023-07-10 16:44 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Julien Jorge <julien.jorge@stuff-o-matic.com> ---
> This is the expected behaviour for those old releases (which are no longer supported or maintained, and so there's no point reporting bugs in them).

Well, it was more an attempt to raise awareness on a behaviour I assumed to be
accidental in recent releases rather than an actual bug report for old releases
:) 

Anyway it is clear that I was mistaken! Thank you for the feedback and the
links :)

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

end of thread, other threads:[~2023-07-10 16:44 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-10 15:21 [Bug libstdc++/110615] New: std::abs converts integers to floats and back julien.jorge@stuff-o-matic.com
2023-07-10 15:53 ` [Bug libstdc++/110615] " redi at gcc dot gnu.org
2023-07-10 15:56 ` redi at gcc dot gnu.org
2023-07-10 16:44 ` julien.jorge@stuff-o-matic.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).