[cfe-dev] Intrinsics for signed arithmetic with well-defined overflow/underflow?

Alex Rønne Petersen xtzgzorex at gmail.com
Fri Aug 3 16:44:59 PDT 2012


On Sat, Aug 4, 2012 at 1:05 AM, Jordan Rose <jordan_rose at apple.com> wrote:
>
> On Aug 3, 2012, at 16:00 , Eli Friedman <eli.friedman at gmail.com> wrote:
>
>> On Fri, Aug 3, 2012 at 3:43 PM, Alex Rønne Petersen <xtzgzorex at gmail.com> wrote:
>>> Hi cfe-dev,
>>>
>>> I'm doing a bit of a crazy project where I compile an IR to
>>> 'Clang-flavored C99'. The IR guarantees sane overflow/underflow
>>> semantics for signed arithmetic, but as far as I can tell, Clang
>>> doesn't (which is in line with the standard). Does Clang provide any
>>> intrinsics to achieve this, which the optimizers in LLVM will respect?
>>
>> What exactly does "sane" mean, in your context?  If you want wrapping,
>> you can just write (int)((unsigned)x+(unsigned)y).
>>
>> -Eli
>
> -fwrapv should permit signed overflow as well, just like in GCC. Not sure if we pass this flag all the way down, but a quick look at CodeGen does look like we're using the same Add as unsigned integers when -fwrapv is passed.
>
> Jordan

Wrapping is indeed the behavior I'd like.

It does look like -fwrapv will do the job without any casts, so I'll
try with that.

Thanks folks!

Regards,
Alex




More information about the cfe-dev mailing list