[cfe-dev] [RFC] Introduce overflow builtins

Dave Zarzycki zarzycki at apple.com
Sun Apr 1 23:55:59 PDT 2012


On Apr 1, 2012, at 10:59 PM, Xi Wang <xi.wang at gmail.com> wrote:

>> 1) Make __builtin_mul_with_overflow() return the "high" half of the result instead of a bool. You should be able to use the normal LLVM multiplication intrinsics for this instead of llvm.*mul.with.overflow.*.
> 
> I guess you mean something like MSVC's intrinsics:
> 
> __int64 _mul128( 
>   __int64 Multiplier, 
>   __int64 Multiplicand, 
>   __int64 *HighProduct 
> );
> 
> __int64 __mulh( 
>   __int64 a, 
>   __int64 b 
> );
> 
> http://msdn.microsoft.com/en-us/library/82cxdw50.aspx
> 
> http://msdn.microsoft.com/en-us/library/17wa28cw.aspx
> 
> I don't think you need a new builtin here.  For example, converting a and b to __int128_t should do the trick.

What do you do when you want to overflow check __int128_t arithmetic? More on this topic below...

>> 2) Make __builtin_*_with_overflow work with 8-bit integers.
> 
> Actually I doubt if types smaller than `int' would require overflow checking in practice..

In practice? Well maybe not your code, but for many others the lack of 8-bit support makes using the __builtin_*_with_overflow() intrinsics in generic code much harder and much uglier.

>> 3) Add __builtin_add_with_carry() and __builtin_sub_with_borrow().
> 
> What could benefit from them?

With the overflow intrinsics that you are proposing, you've solved half of the work required to writing a straightforward, efficient, and somewhat portable "Big Number" library. :-)

For example, 128-bit or larger integers could have been solved in a library instead of directly in the compiler if these intrinsics were available.

davez



More information about the cfe-dev mailing list