我想知道,当两个整数相乘并且结果是类型转换为short并指定为short时,编译器会将它解析为什么?以下是代码段
int a=1,b=2,c;
short x=3,y=4,z;
int p;
short q;
int main()
{
c = a*b; /* Mul two ints and assign to int
[compiler resolves this to __mulsi3()] */
z = x*y; /* Mul two short and assign to short
[compiler resolves this to __mulhi3()] */
p = (x*y); /* Mul two short and assign to int
[compiler resolves this to __mulsi3()] */
q =(short)(a*b); /* Mul two ints typecast to short and assign to short
[compiler resolves this to __mulhi3()] */
return 0;
}
在q =(short)(a*b);
的情况下,应首先进行两次整数乘法(使用__mulsi3()
),然后将其指定为short。但在这种情况并非如此,编译器类型将a
和b
强制转换为short,然后调用__mulhi3()
。
我想知道如何更改gcc源代码[哪个文件],这样我才能达到上述要求。
答案 0 :(得分:0)
编译器可以分析代码并查看当您将结果立即转换为short
时,多重复制可以作为short
乘法进行,而不会影响结果。这与您的示例中的情况二完全相同。
结果相同,您不必担心使用哪个乘法函数。