From b026a0461d77ee3f58b870dcac0ebc8e2ce02082 Mon Sep 17 00:00:00 2001 From: Ken Cooke Date: Tue, 10 Oct 2017 09:13:06 -0700 Subject: [PATCH] Fix clang warnings for left-shift of signed int --- libraries/audio/src/AudioDynamics.h | 52 ++++++++++++++++------------- libraries/audio/src/AudioGate.cpp | 14 ++++---- 2 files changed, 36 insertions(+), 30 deletions(-) diff --git a/libraries/audio/src/AudioDynamics.h b/libraries/audio/src/AudioDynamics.h index b7c168bfab..6f36605025 100644 --- a/libraries/audio/src/AudioDynamics.h +++ b/libraries/audio/src/AudioDynamics.h @@ -135,6 +135,7 @@ static const int32_t exp2Table[1 << EXP2_TABBITS][3] = { static const int IEEE754_FABS_MASK = 0x7fffffff; static const int IEEE754_MANT_BITS = 23; +static const int IEEE754_EXPN_BITS = 8; static const int IEEE754_EXPN_BIAS = 127; // @@ -152,7 +153,7 @@ static inline int32_t peaklog2(float* input) { // split into e and x - 1.0 int32_t e = IEEE754_EXPN_BIAS - (peak >> IEEE754_MANT_BITS) + LOG2_HEADROOM; - int32_t x = (peak << (31 - IEEE754_MANT_BITS)) & 0x7fffffff; + int32_t x = (peak << IEEE754_EXPN_BITS) & 0x7fffffff; // saturate if (e > 31) { @@ -191,7 +192,7 @@ static inline int32_t peaklog2(float* input0, float* input1) { // split into e and x - 1.0 int32_t e = IEEE754_EXPN_BIAS - (peak >> IEEE754_MANT_BITS) + LOG2_HEADROOM; - int32_t x = (peak << (31 - IEEE754_MANT_BITS)) & 0x7fffffff; + int32_t x = (peak << IEEE754_EXPN_BITS) & 0x7fffffff; // saturate if (e > 31) { @@ -234,7 +235,7 @@ static inline int32_t peaklog2(float* input0, float* input1, float* input2, floa // split into e and x - 1.0 int32_t e = IEEE754_EXPN_BIAS - (peak >> IEEE754_MANT_BITS) + LOG2_HEADROOM; - int32_t x = (peak << (31 - IEEE754_MANT_BITS)) & 0x7fffffff; + int32_t x = (peak << IEEE754_EXPN_BITS) & 0x7fffffff; // saturate if (e > 31) { @@ -259,30 +260,30 @@ static inline int32_t peaklog2(float* input0, float* input1, float* input2, floa // Count Leading Zeros // Emulates the CLZ (ARM) and LZCNT (x86) instruction // -static inline int CLZ(uint32_t x) { +static inline int CLZ(uint32_t u) { - if (x == 0) { + if (u == 0) { return 32; } int e = 0; - if (x < 0x00010000) { - x <<= 16; + if (u < 0x00010000) { + u <<= 16; e += 16; } - if (x < 0x01000000) { - x <<= 8; + if (u < 0x01000000) { + u <<= 8; e += 8; } - if (x < 0x10000000) { - x <<= 4; + if (u < 0x10000000) { + u <<= 4; e += 4; } - if (x < 0x40000000) { - x <<= 2; + if (u < 0x40000000) { + u <<= 2; e += 2; } - if (x < 0x80000000) { + if (u < 0x80000000) { e += 1; } return e; @@ -290,19 +291,19 @@ static inline int CLZ(uint32_t x) { // // Compute -log2(x) for x=[0,1] in Q31, result in Q26 -// x = 0 returns 0x7fffffff -// x < 0 undefined +// x <= 0 returns 0x7fffffff // static inline int32_t fixlog2(int32_t x) { - if (x == 0) { + if (x <= 0) { return 0x7fffffff; } // split into e and x - 1.0 - int e = CLZ((uint32_t)x); - x <<= e; // normalize to [0x80000000, 0xffffffff] - x &= 0x7fffffff; // x - 1.0 + uint32_t u = (uint32_t)x; + int e = CLZ(u); + u <<= e; // normalize to [0x80000000, 0xffffffff] + x = u & 0x7fffffff; // x - 1.0 int k = x >> (31 - LOG2_TABBITS); @@ -320,13 +321,18 @@ static inline int32_t fixlog2(int32_t x) { // // Compute exp2(-x) for x=[0,32] in Q26, result in Q31 -// x < 0 undefined +// x <= 0 returns 0x7fffffff // static inline int32_t fixexp2(int32_t x) { + if (x <= 0) { + return 0x7fffffff; + } + // split into e and 1.0 - x - int e = x >> LOG2_FRACBITS; - x = ~(x << LOG2_INTBITS) & 0x7fffffff; + uint32_t u = (uint32_t)x; + int e = u >> LOG2_FRACBITS; + x = ~(u << LOG2_INTBITS) & 0x7fffffff; int k = x >> (31 - EXP2_TABBITS); diff --git a/libraries/audio/src/AudioGate.cpp b/libraries/audio/src/AudioGate.cpp index ac72533806..5ef5ee25b5 100644 --- a/libraries/audio/src/AudioGate.cpp +++ b/libraries/audio/src/AudioGate.cpp @@ -40,7 +40,7 @@ class MonoDCBlock { public: void process(int32_t& x) { - x <<= 15; // scale to Q30 + x *= (1 << 15); // scale to Q30 x -= _dcOffset; // remove DC _dcOffset += x >> 13; // pole = (1.0 - 2^-13) = 0.9999 } @@ -53,8 +53,8 @@ class StereoDCBlock { public: void process(int32_t& x0, int32_t& x1) { - x0 <<= 15; - x1 <<= 15; + x0 *= (1 << 15); + x1 *= (1 << 15); x0 -= _dcOffset[0]; x1 -= _dcOffset[1]; @@ -71,10 +71,10 @@ class QuadDCBlock { public: void process(int32_t& x0, int32_t& x1, int32_t& x2, int32_t& x3) { - x0 <<= 15; - x1 <<= 15; - x2 <<= 15; - x3 <<= 15; + x0 *= (1 << 15); + x1 *= (1 << 15); + x2 *= (1 << 15); + x3 *= (1 << 15); x0 -= _dcOffset[0]; x1 -= _dcOffset[1];