Skip to contentSkip to conversation

SBC encoder scale factors calculation optimized with __builtin_clz

PATCH In review Siarhei Siamashka 4 participants 17 messages Jan 29, 2009 – Mar 16, 2009 Bluetooth drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 SBC encoder scale factors calculation optimized with __builtin_clz

Hello all,

The attached patch contains optimization for scale factors calculation which provides additional SBC encoder speedup.

For non-gcc compilers, CLZ function is implemented with a very simple and slow straightforward code (but it is still faster than current git code even if used instead of __builtin_clz). Something better could be done like:
http://groups.google.com/group/comp.sys.arm/msg/5ae56e3a95a2345e?hl=en
But I'm not sure about license/copyright of the code at this link and decided not to touch it. Anyway, I don't think that gcc implementation of __builtin_clz for the CPU cores which do not support CLZ instruction is any worse.

Joint stereo processing also involves recalculation of scale factors, which can use a similar optimization or even exactly the same function. I intentionally did not benchmark encoding with joint stereo yet as it would spoil the nice numbers :) That's something to improve next.

Benchmark results (sbcenc with default settings):

====

ARM Cortex-A8:

before:

real    1m 4.84s
user    1m 1.05s
sys     0m 3.78s

after:

real    0m 58.93s
user    0m 55.15s
sys     0m 3.78s

Intel Core2:

before:
real    0m7.729s
user    0m7.268s
sys     0m0.376s

after:

real    0m6.473s
user    0m6.116s
sys     0m0.292s

====

Overall, CPU usage in SBC encoder looks more or less like this (oprofile log from ARM Cortex-A8):

samples  %        image name               symbol name
2173     30.6791  sbcenc.neon_new          sbc_encode
1774     25.0459  sbcenc.neon_new          sbc_analyze_4b_8s_neon
1525     21.5304  sbcenc.neon_new          sbc_calculate_bits
916      12.9324  sbcenc.neon_new          sbc_calc_scalefactors
600       8.4710  sbcenc.neon_new          sbc_enc_process_input_8s_be
75        1.0589  libc-2.5.so              memcpy
13        0.1835  sbcenc.neon_new          main
4         0.0565  libc-2.5.so              write
2         0.0282  sbcenc.neon_new          .plt
1         0.0141  ld-2.5.so                _dl_relocate_object

Best regards,
Siarhei Siamashka

Quoted 1 line

From 90c60f04f1540fe2c7d5ab631dbd111c25b03e17 Mon Sep 17 00:00:00 2001

From: Siarhei Siamashka <[email protected]>
Date: Thu, 29 Jan 2009 02:17:36 +0200
Subject: [PATCH] SBC encoder scale factors calculation optimized with __builtin_clz

Count leading zeros operation is often implemented using a special instruction for it on various architectures (at least this is true for ARM and x86). Using __builtin_clz gcc intrinsic allows to eliminate innermost loop in scale factors calculation and improve performance. Also scale factors calculation can be optimized even more using SIMD instructions.

---
 sbc/sbc.c            |   21 +++++----------------
 sbc/sbc_primitives.c |   41 +++++++++++++++++++++++++++++++++++++++++
 sbc/sbc_primitives.h |    4 ++++
 3 files changed, 50 insertions(+), 16 deletions(-)
sbc/sbc.c +5 -16
index 365ee1f..8a2d782 100644
@@ -77,7 +77,7 @@ struct sbc_frame {
 	uint8_t joint;
 
 	/* only the lower 4 bits of every element are to be used */
-	uint8_t scale_factor[2][8];
+	uint32_t scale_factor[2][8];
 
 	/* raw integer subband samples in the frame */
 	int32_t SBC_ALIGNED sb_sample_f[16][2][8];
@@ -745,8 +745,6 @@ static SBC_ALWAYS_INLINE int sbc_pack_frame_internal(
 	uint32_t levels[2][8];	/* levels are derived from that */
 	uint32_t sb_sample_delta[2][8];
 
-	u_int32_t scalefactor[2][8];	/* derived from frame->scale_factor */
-
 	data[0] = SBC_SYNCWORD;
 
 	data[1] = (frame->frequency & 0x03) << 6;
@@ -785,19 +783,6 @@ static SBC_ALWAYS_INLINE int sbc_pack_frame_internal(
 	crc_header[1] = data[2];
 	crc_pos = 16;
 
-	for (ch = 0; ch < frame_channels; ch++) {
-		for (sb = 0; sb < frame_subbands; sb++) {
-			frame->scale_factor[ch][sb] = 0;
-			scalefactor[ch][sb] = 2 << SCALE_OUT_BITS;
-			for (blk = 0; blk < frame->blocks; blk++) {
-				while (scalefactor[ch][sb] < fabs(frame->sb_sample_f[blk][ch][sb])) {
-					frame->scale_factor[ch][sb]++;
-					scalefactor[ch][sb] *= 2;
-				}
-			}
-		}
-	}
-
 	if (frame->mode == JOINT_STEREO) {
 		/* like frame->sb_sample but joint stereo */
 		int32_t sb_sample_j[16][2];
@@ -1115,6 +1100,10 @@ int sbc_encode(sbc_t *sbc, void *input, int input_len, void *output,
 
 	samples = sbc_analyze_audio(&priv->enc_state, &priv->frame);
 
+	priv->enc_state.sbc_calc_scalefactors(
+		priv->frame.sb_sample_f, priv->frame.scale_factor,
+		priv->frame.blocks, priv->frame.channels, priv->frame.subbands);
+
 	framelen = sbc_pack_frame(output, &priv->frame, output_len);
 
 	if (written)
sbc/sbc_primitives.c +41 -0

53 lines not on this page · raw message

sbc/sbc_primitives.h +4 -0
index 5b7c9ac..2708c82 100644
@@ -58,6 +58,10 @@ struct sbc_encoder_state {
 	int (*sbc_enc_process_input_8s_be)(int position,
 			const uint8_t *pcm, int16_t X[2][SBC_X_BUFFER_SIZE],
 			int nsamples, int nchannels);
+	/* Scale factors calculation */
+	void (*sbc_calc_scalefactors)(int32_t sb_sample_f[16][2][8],
+			uint32_t scale_factor[2][8],
+			int blocks, int channels, int subbands);
 };
 
 /*
Signature · 1 line
1.5.6.5
#1 lore raw
Quoted 2 lines · On Thursday 29 January 2009 03:10:03 ext Siarhei Siamashka wrote:

The attached patch contains optimization for scale factors calculation which provides additional SBC encoder speedup.

And MMX variant of this optimization can be implemented with something like this patch. It still needs to be tested on X86-64 systems though.

Best regards,
Siarhei Siamashka

sbc/sbc_primitives_mmx.c +55 -0
index 7db4af7..41c0241 100644
@@ -275,6 +275,59 @@ static inline void sbc_analyze_4b_8s_mmx(int16_t *x, int32_t *out,
 	asm volatile ("emms\n");
 }
 
+static void sbc_calc_scalefactors_mmx(
+	int32_t sb_sample_f[16][2][8],
+	uint32_t scale_factor[2][8],
+	int blocks, int channels, int subbands)
+{
+	static const SBC_ALIGNED int32_t consts[2] = {
+		1 << SCALE_OUT_BITS,
+		1 << SCALE_OUT_BITS,
+	};
+	int ch, sb;
+	intptr_t blk;
+	for (ch = 0; ch < channels; ch++) {
+		for (sb = 0; sb < subbands; sb += 2) {
+			blk = (blocks - 1) * (((char *) &sb_sample_f[1][0][0] -
+				(char *) &sb_sample_f[0][0][0]));
+			asm volatile (
+				"movq         (%4), %%mm0\n"
+			"1:\n"
+				"movq     (%1, %0), %%mm1\n"
+				"pxor        %%mm2, %%mm2\n"
+				"pcmpgtd     %%mm2, %%mm1\n"
+				"paddd    (%1, %0), %%mm1\n"
+				"pcmpgtd     %%mm1, %%mm2\n"
+				"pxor        %%mm2, %%mm1\n"
+
+				"por         %%mm1, %%mm0\n"
+
+				"sub            %2, %0\n"
+				"jns            1b\n"
+
+				"movd        %%mm0, %k0\n"
+				"psrlq         $32, %%mm0\n"
+				"bsrl          %k0, %k0\n"
+				"subl           %5, %k0\n"
+				"movl          %k0, (%3)\n"
+
+				"movd        %%mm0, %k0\n"
+				"bsrl          %k0, %k0\n"
+				"subl           %5, %k0\n"
+				"movl          %k0, 4(%3)\n"
+			: "+r" (blk)
+			: "r" (&sb_sample_f[0][ch][sb]),
+				"i" ((char *) &sb_sample_f[1][0][0] -
+					(char *) &sb_sample_f[0][0][0]),
+				"r" (&scale_factor[ch][sb]),
+				"r" (&consts),
+				"i" (SCALE_OUT_BITS)
+			: "memory");
+		}
+	}
+	asm volatile ("emms\n");
+}
+
 static int check_mmx_support()
 {
 #ifdef __amd64__
@@ -313,6 +366,8 @@ void sbc_init_primitives_mmx(struct sbc_encoder_state *state)
 	if (check_mmx_support()) {
 		state->sbc_analyze_4b_4s = sbc_analyze_4b_4s_mmx;
 		state->sbc_analyze_4b_8s = sbc_analyze_4b_8s_mmx;
+
+		state->sbc_calc_scalefactors = sbc_calc_scalefactors_mmx;
 	}
 }
 
maintainer
#2 lore raw

Hi Siarhei,

Quoted 5 lines
Quoted 2 lines

The attached patch contains optimization for scale factors calculation which provides additional SBC encoder speedup.

And MMX variant of this optimization can be implemented with something like this patch. It still needs to be tested on X86-64 systems though.

I am holding off on this until I get confirmation that is does the right thing. So tester, please test this patch.

Regards

Marcel

#3 lore raw
Quoted 10 lines

Hi Siarhei,

Quoted 5 lines
Quoted 2 lines

The attached patch contains optimization for scale factors calculation which provides additional SBC encoder speedup.

And MMX variant of this optimization can be implemented with something like this patch. It still needs to be tested on X86-64 systems though.

I am holding off on this until I get confirmation that is does the right thing. So tester, please test this patch.

Yes, Sir!
I'll do it.

Regards,
Christian

Quoted 8 lines

Regards

Marcel

--
To unsubscribe from this list: send the line "unsubscribe linux-bluetooth"

in

Quoted 2 lines

the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#4 lore raw
In reply to Siarhei Siamashka

Hi Siarhei,

Quoted 6 lines · Siarhei Siamashka
Quoted 2 lines · On Thursday 29 January 2009 03:10:03 ext Siarhei Siamashka wrote:

The attached patch contains optimization for scale factors calculation which provides additional SBC encoder speedup.

And MMX variant of this optimization can be implemented with something like this patch. It still needs to be tested on X86-64 systems though.

Seems to work fine on my Lenovo X301 (Core 2 Duo) with ubuntu intrepid amd64.

Johan

maintainer
#5 lore raw
In reply to Siarhei Siamashka

Hi Siarhei,

Quoted 11 lines

The attached patch contains optimization for scale factors calculation which provides additional SBC encoder speedup.

For non-gcc compilers, CLZ function is implemented with a very simple and slow straightforward code (but it is still faster than current git code even if used instead of __builtin_clz). Something better could be done like:
http://groups.google.com/group/comp.sys.arm/msg/5ae56e3a95a2345e?hl=en
But I'm not sure about license/copyright of the code at this link and decided not to touch it. Anyway, I don't think that gcc implementation of __builtin_clz for the CPU cores which do not support CLZ instruction is any worse.

personally I don't really care about non-gcc compilers. I think that some of the BlueZ source might not even compile without gcc.

Anyway, patch has been applied. Thanks.

Regards

Marcel

#6 lore raw
Quoted 2 lines
Quoted 2 lines

The attached patch contains optimization for scale factors calculation

which

Quoted 3 lines
Quoted 3 lines

provides additional SBC encoder speedup.

For non-gcc compilers, CLZ function is implemented with a very simple

and

Quoted 1 line
Quoted 1 line

slow straightforward code (but it is still faster than current git code

even

Quoted 6 lines
Quoted 3 lines

if used instead of __builtin_clz). Something better could be done like:
http://groups.google.com/group/comp.sys.arm/msg/5ae56e3a95a2345e?hl=en
But I'm not sure about license/copyright of the code at this link and

decided

Quoted 2 lines

not to touch it. Anyway, I don't think that gcc implementation of __builtin_clz for the CPU cores which do not support CLZ instruction is

any

Quoted 6 lines
Quoted 1 line

worse.

personally I don't really care about non-gcc compilers. I think that some of the BlueZ source might not even compile without gcc.

Anyway, patch has been applied. Thanks.

The testing results are not positive. It is better to revoke the patch.
http://net.cs.uni-tuebingen.de/html/nexgenvoip/
http://net.cs.uni-tuebingen.de/html/nexgenvoip/html/encoder.bluez.03.wav

Greetings

Christian

#7 lore raw
Quoted 27 lines · On Thursday 29 January 2009 17:00:11 ext Christian Hoene wrote:
Quoted 1 line
Quoted 1 line

The attached patch contains optimization for scale factors calculation

which

Quoted 3 lines
Quoted 3 lines

provides additional SBC encoder speedup.

For non-gcc compilers, CLZ function is implemented with a very simple

and

Quoted 1 line
Quoted 1 line

slow straightforward code (but it is still faster than current git code

even

Quoted 8 lines
Quoted 3 lines

if used instead of __builtin_clz). Something better could be done like:
http://groups.google.com/group/comp.sys.arm/msg/5ae56e3a95a2345e?hl=en
But I'm not sure about license/copyright of the code at this link and

decided

Quoted 2 lines

not to touch it. Anyway, I don't think that gcc implementation of __builtin_clz for the CPU cores which do not support CLZ instruction is

any

Quoted 2 lines
Quoted 1 line

worse.

Quoted 8 lines
Quoted 4 lines

personally I don't really care about non-gcc compilers. I think that some of the BlueZ source might not even compile without gcc.

Anyway, patch has been applied. Thanks.

The testing results are not positive. It is better to revoke the patch.
http://net.cs.uni-tuebingen.de/html/nexgenvoip/
http://net.cs.uni-tuebingen.de/html/nexgenvoip/html/encoder.bluez.03.wav

Thanks for finding the bug. A common things about these failed testcases is that block size is not 16.

Looks like this was not covered by my own regression tests, I'll try to do something about this problem now.

Best regards,
Siarhei Siamashka

#8 lore raw
Quoted 42 lines · On Thursday 29 January 2009 17:30:31 ext Siarhei Siamashka wrote:
Quoted 32 lines · On Thursday 29 January 2009 17:00:11 ext Christian Hoene wrote:
Quoted 2 lines
Quoted 2 lines

The attached patch contains optimization for scale factors calculation

which

Quoted 3 lines
Quoted 3 lines

provides additional SBC encoder speedup.

For non-gcc compilers, CLZ function is implemented with a very simple

and

Quoted 2 lines
Quoted 2 lines

slow straightforward code (but it is still faster than current git code

even

Quoted 11 lines
Quoted 5 lines

if used instead of __builtin_clz). Something better could be done like:
http://groups.google.com/group/comp.sys.arm/msg/5ae56e3a95a2345e?hl=e
n But I'm not sure about license/copyright of the code at this link and

decided

Quoted 3 lines

not to touch it. Anyway, I don't think that gcc implementation of __builtin_clz for the CPU cores which do not support CLZ instruction is

any

Quoted 2 lines
Quoted 1 line

worse.

Quoted 8 lines
Quoted 4 lines

personally I don't really care about non-gcc compilers. I think that some of the BlueZ source might not even compile without gcc.

Anyway, patch has been applied. Thanks.

The testing results are not positive. It is better to revoke the patch.
http://net.cs.uni-tuebingen.de/html/nexgenvoip/
http://net.cs.uni-tuebingen.de/html/nexgenvoip/html/encoder.bluez.03.wav

Quoted 5 lines

Thanks for finding the bug. A common things about these failed testcases is that block size is not 16.

Looks like this was not covered by my own regression tests, I'll try to do something about this problem now.

A fix is attached. I also extended my regression test script to cover such cases.

Best regards,
Siarhei Siamashka

Quoted 1 line

From d9a207576eb9b1df3c8e820f8c34d03f7276bea9 Mon Sep 17 00:00:00 2001

From: Siarhei Siamashka <[email protected]>
Date: Thu, 29 Jan 2009 18:15:31 +0200
Subject: [PATCH] Fix for SBC encoding with block sizes other than 16

Thanks to Christian Hoene for finding and reporting the problem. This regression was intruduced in commit 19af3c49e61aa046375497108e05a3a0605da158

---
 sbc/sbc.c |   19 +++++++++++++------
 1 files changed, 13 insertions(+), 6 deletions(-)
sbc/sbc.c +13 -6
index 8a2d782..29f1d14 100644
@@ -651,30 +651,37 @@ static int sbc_analyze_audio(struct sbc_encoder_state *state,
 				struct sbc_frame *frame)
 {
 	int ch, blk;
+	int16_t *x;
 
 	switch (frame->subbands) {
 	case 4:
-		for (ch = 0; ch < frame->channels; ch++)
+		for (ch = 0; ch < frame->channels; ch++) {
+			x = &state->X[ch][state->position - 16 +
+							frame->blocks * 4];
 			for (blk = 0; blk < frame->blocks; blk += 4) {
 				state->sbc_analyze_4b_4s(
-					&state->X[ch][state->position +
-							48 - blk * 4],
+					x,
 					frame->sb_sample_f[blk][ch],
 					frame->sb_sample_f[blk + 1][ch] -
 					frame->sb_sample_f[blk][ch]);
+				x -= 16;
 			}
+		}
 		return frame->blocks * 4;
 
 	case 8:
-		for (ch = 0; ch < frame->channels; ch++)
+		for (ch = 0; ch < frame->channels; ch++) {
+			x = &state->X[ch][state->position - 32 +
+							frame->blocks * 8];
 			for (blk = 0; blk < frame->blocks; blk += 4) {
 				state->sbc_analyze_4b_8s(
-					&state->X[ch][state->position +
-							96 - blk * 8],
+					x,
 					frame->sb_sample_f[blk][ch],
 					frame->sb_sample_f[blk + 1][ch] -
 					frame->sb_sample_f[blk][ch]);
+				x -= 32;
 			}
+		}
 		return frame->blocks * 8;
 
 	default:
Signature · 1 line
1.5.6.5
maintainer
#9 lore raw

Hi Siarhei,

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

The testing results are not positive. It is better to revoke the patch.
http://net.cs.uni-tuebingen.de/html/nexgenvoip/
http://net.cs.uni-tuebingen.de/html/nexgenvoip/html/encoder.bluez.03.wav

Quoted 8 lines
Quoted 5 lines

Thanks for finding the bug. A common things about these failed testcases is that block size is not 16.

Looks like this was not covered by my own regression tests, I'll try to do something about this problem now.

A fix is attached. I also extended my regression test script to cover such cases.

patch has been applied. Thanks.

Regards

Marcel

#10 lore raw
Quoted 1 line
Quoted 1 line
Quoted 1 line

Thanks for finding the bug. A common things about these failed

testcases

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

is that block size is not 16.

Looks like this was not covered by my own regression tests, I'll try

to do

Quoted 3 lines
Quoted 3 lines
Quoted 1 line

something about this problem now.

A fix is attached. I also extended my regression test script to cover

such

Quoted 4 lines
Quoted 1 line

cases.

patch has been applied. Thanks.

The tests have passed all.
http://net.cs.uni-tuebingen.de/html/nexgenvoip/html/

Regards,

Christian

#11 lore raw
Quoted 18 lines · On Friday 30 January 2009 13:14:47 ext Christian Hoene wrote:
Quoted 1 line
Quoted 1 line
Quoted 1 line

Thanks for finding the bug. A common things about these failed

testcases

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

is that block size is not 16.

Looks like this was not covered by my own regression tests, I'll try

to do

Quoted 3 lines
Quoted 3 lines
Quoted 1 line

something about this problem now.

A fix is attached. I also extended my regression test script to cover

such

Quoted 2 lines
Quoted 1 line

cases.

Quoted 8 lines
Quoted 1 line

patch has been applied. Thanks.

The tests have passed all.
http://net.cs.uni-tuebingen.de/html/nexgenvoip/html/

Regards,

Christian

Thanks a lot for keeping an eye on bluez sbc implementation quality.

Signature · 2 lines
Best regards,
Siarhei Siamashka
#13 lore raw
Quoted 4 lines · On Sunday 01 February 2009 18:53:43 ext Marcel Holtmann wrote:

Hi Siarhei,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line

cases.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

patch has been applied. Thanks.

The tests have passed all.
http://net.cs.uni-tuebingen.de/html/nexgenvoip/html/

Thanks a lot for keeping an eye on bluez sbc implementation quality.

are all patches applied now or am I missing one?

Yes, thanks, all the patches have been applied except for this one:
http://marc.info/?l=linux-bluetooth&m=123319235104928&w=2

It should work fine, but I'm still considering how to better optimize processing of joint stereo, which might also require changing this code a bit. For now it's more like a demonstration that scale factors processing can be SIMD optimized quite fine.

I will provide a more complete patch with optimizations for both scale factors and joint stereo a bit later.

Signature · 2 lines
Best regards,
Siarhei Siamashka
maintainer
#14 lore raw

Hi Siarhei,

Quoted 14 lines
Quoted 10 lines
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

cases.

patch has been applied. Thanks.

The tests have passed all.
http://net.cs.uni-tuebingen.de/html/nexgenvoip/html/

Thanks a lot for keeping an eye on bluez sbc implementation quality.

are all patches applied now or am I missing one?

Yes, thanks, all the patches have been applied except for this one:
http://marc.info/?l=linux-bluetooth&m=123319235104928&w=2

Quoted 7 lines

It should work fine, but I'm still considering how to better optimize processing of joint stereo, which might also require changing this code a bit. For now it's more like a demonstration that scale factors processing can be SIMD optimized quite fine.

I will provide a more complete patch with optimizations for both scale factors and joint stereo a bit later.

please re-sent this one with a proper commit message and I apply it. Then you can start from there is you like. Or send me a complete new one.

Regards

Marcel

#15 lore raw
Quoted 21 lines · On Monday 02 February 2009 17:20:56 ext Marcel Holtmann wrote:

Hi Siarhei,

Quoted 19 lines
Quoted 10 lines
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

cases.

patch has been applied. Thanks.

The tests have passed all.
http://net.cs.uni-tuebingen.de/html/nexgenvoip/html/

Thanks a lot for keeping an eye on bluez sbc implementation quality.

are all patches applied now or am I missing one?

Yes, thanks, all the patches have been applied except for this one:
http://marc.info/?l=linux-bluetooth&m=123319235104928&w=2

It should work fine, but I'm still considering how to better optimize processing of joint stereo, which might also require changing this code a bit. For now it's more like a demonstration that scale factors processing can be SIMD optimized quite fine.

Quoted 6 lines
Quoted 2 lines

I will provide a more complete patch with optimizations for both scale factors and joint stereo a bit later.

please re-sent this one with a proper commit message and I apply it. Then you can start from there is you like. Or send me a complete new one.

Done. Commit message added and patch attached.

time ./sbcenc tesfile.au > /dev/null

before:

real    0m6.404s
user    0m6.152s
sys     0m0.244s

after:

real    0m5.630s
user    0m5.376s
sys     0m0.248s
Signature · 2 lines
Best regards,
Siarhei Siamashka

From 83bece7b4642dde20b76253b7d18228d6654fa70 Mon Sep 17 00:00:00 2001
From: Siarhei Siamashka <[email protected]>
Date: Mon, 16 Mar 2009 21:10:32 +0200
Subject: [PATCH] sbc: MMX optimization for scale factors calculation

---
 sbc/sbc_primitives_mmx.c |   54 ++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 54 insertions(+), 0 deletions(-)
sbc/sbc_primitives_mmx.c +54 -0
index 08e9ca2..d8373b3 100644
@@ -275,6 +275,59 @@ static inline void sbc_analyze_4b_8s_mmx(int16_t *x, int32_t *out,
 	asm volatile ("emms\n");
 }
 
+static void sbc_calc_scalefactors_mmx(
+	int32_t sb_sample_f[16][2][8],
+	uint32_t scale_factor[2][8],
+	int blocks, int channels, int subbands)
+{
+	static const SBC_ALIGNED int32_t consts[2] = {
+		1 << SCALE_OUT_BITS,
+		1 << SCALE_OUT_BITS,
+	};
+	int ch, sb;
+	intptr_t blk;
+	for (ch = 0; ch < channels; ch++) {
+		for (sb = 0; sb < subbands; sb += 2) {
+			blk = (blocks - 1) * (((char *) &sb_sample_f[1][0][0] -
+				(char *) &sb_sample_f[0][0][0]));
+			asm volatile (
+				"movq         (%4), %%mm0\n"
+			"1:\n"
+				"movq     (%1, %0), %%mm1\n"
+				"pxor        %%mm2, %%mm2\n"
+				"pcmpgtd     %%mm2, %%mm1\n"
+				"paddd    (%1, %0), %%mm1\n"
+				"pcmpgtd     %%mm1, %%mm2\n"
+				"pxor        %%mm2, %%mm1\n"
+
+				"por         %%mm1, %%mm0\n"
+
+				"sub            %2, %0\n"
+				"jns            1b\n"
+
+				"movd        %%mm0, %k0\n"
+				"psrlq         $32, %%mm0\n"
+				"bsrl          %k0, %k0\n"
+				"subl           %5, %k0\n"
+				"movl          %k0, (%3)\n"
+
+				"movd        %%mm0, %k0\n"
+				"bsrl          %k0, %k0\n"
+				"subl           %5, %k0\n"
+				"movl          %k0, 4(%3)\n"
+			: "+r" (blk)
+			: "r" (&sb_sample_f[0][ch][sb]),
+				"i" ((char *) &sb_sample_f[1][0][0] -
+					(char *) &sb_sample_f[0][0][0]),
+				"r" (&scale_factor[ch][sb]),
+				"r" (&consts),
+				"i" (SCALE_OUT_BITS)
+			: "memory");
+		}
+	}
+	asm volatile ("emms\n");
+}
+
 static int check_mmx_support(void)
 {
 #ifdef __amd64__
@@ -313,6 +366,7 @@ void sbc_init_primitives_mmx(struct sbc_encoder_state *state)
 	if (check_mmx_support()) {
 		state->sbc_analyze_4b_4s = sbc_analyze_4b_4s_mmx;
 		state->sbc_analyze_4b_8s = sbc_analyze_4b_8s_mmx;
+		state->sbc_calc_scalefactors = sbc_calc_scalefactors_mmx;
 		state->implementation_info = "MMX";
 	}
 }
Signature · 1 line
1.5.6.5
#16 lore raw
In reply to Marcel Holtmann
Quoted 15 lines · On Thursday 29 January 2009 09:27:38 ext Marcel Holtmann wrote:

Hi Siarhei,

Quoted 12 lines

The attached patch contains optimization for scale factors calculation which provides additional SBC encoder speedup.

For non-gcc compilers, CLZ function is implemented with a very simple and slow straightforward code (but it is still faster than current git code even if used instead of __builtin_clz). Something better could be done like:
http://groups.google.com/group/comp.sys.arm/msg/5ae56e3a95a2345e?hl=en
But I'm not sure about license/copyright of the code at this link and decided not to touch it. Anyway, I don't think that gcc implementation of __builtin_clz for the CPU cores which do not support CLZ instruction is any worse.

Quoted 2 lines

personally I don't really care about non-gcc compilers. I think that some of the BlueZ source might not even compile without gcc.

At least for the SBC codec, there is no harm in trying to keep it as portable as it is possible. Somebody might want to compile it with some other, better optimizing compiler and link with the rest of gcc compiled bluez. Or SBC processing can be offloaded to DSP (C55x port exists) with the rest of bluez stack running on main core.

Quoted 1 line

Anyway, patch has been applied. Thanks.

Thanks for applying it.

Signature · 2 lines
Best regards,
Siarhei Siamashka