ZHCU876Z July 2001 – October 2023 SM320F28335-EP
如果使用 --idiv_support=idiv0 命令行选项,编译器将在使用除法 ("/") 或模 ("%") 运算符,或者 div() 或 ldiv() 函数时生成更快的指令来执行整数除法。无论是否包含 stdlib.h
头文件,都将使用这些内置运算符的更快速版本。
下表显示了使用除法 ("/") 和模 ("%") 运算符执行整数运算时用到的内在函数。
操作的类型 | 等效的内在函数调用 |
---|---|
int/int | __traditional_div_i16byi16(int, int).quot |
int % int | __traditional_div_i16byi16(int, int).rem |
unsigned int/unsigned int | __traditional_div_u16byu16(unsigned int, unsigned int).quot |
unsigned int % unsigned int | __traditional_div_u16byu16(unsigned int, unsigned int).rem |
long/long | __traditional_div_i32byi32(long, long).quot |
long % long | __traditional_div_i32byi32(long, long).rem |
unsigned long/unsigned long | __traditional_div_u32byu32(unsigned long, unsigned long).quot |
unsigned long % unsigned long | __traditional_div_u32byu32(unsigned long, unsigned long).rem |
long long/long long | __traditional_div_i64byi64(long long, long long).quot |
long long % long long | __traditional_div_i64byi64(long long, long long).rem |
unsigned long long/unsigned long long | __traditional_div_u64byu64(unsigned long long, unsigned long long).quot |
unsigned long long % unsigned long long | __traditional_div_u64byu64(unsigned long long, unsigned long long).rem |
节 7.6.4中列出了使您能够更准确地指定要执行的运算的内在函数。
在 C 语言中,当整数除法或模数运算的操作数具有不同的类型时,编译器会自动执行“整型提升”(也被称为隐式类型转换)。也就是说,编译器会插入隐式转换以转换为公共类型,然后在该类型中执行运算。
根据所划分的类型,所执行的内在函数可能不同于预期的内在函数。例如:
long dividend_i32, quotient_i32;
unsigned long divisor_u32, quotient_u32;
int divisor_i16;
/* uses __traditional_div_u32byu32, not __traditional_div_i32byu32 */
quotient_u32 = dividend_i32 / divisor_u32;
/* uses __traditional_div_i32byi32, not __traditional_div_i32byi16 */
quotient_i32 = dividend_i32 / divisor_i16;
整型提升可能会造成混淆,因此最好通过确保操作数类型一致来避免此问题,可能需要使用强制转换运算符。有关整型提升的更多信息,请参阅如何在 C 代码中正确编写乘法 (SPRA683)。