# GCC 中的整数转换

Posted by w@hidva.com on December 1, 2019

A value of any integer type can be implicitly converted to any other integer type. Except where covered by promotions and boolean conversions above, the rules are:

• if the target type can represent the value, the value is unchanged
• otherwise, if the target type is unsigned, the value 2^b , where b is the number of bits in the target type, is repeatedly subtracted or added to the source value until the result fits in the target type. In other words, unsigned integers implement modulo arithmetic.
• otherwise, if the target type is signed, the behavior is implementation-defined (which may include raising a signal)

  {
convert_optab ctab;

if (GET_MODE_PRECISION (from_mode) > GET_MODE_PRECISION (to_mode))
ctab = trunc_optab;
else if (unsignedp)
ctab = zext_optab;
else
ctab = sext_optab;

if (convert_optab_handler (ctab, to_mode, from_mode)
!= CODE_FOR_nothing)
{
emit_unop_insn (convert_optab_handler (ctab, to_mode, from_mode),
to, from, UNKNOWN);
return;
}
}


GNU 关于 C 的代码风格很是奇怪…