2 ms·
I've tried your switch, and yes, switching to a non-flag-setting 'INC RAX' causes both the SUB and SBB versions to run slowly. Since this is not explained by
by nkurz 12y ago
I've tried your switch, and yes, switching to a non-flag-setting 'INC RAX' causes both the SUB and SBB versions to run slowly. Since this is not explained by my theory, my explanation must be at least partly wrong. I suspect you are on the correct path, and the dependence on the carry flag is significant
But I keep finding all sorts of other timing differences that I can't explain. For example, using the 'INC RAX' version but adding a stray 'XOR RDX, RAX' into the loop makes both SUB and SBB run fast rather than slow. Does this fit with your explanation?
- pbsd 12y agoBoth fast examples create a dependence between loop iterations. The ADD+SBB case creates a dependence between the carry flag and the value loaded from memory; the INC + XOR creates a dependence on the value of RDX and the value loaded from memory. I don't know what exactly is the mechanism here, but the data dependencies seem to prevent memory loads to be speculated like crazy, presumably the source of the slowdown. Another data point: replace SUB RCX, 1 / JNZ by LOOP. LOOP is much slower, something like 7 uops, but it still results in a speedup. Here I cannot blame data dependencies. LOOP is microcoded, so perhaps its internal implementation has the same effect.