[cfe-users] Weird optimization

Artem Alimarin artem.alimarin at gmail.com
Mon Feb 23 03:22:41 PST 2015


Hi Csaba,

Yes, thanks. nasal demons, indeed. I forgot that the signed integer 
overflow causes undefined behavior unlike unsigned integer overflow, 
which causes wrap around.

The code
     return (long)(0ul - (unsigned long)x) > 0;
does what I want.

I am impressed by clang's ability to use this undefinedness to enable 
optimizations like (-x) > 0 to x < 0, which seems to be valid for all 
valid arguments.

Best regards,
Artem

On 23-2-2015 9:44, Csaba Raduly wrote:
> cast to an unsigned type to negate
> this value to itself
>
>

-------------- next part --------------
A non-text attachment was scrubbed...
Name: artem_alimarin.vcf
Type: text/x-vcard
Size: 362 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20150223/05fc01a2/attachment.vcf>


More information about the cfe-users mailing list