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?
* Ulf Magnusson:
> I've been experimenting with different methods for emulating the
> signed overflow of an 8-bit CPU. The method I've found that seems to
> generate the most efficient code on both ARM and x86 is
>
> bool overflow(unsigned int a, unsigned int b) {
> const unsigned int sum = (int8_t)a + (int8_t)b;
> return (int8_t)sum != sum;
> }
There's a GCC extension which is relevant here:
| For conversion to a type of width N, the value is reduced modulo 2^N
| to be within range of the type; no signal is raised.
<http://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html#Integers-implementation>
Using that, you can replace the final "& 0x80" with a signed
comparison to zero, which should be give you the best possible code
(for the generic RISC). You only need to hunt down a copy of Hacker's
Delight or find the right bit twiddling by other means. 8-)