2

Is the adc $0 instruction the only way to add the Carry Flag to %rdx on x86/64?

add %rax,%rcx
adc $0  ,%rdx

1 Answers1

2

It's the best way.

The other options include using jc (to skip an inc), cmovc (probably with lea for a no-flags add-1), or setc. With setc:

xor  %edi, %edi
add  %rax, %rcx
setc %dil         # %rdi = CF, since we already zeroed the upper bytes
add  %rdi, %rdx   # no partial-register stall/extra uop from reading rdi after writing dil, because we used a recognized zeroing idiom (xor) to zero it.

These options are obviously much worse, even though adc is a 2-uop instruction on Intel CPUs before Broadwell. (Since Haswell introduced the hardware changes for FMA to decode to a single uop, Broadwell and Skylake also handle some other 3-input instructions as a single uop, including adc and cmov.)

An even worse option for getting CF into a register would the slow rotate-with-carry rcl (on a zeroed register).

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847