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

Florian Weimer fw@deneb.enyo.de
Fri Oct 7 22:24:00 GMT 2011


* Ulf Magnusson:

> Are you thinking of something like this?
>
> bool overflow_bit2(unsigned int a, unsigned int b) {
>     const unsigned int ashift = a << 24;
>     const unsigned int bshift = b << 24;
>     const unsigned int sum = a + b;
>     return (int)(~(a ^ b) & (a ^ sum)) < 0;
> }

Yes, but rather like :

  bool overflow_bit2(unsigned char a, unsigned char b) {
    const unsigned char sum = a + b;
    return ((signed char)(~(a ^ b) & (a ^ sum))) < 0;
  }

It still results in abysmal code, given that this should result in two
or three instructions on most architectures.

Are machine code insertions an option?



More information about the Gcc mailing list