Option to make unsigned->signed conversion always well-defined?

Jeremy Hall gcc.hall@gmail.com
Thu Oct 6 18:24:00 GMT 2011


Instead of all the clever bit twiddling I have used code similar to

   sum > UINT8_MAX

which just generates

      cmp  ax,255
      seta  al

which seems to be far more efficient (even the signed version gets optimized
down to the above single check).
Please could someone tell me if I have missed something here????

Signed check:

bool overflow(int16_t a, int16_t b)
   const int16_t sum = a + b;
   return sum > INT8_MAX || sum < INT8_MIN;

Unsigned check

bool overflow(uint16_t a, uint16_t b)
   const uint16_t sum = a + b;
   return sum > UINT8_MAX;


More information about the Gcc mailing list