This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH]: convert (long)round(d) -> lround(d), etc
- From: Roger Sayle <roger at eyesopen dot com>
- To: "Kaveh R. Ghazi" <ghazi at caip dot rutgers dot edu>
- Cc: gcc-patches at gcc dot gnu dot org
- Date: Tue, 27 Apr 2004 20:00:51 -0600 (MDT)
- Subject: Re: [PATCH]: convert (long)round(d) -> lround(d), etc
On Tue, 27 Apr 2004, Kaveh R. Ghazi wrote:
> 2004-04-27 Kaveh R. Ghazi <ghazi@caip.rutgers.edu>
>
> * convert.c (convert_to_integer): Convert (long)round -> lround,
> etc.
>
> testsuite:
> * gcc.dg/torture/builtin-convert-2.c: New test.
>
Having given this some thought, I think this patch is OK for mainline
provided that its conditionalized on flag_unsafe_math_optimizations.
Given the expression (char)round(d), we'll substitute this for
(char)lround(d). If the value "d" is greater than CHAR_MAX but less
than LONG_MAX, we may end up with slightly different behaviour:
double->char overflow invokes undefined behaviour (or satuatation
in Java), but long->char overflow always truncates.
To demonstrate consider the output of:
int main()
{
char c;
c = (char)round(400.0);
printf("%d\n",c);
c = (char)lround(400.0);
printf("%d\n",c);
return 0;
}
Which with mainline on IA-32 produces the output:
127
-112
Whilst it is true, in C/C++, that (char)round(400.0) invokes undefined
behaviour, and we're within our rights to change the result from 127 to
-112, I'd feel slightly more comfortable if we only made such changes
when the user explicitly requests them.
If you don't have a strong objection, I think it would be safer to
tweak your patch to "if (optimize && flag_unsafe_math_optimizations)".
Ok for mainline with that change.
[Your new test case specifies -ffast-math already, so it doesn't even
need to be modified].
Roger
--