Warning for converting (possibly) negative float/double to unsigned int

Bradley Lucier lucier@math.purdue.edu
Fri Feb 26 21:28:00 GMT 2016


Perhaps this question is appropriate for the gcc mail list.

Converting a float/double to unsigned int is undefined if the result 
would be negative when converted to a signed int.

x86-64 and arm treat this condition differently---x86-64 returns a value 
whose bit pattern is the same as the bit pattern for converting to 
signed int, and arm returns zero.  So it would be nice to have a warning 
that this will (or could) happen.

I couldn't find such a warning in the GCC manual or in the GCC code 
base.  Looking through the code, it seemed it might go in this code in 
force_operand() in expr.c on mainline:

   if (UNARY_P (value))
     {
       if (!target)
         target = gen_reg_rtx (GET_MODE (value));
       op1 = force_operand (XEXP (value, 0), NULL_RTX);
       switch (code)
         {
         case ZERO_EXTEND:
         case SIGN_EXTEND:
         case TRUNCATE:
         case FLOAT_EXTEND:
         case FLOAT_TRUNCATE:
           convert_move (target, op1, code == ZERO_EXTEND);
           return target;

         case FIX:
         case UNSIGNED_FIX:
           expand_fix (target, op1, code == UNSIGNED_FIX);
           return target;

         case FLOAT:
         case UNSIGNED_FLOAT:
           expand_float (target, op1, code == UNSIGNED_FLOAT);
           return target;

         default:
           return expand_simple_unop (GET_MODE (value), code, op1, 
target, 0);
         }
     }

But maybe not.

Any advice on how to proceed?  I'd be willing to write and test the few 
lines of code myself if I knew where to put them.

Brad



More information about the Gcc mailing list