This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Why does GCC convert short operation to short unsigned?


On Fri, Jun 17, 2011 at 11:14 AM, Bingfeng Mei <bmei@broadcom.com> wrote:
> Hi,
> I noticed that GCC converts short arithmetic to unsigned short.
>
> short foo2 (short a, short b)
> {
> ?return a - b;
> }
>
> In .gimple file:
>
> foo2 (short int a, short int b)
> {
> ?short int D.3347;
> ?short unsigned int a.0;
> ?short unsigned int b.1;
> ?short unsigned int D.3350;
>
> ?a.0 = (short unsigned int) a;
> ?b.1 = (short unsigned int) b;
> ?D.3350 = a.0 - b.1;
> ?D.3347 = (short int) D.3350;
> ?return D.3347;
> }
>
> Is this for some C standard conformance, or optimization purpose?
> This doesn't happen with int type.

GCC optimizes (short)((int)a - (int)b) (which is what your source code example
does) to (short)((unsigned short)a - (unsigned short)b) because it
cannot shorten it to use signed short as the operation may overflow in
that type which would result in undefined behavior.  unsigned types
do not suffer from this problem.

Richard.

> Thanks,
> Bingfeng Mei
>
>


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]