sconv: Change/fix conversion to/from float32

use (1<<15) instead of 0x7fff as a factor when converting from s16 to float32
use (1<<31) instead of 0x7fffffff as a factor when converting from s32 to float32

the change is motivated by the following desireable properties:
* s16_from_f32(f32_from_s16(x)) == x for all possible s16 values
* x / (1.0f << 15) == x * (1.0f / (1 << 15)) for all x in s16

above changes enable easier optimization while guaranteeing bit-exact results

further, other audio sample conversion code (libavresample) does it the same way

v3 (comments Tanu):
* fix saturation in pa_sconv_s16le_from_f32ne_neon(), use vqrshrn
v2 (comments Tanu):
* fix comments in ARM NEON code
* use llrintf() in pa_sconv_s32le_from_float32ne()

Signed-off-by: Peter Meerwald <p.meerwald@bct-electronic.com>
Cc: Tanu Kaskinen <tanuk@iki.fi>
This commit is contained in:
Peter Meerwald 2013-02-04 01:30:19 +01:00 committed by Tanu Kaskinen
parent 596d9aa740
commit e66e846418
3 changed files with 79 additions and 96 deletions

View file

@ -35,17 +35,13 @@
#if !defined(__APPLE__) && defined (__i386__) || defined (__amd64__)
static const PA_DECLARE_ALIGNED (16, float, one[4]) = { 1.0, 1.0, 1.0, 1.0 };
static const PA_DECLARE_ALIGNED (16, float, mone[4]) = { -1.0, -1.0, -1.0, -1.0 };
static const PA_DECLARE_ALIGNED (16, float, scale[4]) = { 0x7fff, 0x7fff, 0x7fff, 0x7fff };
static const PA_DECLARE_ALIGNED (16, float, scale[4]) = { 0x8000, 0x8000, 0x8000, 0x8000 };
static void pa_sconv_s16le_from_f32ne_sse(unsigned n, const float *a, int16_t *b) {
pa_reg_x86 temp, i;
__asm__ __volatile__ (
" movaps %5, %%xmm5 \n\t"
" movaps %6, %%xmm6 \n\t"
" movaps %7, %%xmm7 \n\t"
" xor %0, %0 \n\t"
" mov %4, %1 \n\t"
@ -56,12 +52,8 @@ static void pa_sconv_s16le_from_f32ne_sse(unsigned n, const float *a, int16_t *b
"1: \n\t"
" movups (%q2, %0, 2), %%xmm0 \n\t" /* read 8 floats */
" movups 16(%q2, %0, 2), %%xmm2 \n\t"
" minps %%xmm5, %%xmm0 \n\t" /* clamp to 1.0 */
" minps %%xmm5, %%xmm2 \n\t"
" maxps %%xmm6, %%xmm0 \n\t" /* clamp to -1.0 */
" maxps %%xmm6, %%xmm2 \n\t"
" mulps %%xmm7, %%xmm0 \n\t" /* *= 0x7fff */
" mulps %%xmm7, %%xmm2 \n\t"
" mulps %%xmm5, %%xmm0 \n\t" /* *= 0x8000 */
" mulps %%xmm5, %%xmm2 \n\t"
" cvtps2pi %%xmm0, %%mm0 \n\t" /* low part to int */
" cvtps2pi %%xmm2, %%mm2 \n\t"
@ -73,7 +65,7 @@ static void pa_sconv_s16le_from_f32ne_sse(unsigned n, const float *a, int16_t *b
" packssdw %%mm1, %%mm0 \n\t" /* pack parts */
" packssdw %%mm3, %%mm2 \n\t"
" movq %%mm0, (%q3, %0) \n\t"
" movq %%mm2, 8(%q3, %0) \n\t"
" movq %%mm2, 8(%q3, %0) \n\t"
" add $16, %0 \n\t"
" dec %1 \n\t"
@ -82,24 +74,30 @@ static void pa_sconv_s16le_from_f32ne_sse(unsigned n, const float *a, int16_t *b
"2: \n\t"
" mov %4, %1 \n\t" /* prepare for leftovers */
" and $7, %1 \n\t"
" je 4f \n\t"
" je 5f \n\t"
"3: \n\t"
" movss (%q2, %0, 2), %%xmm0 \n\t"
" minss %%xmm5, %%xmm0 \n\t"
" maxss %%xmm6, %%xmm0 \n\t"
" mulss %%xmm7, %%xmm0 \n\t"
" mulss %%xmm5, %%xmm0 \n\t"
" cvtss2si %%xmm0, %4 \n\t"
" movw %w4, (%q3, %0) \n\t"
" add $0x8000, %4 \n\t" /* check for saturation */
" and $~0xffff, %4 \n\t"
" cvtss2si %%xmm0, %4 \n\t"
" je 4f \n\t"
" sar $31, %4 \n\t"
" xor $0x7fff, %4 \n\t"
"4: \n\t"
" movw %w4, (%q3, %0) \n\t" /* store leftover */
" add $2, %0 \n\t"
" dec %1 \n\t"
" jne 3b \n\t"
"4: \n\t"
"5: \n\t"
" emms \n\t"
: "=&r" (i), "=&r" (temp)
: "r" (a), "r" (b), "r" ((pa_reg_x86)n), "m" (*one), "m" (*mone), "m" (*scale)
: "r" (a), "r" (b), "r" ((pa_reg_x86)n), "m" (*scale)
: "cc", "memory"
);
}
@ -109,8 +107,6 @@ static void pa_sconv_s16le_from_f32ne_sse2(unsigned n, const float *a, int16_t *
__asm__ __volatile__ (
" movaps %5, %%xmm5 \n\t"
" movaps %6, %%xmm6 \n\t"
" movaps %7, %%xmm7 \n\t"
" xor %0, %0 \n\t"
" mov %4, %1 \n\t"
@ -121,12 +117,8 @@ static void pa_sconv_s16le_from_f32ne_sse2(unsigned n, const float *a, int16_t *
"1: \n\t"
" movups (%q2, %0, 2), %%xmm0 \n\t" /* read 8 floats */
" movups 16(%q2, %0, 2), %%xmm2 \n\t"
" minps %%xmm5, %%xmm0 \n\t" /* clamp to 1.0 */
" minps %%xmm5, %%xmm2 \n\t"
" maxps %%xmm6, %%xmm0 \n\t" /* clamp to -1.0 */
" maxps %%xmm6, %%xmm2 \n\t"
" mulps %%xmm7, %%xmm0 \n\t" /* *= 0x7fff */
" mulps %%xmm7, %%xmm2 \n\t"
" mulps %%xmm5, %%xmm0 \n\t" /* *= 0x8000 */
" mulps %%xmm5, %%xmm2 \n\t"
" cvtps2dq %%xmm0, %%xmm0 \n\t"
" cvtps2dq %%xmm2, %%xmm2 \n\t"
@ -141,23 +133,29 @@ static void pa_sconv_s16le_from_f32ne_sse2(unsigned n, const float *a, int16_t *
"2: \n\t"
" mov %4, %1 \n\t" /* prepare for leftovers */
" and $7, %1 \n\t"
" je 4f \n\t"
" je 5f \n\t"
"3: \n\t"
" movss (%q2, %0, 2), %%xmm0 \n\t"
" minss %%xmm5, %%xmm0 \n\t"
" maxss %%xmm6, %%xmm0 \n\t"
" mulss %%xmm7, %%xmm0 \n\t"
" mulss %%xmm5, %%xmm0 \n\t"
" cvtss2si %%xmm0, %4 \n\t"
" movw %w4, (%q3, %0) \n\t"
" add $0x8000, %4 \n\t"
" and $~0xffff, %4 \n\t" /* check for saturation */
" cvtss2si %%xmm0, %4 \n\t"
" je 4f \n\t"
" sar $31, %4 \n\t"
" xor $0x7fff, %4 \n\t"
"4: \n\t"
" movw %w4, (%q3, %0) \n\t" /* store leftover */
" add $2, %0 \n\t"
" dec %1 \n\t"
" jne 3b \n\t"
"4: \n\t"
"5: \n\t"
: "=&r" (i), "=&r" (temp)
: "r" (a), "r" (b), "r" ((pa_reg_x86)n), "m" (*one), "m" (*mone), "m" (*scale)
: "r" (a), "r" (b), "r" ((pa_reg_x86)n), "m" (*scale)
: "cc", "memory"
);
}