[Bug libstdc++/110615] std::abs converts integers to floats and back

2023-07-10 Thread julien.jorge--- via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110615

--- Comment #3 from Julien Jorge  ---
> 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 :)

[Bug libstdc++/110615] New: std::abs converts integers to floats and back

2023-07-10 Thread julien.jorge--- via 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.jo...@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
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