@samitouri / QOSamiQemu / commits / 64abf45516

audio: Use unsigned PCM bias

Clang warns for the uint32_t clip_ instantiations because HALF cannot be represented with mixeng_real: [1115/2559] Compiling C object libqemuaudio.a.p/audio_mixeng.c.o In file included from ../../qemu/audio/mixeng.c:147: ../../qemu/audio/mixeng_template.h:68:70: warning: implicit conversion from 'unsigned int' to 'float' changes value from 2147483647 to 2147483648 [-Wimplicit-const-int-float-conversion] 68 | return ENDIAN_CONVERT((IN_T)((v * ((mixeng_real)IN_MAX / 2.f)) + HALF)); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~ ../../qemu/audio/mixeng_template.h:31:22: note: expanded from macro 'HALF' 31 | #define HALF (IN_MAX >> 1) | ^ ../../qemu/audio/mixeng.c:146:28: note: expanded from macro 'ENDIAN_CONVERT' 146 | #define ENDIAN_CONVERT(v) (v) | ^ In file included from ../../qemu/audio/mixeng.c:152: ../../qemu/audio/mixeng_template.h:68:70: warning: implicit conversion from 'unsigned int' to 'float' changes value from 2147483647 to 2147483648 [-Wimplicit-const-int-float-conversion] 68 | return ENDIAN_CONVERT((IN_T)((v * ((mixeng_real)IN_MAX / 2.f)) + HALF)); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~ ../../qemu/audio/mixeng_template.h:31:22: note: expanded from macro 'HALF' 31 | #define HALF (IN_MAX >> 1) | ^ ../../qemu/audio/mixeng.c:151:36: note: expanded from macro 'ENDIAN_CONVERT' 151 | #define ENDIAN_CONVERT(v) bswap32 (v) | ~~~~~~~~~^~ /Users/person/v/qemu/include/qemu/bswap.h:10:39: note: expanded from macro 'bswap32' 10 | #define bswap32(_x) __builtin_bswap32(_x) | ^~ 2 warnings generated. HALF is not the right value here anyway. IN_MAX is odd, so the integer sample range has two middle codes. Unsigned PCM normally uses the upper middle code as the "bias": 0x80, 0x8000, or 0x80000000. HALF is instead defined as the lower middle code: 0x7f, 0x7fff, or 0x7fffffff. Replace HALF with BIAS, defined as the upper middle code. This fixes the warnings, since the value can be exactly represented with mixeng_real. Signed-off-by: Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp> Acked-by: Marc-André Lureau <marcandre.lureau@redhat.com> Message-Id: <20260423-audio-v1-3-e1d6b65c76f9@rsg.ci.i.u-tokyo.ac.jp>

Akihiko Odaki committed Apr 23, 2026 at 22:55 UTC 64abf45516f884c344a894cebebe06d861b16b2a
1 file changed +7 -9
audio/mixeng_template.h
+7 -9
@@ -28,7 +28,7 @@
28 */
29
30 #ifndef SIGNED
31 -#define HALF (IN_MAX >> 1)
31 +#define BIAS ((IN_T)1 << (SHIFT - 1))
32 #endif
33
34 #define ET glue (ENDIAN_CONVERSION, glue (glue (glue (_, ITYPE), BSIZE), _t))
@@ -43,13 +43,13 @@ static inline mixeng_real glue (conv_, ET) (IN_T v)
43 #ifdef SIGNED
44 return nv * (2.f / ((mixeng_real)IN_MAX - IN_MIN));
45 #else
46 - return ((mixeng_real)nv - HALF) * (2.f / (mixeng_real)IN_MAX);
46 + return ((mixeng_real)nv - BIAS) * (1.f / BIAS);
47 #endif
48 #else /* !RECIPROCAL */
49 #ifdef SIGNED
50 return nv / (((mixeng_real)IN_MAX - IN_MIN) / 2.f);
51 #else
52 - return ((mixeng_real)nv - HALF) / ((mixeng_real)IN_MAX / 2.f);
52 + return ((mixeng_real)nv - BIAS) / BIAS;
53 #endif
54 #endif
55 }
@@ -65,9 +65,7 @@ static inline IN_T glue (clip_, ET) (mixeng_real v)
65 #ifdef SIGNED
66 return ENDIAN_CONVERT((IN_T)(v * (((mixeng_real)IN_MAX - IN_MIN) / 2.f)));
67 #else
68 - return ENDIAN_CONVERT(MIN((int64_t)((v * ((mixeng_real)IN_MAX / 2.f)) +
69 - HALF),
70 - IN_MAX));
68 + return ENDIAN_CONVERT(MIN((int64_t)(v * BIAS) + BIAS, IN_MAX));
69 #endif
70 }
71
@@ -79,7 +77,7 @@ static inline int64_t glue (conv_, ET) (IN_T v)
77 #ifdef SIGNED
78 return ((int64_t) nv) << (32 - SHIFT);
79 #else
82 - return ((int64_t) nv - HALF) << (32 - SHIFT);
80 + return ((int64_t) nv - BIAS) << (32 - SHIFT);
81 #endif
82 }
83
@@ -94,7 +92,7 @@ static inline IN_T glue (clip_, ET) (int64_t v)
92 #ifdef SIGNED
93 return ENDIAN_CONVERT ((IN_T) (v >> (32 - SHIFT)));
94 #else
97 - return ENDIAN_CONVERT ((IN_T) ((v >> (32 - SHIFT)) + HALF));
95 + return ENDIAN_CONVERT((IN_T)((v >> (32 - SHIFT)) + BIAS));
96 #endif
97 }
98 #endif
@@ -150,5 +148,5 @@ static void glue (glue (clip_, ET), _from_mono)
148 }
149
150 #undef ET
153 -#undef HALF
151 +#undef BIAS
152 #undef IN_T