gcc: Integer Overflow Builtins

1 
1 6.54 Built-in Functions to Perform Arithmetic with Overflow Checking
1 ====================================================================
1 
1 The following built-in functions allow performing simple arithmetic
1 operations together with checking whether the operations overflowed.
1 
1  -- Built-in Function: bool __builtin_add_overflow (TYPE1 a, TYPE2 b,
1           TYPE3 *res)
1  -- Built-in Function: bool __builtin_sadd_overflow (int a, int b, int
1           *res)
1  -- Built-in Function: bool __builtin_saddl_overflow (long int a, long
1           int b, long int *res)
1  -- Built-in Function: bool __builtin_saddll_overflow (long long int a,
1           long long int b, long long int *res)
1  -- Built-in Function: bool __builtin_uadd_overflow (unsigned int a,
1           unsigned int b, unsigned int *res)
1  -- Built-in Function: bool __builtin_uaddl_overflow (unsigned long int
1           a, unsigned long int b, unsigned long int *res)
1  -- Built-in Function: bool __builtin_uaddll_overflow (unsigned long
1           long int a, unsigned long long int b, unsigned long long int
1           *res)
1 
1      These built-in functions promote the first two operands into
1      infinite precision signed type and perform addition on those
1      promoted operands.  The result is then cast to the type the third
1      pointer argument points to and stored there.  If the stored result
1      is equal to the infinite precision result, the built-in functions
1      return false, otherwise they return true.  As the addition is
1      performed in infinite signed precision, these built-in functions
1      have fully defined behavior for all argument values.
1 
1      The first built-in function allows arbitrary integral types for
1      operands and the result type must be pointer to some integral type
1      other than enumerated or boolean type, the rest of the built-in
1      functions have explicit integer types.
1 
1      The compiler will attempt to use hardware instructions to implement
1      these built-in functions where possible, like conditional jump on
1      overflow after addition, conditional jump on carry etc.
1 
1  -- Built-in Function: bool __builtin_sub_overflow (TYPE1 a, TYPE2 b,
1           TYPE3 *res)
1  -- Built-in Function: bool __builtin_ssub_overflow (int a, int b, int
1           *res)
1  -- Built-in Function: bool __builtin_ssubl_overflow (long int a, long
1           int b, long int *res)
1  -- Built-in Function: bool __builtin_ssubll_overflow (long long int a,
1           long long int b, long long int *res)
1  -- Built-in Function: bool __builtin_usub_overflow (unsigned int a,
1           unsigned int b, unsigned int *res)
1  -- Built-in Function: bool __builtin_usubl_overflow (unsigned long int
1           a, unsigned long int b, unsigned long int *res)
1  -- Built-in Function: bool __builtin_usubll_overflow (unsigned long
1           long int a, unsigned long long int b, unsigned long long int
1           *res)
1 
1      These built-in functions are similar to the add overflow checking
1      built-in functions above, except they perform subtraction, subtract
1      the second argument from the first one, instead of addition.
1 
1  -- Built-in Function: bool __builtin_mul_overflow (TYPE1 a, TYPE2 b,
1           TYPE3 *res)
1  -- Built-in Function: bool __builtin_smul_overflow (int a, int b, int
1           *res)
1  -- Built-in Function: bool __builtin_smull_overflow (long int a, long
1           int b, long int *res)
1  -- Built-in Function: bool __builtin_smulll_overflow (long long int a,
1           long long int b, long long int *res)
1  -- Built-in Function: bool __builtin_umul_overflow (unsigned int a,
1           unsigned int b, unsigned int *res)
1  -- Built-in Function: bool __builtin_umull_overflow (unsigned long int
1           a, unsigned long int b, unsigned long int *res)
1  -- Built-in Function: bool __builtin_umulll_overflow (unsigned long
1           long int a, unsigned long long int b, unsigned long long int
1           *res)
1 
1      These built-in functions are similar to the add overflow checking
1      built-in functions above, except they perform multiplication,
1      instead of addition.
1 
1  The following built-in functions allow checking if simple arithmetic
1 operation would overflow.
1 
1  -- Built-in Function: bool __builtin_add_overflow_p (TYPE1 a, TYPE2 b,
1           TYPE3 c)
1  -- Built-in Function: bool __builtin_sub_overflow_p (TYPE1 a, TYPE2 b,
1           TYPE3 c)
1  -- Built-in Function: bool __builtin_mul_overflow_p (TYPE1 a, TYPE2 b,
1           TYPE3 c)
1 
1      These built-in functions are similar to '__builtin_add_overflow',
1      '__builtin_sub_overflow', or '__builtin_mul_overflow', except that
1      they don't store the result of the arithmetic operation anywhere
1      and the last argument is not a pointer, but some expression with
1      integral type other than enumerated or boolean type.
1 
1      The built-in functions promote the first two operands into infinite
1      precision signed type and perform addition on those promoted
1      operands.  The result is then cast to the type of the third
1      argument.  If the cast result is equal to the infinite precision
1      result, the built-in functions return false, otherwise they return
1      true.  The value of the third argument is ignored, just the side
1      effects in the third argument are evaluated, and no integral
1      argument promotions are performed on the last argument.  If the
1      third argument is a bit-field, the type used for the result cast
1      has the precision and signedness of the given bit-field, rather
1      than precision and signedness of the underlying type.
1 
1      For example, the following macro can be used to portably check, at
1      compile-time, whether or not adding two constant integers will
1      overflow, and perform the addition only when it is known to be safe
1      and not to trigger a '-Woverflow' warning.
1 
1           #define INT_ADD_OVERFLOW_P(a, b) \
1              __builtin_add_overflow_p (a, b, (__typeof__ ((a) + (b))) 0)
1 
1           enum {
1               A = INT_MAX, B = 3,
1               C = INT_ADD_OVERFLOW_P (A, B) ? 0 : A + B,
1               D = __builtin_add_overflow_p (1, SCHAR_MAX, (signed char) 0)
1           };
1 
1      The compiler will attempt to use hardware instructions to implement
1      these built-in functions where possible, like conditional jump on
1      overflow after addition, conditional jump on carry etc.
1