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>
This commit is contained in:
Akihiko Odaki
2026-04-23 22:55:23 +09:00
committed by Marc-André Lureau
parent 698399705c
commit 64abf45516

View File

@@ -28,7 +28,7 @@
*/
#ifndef SIGNED
#define HALF (IN_MAX >> 1)
#define BIAS ((IN_T)1 << (SHIFT - 1))
#endif
#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)
#ifdef SIGNED
return nv * (2.f / ((mixeng_real)IN_MAX - IN_MIN));
#else
return ((mixeng_real)nv - HALF) * (2.f / (mixeng_real)IN_MAX);
return ((mixeng_real)nv - BIAS) * (1.f / BIAS);
#endif
#else /* !RECIPROCAL */
#ifdef SIGNED
return nv / (((mixeng_real)IN_MAX - IN_MIN) / 2.f);
#else
return ((mixeng_real)nv - HALF) / ((mixeng_real)IN_MAX / 2.f);
return ((mixeng_real)nv - BIAS) / BIAS;
#endif
#endif
}
@@ -65,9 +65,7 @@ static inline IN_T glue (clip_, ET) (mixeng_real v)
#ifdef SIGNED
return ENDIAN_CONVERT((IN_T)(v * (((mixeng_real)IN_MAX - IN_MIN) / 2.f)));
#else
return ENDIAN_CONVERT(MIN((int64_t)((v * ((mixeng_real)IN_MAX / 2.f)) +
HALF),
IN_MAX));
return ENDIAN_CONVERT(MIN((int64_t)(v * BIAS) + BIAS, IN_MAX));
#endif
}
@@ -79,7 +77,7 @@ static inline int64_t glue (conv_, ET) (IN_T v)
#ifdef SIGNED
return ((int64_t) nv) << (32 - SHIFT);
#else
return ((int64_t) nv - HALF) << (32 - SHIFT);
return ((int64_t) nv - BIAS) << (32 - SHIFT);
#endif
}
@@ -94,7 +92,7 @@ static inline IN_T glue (clip_, ET) (int64_t v)
#ifdef SIGNED
return ENDIAN_CONVERT ((IN_T) (v >> (32 - SHIFT)));
#else
return ENDIAN_CONVERT ((IN_T) ((v >> (32 - SHIFT)) + HALF));
return ENDIAN_CONVERT((IN_T)((v >> (32 - SHIFT)) + BIAS));
#endif
}
#endif
@@ -150,5 +148,5 @@ static void glue (glue (clip_, ET), _from_mono)
}
#undef ET
#undef HALF
#undef BIAS
#undef IN_T