This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Option to make unsigned->signed conversion always well-defined?
- From: Jeremy Hall <gcc dot hall at gmail dot com>
- To: gcc at gcc dot gnu dot org
- Date: Thu, 6 Oct 2011 16:23:39 +0100
- Subject: Re: Option to make unsigned->signed conversion always well-defined?
Hi.
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;
}
Jeremy