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