230-cg-shr-mixed-sign.c (866B)
1 // tests/cc/230-cg-shr-mixed-sign.c — `signed >> unsigned` must use 2 // arithmetic (sign-preserving) shift, since C uses the promoted left 3 // operand's type as the result type. cg-arith-conv currently relabels 4 // the signed lhs to unsigned when rhs is unsigned of equal/greater 5 // rank, causing cg-binop to emit a logical shift. The wrong high bits 6 // only surface when the result is widened to a 64-bit type without 7 // narrowing through a 32-bit lvalue. Returns 0 when the compiler is 8 // correct. 9 int main(void) { 10 int x = -8; 11 unsigned int y = 1; 12 long long r = (long long)(x >> y); 13 if (r != -4) return 1; 14 15 // Compound form via >>=: assignment back into an int truncates, 16 // so widen by reading through a long long. 17 int s = -16; 18 unsigned int t = 2; 19 long long r2 = (long long)(s >> t); 20 if (r2 != -4) return 2; 21 22 return 0; 23 }