Are there any built-in means in the Oracle Developer Studio C and C++ compiler to check overflows occurred in computations with integer numbers of various types, size, represented both in signed and unsigned form?
In late GCC (starting from 5.0.0) such built-in functions have appeared for add, subtract and multiply operations. These are different __builtin_<operation>_overflow() functions, see the link http://gcc.gnu.org/onlinedocs//gcc/Integer-Overflow-Builtins.html.
For example, the following set of built-in operations allows to check overflow for addition:
bool __builtin_add_overflow (type1 a, type2 b, type3 *res);
bool __builtin_sadd_overflow (int a, int b, int *res);
bool __builtin_saddl_overflow (long int a, long int b, long int *res);
bool __builtin_saddll_overflow (long long int a, long long int b, long long int *res);
bool __builtin_uadd_overflow (unsigned int a, unsigned int b, unsigned int *res);
bool __builtin_uaddl_overflow (unsigned long int a, unsigned long int b, unsigned long int *res);
bool __builtin_uaddll_overflow (unsigned long long int a, unsigned long long int b, unsigned long long int *res);
The first function in the list above is intended for addition of integer numbers of arbitrary integral types (except of bool and enum), subsequent built-ins of this list are specialized for concrete types.
For example I can check overflow in case of addition executed on chars in the following way:
char a, b, c;
bool o;
o = __builtin_add_overflow (a, b, &c);
if(o) printf("Overflow has occurred!\n");
In the GCC manual it is written that the gcc compiler tries to generate code using hardware capabilities of the processor where possible. If overflow check on the integer operands of the given size is supported by the hardware, probably it will be used by the compiler. For example in case of x86 and x86_64 processors the output code will use OF and CF flags and contain jo and jc instructions.
Are there any similar media and extensions in the Oracle Developer Studio C/C++ compiler that allow to control overflow in integer arithmetic operations? Perhaps they are implemented as built-in functions, operators, macros or pragmas, extending syntax of the standard C and C++ languages?