public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* warning: right shift count >= width of type
@ 2004-11-29 16:30 Dave Korn
  2004-11-29 16:33 ` Nathan Sidwell
                   ` (2 more replies)
  0 siblings, 3 replies; 19+ messages in thread
From: Dave Korn @ 2004-11-29 16:30 UTC (permalink / raw)
  To: gcc



  Afternoon all.  Here's something that's piqued my curiosity; it's probably
owing to some language-lawyerly issue, but it isn't obvious to me.  This is
on gcc-3.3.3, (cygwin variant, but that's probably not relevant):

-------------------------<snip!>-------------------------
dk@mace /test/shift-test> cat foo.c

unsigned int bar (unsigned int baz)
{
unsigned int quux;

        quux = baz >> 32;
        return quux;
}

dk@mace /test/shift-test> gcc -S foo.c -O2 -o foo.s
foo.c: In function `bar':
foo.c:7: warning: right shift count >= width of type
dk@mace /test/shift-test> cat foo.s
        .file   "foo.c"
        .text
        .p2align 4,,15
.globl _bar
        .def    _bar;   .scl    2;      .type   32;     .endef
_bar:
        pushl   %ebp
        movl    %esp, %ebp
        movl    8(%ebp), %eax
        movb    $32, %cl
        shrl    %cl, %eax
        popl    %ebp
        ret
dk@mace /test/shift-test>
-------------------------<snip!>-------------------------

  Why isn't the shift operation optimised away and replaced with const_int
0?


    cheers, 
      DaveK
-- 
Can't think of a witty .sigline today....

^ permalink raw reply	[flat|nested] 19+ messages in thread
* Re: warning: right shift count >= width of type
@ 2004-11-29 23:49 Paul Schlie
  2004-11-30  1:50 ` Andreas Schwab
  0 siblings, 1 reply; 19+ messages in thread
From: Paul Schlie @ 2004-11-29 23:49 UTC (permalink / raw)
  To: gcc

>> On Nov 29, 2004, at 10:51 AM, Andrew Haley wrote:
>>> I myself would want "(n >> 32)" to produce the same result as "((n >> 16)
>>> 16)" and indeed "for (int i = 32; i > 0; i--, n >>= 1) ;", and it seems
>>> to be generally agreed that the compiler would be at liberty to so do if
>>> it wants to.
>>
>> But you could never depend on it.  If it only works when the shift
>> count is a constant, a failure to do constant folding would break it.
>
> Or constant propagation, or inlining, or ...

Would think that if GCC wants to define an equivalence between:

 (x * <pow2>) == (x << (log2 <pow2>))

and/or

 (x / <pow2>) == (x >> (log2 <pow2>))


And generally algebraically manipulate such expressions:

 (x * <pow2-a>) * (y >> z) / <pow2-b>

=> 

 (x * y) << (+ z (- <pow2-a> <pow2-b>))
or
 (x * y) >> (+ z (- <pow2-a> <pow2-b>))


It likely needs to define what (x << z) means if:

- z lesser-or-equal-to 0, [i.e. does (x << -1) == (x >> 1) ?]

- z greater-or-equal-to bit-size-of( x * z )
  [i.e. does (x * <pow2-bits-in-x>) == (x << (log2 <pow2-bits-in-x>)) ?]

Regardless of what the "less than concise" C standard says, or the
assumption of such equivalences are fragile.





^ permalink raw reply	[flat|nested] 19+ messages in thread
* Re: warning: right shift count >= width of type
@ 2004-11-30  4:23 Paul Schlie
  0 siblings, 0 replies; 19+ messages in thread
From: Paul Schlie @ 2004-11-30  4:23 UTC (permalink / raw)
  To: gcc

> Andreas wrote;
>> Paul Schlie <schlie@comcast.net> writes:
>> ...
>> It likely needs to define what (x << z) means if:
>>
>> - z lesser-or-equal-to 0, [i.e. does (x << -1) == (x >> 1) ?]
>
> Since left and right shift are usually separate insns this would result
> in a runtime penalty.

Sorry if I wasn't clear, the statement was made within the context of
what would likely be necessary if (x * <pow2>) == (x << (log2 <pow2>), etc.
equivalence was generally assumed to algebraically simplify expressions.

As:

((x * <pow2a>) / <pow2b>) => (x << (- <pow2a> <pow2b))

would be valid if the above and previously cited were consistently defined.


^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2004-11-30  2:58 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-11-29 16:30 warning: right shift count >= width of type Dave Korn
2004-11-29 16:33 ` Nathan Sidwell
2004-11-29 17:01 ` Andrew Haley
2004-11-29 17:12 ` Dale Johannesen
2004-11-29 17:14   ` Dave Korn
2004-11-29 17:26     ` Andrew Haley
2004-11-29 18:29       ` Peter Barada
2004-11-29 18:53         ` Dave Korn
2004-11-29 18:52       ` Dave Korn
2004-11-29 19:09         ` Andrew Haley
2004-11-29 19:17           ` Dale Johannesen
2004-11-29 19:54             ` Dave Korn
2004-11-29 17:38     ` Chris Jefferson
2004-11-29 18:46       ` Dave Korn
2004-11-29 19:19         ` Chris Jefferson
2004-11-29 19:46           ` Dave Korn
2004-11-29 23:49 Paul Schlie
2004-11-30  1:50 ` Andreas Schwab
2004-11-30  4:23 Paul Schlie

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).