A simple optimization
I’ll be posting about the Nehalem optimizations soon, but in the meantime, a short and simple post.
You want to find the last nonzero value in an array of 16 16-bit values (DCT coefficients, in this case). How do you do this really quickly, especially in the case that most of the array is expected to be zero? Well here’s my way (l[16] is the array):
i_last = 0;
if( *(uint64_t*)(l+i_last+8)|*(uint64_t*)(l+i_last+12) ) i_last += 8;
if( *(uint64_t*)(l+i_last+4) ) i_last += 4;
if( *(uint32_t*)(l+i_last+2) ) i_last += 2;
if( l[i_last+1] ) i_last++;
This assumes the array isn’t all zero, but where I used this code, we already knew that it wasn’t, so that wasn’t an issue.
Only 4 conditionals (cmov and setne in the compiled asm) to find the exact index of the coefficient. Over twice as fast as the previous code:
for( j = i_count - 4; j >= 4; j -= 4 ) if( *(uint64_t*)(l+j) ) break;
for( i = j; i < j+4; i++ ) if( l[i] ) i_last = i;
And I would guess yet another factor of 2 or similar faster than the naive implementation:
for( i = 0; i < 16; i++ ) if( l[i] ) i_last = i;
November 14th, 2008 at 5:45 am
That’s somewhat like binary search, clever
. And it isn’t even much more code.
November 14th, 2008 at 10:16 pm
Clever indeed. You know that the data types are 16 bit each, so casting it to a 64/32 bit allows you to search for many more non-zero entries at once.
November 16th, 2008 at 7:22 pm
Nice.
Minor point – you don’t actually need the two ‘+i_last’ in the first ‘if’ do you? I’m sure the compiler optimises them away, but I did spend a couple of seconds trying to work out why they were there.
November 18th, 2008 at 3:12 pm
if there was a uint128_t the code would look even cleaner
November 19th, 2008 at 3:10 pm
@mikel:
The original code actually allowed i_last to start as 0 or -1, for technical reasons relating to how the dct data was passed to the function. So in that case, the +i_last was in fact necessary.
November 20th, 2008 at 1:39 am
Dark, hi,
want improve it bit more?
there is in fact 128-bit zero test with SSE4.1 PTEST instruction, and bsr is better for the final check withing 64-bit block, so your code would look like:
size_t last_zero_word_idx_16(const short* l)
{
size_t i = 0;
__m128i high_part = _mm_loadu_si128( (__m128i*)(l+i+8) ); // unaligned load on Nehalem is fast, change to _mm_load_si128 if you know data are aligned to sightly improve Penryn’s performance
if( ! _mm_testz_si128( high_part, high_part ) ) i += 8;
// 64-bit test is fine
if( *(uint64_t*)(l+i+4) ) i += 4;
// 64-bit bsr is faster for tail
// bsr result is undefined for 0, but you said it is never all zeros, otherwise would require one more ZF flag check
size_t idx;
__asm__( “bsrq (%[l],%[i],2), %[idx]” : [idx] “=r” (idx) : [l] “r” (l), [i] “r” (i) );
i += idx >> 4; // i.e. bit index 48 means +3 word
return ( i );
}
ICC generates very nice:
..___tag_value_last_zero_word_idx_16.1:
movdqu 16(%rdi), %xmm0
movl $8, %edx
xorl %eax, %eax
ptest %xmm0, %xmm0
cmovne %rdx, %rax
cmpq $0, 8(%rdi,%rax,2)
lea 4(%rax), %rcx
cmovne %rcx, %rax
bsr (%rdi,%rax,2), %rsi
shrq $4, %rsi
addq %rsi, %rax
ret
// 3 loads vs. 4, 2 cond. vs. 4
-Max
P.S. sorry, I remember I still owe you test of pmovzx on Intel forum …
November 20th, 2008 at 1:12 pm
Max: if you want to do asm, you can get even better than that. Simply use pmovmskb to generate a 16-bit value representing the array, with nonzero values being 1s, and zero values being 0s, and use bsr to find the last nonzero coefficient.
And indeed, I’m awaiting that response on pmovzx
November 24th, 2008 at 6:07 am
right, you also need pcmpeqb though, so something like:
__m128i chunk1 = _mm_loadu_si128( l + 8 ),
__m128i chunk2 = _mm_loadu_si128( l ),
__m128i mask128 = _mm_cmpeq_epi8( chunk1, _mm_setzero_si128() );
int mask = _mm_movemask_epi8( mask128 ) << 32;
mask128 = _mm_cmpeq_epi8( chunk2, _mm_setzero_si128() );
mask |= _mm_movemask_epi8( mask128 );
mask ~= mask;
int idx;
__asm__( “bsrl %[mask], %[idx]” : [idx] “=r” (idx) : [mask] “r” (mask) );
latency wise probably will not be faster than above, even it has 1 less load … I might check it bit later …
-Max
November 29th, 2008 at 2:28 pm
@Max: Check the coeff_last asm functions in this revision:
http://www.zmx.ovh/git.videolan.org/?p=x264.git;a=commitdiff;h=12724e638e8d28ce97bcb9c77d2bb7336b087af3