* [Bug c/28473] with -O, casting result of round(x) to uint64_t produces wrong values for x > INT_MAX
2006-07-24 20:53 [Bug c/28473] New: with -O, casting result of round(x) to uint64_t produces wrong values for x > INT_MAX maxp at alum dot mit dot edu
@ 2006-07-24 20:54 ` maxp at alum dot mit dot edu
2006-07-24 22:00 ` [Bug c/28473] New: " Andrew Pinski
` (11 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: maxp at alum dot mit dot edu @ 2006-07-24 20:54 UTC (permalink / raw)
To: gcc-bugs
------- Comment #1 from maxp at alum dot mit dot edu 2006-07-24 20:54 -------
Created an attachment (id=11930)
--> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=11930&action=view)
preprocessor output
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28473
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [Bug c/28473] New: with -O, casting result of round(x) to uint64_t produces wrong values for x > INT_MAX
2006-07-24 20:53 [Bug c/28473] New: with -O, casting result of round(x) to uint64_t produces wrong values for x > INT_MAX maxp at alum dot mit dot edu
2006-07-24 20:54 ` [Bug c/28473] " maxp at alum dot mit dot edu
@ 2006-07-24 22:00 ` Andrew Pinski
2006-07-24 22:01 ` [Bug c/28473] " pinskia at physics dot uc dot edu
` (10 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: Andrew Pinski @ 2006-07-24 22:00 UTC (permalink / raw)
To: gcc-bugzilla; +Cc: gcc-bugs
>
> When compiling with -O or greater optimization, and if x > INT_MAX, then code
> like this:
> uint64_t y = (uint64_t)round(x);
> assigns the wrong value to y (the top 32 bits are all 1s). But this code
> assigns the right value to z:
> double dz = round(x);
> uint64_t z = dz;
>
> It almost seems as if gcc -O in some cases compiles using a built-in
> declaration of round() that returns a 32-bit int.
Yes this is wrong code, GCC is generating for some reason lround instead of
llround.
Thanks,
Andrew Pinski
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug c/28473] with -O, casting result of round(x) to uint64_t produces wrong values for x > INT_MAX
2006-07-24 20:53 [Bug c/28473] New: with -O, casting result of round(x) to uint64_t produces wrong values for x > INT_MAX maxp at alum dot mit dot edu
2006-07-24 20:54 ` [Bug c/28473] " maxp at alum dot mit dot edu
2006-07-24 22:00 ` [Bug c/28473] New: " Andrew Pinski
@ 2006-07-24 22:01 ` pinskia at physics dot uc dot edu
2006-07-25 2:12 ` pinskia at gcc dot gnu dot org
` (9 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: pinskia at physics dot uc dot edu @ 2006-07-24 22:01 UTC (permalink / raw)
To: gcc-bugs
------- Comment #2 from pinskia at physics dot uc dot edu 2006-07-24 22:01 -------
Subject: Re: New: with -O, casting result of round(x) to uint64_t produces
wrong values for x > INT_MAX
>
> When compiling with -O or greater optimization, and if x > INT_MAX, then code
> like this:
> uint64_t y = (uint64_t)round(x);
> assigns the wrong value to y (the top 32 bits are all 1s). But this code
> assigns the right value to z:
> double dz = round(x);
> uint64_t z = dz;
>
> It almost seems as if gcc -O in some cases compiles using a built-in
> declaration of round() that returns a 32-bit int.
Yes this is wrong code, GCC is generating for some reason lround instead of
llround.
Thanks,
Andrew Pinski
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28473
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug c/28473] with -O, casting result of round(x) to uint64_t produces wrong values for x > INT_MAX
2006-07-24 20:53 [Bug c/28473] New: with -O, casting result of round(x) to uint64_t produces wrong values for x > INT_MAX maxp at alum dot mit dot edu
` (2 preceding siblings ...)
2006-07-24 22:01 ` [Bug c/28473] " pinskia at physics dot uc dot edu
@ 2006-07-25 2:12 ` pinskia at gcc dot gnu dot org
2006-07-25 8:46 ` [Bug middle-end/28473] [4.0/4.1/4.2 Regression] " rguenth at gcc dot gnu dot org
` (8 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2006-07-25 2:12 UTC (permalink / raw)
To: gcc-bugs
------- Comment #3 from pinskia at gcc dot gnu dot org 2006-07-25 02:12 -------
Hmm, this works correctly on powerpc-darwin, where we get no round function at
all.
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28473
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug middle-end/28473] [4.0/4.1/4.2 Regression] with -O, casting result of round(x) to uint64_t produces wrong values for x > INT_MAX
2006-07-24 20:53 [Bug c/28473] New: with -O, casting result of round(x) to uint64_t produces wrong values for x > INT_MAX maxp at alum dot mit dot edu
` (3 preceding siblings ...)
2006-07-25 2:12 ` pinskia at gcc dot gnu dot org
@ 2006-07-25 8:46 ` rguenth at gcc dot gnu dot org
2006-07-25 8:58 ` rguenth at gcc dot gnu dot org
` (7 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2006-07-25 8:46 UTC (permalink / raw)
To: gcc-bugs
------- Comment #4 from rguenth at gcc dot gnu dot org 2006-07-25 08:46 -------
Confirmed. Testcase:
typedef unsigned uint64_t __attribute__((mode(DI)));
uint64_t foo(void)
{
uint64_t x = 2147483648ULL; /* INT_MAX+1 */
uint64_t y = (uint64_t)__builtin_round(x);
return y;
}
where we fold it to
uint64_t y = (long long unsigned int) __builtin_lround ((double) x);
--
rguenth at gcc dot gnu dot org changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |rguenth at gcc dot gnu dot
| |org
Status|UNCONFIRMED |NEW
Component|c |middle-end
Ever Confirmed|0 |1
Keywords| |wrong-code
Known to work| |3.4.6
Last reconfirmed|0000-00-00 00:00:00 |2006-07-25 08:46:28
date| |
Summary|with -O, casting result of |[4.0/4.1/4.2 Regression]
|round(x) to uint64_t |with -O, casting result of
|produces wrong values for x |round(x) to uint64_t
|> INT_MAX |produces wrong values for x
| |> INT_MAX
Target Milestone|--- |4.0.4
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28473
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug middle-end/28473] [4.0/4.1/4.2 Regression] with -O, casting result of round(x) to uint64_t produces wrong values for x > INT_MAX
2006-07-24 20:53 [Bug c/28473] New: with -O, casting result of round(x) to uint64_t produces wrong values for x > INT_MAX maxp at alum dot mit dot edu
` (4 preceding siblings ...)
2006-07-25 8:46 ` [Bug middle-end/28473] [4.0/4.1/4.2 Regression] " rguenth at gcc dot gnu dot org
@ 2006-07-25 8:58 ` rguenth at gcc dot gnu dot org
2006-07-25 20:03 ` roger at eyesopen dot com
` (6 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2006-07-25 8:58 UTC (permalink / raw)
To: gcc-bugs
------- Comment #5 from rguenth at gcc dot gnu dot org 2006-07-25 08:58 -------
I have a patch.
--
rguenth at gcc dot gnu dot org changed:
What |Removed |Added
----------------------------------------------------------------------------
AssignedTo|unassigned at gcc dot gnu |rguenth at gcc dot gnu dot
|dot org |org
Status|NEW |ASSIGNED
Last reconfirmed|2006-07-25 08:46:28 |2006-07-25 08:58:05
date| |
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28473
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug middle-end/28473] [4.0/4.1/4.2 Regression] with -O, casting result of round(x) to uint64_t produces wrong values for x > INT_MAX
2006-07-24 20:53 [Bug c/28473] New: with -O, casting result of round(x) to uint64_t produces wrong values for x > INT_MAX maxp at alum dot mit dot edu
` (5 preceding siblings ...)
2006-07-25 8:58 ` rguenth at gcc dot gnu dot org
@ 2006-07-25 20:03 ` roger at eyesopen dot com
2006-07-25 20:08 ` roger at eyesopen dot com
` (5 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: roger at eyesopen dot com @ 2006-07-25 20:03 UTC (permalink / raw)
To: gcc-bugs
------- Comment #6 from roger at eyesopen dot com 2006-07-25 20:02 -------
Grr. I've just noticed richi has just assigned this patch to himself.
I also have a patch that been bootstrapped and has nearly finished
regression testing, that I was just about to post/commit. richi what
does your fix look like?
Mine contains several copies of:
if (!TARGET_C99_FUNCTIONS)
break;
! if (outprec < TYPE_PRECISION (long_integer_type_node)
! || (outprec == TYPE_PRECISION (long_integer_type_node)
! && !TYPE_UNSIGNED (type)))
fn = mathfn_built_in (s_intype, BUILT_IN_LCEIL);
+ else if (outprec == TYPE_PRECISION (long_long_integer_type_node)
+ && !TYPE_UNSIGNED (type))
+ fn = mathfn_built_in (s_intype, BUILT_IN_LLCEIL);
break;
[Serves me right for not assigning this when pinkia asked me to investigate.
I knew there was a good reason I don't normally bother with recent PRs].
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28473
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug middle-end/28473] [4.0/4.1/4.2 Regression] with -O, casting result of round(x) to uint64_t produces wrong values for x > INT_MAX
2006-07-24 20:53 [Bug c/28473] New: with -O, casting result of round(x) to uint64_t produces wrong values for x > INT_MAX maxp at alum dot mit dot edu
` (6 preceding siblings ...)
2006-07-25 20:03 ` roger at eyesopen dot com
@ 2006-07-25 20:08 ` roger at eyesopen dot com
2006-07-25 23:22 ` sayle at gcc dot gnu dot org
` (4 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: roger at eyesopen dot com @ 2006-07-25 20:08 UTC (permalink / raw)
To: gcc-bugs
------- Comment #7 from roger at eyesopen dot com 2006-07-25 20:08 -------
Ahh, I've just found the Richard's patch submission posting at
http://gcc.gnu.org/ml/gcc-patches/2006-07/msg01065.html
I agree with Andrew Pinski, I think my changes are the better fix.
We also need to investigate wether (unsigned int)round(x) is better
implemented as (unsigned int)llround(x). For the time being, my
patch doesn't perform this transformation, and using lround is unsafe.
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28473
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug middle-end/28473] [4.0/4.1/4.2 Regression] with -O, casting result of round(x) to uint64_t produces wrong values for x > INT_MAX
2006-07-24 20:53 [Bug c/28473] New: with -O, casting result of round(x) to uint64_t produces wrong values for x > INT_MAX maxp at alum dot mit dot edu
` (7 preceding siblings ...)
2006-07-25 20:08 ` roger at eyesopen dot com
@ 2006-07-25 23:22 ` sayle at gcc dot gnu dot org
2006-07-26 0:24 ` [Bug middle-end/28473] [4.0/4.1 " pinskia at gcc dot gnu dot org
` (3 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: sayle at gcc dot gnu dot org @ 2006-07-25 23:22 UTC (permalink / raw)
To: gcc-bugs
------- Comment #8 from sayle at gcc dot gnu dot org 2006-07-25 23:22 -------
Subject: Bug 28473
Author: sayle
Date: Tue Jul 25 23:21:56 2006
New Revision: 115742
URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=115742
Log:
PR middle-end/28473
* convert.c (convert_to_integer): When transforming (T)foo(x) into
bar(x) check that bar's result type can represent all the values of T.
* gcc.dg/fold-convround-1.c: New test case.
Added:
trunk/gcc/testsuite/gcc.dg/fold-convround-1.c
Modified:
trunk/gcc/ChangeLog
trunk/gcc/convert.c
trunk/gcc/testsuite/ChangeLog
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28473
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug middle-end/28473] [4.0/4.1 Regression] with -O, casting result of round(x) to uint64_t produces wrong values for x > INT_MAX
2006-07-24 20:53 [Bug c/28473] New: with -O, casting result of round(x) to uint64_t produces wrong values for x > INT_MAX maxp at alum dot mit dot edu
` (8 preceding siblings ...)
2006-07-25 23:22 ` sayle at gcc dot gnu dot org
@ 2006-07-26 0:24 ` pinskia at gcc dot gnu dot org
2006-07-29 19:08 ` sayle at gcc dot gnu dot org
` (2 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2006-07-26 0:24 UTC (permalink / raw)
To: gcc-bugs
------- Comment #9 from pinskia at gcc dot gnu dot org 2006-07-26 00:24 -------
Fixed on the mainline at least.
--
pinskia at gcc dot gnu dot org changed:
What |Removed |Added
----------------------------------------------------------------------------
Known to work|3.4.6 |3.4.6 4.2.0
Summary|[4.0/4.1/4.2 Regression] |[4.0/4.1 Regression] with -
|with -O, casting result of |O, casting result of
|round(x) to uint64_t |round(x) to uint64_t
|produces wrong values for x |produces wrong values for x
|> INT_MAX |> INT_MAX
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28473
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug middle-end/28473] [4.0/4.1 Regression] with -O, casting result of round(x) to uint64_t produces wrong values for x > INT_MAX
2006-07-24 20:53 [Bug c/28473] New: with -O, casting result of round(x) to uint64_t produces wrong values for x > INT_MAX maxp at alum dot mit dot edu
` (9 preceding siblings ...)
2006-07-26 0:24 ` [Bug middle-end/28473] [4.0/4.1 " pinskia at gcc dot gnu dot org
@ 2006-07-29 19:08 ` sayle at gcc dot gnu dot org
2006-07-30 13:22 ` [Bug middle-end/28473] [4.0 " sayle at gcc dot gnu dot org
2006-07-30 13:31 ` pinskia at gcc dot gnu dot org
12 siblings, 0 replies; 14+ messages in thread
From: sayle at gcc dot gnu dot org @ 2006-07-29 19:08 UTC (permalink / raw)
To: gcc-bugs
------- Comment #10 from sayle at gcc dot gnu dot org 2006-07-29 19:07 -------
Subject: Bug 28473
Author: sayle
Date: Sat Jul 29 19:07:40 2006
New Revision: 115812
URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=115812
Log:
PR middle-end/28473
Backport from mainline.
* convert.c (convert_to_integer): When transforming (T)foo(x) into
bar(x) check that bar's result type can represent all the values of T.
* builtins.c (fold_fixed_mathfn): When long and long long are the
same size, canonicalize llceil*, llfloor*, llround* and llrint*
functions to their lceil*, lfloor*, lround* and lrint* forms.
* gcc.dg/fold-convround-1.c: New test case.
* gcc.dg/builtins-55.c: New test case.
Added:
branches/gcc-4_1-branch/gcc/testsuite/gcc.dg/builtins-55.c
branches/gcc-4_1-branch/gcc/testsuite/gcc.dg/fold-convround-1.c
Modified:
branches/gcc-4_1-branch/gcc/ChangeLog
branches/gcc-4_1-branch/gcc/builtins.c
branches/gcc-4_1-branch/gcc/convert.c
branches/gcc-4_1-branch/gcc/testsuite/ChangeLog
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28473
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug middle-end/28473] [4.0 Regression] with -O, casting result of round(x) to uint64_t produces wrong values for x > INT_MAX
2006-07-24 20:53 [Bug c/28473] New: with -O, casting result of round(x) to uint64_t produces wrong values for x > INT_MAX maxp at alum dot mit dot edu
` (10 preceding siblings ...)
2006-07-29 19:08 ` sayle at gcc dot gnu dot org
@ 2006-07-30 13:22 ` sayle at gcc dot gnu dot org
2006-07-30 13:31 ` pinskia at gcc dot gnu dot org
12 siblings, 0 replies; 14+ messages in thread
From: sayle at gcc dot gnu dot org @ 2006-07-30 13:22 UTC (permalink / raw)
To: gcc-bugs
------- Comment #11 from sayle at gcc dot gnu dot org 2006-07-30 13:22 -------
Subject: Bug 28473
Author: sayle
Date: Sun Jul 30 13:21:59 2006
New Revision: 115821
URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=115821
Log:
PR middle-end/28473
Backport from mainline.
* convert.c (convert_to_integer): When transforming (T)foo(x) into
bar(x) check that bar's result type can represent all the values of T.
* builtins.c (fold_fixed_mathfn): When long and long long are the
same size, canonicalize llceil*, llfloor*, llround* and llrint*
functions to their lceil*, lfloor*, lround* and lrint* forms.
* gcc.dg/fold-convround-1.c: New test case.
* gcc.dg/builtins-55.c: New test case.
Added:
branches/gcc-4_0-branch/gcc/testsuite/gcc.dg/builtins-55.c
branches/gcc-4_0-branch/gcc/testsuite/gcc.dg/fold-convround-1.c
Modified:
branches/gcc-4_0-branch/gcc/ChangeLog
branches/gcc-4_0-branch/gcc/builtins.c
branches/gcc-4_0-branch/gcc/convert.c
branches/gcc-4_0-branch/gcc/testsuite/ChangeLog
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28473
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug middle-end/28473] [4.0 Regression] with -O, casting result of round(x) to uint64_t produces wrong values for x > INT_MAX
2006-07-24 20:53 [Bug c/28473] New: with -O, casting result of round(x) to uint64_t produces wrong values for x > INT_MAX maxp at alum dot mit dot edu
` (11 preceding siblings ...)
2006-07-30 13:22 ` [Bug middle-end/28473] [4.0 " sayle at gcc dot gnu dot org
@ 2006-07-30 13:31 ` pinskia at gcc dot gnu dot org
12 siblings, 0 replies; 14+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2006-07-30 13:31 UTC (permalink / raw)
To: gcc-bugs
------- Comment #12 from pinskia at gcc dot gnu dot org 2006-07-30 13:31 -------
Fixed.
--
pinskia at gcc dot gnu dot org changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|ASSIGNED |RESOLVED
Resolution| |FIXED
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28473
^ permalink raw reply [flat|nested] 14+ messages in thread