sound tests: Fix downshift calculation in pcm_read_write test

In some situations the feeders in the sound module lower the pcm sample
resolution through a downshift of the sample value. The pcm_read_write
test implements this operation with an arithmetic division to avoid
implementation defined or architecture specific behavior. Due to
different rounding, the test produced marginally different sample
values, which made the test fail on 32 bit architectures. Correct this.

Reported by:	CI
Fixes:		27ef5d48c7 ("sound: Unit test the pcm sample read and write macros")
MFC after:	1 week
Reviewed by:	christos, markj
Differential revision:	https://reviews.freebsd.org/D48926

(cherry picked from commit 6672831bda)
This commit is contained in:
Florian Walpen 2025-02-18 21:35:54 +02:00 committed by Christos Margiolis
parent 999e714432
commit 47a067308e

View file

@ -81,13 +81,21 @@ static struct afmt_test_data {
static intpcm_t
local_normalize(intpcm_t value, int val_bits, int norm_bits)
{
int32_t divisor;
intpcm_t remainder;
/* Avoid undefined or implementation defined behavior. */
if (val_bits < norm_bits)
/* Multiply instead of left shift (value may be negative). */
return (value * (1 << (norm_bits - val_bits)));
else if (val_bits > norm_bits)
else if (val_bits > norm_bits) {
divisor = (1 << (val_bits - norm_bits));
/* Positive remainder, to discard lowest bits from value. */
remainder = value % divisor;
remainder = (remainder + divisor) % divisor;
/* Divide instead of right shift (value may be negative). */
return (value / (1 << (val_bits - norm_bits)));
return ((value - remainder) / divisor);
}
return value;
}
@ -103,8 +111,7 @@ local_calc_limit(intpcm_t value, int val_bits)
* behavior here.
*/
if (sizeof(intpcm32_t) == (32 / 8) && val_bits == 32)
/* Divide instead of right shift (value may be negative). */
return (value / (1 << 8));
return (local_normalize(value, 32, 24));
return value;
}