mirror of
https://gitlab.freedesktop.org/pipewire/pipewire.git
synced 2025-11-02 09:01:50 -05:00
audioconvert: avoid even more precision loss in F32 to S32 conversion
This is somewhat similar to the S32->F32 conversion improvements, but here things a bit more tricky... The main consideration is that the limits to which we clamp must be valid 32-bit signed integers, but not all such integers are exactly losslessly representable in `float32_t`. For example it we'd clamp to `2147483647`, that is actually a `2147483648.0f`, and `2147483648` is not a valid 32-bit signed integer, so the post-clamp conversion would basically be UB. We don't have this problem for negative bound, though. But as we know, any 25-bit signed integer is losslessly round-trippable through float32_t, and since multiplying by 2 only changes the float's exponent, we can clamp to `2147483520`! The algorithm of selection of the pre-clamping scale is unaffected. This additionally avoids right-shift, and thus is even faster. As `test_lossless_s32_lossless_subset` shows, if the integer is in the form of s25+shift, the maximal absolute error is finally zero. Without going through `float`->`double`->`int`, i'm not sure if the `float`->`int` conversion can be improved further.
This commit is contained in:
parent
f4c89b1b40
commit
7c40cafa7c
4 changed files with 50 additions and 70 deletions
|
|
@ -335,7 +335,7 @@ conv_s32_to_f32d_1s_sse2(void *data, void * SPA_RESTRICT dst[], const void * SPA
|
|||
float *d0 = dst[0];
|
||||
uint32_t n, unrolled;
|
||||
__m128i in;
|
||||
__m128 out, factor = _mm_set1_ps(1.0f / S32_SCALE);
|
||||
__m128 out, factor = _mm_set1_ps(1.0f / S32_SCALE_I2F);
|
||||
|
||||
if (SPA_IS_ALIGNED(d0, 16))
|
||||
unrolled = n_samples & ~3;
|
||||
|
|
@ -380,9 +380,9 @@ conv_f32d_to_s32_1s_sse2(void *data, void * SPA_RESTRICT dst, const void * SPA_R
|
|||
uint32_t n, unrolled;
|
||||
__m128 in[1];
|
||||
__m128i out[4];
|
||||
__m128 scale = _mm_set1_ps(S25_SCALE);
|
||||
__m128 int_min = _mm_set1_ps(S25_MIN);
|
||||
__m128 int_max = _mm_set1_ps(S25_MAX);
|
||||
__m128 scale = _mm_set1_ps(S32_SCALE_F2I);
|
||||
__m128 int_min = _mm_set1_ps(S32_MIN_F2I);
|
||||
__m128 int_max = _mm_set1_ps(S32_MAX_F2I);
|
||||
|
||||
if (SPA_IS_ALIGNED(s0, 16))
|
||||
unrolled = n_samples & ~3;
|
||||
|
|
@ -393,7 +393,6 @@ conv_f32d_to_s32_1s_sse2(void *data, void * SPA_RESTRICT dst, const void * SPA_R
|
|||
in[0] = _mm_mul_ps(_mm_load_ps(&s0[n]), scale);
|
||||
in[0] = _MM_CLAMP_PS(in[0], int_min, int_max);
|
||||
out[0] = _mm_cvtps_epi32(in[0]);
|
||||
out[0] = _mm_slli_epi32(out[0], 7);
|
||||
out[1] = _mm_shuffle_epi32(out[0], _MM_SHUFFLE(0, 3, 2, 1));
|
||||
out[2] = _mm_shuffle_epi32(out[0], _MM_SHUFFLE(1, 0, 3, 2));
|
||||
out[3] = _mm_shuffle_epi32(out[0], _MM_SHUFFLE(2, 1, 0, 3));
|
||||
|
|
@ -408,7 +407,7 @@ conv_f32d_to_s32_1s_sse2(void *data, void * SPA_RESTRICT dst, const void * SPA_R
|
|||
in[0] = _mm_load_ss(&s0[n]);
|
||||
in[0] = _mm_mul_ss(in[0], scale);
|
||||
in[0] = _MM_CLAMP_SS(in[0], int_min, int_max);
|
||||
*d = _mm_cvtss_si32(in[0]) << 7;
|
||||
*d = _mm_cvtss_si32(in[0]);
|
||||
d += n_channels;
|
||||
}
|
||||
}
|
||||
|
|
@ -422,9 +421,9 @@ conv_f32d_to_s32_2s_sse2(void *data, void * SPA_RESTRICT dst, const void * SPA_R
|
|||
uint32_t n, unrolled;
|
||||
__m128 in[2];
|
||||
__m128i out[2], t[2];
|
||||
__m128 scale = _mm_set1_ps(S25_SCALE);
|
||||
__m128 int_min = _mm_set1_ps(S25_MIN);
|
||||
__m128 int_max = _mm_set1_ps(S25_MAX);
|
||||
__m128 scale = _mm_set1_ps(S32_SCALE_F2I);
|
||||
__m128 int_min = _mm_set1_ps(S32_MIN_F2I);
|
||||
__m128 int_max = _mm_set1_ps(S32_MAX_F2I);
|
||||
|
||||
if (SPA_IS_ALIGNED(s0, 16) &&
|
||||
SPA_IS_ALIGNED(s1, 16))
|
||||
|
|
@ -441,8 +440,6 @@ conv_f32d_to_s32_2s_sse2(void *data, void * SPA_RESTRICT dst, const void * SPA_R
|
|||
|
||||
out[0] = _mm_cvtps_epi32(in[0]);
|
||||
out[1] = _mm_cvtps_epi32(in[1]);
|
||||
out[0] = _mm_slli_epi32(out[0], 7);
|
||||
out[1] = _mm_slli_epi32(out[1], 7);
|
||||
|
||||
t[0] = _mm_unpacklo_epi32(out[0], out[1]);
|
||||
t[1] = _mm_unpackhi_epi32(out[0], out[1]);
|
||||
|
|
@ -462,7 +459,6 @@ conv_f32d_to_s32_2s_sse2(void *data, void * SPA_RESTRICT dst, const void * SPA_R
|
|||
in[0] = _mm_mul_ps(in[0], scale);
|
||||
in[0] = _MM_CLAMP_PS(in[0], int_min, int_max);
|
||||
out[0] = _mm_cvtps_epi32(in[0]);
|
||||
out[0] = _mm_slli_epi32(out[0], 7);
|
||||
_mm_storel_epi64((__m128i*)d, out[0]);
|
||||
d += n_channels;
|
||||
}
|
||||
|
|
@ -477,9 +473,9 @@ conv_f32d_to_s32_4s_sse2(void *data, void * SPA_RESTRICT dst, const void * SPA_R
|
|||
uint32_t n, unrolled;
|
||||
__m128 in[4];
|
||||
__m128i out[4];
|
||||
__m128 scale = _mm_set1_ps(S25_SCALE);
|
||||
__m128 int_min = _mm_set1_ps(S25_MIN);
|
||||
__m128 int_max = _mm_set1_ps(S25_MAX);
|
||||
__m128 scale = _mm_set1_ps(S32_SCALE_F2I);
|
||||
__m128 int_min = _mm_set1_ps(S32_MIN_F2I);
|
||||
__m128 int_max = _mm_set1_ps(S32_MAX_F2I);
|
||||
|
||||
if (SPA_IS_ALIGNED(s0, 16) &&
|
||||
SPA_IS_ALIGNED(s1, 16) &&
|
||||
|
|
@ -506,10 +502,6 @@ conv_f32d_to_s32_4s_sse2(void *data, void * SPA_RESTRICT dst, const void * SPA_R
|
|||
out[1] = _mm_cvtps_epi32(in[1]);
|
||||
out[2] = _mm_cvtps_epi32(in[2]);
|
||||
out[3] = _mm_cvtps_epi32(in[3]);
|
||||
out[0] = _mm_slli_epi32(out[0], 7);
|
||||
out[1] = _mm_slli_epi32(out[1], 7);
|
||||
out[2] = _mm_slli_epi32(out[2], 7);
|
||||
out[3] = _mm_slli_epi32(out[3], 7);
|
||||
|
||||
_mm_storeu_si128((__m128i*)(d + 0*n_channels), out[0]);
|
||||
_mm_storeu_si128((__m128i*)(d + 1*n_channels), out[1]);
|
||||
|
|
@ -530,7 +522,6 @@ conv_f32d_to_s32_4s_sse2(void *data, void * SPA_RESTRICT dst, const void * SPA_R
|
|||
in[0] = _mm_mul_ps(in[0], scale);
|
||||
in[0] = _MM_CLAMP_PS(in[0], int_min, int_max);
|
||||
out[0] = _mm_cvtps_epi32(in[0]);
|
||||
out[0] = _mm_slli_epi32(out[0], 7);
|
||||
_mm_storeu_si128((__m128i*)d, out[0]);
|
||||
d += n_channels;
|
||||
}
|
||||
|
|
@ -629,9 +620,9 @@ conv_f32d_to_s32_1s_noise_sse2(struct convert *conv, void * SPA_RESTRICT dst, co
|
|||
uint32_t n, unrolled;
|
||||
__m128 in[1];
|
||||
__m128i out[4];
|
||||
__m128 scale = _mm_set1_ps(S25_SCALE);
|
||||
__m128 int_min = _mm_set1_ps(S25_MIN);
|
||||
__m128 int_max = _mm_set1_ps(S25_MAX);
|
||||
__m128 scale = _mm_set1_ps(S32_SCALE_F2I);
|
||||
__m128 int_min = _mm_set1_ps(S32_MIN_F2I);
|
||||
__m128 int_max = _mm_set1_ps(S32_MAX_F2I);
|
||||
|
||||
if (SPA_IS_ALIGNED(s, 16))
|
||||
unrolled = n_samples & ~3;
|
||||
|
|
@ -643,7 +634,6 @@ conv_f32d_to_s32_1s_noise_sse2(struct convert *conv, void * SPA_RESTRICT dst, co
|
|||
in[0] = _mm_add_ps(in[0], _mm_load_ps(&noise[n]));
|
||||
in[0] = _MM_CLAMP_PS(in[0], int_min, int_max);
|
||||
out[0] = _mm_cvtps_epi32(in[0]);
|
||||
out[0] = _mm_slli_epi32(out[0], 7);
|
||||
out[1] = _mm_shuffle_epi32(out[0], _MM_SHUFFLE(0, 3, 2, 1));
|
||||
out[2] = _mm_shuffle_epi32(out[0], _MM_SHUFFLE(1, 0, 3, 2));
|
||||
out[3] = _mm_shuffle_epi32(out[0], _MM_SHUFFLE(2, 1, 0, 3));
|
||||
|
|
@ -659,7 +649,7 @@ conv_f32d_to_s32_1s_noise_sse2(struct convert *conv, void * SPA_RESTRICT dst, co
|
|||
in[0] = _mm_mul_ss(in[0], scale);
|
||||
in[0] = _mm_add_ss(in[0], _mm_load_ss(&noise[n]));
|
||||
in[0] = _MM_CLAMP_SS(in[0], int_min, int_max);
|
||||
*d = _mm_cvtss_si32(in[0]) << 7;
|
||||
*d = _mm_cvtss_si32(in[0]);
|
||||
d += n_channels;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue