diff mbox series

audio/mixeng: fix clang 10+ warning

Message ID 20200523201712.23908-1-vr_qemu@t-online.de
State New
Headers show
Series audio/mixeng: fix clang 10+ warning | expand

Commit Message

Volker Rümelin May 23, 2020, 8:17 p.m. UTC
The code in CONV_NATURAL_FLOAT() and CLIP_NATURAL_FLOAT()
seems to use the constant 2^31-0.5 to convert float to integer
and back. But the float type lacks the required precision and
the constant used for the conversion is 2^31. This is equiva-
lent to a [-1.f, 1.f] <-> [INT32_MIN, INT32_MAX + 1] mapping.

This patch explicitly writes down the used constant. The
compiler generated code doesn't change.

The constant 2^31 has an exact float representation and the
clang 10 compiler stops complaining about an implicit int to
float conversion with a changed value.

A few notes:
- The conversion of 1.f to INT32_MAX + 1 doesn't overflow. The
  type of the destination variable is int64_t.
- At a later stage one of the clip_* functions in
  audio/mixeng_template.h limits INT32_MAX + 1 to the integer
  range.
- The clip_natural_float_* functions in audio/mixeng.c convert
  INT32_MAX and INT32_MAX + 1 to 1.f.

Buglink: https://bugs.launchpad.net/bugs/1878627
Signed-off-by: Volker Rümelin <vr_qemu@t-online.de>
---
 audio/mixeng.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

Comments

Gerd Hoffmann May 26, 2020, 5:51 a.m. UTC | #1
On Sat, May 23, 2020 at 10:17:12PM +0200, Volker Rümelin wrote:
> The code in CONV_NATURAL_FLOAT() and CLIP_NATURAL_FLOAT()
> seems to use the constant 2^31-0.5 to convert float to integer
> and back. But the float type lacks the required precision and
> the constant used for the conversion is 2^31. This is equiva-
> lent to a [-1.f, 1.f] <-> [INT32_MIN, INT32_MAX + 1] mapping.
> 
> This patch explicitly writes down the used constant. The
> compiler generated code doesn't change.
> 
> The constant 2^31 has an exact float representation and the
> clang 10 compiler stops complaining about an implicit int to
> float conversion with a changed value.
> 
> A few notes:
> - The conversion of 1.f to INT32_MAX + 1 doesn't overflow. The
>   type of the destination variable is int64_t.
> - At a later stage one of the clip_* functions in
>   audio/mixeng_template.h limits INT32_MAX + 1 to the integer
>   range.
> - The clip_natural_float_* functions in audio/mixeng.c convert
>   INT32_MAX and INT32_MAX + 1 to 1.f.
> 
> Buglink: https://bugs.launchpad.net/bugs/1878627
> Signed-off-by: Volker Rümelin <vr_qemu@t-online.de>

Added to audio queue.

thanks,
  Gerd
diff mbox series

Patch

diff --git a/audio/mixeng.c b/audio/mixeng.c
index 739a500449..924dcb858a 100644
--- a/audio/mixeng.c
+++ b/audio/mixeng.c
@@ -271,11 +271,12 @@  f_sample *mixeng_clip[2][2][2][3] = {
 #define CONV_NATURAL_FLOAT(x) (x)
 #define CLIP_NATURAL_FLOAT(x) (x)
 #else
-static const float float_scale = UINT_MAX / 2.f;
+/* macros to map [-1.f, 1.f] <-> [INT32_MIN, INT32_MAX + 1] */
+static const float float_scale = (int64_t)INT32_MAX + 1;
 #define CONV_NATURAL_FLOAT(x) ((x) * float_scale)
 
 #ifdef RECIPROCAL
-static const float float_scale_reciprocal = 2.f / UINT_MAX;
+static const float float_scale_reciprocal = 1.f / ((int64_t)INT32_MAX + 1);
 #define CLIP_NATURAL_FLOAT(x) ((x) * float_scale_reciprocal)
 #else
 #define CLIP_NATURAL_FLOAT(x) ((x) / float_scale)