audioconvert: somewhat avoid precision loss in S32 to F32 conversion

At the very least, we should go through s25_32 intermediate
instead of s24_32, to avoid needlessly loosing 1 LSB precision bit.

That being said, i suspect it's still not doing the right thing.
Why are we silently dropping those 7 LSB bits?
Is that really the way to do it?
This commit is contained in:
Roman Lebedev 2024-06-14 02:00:27 +03:00
parent 175d533b56
commit c517865864
No known key found for this signature in database
GPG key ID: 083C3EBB4A1689E0
4 changed files with 38 additions and 46 deletions

View file

@ -316,7 +316,7 @@ conv_s32_to_f32d_4s_avx2(void *data, void * SPA_RESTRICT dst[], const void * SPA
float *d0 = dst[0], *d1 = dst[1], *d2 = dst[2], *d3 = dst[3];
uint32_t n, unrolled;
__m256i in[4];
__m256 out[4], factor = _mm256_set1_ps(1.0f / S24_SCALE);
__m256 out[4], factor = _mm256_set1_ps(1.0f / S25_SCALE);
__m256i mask1 = _mm256_setr_epi32(0*n_channels, 1*n_channels, 2*n_channels, 3*n_channels,
4*n_channels, 5*n_channels, 6*n_channels, 7*n_channels);
@ -334,10 +334,10 @@ conv_s32_to_f32d_4s_avx2(void *data, void * SPA_RESTRICT dst[], const void * SPA
in[2] = _mm256_i32gather_epi32((int*)&s[2], mask1, 4);
in[3] = _mm256_i32gather_epi32((int*)&s[3], mask1, 4);
in[0] = _mm256_srai_epi32(in[0], 8);
in[1] = _mm256_srai_epi32(in[1], 8);
in[2] = _mm256_srai_epi32(in[2], 8);
in[3] = _mm256_srai_epi32(in[3], 8);
in[0] = _mm256_srai_epi32(in[0], 7);
in[1] = _mm256_srai_epi32(in[1], 7);
in[2] = _mm256_srai_epi32(in[2], 7);
in[3] = _mm256_srai_epi32(in[3], 7);
out[0] = _mm256_cvtepi32_ps(in[0]);
out[1] = _mm256_cvtepi32_ps(in[1]);
@ -357,11 +357,11 @@ conv_s32_to_f32d_4s_avx2(void *data, void * SPA_RESTRICT dst[], const void * SPA
s += 8*n_channels;
}
for(; n < n_samples; n++) {
__m128 out[4], factor = _mm_set1_ps(1.0f / S24_SCALE);
out[0] = _mm_cvtsi32_ss(factor, s[0] >> 8);
out[1] = _mm_cvtsi32_ss(factor, s[1] >> 8);
out[2] = _mm_cvtsi32_ss(factor, s[2] >> 8);
out[3] = _mm_cvtsi32_ss(factor, s[3] >> 8);
__m128 out[4], factor = _mm_set1_ps(1.0f / S25_SCALE);
out[0] = _mm_cvtsi32_ss(factor, s[0] >> 7);
out[1] = _mm_cvtsi32_ss(factor, s[1] >> 7);
out[2] = _mm_cvtsi32_ss(factor, s[2] >> 7);
out[3] = _mm_cvtsi32_ss(factor, s[3] >> 7);
out[0] = _mm_mul_ss(out[0], factor);
out[1] = _mm_mul_ss(out[1], factor);
out[2] = _mm_mul_ss(out[2], factor);
@ -382,7 +382,7 @@ conv_s32_to_f32d_2s_avx2(void *data, void * SPA_RESTRICT dst[], const void * SPA
float *d0 = dst[0], *d1 = dst[1];
uint32_t n, unrolled;
__m256i in[4];
__m256 out[4], factor = _mm256_set1_ps(1.0f / S24_SCALE);
__m256 out[4], factor = _mm256_set1_ps(1.0f / S25_SCALE);
__m256i mask1 = _mm256_setr_epi32(0*n_channels, 1*n_channels, 2*n_channels, 3*n_channels,
4*n_channels, 5*n_channels, 6*n_channels, 7*n_channels);
@ -396,8 +396,8 @@ conv_s32_to_f32d_2s_avx2(void *data, void * SPA_RESTRICT dst[], const void * SPA
in[0] = _mm256_i32gather_epi32((int*)&s[0], mask1, 4);
in[1] = _mm256_i32gather_epi32((int*)&s[1], mask1, 4);
in[0] = _mm256_srai_epi32(in[0], 8);
in[1] = _mm256_srai_epi32(in[1], 8);
in[0] = _mm256_srai_epi32(in[0], 7);
in[1] = _mm256_srai_epi32(in[1], 7);
out[0] = _mm256_cvtepi32_ps(in[0]);
out[1] = _mm256_cvtepi32_ps(in[1]);
@ -411,9 +411,9 @@ conv_s32_to_f32d_2s_avx2(void *data, void * SPA_RESTRICT dst[], const void * SPA
s += 8*n_channels;
}
for(; n < n_samples; n++) {
__m128 out[2], factor = _mm_set1_ps(1.0f / S24_SCALE);
out[0] = _mm_cvtsi32_ss(factor, s[0] >> 8);
out[1] = _mm_cvtsi32_ss(factor, s[1] >> 8);
__m128 out[2], factor = _mm_set1_ps(1.0f / S25_SCALE);
out[0] = _mm_cvtsi32_ss(factor, s[0] >> 7);
out[1] = _mm_cvtsi32_ss(factor, s[1] >> 7);
out[0] = _mm_mul_ss(out[0], factor);
out[1] = _mm_mul_ss(out[1], factor);
_mm_store_ss(&d0[n], out[0]);
@ -430,7 +430,7 @@ conv_s32_to_f32d_1s_avx2(void *data, void * SPA_RESTRICT dst[], const void * SPA
float *d0 = dst[0];
uint32_t n, unrolled;
__m256i in[2];
__m256 out[2], factor = _mm256_set1_ps(1.0f / S24_SCALE);
__m256 out[2], factor = _mm256_set1_ps(1.0f / S25_SCALE);
__m256i mask1 = _mm256_setr_epi32(0*n_channels, 1*n_channels, 2*n_channels, 3*n_channels,
4*n_channels, 5*n_channels, 6*n_channels, 7*n_channels);
@ -443,8 +443,8 @@ conv_s32_to_f32d_1s_avx2(void *data, void * SPA_RESTRICT dst[], const void * SPA
in[0] = _mm256_i32gather_epi32(&s[0*n_channels], mask1, 4);
in[1] = _mm256_i32gather_epi32(&s[8*n_channels], mask1, 4);
in[0] = _mm256_srai_epi32(in[0], 8);
in[1] = _mm256_srai_epi32(in[1], 8);
in[0] = _mm256_srai_epi32(in[0], 7);
in[1] = _mm256_srai_epi32(in[1], 7);
out[0] = _mm256_cvtepi32_ps(in[0]);
out[1] = _mm256_cvtepi32_ps(in[1]);
@ -458,8 +458,8 @@ conv_s32_to_f32d_1s_avx2(void *data, void * SPA_RESTRICT dst[], const void * SPA
s += 16*n_channels;
}
for(; n < n_samples; n++) {
__m128 out, factor = _mm_set1_ps(1.0f / S24_SCALE);
out = _mm_cvtsi32_ss(factor, s[0] >> 8);
__m128 out, factor = _mm_set1_ps(1.0f / S25_SCALE);
out = _mm_cvtsi32_ss(factor, s[0] >> 7);
out = _mm_mul_ss(out, factor);
_mm_store_ss(&d0[n], out);
s += n_channels;

View file

@ -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 / S24_SCALE);
__m128 out, factor = _mm_set1_ps(1.0f / S25_SCALE);
if (SPA_IS_ALIGNED(d0, 16))
unrolled = n_samples & ~3;
@ -347,14 +347,14 @@ conv_s32_to_f32d_1s_sse2(void *data, void * SPA_RESTRICT dst[], const void * SPA
s[1*n_channels],
s[2*n_channels],
s[3*n_channels]);
in = _mm_srai_epi32(in, 8);
in = _mm_srai_epi32(in, 7);
out = _mm_cvtepi32_ps(in);
out = _mm_mul_ps(out, factor);
_mm_store_ps(&d0[n], out);
s += 4*n_channels;
}
for(; n < n_samples; n++) {
out = _mm_cvtsi32_ss(factor, s[0]>>8);
out = _mm_cvtsi32_ss(factor, s[0]>>7);
out = _mm_mul_ss(out, factor);
_mm_store_ss(&d0[n], out);
s += n_channels;

View file

@ -121,7 +121,7 @@
#define S32_MIN (S24_MIN * 256)
#define S32_MAX (S24_MAX * 256)
#define S32_TO_F32(v) ITOF(int32_t, S32_TO_S24_32(v), S24_SCALE, 0.0f)
#define S32_TO_F32(v) ITOF(int32_t, S32_TO_S25_32(v), S25_SCALE, 0.0f)
#define S32S_TO_F32(v) S32_TO_F32(bswap_32(v))
#define F32_TO_S32_D(v,d) S25_32_TO_S32(F32_TO_S25_32_D(v,d))
#define F32_TO_S32(v) F32_TO_S32_D(v, 0.0f)

View file

@ -335,10 +335,12 @@ static void test_s32_f32(void)
0x80000100, 0x40000000, 0xc0000000, 0x0080, 0xFFFFFF80, 0x0100,
0xFFFFFF00, 0x0200, 0xFFFFFE00
};
static const float out[] = { 0.e+00f, 9.9999988079071044921875e-01f, -1.e+00f,
static const float out[] = { 0.e+00f, 9.99999940395355224609375e-01f, -1.e+00f,
9.9999988079071044921875e-01f, -9.9999988079071044921875e-01f, 5.e-01f,
-5.e-01f, 0.e+00f, -1.1920928955078125e-07f, 1.1920928955078125e-07f,
-1.1920928955078125e-07f, 2.384185791015625e-07f, -2.384185791015625e-07f
-5.e-01f, 5.9604644775390625e-08f, -5.9604644775390625e-08f,
1.1920928955078125e-07f, -1.1920928955078125e-07f,
2.384185791015625e-07f, -2.384185791015625e-07f
};
run_test("test_s32_f32d", in, sizeof(in[0]), out, sizeof(out[0]), SPA_N_ELEMENTS(out),
@ -630,38 +632,28 @@ static void test_lossless_s25_32_to_f32_to_s25_32(void)
}
}
static void test_lossless_s25_32_to_s32_to_f32_to_s25_32_XFAIL(void)
static void test_lossless_s25_32_to_s32_to_f32_to_s25_32(void)
{
int32_t i;
int all_lossless = 1;
int32_t max_abs_err = -1;
fprintf(stderr, "test %s:\n", __func__);
for (i = S25_MIN; i <= S25_MAX; i+=1) {
float v = S32_TO_F32(S25_32_TO_S32(i));
int32_t t = F32_TO_S25_32(v);
all_lossless &= i == t;
max_abs_err = SPA_MAX(max_abs_err, SPA_ABS(i - t));
spa_assert_se(i == t);
}
spa_assert_se(!all_lossless);
spa_assert_se(max_abs_err == 1);
}
static void test_lossless_s25_32_to_s32_to_f32_to_s32_to_s25_32_XFAIL(void)
static void test_lossless_s25_32_to_s32_to_f32_to_s32_to_s25_32(void)
{
int32_t i;
int all_lossless = 1;
int32_t max_abs_err = -1;
fprintf(stderr, "test %s:\n", __func__);
for (i = S25_MIN; i <= S25_MAX; i+=1) {
float v = S32_TO_F32(S25_32_TO_S32(i));
int32_t t = S32_TO_S25_32(F32_TO_S32(v));
all_lossless &= i == t;
max_abs_err = SPA_MAX(max_abs_err, SPA_ABS(i - t));
spa_assert_se(i == t);
}
spa_assert_se(!all_lossless);
spa_assert_se(max_abs_err == 1);
}
static void test_lossless_s25_32_to_f32_to_s32_to_s25_32(void)
@ -681,7 +673,7 @@ static void test_lossless_s32(void)
int64_t i;
int32_t max_abs_err = -1;
const int32_t expected_max_abs_err = 255;
const int32_t expected_max_abs_err = 127;
fprintf(stderr, "test %s:\n", __func__);
for (i = S32_MIN; i < S32_MAX; i += (expected_max_abs_err >> 1)) {
float v = S32_TO_F32(i);
@ -708,7 +700,7 @@ static void test_lossless_s32_lossless_subset(void)
}
}
spa_assert_se(!all_lossless);
spa_assert_se(max_abs_err == 255);
spa_assert_se(max_abs_err == 127);
}
static void test_lossless_u32(void)
@ -875,8 +867,8 @@ int main(int argc, char *argv[])
test_lossless_s24();
test_lossless_u24();
test_lossless_s25_32_to_f32_to_s25_32();
test_lossless_s25_32_to_s32_to_f32_to_s25_32_XFAIL();
test_lossless_s25_32_to_s32_to_f32_to_s32_to_s25_32_XFAIL();
test_lossless_s25_32_to_s32_to_f32_to_s25_32();
test_lossless_s25_32_to_s32_to_f32_to_s32_to_s25_32();
test_lossless_s25_32_to_f32_to_s32_to_s25_32();
test_lossless_s32();
test_lossless_s32_lossless_subset();