Chapter 6 Branch Optimizations 131
Software Optimization Guide for AMD64 Processors
25112 Rev. 3.06 September 2005
Conditional Write
// C code:
int a, b, i, dummy, c[BUFSIZE];
if (a < b) {
c[i++] = a;
}
;--------------
; Assembly code:
lea esi, [dummy] ; &dummy
xor ecx, ecx ; i = 0
...
lea edi, [c+ecx*4] ; &c[i]
lea edx, [ecx+1] ; i++
cmp eax, ebx ; a < b ?
cmovge edi, esi ; ptr = (a >= b) ? &dummy : &c[i]
cmovl ecx, edx ; a < b ? i : i + 1
mov [edi], eax ; *ptr = a