Are chars automatically promoted in C expressions?

拥有回忆 提交于 2021-02-07 19:16:20

问题


I made a statement to a colleague of mine, which was:

"chars are automatically promoted to integers in C expressions, and that's fine for performance since CPUs work fastest with their natural word size.

I believe char promotion behavior is stated somewhere in the standard due to a char's rank.

This is the response I got back:

"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."

I'm not sure who is right, and what to believe. What are the facts?


回答1:


chars are automatically promoted to integers in C expressions

Yes, they are. C99 section 6.3.1.8, Usual arithmetic conversions:

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.

Integer promotions are described on Section 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.

The rank of a char is less than or equal to that of an int, so char is included in here.

(As a footnote, it is mentioned that integer promotions are only applied as part of the usual arithmetic conversions, to certain argument expressions, to the operands of the unary +, - and ~, and to both operands of the shift operators).

As mentioned in the comments, integer promotion is also performed on function-call arguments.




回答2:


Yes, expressions with multiple chars, like addition etc.etc. (but not stuff like the comma operator), and some other things, are done on promoted values (promoted to int). See eg. N3797, §4.5

About the statement of your colleague, there are many wrong things in it:

  • A "registry" (register) size is not generally 32 bit, not at all.

  • If a byte has 8 bit, of course a register with 32 bit can hold multiple bytes,
    but this isn´t relevant, and the compiler is not the reason why it is possible.

  • What about this is "predictive"?

  • The bit about the standard and 32 bit is completely wrong.

  • Integer promotion has nothing to do with struct

  • In the standard, there is no "stack". That the concept
    of a stack is used in reality is not mandatory (as others said).

  • He´s saying that everything needs to be 32 bit, but as CPUs
    could process other sizes too, nothing can be said for sure? What now?

...




回答3:


C does not require a stack or specify anything about 32-bit registers.

One of the rationale of integer promotions is as CERT put it:

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;

Note that not all operators cause their argument to be the subject of the usual arithmetic conversions, for example there is no integer promotion with the assignment operators or the cast operator.




回答4:


Logically, yes, all operations are performed on promoted values. However, under the as-if rule, a compiler that can prove results are identical may choose to omit the actual promotion. Trivially if (ch==0) would require promoting ch to int but in practice this is not needed at all. An optimizer can trivially see that (int)ch is zero if and only ch is zero.

So the actual CPU performance and the different CPU flavors matter less than you'd think for performance; it's whether the optimizer can find a decent set of instructions.



来源:https://stackoverflow.com/questions/32383507/are-chars-automatically-promoted-in-c-expressions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!