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?
On Fri, Oct 7, 2011 at 7:35 PM, Florian Weimer <fw@deneb.enyo.de> wrote:
> * 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?
>
Tried that version, but it seems to generate worse (or bigger anyway -
haven't benchmarked it) code:
90: eb01 0c00 add.w ip, r1, r0
94: b2c2 uxtb r2, r0
96: ea82 030c eor.w r3, r2, ip
9a: ea82 0101 eor.w r1, r2, r1
9e: ea23 0001 bic.w r0, r3, r1
a2: f3c0 10c0 ubfx r0, r0, #7, #1
a6: 4770 bx lr
a8: f3af 8000 nop.w
ac: f3af 8000 nop.w
Good machine code would be fun to see, though I might need to brush up
on my ARM.
/Ulf