[cfe-commits] [PATCH] PR12290: introducing overflow builtins
Xi Wang
xi.wang at gmail.com
Tue Apr 17 09:28:07 PDT 2012
The attached patch makes two changes.
- Enabled 128-bit integer operation in overflow builtins.
- Rebased on the latest trunk.
Please review! Thanks.
- xi
-------------- next part --------------
A non-text attachment was scrubbed...
Name: overflow.patch
Type: application/octet-stream
Size: 11628 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20120417/805e91eb/attachment.obj>
-------------- next part --------------
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
More information about the cfe-commits
mailing list