git: e02b579b5379 - main - sound tests: Fix 32bit calculation detection in pcm_read_write

From: Christos Margiolis <christos_at_FreeBSD.org>
Date: Thu, 23 Jan 2025 12:39:08 UTC
The branch main has been updated by christos:

URL: https://cgit.FreeBSD.org/src/commit/?id=e02b579b537998495b06d02be6aa07f03db3a42a

commit e02b579b537998495b06d02be6aa07f03db3a42a
Author:     Florian Walpen <dev@submerge.ch>
AuthorDate: 2025-01-23 12:38:00 +0000
Commit:     Christos Margiolis <christos@FreeBSD.org>
CommitDate: 2025-01-23 12:38:00 +0000

    sound tests: Fix 32bit calculation detection in pcm_read_write
    
    Fix a mistake in the pcm_read_write test that would result in not
    properly detecting 32bit calculation on 32bit architectures like i386.
    As a consequence, the wrong values would be checked, thus failing the
    test.
    
    Reported by:    CI
    Fixes:          27ef5d48c729 ("sound: Unit test the pcm sample read and write macros")
    MFC after:      1 week
    Reviewed by:    christos
    Differential Revision:  https://reviews.freebsd.org/D48617
---
 tests/sys/sound/pcm_read_write.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/tests/sys/sound/pcm_read_write.c b/tests/sys/sound/pcm_read_write.c
index e40ce52b67cc..7ef310a35c25 100644
--- a/tests/sys/sound/pcm_read_write.c
+++ b/tests/sys/sound/pcm_read_write.c
@@ -95,8 +95,14 @@ local_normalize(intpcm_t value, int val_bits, int norm_bits)
 static intpcm_t
 local_calc_limit(intpcm_t value, int val_bits)
 {
-	/* Avoid implementation defined behavior. */
-	if (sizeof(intpcm32_t) == 32 && val_bits == 32)
+	/*
+	 * When intpcm32_t is defined to be 32bit, calculations for mixing and
+	 * volume changes use 32bit integers instead of 64bit. To get some
+	 * headroom for calculations, 32bit sample values are restricted to
+	 * 24bit magnitude in that case. Also avoid implementation defined
+	 * 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 value;
@@ -456,7 +462,7 @@ ATF_TC_BODY(pcm_write, tc)
 		}
 		local_pcm_write_calc(dst, value, test->format);
 		ATF_CHECK_MSG(memcmp(dst, expected, sizeof(dst)) == 0,
-		    "pcm_write[\"%s\"].value: "
+		    "pcm_write[\"%s\"].calc: "
 		    "expected={0x%02x, 0x%02x, 0x%02x, 0x%02x}, "
 		    "result={0x%02x, 0x%02x, 0x%02x, 0x%02x}, ", test->label,
 		    expected[0], expected[1], expected[2], expected[3],