Re: GCC casts short to int

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Leonitis wrote:
> I have a problem with GCC casting my short variables to integers.
> 
> My code snippet is:
>   short a = 0;
>   short b = 0;
> 
>   int count;
> 
>   for(count = 0; count < cycles; count++, a++, b++)
>   {
>     c += (short)a * (short)b;
>   }
> 
> However, the instructions produced by this code (for an ARM processor)
> include:
>   mla  r1[c], r3[a], r3[b], r1[c]                 (multiply accumulate)
> 
> However, this instruction takes 32-bit inputs and so when a and b overflow,
> they are treated as integers and not shorts.
> 
> Isn't this technically an incorrect compilation of the code, when I
> explicitly (and redundantly) cast a and b to shorts?

No, it's not incorrect.  In C, integer overflow is undefined
behaviour, so a compiler is allowed to do this.

> What I want GCC to do is left-shift, right-shift the values to make sure
> that the short variables remain as short variables.

If there's some way you can use unsigned arithmetic I'd do that.

Andrew.

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux