Bug 110615 - std::abs converts integers to floats and back
Summary: std::abs converts integers to floats and back
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 6.5.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-07-10 15:21 UTC by Julien Jorge
Modified: 2023-07-10 16:44 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Julien Jorge 2023-07-10 15:21:58 UTC
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
Comment 1 Jonathan Wakely 2023-07-10 15:53:16 UTC
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
Comment 2 Jonathan Wakely 2023-07-10 15:56:30 UTC
(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.
Comment 3 Julien Jorge 2023-07-10 16:44:44 UTC
> 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 :)