, GCC a++, , .
a () , GCC lea, inc add, mov [CN00 ].
, , gcc , .
a++; asm(...) a++; asm(...)
, . asm, add inc asm, . , SETCC, , seto %0, 8- . GCC6, , OF FLAGS .
, a++ C - , a++ . , - , a - , gcc, , 64-, -.
GCC , gcc5
/ add, sub mul, . GCC, UB , .
bool __builtin_add_overflow (type1 a, type2 b, type3 *res)bool __builtin_sadd_overflow (int a, int b, int *res) - intbool __builtin_saddll_overflow (long long int a, long long int b, long long int *res) - 64- long long .
, , , , ..
saddl , long . ( x86-64 gcc int 32-, long long 64-, long Windows Windows. , AVR, int 16-, long 32-).
int checked_add_int(int a, int b, bool *of) {
int result;
*of = __builtin_sadd_overflow(a, b, &result);
return result;
}
gcc -O3 x86-64 System V asm, int overflows(int+a, int+b) {%0a++ +bool+of = __builtin_add_overflow_p(a,+b, (int)0);%0a++ return of;
}
int+Checked_add_int(int+a, int+b,+bool+*of) {%0a++ int result;%0a++ *of = __builtin_sadd_overflow(a,+b, &result);%0a++ return result;
}
#include
#include
int main( int+argc,+Char+**argv+) {%0a++if(+argc+< 3 ) {%0a++ printf( "Usage: %25s+\n", *argv+);%0a++ return 0;%0a++}%0a++int+a;%0A++bool+of = __builtin_sadd_overflow(atoi(argv[1]),+atoi(argv[2]), &a);%0a++printf("%d overflowed%3F+%d\n",+a, of);
}
'),l:'5',n:'0',o:'C++ source #1',t:'0')),k:31.79650238473768,l:'4',m:100,n:'0',o:'',s:0,t:'0'),(g:!((g:!((h:compiler,i:(compiler:g91,filters:(b:'0',binary:'1',commentOnly:'0',demangle:'0',directives:'0',execute:'1',intel:'0',libraryCode:'1',trim:'1'),lang:c++,libs:!(),options:'-xc -O3 -Wall',source:1),l:'5',n:'0',o:'x86-64 gcc 9.1+(Editor #1,+Compiler+#1)+C++',t:'0')),header:(),k:50,l:'4',m:50,n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:icc191,filters:(b:'0',binary:'1',commentOnly:'0',demangle:'0',directives:'0',execute:'1',intel:'0',libraryCode:'1',trim:'1'),lang:c++,libs:!(),options:'-xc -O3 -Wall',source:1),l:'5',n:'0',o:'x86-64 icc 19.0.1+(Editor #1,+Compiler+#2)+C++',t:'0')),header:(),k:50,l:'4',n:'0',o:'',s:0,t:'0')),k:68.20349761526232,l:'2',m:100,n:'0',o:'',t:'0')),l:'2',n:'0',o:'',t:'0')),version:4 rel="nofollow noreferrer"> Godbolt
checked_add_int:
mov eax, edi
add eax, esi # can't use the normal lea eax, [rdi+rsi]
seto BYTE PTR [rdx]
and BYTE PTR [rdx], 1 # silly compiler, it already 0/1
ret
ICC19 setcc , , , , .
, if(of) {} jo jno setcc 0/1; , .
, gcc7, ( ) .
#include <stdbool.h>
int overflows(int a, int b) {
bool of = __builtin_add_overflow_p(a, b, (int)0);
return of;
}
gcc -O3 x86-64 System V asm, Godbolt
overflows:
xor eax, eax
add edi, esi
seto al
ret
. C/C++