http://gcc.gnu.org/bugzilla/show_bug.cgi?id=61029
Bug ID: 61029
Summary: Bogus conversion warnings are issued on a sign change
"a = -a;" operation
Product: gcc
Version: 4.8.1
Status: UNCONFIRMED
Severity: trivial
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: dchichkov at gmail dot com
The following code, if compiled with '-Wconversion' with GCC 4.8.1:
// gcc conv.c -Wconversion
int main() {
short int a = 1;
a = -a;
return a;
}
Will produce a warning:
conv.c: In function ‘main’:
conv.c:3:9: warning: conversion to ‘short int’ from ‘int’ may alter its value
[-Wconversion]
a = -a;
^
Which doesn't seem to be correct. As far as I understand it, in my target
architecture (x64) this operation is only supposed to flip the sign bit. And
not do anything else. Which in fact it does not, as it does a complete bogus,
using movzwls and "negl %eax" rather than more appropriate "neg %ax" (is it
available in x64?):
2:conv.c **** short int a = 1;
16 .loc 1 2 0
17 0004 66C745FE movw $1, -2(%rbp)
17 0100
3:conv.c **** a = -a;
18 .loc 1 3 0
19 000a 0FB745FE movzwl -2(%rbp), %eax
20 000e F7D8 negl %eax
21 0010 668945FE movw %ax, -2(%rbp)
4:conv.c **** return a;
22 .loc 1 4 0
23 0014 0FBF45FE movswl -2(%rbp), %eax
5:conv.c **** }
But that's besides the point. Clearly "a = -a;" should not generate a warning.