字符是否在 C 表达式中自动提升?
Are chars automatically promoted in C expressions?
我对我的一位同事发表了声明,内容是:
"chars are automatically promoted to integers in C expressions, and that's fine for performance since CPUs work fastest with their natural word size.
我相信由于字符的排名,字符提升行为在标准中的某处有所说明。
这是我得到的回复:
"Characters are not default promoted to an integer. The register size
is 32 bit, but multiple byte values in a row can be packed into a
single register as a compiler implementation. This is not always
predictive. The only time you can verify automatic promotion is when
the type is passed into the call stack when not wrapped around a
structure because C standard officially needs 32-bit values in the
call stack memory. A great deal of CPU architectures have optimized
assembly calls for non-32 bit values, so no assumptions can be made
about the CPU or compiler in this case."
我不确定谁是对的,也不知道该相信什么。事实是什么?
C 不需要堆栈或指定有关 32 位寄存器的任何内容。
整数提升的一个基本原理如CERT所说:
Integer promotions are performed to avoid arithmetic errors resulting from the overflow of intermediate values. For example:
signed char cresult, c1, c2, c3;
c1 = 100;
c2 = 3;
c3 = 4;
cresult = c1 * c2 / c3;
请注意,并非所有运算符都会使其参数成为通常算术转换的主题,例如,赋值运算符或强制转换运算符没有整数提升。
是的,具有多个 char
的表达式,例如加法 etc.etc。 (但不是逗号运算符之类的东西)和其他一些东西是在提升的值(提升为 int
)上完成的。见例如。 N3797,§4.5
关于你同事的说法,里面有很多错误的地方:
一个"registry"(寄存器)大小是不是一般是32位的,根本不是。
如果一个字节有8位,当然一个32位的寄存器可以容纳多个字节,
但这无关紧要,编译器不是可能的原因。
这个"predictive"呢?
关于标准和32位的说法是完全错误的。
整数提升与struct
无关
标准中没有"stack"。即概念
在现实中使用堆栈并不是强制性的(正如其他人所说)。
他说一切都需要 32 位,但作为 CPU
也可以加工其他尺寸,不能确定吗?现在怎么办?
...
chars are automatically promoted to integers in C expressions
是的,他们是。 C99 第 6.3.1.8 节,常规算术转换:
Many operators that expect operands of arithmetic type cause conversions and yield result
types in a similar way. The purpose is to determine a common real type for the operands
and result. For the specified operands, each operand is converted, without change of type
domain, to a type whose corresponding real type is the common real type. Unless
explicitly stated otherwise, the common real type is also the corresponding real type of
the result, whose type domain is the type domain of the operands if they are the same,
and complex otherwise. This pattern is called the usual arithmetic conversions:
- First, if the corresponding real type of either operand is long double, the other
operand is converted, without change of type domain, to a type whose corresponding real type is long double.
- Otherwise, if the corresponding real type of either operand is double, the other
operand is converted, without change of type domain, to a type whose
corresponding real type is double.
- Otherwise, if the corresponding real type of either operand is float, the other
operand is converted, without change of type domain, to a type whose
corresponding real type is float.62)
- Otherwise, the integer promotions are performed on both operands. Then the
following rules are applied to the promoted operands:
- If both operands have the same type, then no further conversion is needed.
- Otherwise, if both operands have signed integer types or both have unsigned
integer types, the operand with the type of lesser integer conversion rank is
converted to the type of the operand with greater rank.
- Otherwise, if the operand that has unsigned integer type has rank greater or
equal to the rank of the type of the other operand, then the operand with
signed integer type is converted to the type of the operand with unsigned
integer type.
- Otherwise, if the type of the operand with signed integer type can represent
all of the values of the type of the operand with unsigned integer type, then
the operand with unsigned integer type is converted to the type of the
operand with signed integer type.
- Otherwise, both operands are converted to the unsigned integer type
corresponding to the type of the operand with signed integer type.
整数提升在第 6.3.1.1.2 节中有描述:
The following may be used in an expression wherever an int or unsigned
int may be used:
- An object or expression with an integer type whose integer conversion rank is less than or equal to the rank of int and unsigned
int.
- A bit-field of type _Bool, int, signed int, or unsigned int
If an int can represent all values of the original type, the value is
converted to an int; otherwise, it is converted to an unsigned int.
These are called the integer promotions. All other types are unchanges
by the integer promotions.
char
的排名小于或等于int
的排名,所以char
被包含在这里。
(作为脚注,提到整数提升仅作为通常算术转换的一部分应用于某些参数表达式、一元 +
、-
和 -
的操作数~
,以及移位运算符的两个操作数)。
如评论中所述,整数提升也对函数调用参数执行。
从逻辑上讲,是的,所有操作都是在提升的值上执行的。但是,在 as-if 规则下,可以证明结果相同的编译器可能会选择省略实际的提升。平凡地 if (ch==0)
需要将 ch
提升为 int
,但实际上根本不需要这样做。当且仅 ch
为零时,优化器可以简单地看到 (int)ch
为零。
因此,实际的 CPU 性能和不同的 CPU 风格对性能的影响没有您想象的那么大;关键在于优化器能否找到合适的指令集。
我对我的一位同事发表了声明,内容是:
"chars are automatically promoted to integers in C expressions, and that's fine for performance since CPUs work fastest with their natural word size.
我相信由于字符的排名,字符提升行为在标准中的某处有所说明。
这是我得到的回复:
"Characters are not default promoted to an integer. The register size is 32 bit, but multiple byte values in a row can be packed into a single register as a compiler implementation. This is not always predictive. The only time you can verify automatic promotion is when the type is passed into the call stack when not wrapped around a structure because C standard officially needs 32-bit values in the call stack memory. A great deal of CPU architectures have optimized assembly calls for non-32 bit values, so no assumptions can be made about the CPU or compiler in this case."
我不确定谁是对的,也不知道该相信什么。事实是什么?
C 不需要堆栈或指定有关 32 位寄存器的任何内容。
整数提升的一个基本原理如CERT所说:
Integer promotions are performed to avoid arithmetic errors resulting from the overflow of intermediate values. For example:
signed char cresult, c1, c2, c3;
c1 = 100;
c2 = 3;
c3 = 4;
cresult = c1 * c2 / c3;
请注意,并非所有运算符都会使其参数成为通常算术转换的主题,例如,赋值运算符或强制转换运算符没有整数提升。
是的,具有多个 char
的表达式,例如加法 etc.etc。 (但不是逗号运算符之类的东西)和其他一些东西是在提升的值(提升为 int
)上完成的。见例如。 N3797,§4.5
关于你同事的说法,里面有很多错误的地方:
一个"registry"(寄存器)大小是不是一般是32位的,根本不是。
如果一个字节有8位,当然一个32位的寄存器可以容纳多个字节,
但这无关紧要,编译器不是可能的原因。这个"predictive"呢?
关于标准和32位的说法是完全错误的。
整数提升与
struct
无关标准中没有"stack"。即概念
在现实中使用堆栈并不是强制性的(正如其他人所说)。他说一切都需要 32 位,但作为 CPU
也可以加工其他尺寸,不能确定吗?现在怎么办?
...
chars are automatically promoted to integers in C expressions
是的,他们是。 C99 第 6.3.1.8 节,常规算术转换:
Many operators that expect operands of arithmetic type cause conversions and yield result types in a similar way. The purpose is to determine a common real type for the operands and result. For the specified operands, each operand is converted, without change of type domain, to a type whose corresponding real type is the common real type. Unless explicitly stated otherwise, the common real type is also the corresponding real type of the result, whose type domain is the type domain of the operands if they are the same, and complex otherwise. This pattern is called the usual arithmetic conversions:
- First, if the corresponding real type of either operand is long double, the other operand is converted, without change of type domain, to a type whose corresponding real type is long double.
- Otherwise, if the corresponding real type of either operand is double, the other operand is converted, without change of type domain, to a type whose corresponding real type is double.
- Otherwise, if the corresponding real type of either operand is float, the other operand is converted, without change of type domain, to a type whose corresponding real type is float.62)
- Otherwise, the integer promotions are performed on both operands. Then the following rules are applied to the promoted operands:
- If both operands have the same type, then no further conversion is needed.
- Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank.
- Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type.
- Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, then the operand with unsigned integer type is converted to the type of the operand with signed integer type.
- Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type.
整数提升在第 6.3.1.1.2 节中有描述:
The following may be used in an expression wherever an int or unsigned int may be used:
- An object or expression with an integer type whose integer conversion rank is less than or equal to the rank of int and unsigned int.
- A bit-field of type _Bool, int, signed int, or unsigned int
If an int can represent all values of the original type, the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions. All other types are unchanges by the integer promotions.
char
的排名小于或等于int
的排名,所以char
被包含在这里。
(作为脚注,提到整数提升仅作为通常算术转换的一部分应用于某些参数表达式、一元 +
、-
和 -
的操作数~
,以及移位运算符的两个操作数)。
如评论中所述,整数提升也对函数调用参数执行。
从逻辑上讲,是的,所有操作都是在提升的值上执行的。但是,在 as-if 规则下,可以证明结果相同的编译器可能会选择省略实际的提升。平凡地 if (ch==0)
需要将 ch
提升为 int
,但实际上根本不需要这样做。当且仅 ch
为零时,优化器可以简单地看到 (int)ch
为零。
因此,实际的 CPU 性能和不同的 CPU 风格对性能的影响没有您想象的那么大;关键在于优化器能否找到合适的指令集。