Getting the compiler to generate the adc command

Is there a way to get Clang, GCC or VS to generate adc instructions (add with wrapping) only using Standard-C ++ (98/11/14)? (Edit: I mean in x64 mode, sorry if that was not clear.)

+6
source share
3 answers

If you compile a 64-bit signed add-on for X86 ( int64_t in C ++ 11), the compiled code will contain an adc statement.

Edit: sample code:

 int64_t add_numbers(int64_t x, int64_t y) { return x + y; } 

In X86, adding is done using the add command, followed by the adc statement. On X64, only one add statement is used.

+2
source

There is a __int128_t type available for GCC for amd64 and other 64-bit purposes, which will use a couple of add / adc instructions for easy addition. (See Godbolt Link below).

Additionally, this clean ISO C code can compile in adc:

 uint64_t adc(uint64_t a, uint64_t b) { a += b; if (a < b) /* should simplify to nothing (setting carry is implicit in the add) */ a++; /* should simplify to adc r0, 0 */ return a; } 

For me (ARM), this is something stupid, but it compiles for x86-64 (in the Godbolt explorer compiler ) to this

  mov rax, rdi # a, a add rax, rsi # a, b adc rax, 0 # a, ret 
+2
source

If your code does the comparison and adds the result of the comparison to something, then adc usually emitted by gcc 5 (by the way, gcc 4.8 does not highlight adc ). For instance,

 unsigned foo(unsigned a, unsigned b, unsigned c, unsigned d) { return (a + b + (c < d)); } 

going to

 foo: cmpl %ecx, %edx movl %edi, %eax adcl %esi, %eax ret 

However, it is a bit difficult to get gcc to really emit adc .

+1
source

Source: https://habr.com/ru/post/948345/


All Articles