GCC casts short to int
Andrew Haley
aph@redhat.com
Thu Jun 25 13:46:00 GMT 2009
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.
More information about the Gcc-help
mailing list