[FFmpeg-devel] [PATCH 3/3] x86/blockdsp: add sse2 and avx2 versions of fill_block_tab

James Almer jamrial at gmail.com
Tue May 7 18:15:35 EEST 2024


On 5/7/2024 12:10 PM, Andreas Rheinhardt wrote:
> James Almer:
>> Signed-off-by: James Almer <jamrial at gmail.com>
>> ---
>>   libavcodec/x86/blockdsp.asm    | 33 +++++++++++++++++++++++++++++++++
>>   libavcodec/x86/blockdsp_init.c | 13 +++++++++++++
>>   2 files changed, 46 insertions(+)
>>
>> diff --git a/libavcodec/x86/blockdsp.asm b/libavcodec/x86/blockdsp.asm
>> index e380308d4a..cccc9a801a 100644
>> --- a/libavcodec/x86/blockdsp.asm
>> +++ b/libavcodec/x86/blockdsp.asm
>> @@ -80,3 +80,36 @@ INIT_XMM sse
>>   CLEAR_BLOCKS 1
>>   INIT_YMM avx
>>   CLEAR_BLOCKS 1
>> +
>> +;-----------------------------------------
>> +; void ff_fill_block_tab_%1(uint8_t *block, uint8_t value,
>> +;                           ptrdiff_t line_size, int h);
>> +;-----------------------------------------
>> +%macro FILL_BLOCK_TAB 2
>> +cglobal fill_block_tab_%1, 4, 5, 1, block, value, stride, h, stride3
>> +    lea stride3q, [strideq + strideq * 2]
>> +%if cpuflag(avx2)
>> +    movd m0, valued
>> +    vpbroadcastb m0, m0
>> +%else
>> +    SPLATB_REG m0, value, x
>> +%endif
>> +.loop:
>> +    mov%2 [blockq], m0
>> +    mov%2 [blockq + strideq], m0
>> +    mov%2 [blockq + strideq * 2], m0
>> +    mov%2 [blockq + stride3q], m0
>> +    lea blockq, [blockq + strideq * 4]
>> +    sub hd, 4
>> +    jg .loop
>> +    RET
>> +%endmacro
>> +
>> +INIT_XMM sse2
>> +FILL_BLOCK_TAB 8, q
>> +FILL_BLOCK_TAB 16, a
>> +%if HAVE_AVX2_EXTERNAL
>> +INIT_XMM avx2
>> +FILL_BLOCK_TAB 8, q
>> +FILL_BLOCK_TAB 16, a
>> +%endif
>> diff --git a/libavcodec/x86/blockdsp_init.c b/libavcodec/x86/blockdsp_init.c
>> index 996124114f..37f3bb6a84 100644
>> --- a/libavcodec/x86/blockdsp_init.c
>> +++ b/libavcodec/x86/blockdsp_init.c
>> @@ -29,6 +29,11 @@ void ff_clear_block_avx(int16_t *block);
>>   void ff_clear_blocks_sse(int16_t *blocks);
>>   void ff_clear_blocks_avx(int16_t *blocks);
>>   
>> +void ff_fill_block_tab_16_sse2(uint8_t *block, uint8_t value, ptrdiff_t line_size, int h);
>> +void ff_fill_block_tab_8_sse2(uint8_t *block, uint8_t value, ptrdiff_t line_size, int h);
>> +void ff_fill_block_tab_16_avx2(uint8_t *block, uint8_t value, ptrdiff_t line_size, int h);
>> +void ff_fill_block_tab_8_avx2(uint8_t *block, uint8_t value, ptrdiff_t line_size, int h);
>> +
>>   av_cold void ff_blockdsp_init_x86(BlockDSPContext *c)
>>   {
>>   #if HAVE_X86ASM
>> @@ -38,9 +43,17 @@ av_cold void ff_blockdsp_init_x86(BlockDSPContext *c)
>>           c->clear_block  = ff_clear_block_sse;
>>           c->clear_blocks = ff_clear_blocks_sse;
>>       }
>> +    if (EXTERNAL_SSE2(cpu_flags)) {
>> +        c->fill_block_tab[0] = ff_fill_block_tab_16_sse2;
>> +        c->fill_block_tab[1] = ff_fill_block_tab_8_sse2;
>> +    }
>>       if (EXTERNAL_AVX_FAST(cpu_flags)) {
>>           c->clear_block  = ff_clear_block_avx;
>>           c->clear_blocks = ff_clear_blocks_avx;
>>       }
>> +    if (EXTERNAL_AVX2(cpu_flags)) {
>> +        c->fill_block_tab[0] = ff_fill_block_tab_16_avx2;
>> +        c->fill_block_tab[1] = ff_fill_block_tab_8_avx2;
>> +    }
>>   #endif /* HAVE_X86ASM */
>>   }
> 
> Benchmarks?

blockdsp.fill_block_tab[0]_c: 13.4
blockdsp.fill_block_tab[0]_sse2: 8.4
blockdsp.fill_block_tab[0]_avx2: 7.4
blockdsp.fill_block_tab[1]_c: 7.9
blockdsp.fill_block_tab[1]_sse2: 4.5
blockdsp.fill_block_tab[1]_avx2: 4.0

On an Alder Lake using --cpu-alderlake. If i let gcc compile without cpu 
specific optimizations, the C version is even slower.


More information about the ffmpeg-devel mailing list