On Fri, Jun 17, 2011 at 12:56 PM, Paul Koning <paul_kon...@dell.com> wrote:
>
> On Jun 17, 2011, at 5:26 AM, Richard Guenther wrote:
>
>> On Fri, Jun 17, 2011 at 11:14 AM, Bingfeng Mei <b...@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.
>
> But how did it end up with ...(int)a ... when the source declares a as short?

That's what the C standard does to your expressions - integral promotions.

Richard.

Reply via email to