From 5a97469a4fd12ef0327292ad6062f89a5e055a62 Mon Sep 17 00:00:00 2001 From: Christophe Gisquet Date: Tue, 9 Apr 2013 21:57:07 +0000 Subject: [PATCH 1/3] x86: sbrdsp: Implement SSE2 qmf_deint_bfly Sandybridge: 47 cycles Having a loop counter is a 7 cycle gain. Unrolling is another 7 cycle gain. Working in reverse scan is another 6 cycles. Signed-off-by: Diego Biurrun --- libavcodec/x86/sbrdsp.asm | 28 ++++++++++++++++++++++++++++ libavcodec/x86/sbrdsp_init.c | 5 +++++ 2 files changed, 33 insertions(+) diff --git a/libavcodec/x86/sbrdsp.asm b/libavcodec/x86/sbrdsp.asm index 5e545055a7..bfc37c2a1f 100644 --- a/libavcodec/x86/sbrdsp.asm +++ b/libavcodec/x86/sbrdsp.asm @@ -245,3 +245,31 @@ cglobal sbr_neg_odd_64, 1,2,4,z cmp zq, r1q jne .loop REP_RET + +INIT_XMM sse2 +; sbr_qmf_deint_bfly(float *v, const float *src0, const float *src1) +cglobal sbr_qmf_deint_bfly, 3,5,8, v,src0,src1,vrev,c + mov cq, 64*4-2*mmsize + lea vrevq, [vq + 64*4] +.loop: + mova m0, [src0q+cq] + mova m1, [src1q] + mova m2, [src0q+cq+mmsize] + mova m3, [src1q+mmsize] + pshufd m4, m0, q0123 + pshufd m5, m1, q0123 + pshufd m6, m2, q0123 + pshufd m7, m3, q0123 + addps m3, m4 + subps m0, m7 + addps m1, m6 + subps m2, m5 + mova [vrevq], m1 + mova [vrevq+mmsize], m3 + mova [vq+cq], m0 + mova [vq+cq+mmsize], m2 + add src1q, 2*mmsize + add vrevq, 2*mmsize + sub cq, 2*mmsize + jge .loop + REP_RET diff --git a/libavcodec/x86/sbrdsp_init.c b/libavcodec/x86/sbrdsp_init.c index cb65a23f22..f3f6c65d8f 100644 --- a/libavcodec/x86/sbrdsp_init.c +++ b/libavcodec/x86/sbrdsp_init.c @@ -34,6 +34,7 @@ void ff_sbr_hf_gen_sse(float (*X_high)[2], const float (*X_low)[2], float bw, int start, int end); void ff_sbr_neg_odd_64_sse(float *z); void ff_sbr_qmf_post_shuffle_sse(float W[32][2], const float *z); +void ff_sbr_qmf_deint_bfly_sse2(float *v, const float *src0, const float *src1); av_cold void ff_sbrdsp_init_x86(SBRDSPContext *s) { @@ -47,4 +48,8 @@ av_cold void ff_sbrdsp_init_x86(SBRDSPContext *s) s->hf_gen = ff_sbr_hf_gen_sse; s->qmf_post_shuffle = ff_sbr_qmf_post_shuffle_sse; } + + if (EXTERNAL_SSE2(mm_flags)) { + s->qmf_deint_bfly = ff_sbr_qmf_deint_bfly_sse2; + } } From 8394d9a676db1ffa1c8e4f6dd239e154d051ce03 Mon Sep 17 00:00:00 2001 From: Christophe Gisquet Date: Tue, 9 Apr 2013 21:57:09 +0000 Subject: [PATCH 2/3] sbrdsp: Unroll sbr_autocorrelate_c 1410 cycles to 1148 on Arrandale/Win64 Signed-off-by: Diego Biurrun --- libavcodec/sbrdsp.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/libavcodec/sbrdsp.c b/libavcodec/sbrdsp.c index 781ec83fe8..85b4ebefb4 100644 --- a/libavcodec/sbrdsp.c +++ b/libavcodec/sbrdsp.c @@ -122,9 +122,34 @@ static av_always_inline void autocorrelate(const float x[40][2], static void sbr_autocorrelate_c(const float x[40][2], float phi[3][2][2]) { +#if 0 + /* This code is slower because it multiplies memory accesses. + * It is left for educational purposes and because it may offer + * a better reference for writing arch-specific DSP functions. */ autocorrelate(x, phi, 0); autocorrelate(x, phi, 1); autocorrelate(x, phi, 2); +#else + float real_sum2 = x[0][0] * x[2][0] + x[0][1] * x[2][1]; + float imag_sum2 = x[0][0] * x[2][1] - x[0][1] * x[2][0]; + float real_sum1 = 0.0f, imag_sum1 = 0.0f, real_sum0 = 0.0f; + int i; + for (i = 1; i < 38; i++) { + real_sum0 += x[i][0] * x[i ][0] + x[i][1] * x[i ][1]; + real_sum1 += x[i][0] * x[i + 1][0] + x[i][1] * x[i + 1][1]; + imag_sum1 += x[i][0] * x[i + 1][1] - x[i][1] * x[i + 1][0]; + real_sum2 += x[i][0] * x[i + 2][0] + x[i][1] * x[i + 2][1]; + imag_sum2 += x[i][0] * x[i + 2][1] - x[i][1] * x[i + 2][0]; + } + phi[2 - 2][1][0] = real_sum2; + phi[2 - 2][1][1] = imag_sum2; + phi[2 ][1][0] = real_sum0 + x[ 0][0] * x[ 0][0] + x[ 0][1] * x[ 0][1]; + phi[1 ][0][0] = real_sum0 + x[38][0] * x[38][0] + x[38][1] * x[38][1]; + phi[2 - 1][1][0] = real_sum1 + x[ 0][0] * x[ 1][0] + x[ 0][1] * x[ 1][1]; + phi[2 - 1][1][1] = imag_sum1 + x[ 0][0] * x[ 1][1] - x[ 0][1] * x[ 1][0]; + phi[0 ][0][0] = real_sum1 + x[38][0] * x[39][0] + x[38][1] * x[39][1]; + phi[0 ][0][1] = imag_sum1 + x[38][0] * x[39][1] - x[38][1] * x[39][0]; +#endif } static void sbr_hf_gen_c(float (*X_high)[2], const float (*X_low)[2], From 4a7af92cc80ced8498626401ed21f25ffe6740c8 Mon Sep 17 00:00:00 2001 From: Christophe Gisquet Date: Tue, 9 Apr 2013 21:57:10 +0000 Subject: [PATCH 3/3] sbrdsp: Unroll and use integer operations This patch can be controversial, by assuming floats are IEEE-754 and particular behaviour of the FPU will get in the way. Timing on Arrandale and Win32 (thus, x87 FPU is used in the reference). sbr_qmf_pre_shuffle_c: 115 to 76 sbr_neg_odd_64_c: 84 to 55 sbr_qmf_post_shuffle_c: 112 to 83 Signed-off-by: Diego Biurrun --- libavcodec/sbrdsp.c | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/libavcodec/sbrdsp.c b/libavcodec/sbrdsp.c index 85b4ebefb4..4d07af58f8 100644 --- a/libavcodec/sbrdsp.c +++ b/libavcodec/sbrdsp.c @@ -22,6 +22,7 @@ #include "config.h" #include "libavutil/attributes.h" +#include "libavutil/intfloat.h" #include "sbrdsp.h" static void sbr_sum64x5_c(float *z) @@ -51,37 +52,51 @@ static float sbr_sum_square_c(float (*x)[2], int n) static void sbr_neg_odd_64_c(float *x) { + union av_intfloat32 *xi = (union av_intfloat32*) x; int i; - for (i = 1; i < 64; i += 2) - x[i] = -x[i]; + for (i = 1; i < 64; i += 4) { + xi[i + 0].i ^= 1U << 31; + xi[i + 2].i ^= 1U << 31; + } } static void sbr_qmf_pre_shuffle_c(float *z) { + union av_intfloat32 *zi = (union av_intfloat32*) z; int k; - z[64] = z[0]; - z[65] = z[1]; - for (k = 1; k < 32; k++) { - z[64+2*k ] = -z[64 - k]; - z[64+2*k+1] = z[ k + 1]; + zi[64].i = zi[0].i; + zi[65].i = zi[1].i; + for (k = 1; k < 31; k += 2) { + zi[64 + 2 * k + 0].i = zi[64 - k].i ^ (1U << 31); + zi[64 + 2 * k + 1].i = zi[ k + 1].i; + zi[64 + 2 * k + 2].i = zi[63 - k].i ^ (1U << 31); + zi[64 + 2 * k + 3].i = zi[ k + 2].i; } + zi[64 + 2 * 31 + 0].i = zi[64 - 31].i ^ (1U << 31); + zi[64 + 2 * 31 + 1].i = zi[31 + 1].i; } static void sbr_qmf_post_shuffle_c(float W[32][2], const float *z) { + const union av_intfloat32 *zi = (const union av_intfloat32*) z; + union av_intfloat32 *Wi = (union av_intfloat32*) W; int k; - for (k = 0; k < 32; k++) { - W[k][0] = -z[63-k]; - W[k][1] = z[k]; + for (k = 0; k < 32; k += 2) { + Wi[2 * k + 0].i = zi[63 - k].i ^ (1U << 31); + Wi[2 * k + 1].i = zi[ k + 0].i; + Wi[2 * k + 2].i = zi[62 - k].i ^ (1U << 31); + Wi[2 * k + 3].i = zi[ k + 1].i; } } static void sbr_qmf_deint_neg_c(float *v, const float *src) { + const union av_intfloat32 *si = (const union av_intfloat32*)src; + union av_intfloat32 *vi = (union av_intfloat32*)v; int i; for (i = 0; i < 32; i++) { - v[ i] = src[63 - 2*i ]; - v[63 - i] = -src[63 - 2*i - 1]; + vi[ i].i = si[63 - 2 * i ].i; + vi[63 - i].i = si[63 - 2 * i - 1].i ^ (1U << 31); } }