Ping?  Please take a look.  Thanks.

There was some related discussion about overflow builtins from GCC, FYI.

        http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48580

I also created a higher-level library on top of these builtins for 
demonstration.

        https://github.com/xiw/libo

Basically it provides the following functions (macros/wrappers).

        bool overflow_add(type *, type, type);
        bool overflow_sub(type *, type, type);
        bool overflow_mul(type *, type, type);
        bool overflow_div(type *, type, type);

You even can use them with GCC (or Clang without overflow builtins).  Their 
implementations (in assembly) are generated automatically via Clang, except for 
overflow_div(), which doesn't depend on overflow builtins.

- xi

Attachment: overflow.patch
Description: Binary data

On Apr 4, 2012, at 8:18 PM, Xi Wang wrote:

> Hi,
> 
> Attached is a patch that adds arithmetic with overflow builtins to Clang.  
> Please review.  Thanks.
> 
> Arithmetic with overflow builtins are used to perform arithmetic operations 
> with overflow detection.
> 
> Syntax:
> 
> bool __builtin_add_with_overflow(type *ptr, type a, type b);
> bool __builtin_sub_with_overflow(type *ptr, type a, type b);
> bool __builtin_mul_with_overflow(type *ptr, type a, type b);
> 
> Example of Use:
> 
> void *malloc_array(size_t n, size_t size) {
>  size_t bytes;
>  if (__builtin_mul_with_overflow(&bytes, n, size))
>    return NULL;
>  return malloc(bytes);
> }
> 
> Description:
> 
> __builtin_op_with_overflow(ptr, a, b) stores the result of a op b in ptr, and 
> returns true if an overflow occurred during the arithmetic operation. Note 
> that type is inferred from *ptr. These builtins help developers write more 
> efficient and correct code by avoiding ad hoc overflow checks.
> 
> Query for this feature with __has_builtin(__builtin_op_with_overflow).
> 
> - xi
> <overflow.patch>

_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to