From 65d06cb4ea973b2a92ffa0c0a6d3d212c8641577 Mon Sep 17 00:00:00 2001
From: William Escande <wescande@google.com>
Date: Wed, 19 Oct 2022 18:36:46 -0700
Subject: [PATCH] Apply and fix clang-tidy on aptx and aptxhd

Bug: 226572369
Test: Build
Change-Id: I26f4b725659c7b1cfbf76a1e91eaec49e280966b
---
 .../encoder_for_aptx/src/CodewordPacker.h       |  4 ++--
 system/embdrv/encoder_for_aptx/src/QmfConv.c    |  2 +-
 .../encoder_for_aptx/src/QuantiseDifference.c   |  9 +++++----
 .../encoder_for_aptx/src/SubbandFunctions.h     |  8 +++-----
 system/embdrv/encoder_for_aptx/src/aptXbtenc.c  |  4 ++--
 system/embdrv/encoder_for_aptxhd/src/QmfConv.c  |  2 +-
 .../encoder_for_aptxhd/src/QuantiseDifference.c | 17 ++++++++---------
 .../encoder_for_aptxhd/src/SubbandFunctions.h   |  2 +-
 .../embdrv/encoder_for_aptxhd/src/aptXHDbtenc.c |  2 +-
 9 files changed, 24 insertions(+), 26 deletions(-)

diff --git a/system/embdrv/encoder_for_aptx/src/CodewordPacker.h b/system/embdrv/encoder_for_aptx/src/CodewordPacker.h
index 68b7a1dea6b..a4b96ebf95c 100644
--- a/system/embdrv/encoder_for_aptx/src/CodewordPacker.h
+++ b/system/embdrv/encoder_for_aptx/src/CodewordPacker.h
@@ -27,7 +27,7 @@
 
 #include "AptxParameters.h"
 
-XBT_INLINE_ int32_t packCodeword(Encoder_data* EncoderDataPt,
+XBT_INLINE_ int16_t packCodeword(Encoder_data* EncoderDataPt,
                                  uint32_t aligned) {
   int32_t syncContribution;
   int32_t hhCode;
@@ -58,7 +58,7 @@ XBT_INLINE_ int32_t packCodeword(Encoder_data* EncoderDataPt,
                ((EncoderDataPt->m_qdata[HL].qCode & 0x3L) << 11) |
                ((EncoderDataPt->m_qdata[HH].qCode & 0x7L) << 13);
   }
-  return codeword;
+  return (int16_t)codeword;
 }
 
 #endif  // CODEWORDPACKER_H
diff --git a/system/embdrv/encoder_for_aptx/src/QmfConv.c b/system/embdrv/encoder_for_aptx/src/QmfConv.c
index e584129820b..de3057f4e23 100644
--- a/system/embdrv/encoder_for_aptx/src/QmfConv.c
+++ b/system/embdrv/encoder_for_aptx/src/QmfConv.c
@@ -19,7 +19,7 @@
  *
  *----------------------------------------------------------------------------*/
 
-#include "AptxParameters.h"
+#include "Qmf.h"
 
 void AsmQmfConvO(const int16_t* p1dl_buffPtr, const int16_t* p2dl_buffPtr,
                  const int32_t* coeffPtr, int32_t* convSumDiff) {
diff --git a/system/embdrv/encoder_for_aptx/src/QuantiseDifference.c b/system/embdrv/encoder_for_aptx/src/QuantiseDifference.c
index 697ed3e1989..9d87a122c90 100644
--- a/system/embdrv/encoder_for_aptx/src/QuantiseDifference.c
+++ b/system/embdrv/encoder_for_aptx/src/QuantiseDifference.c
@@ -228,7 +228,7 @@ void quantiseDifferenceHL(const int32_t diffSignal, const int32_t ditherVal,
   if (tmp_round0 == 0x40000000L) acc--;
   _delta = -delta << 8;
 
-  acc = acc << 4;
+  acc = (int32_t)((uint32_t)acc << 4);
 
   /* Form (absDiffSignal * 0.125) - (acc * delta), which is the final distance
    * signal used to determine if dithering alters the quantised code value or
@@ -373,7 +373,7 @@ void quantiseDifferenceHH(const int32_t diffSignal, const int32_t ditherVal,
   if (tmp_round0 == 0x40000000L) acc--;
   _delta = -delta << 8;
 
-  acc = acc << 4;
+  acc = (int32_t)((uint32_t)acc << 4);
 
   /* Form (absDiffSignal * 0.125) - (acc * delta), which is the final distance
    * signal used to determine if dithering alters the quantised code value or
@@ -518,7 +518,8 @@ void quantiseDifferenceLL(const int32_t diffSignal, const int32_t ditherVal,
   if (tmp_round0 == 0x40000000L) acc--;
   _delta = -delta << 8;
 
-  acc = acc << 4;
+  acc = (int32_t)((uint32_t)acc << 4);
+
   /* Form (absDiffSignal * 0.125) - (acc * delta), which is the final distance
    * signal used to determine if dithering alters the quantised code value or
    * not. */
@@ -661,7 +662,7 @@ void quantiseDifferenceLH(const int32_t diffSignal, const int32_t ditherVal,
   if (tmp_round0 == 0x40000000L) acc--;
   _delta = -delta << 8;
 
-  acc = acc << 4;
+  acc = (int32_t)((uint32_t)acc << 4);
 
   /* Form (absDiffSignal * 0.125) - (acc * delta), which is the final distance
    * signal used to determine if dithering alters the quantised code value or
diff --git a/system/embdrv/encoder_for_aptx/src/SubbandFunctions.h b/system/embdrv/encoder_for_aptx/src/SubbandFunctions.h
index 949846f3938..16c1b9fb2d2 100644
--- a/system/embdrv/encoder_for_aptx/src/SubbandFunctions.h
+++ b/system/embdrv/encoder_for_aptx/src/SubbandFunctions.h
@@ -71,16 +71,14 @@ XBT_INLINE_ void updatePredictorPoleCoefficients(
     PoleCoeffDataPt->m_poleAdaptDelayLine.s16.h =
         PoleCoeffDataPt->m_poleAdaptDelayLine.s16.l;
     PoleCoeffDataPt->m_poleAdaptDelayLine.s16.l = -1;
-  }
-  if (adaptSum == 0L) {
+  } else if (adaptSum == 0L) {
     sgnP[k] = 0L;
     sgnP[k_1] = 0L;
     sgnP[k_2] = 0L;
     PoleCoeffDataPt->m_poleAdaptDelayLine.s16.h =
         PoleCoeffDataPt->m_poleAdaptDelayLine.s16.l;
     PoleCoeffDataPt->m_poleAdaptDelayLine.s16.l = 1;
-  }
-  if (adaptSum > 0L) {
+  } else {  // adaptSum > 0L
     sgnP[k] = oneQ22;
     sgnP[k_1] = ((int32_t)PoleCoeffDataPt->m_poleAdaptDelayLine.s16.l) << 22;
     sgnP[k_2] = ((int32_t)PoleCoeffDataPt->m_poleAdaptDelayLine.s16.h) << 22;
@@ -129,7 +127,7 @@ XBT_INLINE_ void updatePredictorPoleCoefficients(
    * back out. */
   acc2 = PoleCoeffDataPt->m_poleCoeff[a2] << 8;
   acc2 -= PoleCoeffDataPt->m_poleCoeff[a2] << 1;
-  Bacc <<= 8;
+  Bacc = (int32_t)((uint32_t)Bacc << 8);
   Bacc += acc2;
 
   tmp2_round0 = (int16_t)Bacc & 0x01FFL;
diff --git a/system/embdrv/encoder_for_aptx/src/aptXbtenc.c b/system/embdrv/encoder_for_aptx/src/aptXbtenc.c
index cd819ce20a5..9a69bf66770 100644
--- a/system/embdrv/encoder_for_aptx/src/aptXbtenc.c
+++ b/system/embdrv/encoder_for_aptx/src/aptXbtenc.c
@@ -56,7 +56,7 @@ static const int32_t IQuant_tableLogT[32] = {
     28160 * 256, 28776 * 256, 29408 * 256, 30048 * 256, 30704 * 256,
     31376 * 256, 32064 * 256};
 
-void clearmem(void* mem, int32_t sz) {
+static void clearmem(void* mem, int32_t sz) {
   int8_t* m = (int8_t*)mem;
   int32_t i = 0;
   for (; i < sz; i++) {
@@ -183,7 +183,7 @@ APTXBTENCEXPORT int aptxbtenc_encodestereo(void* _state, void* _pcmL,
   int32_t* pcmL = (int32_t*)_pcmL;
   int32_t* pcmR = (int32_t*)_pcmR;
   int16_t* buffer = (int16_t*)_buffer;
-  int32_t tmp_reg;
+  int16_t tmp_reg;
   int16_t tmp_out;
   // Feed the PCM to the dual aptX encoders
   aptxEncode(pcmL, &state->m_qmf_l, &state->m_encoderData[0]);
diff --git a/system/embdrv/encoder_for_aptxhd/src/QmfConv.c b/system/embdrv/encoder_for_aptxhd/src/QmfConv.c
index bae3c48fdab..e32b3ee1d9f 100644
--- a/system/embdrv/encoder_for_aptxhd/src/QmfConv.c
+++ b/system/embdrv/encoder_for_aptxhd/src/QmfConv.c
@@ -19,7 +19,7 @@
  *
  *----------------------------------------------------------------------------*/
 
-#include "AptxParameters.h"
+#include "Qmf.h"
 
 void AsmQmfConvO_HD(const int32_t* p1dl_buffPtr, const int32_t* p2dl_buffPtr,
                     const int32_t* coeffPtr, int32_t* convSumDiff) {
diff --git a/system/embdrv/encoder_for_aptxhd/src/QuantiseDifference.c b/system/embdrv/encoder_for_aptxhd/src/QuantiseDifference.c
index 899ab3f815c..f7e4b3a63db 100644
--- a/system/embdrv/encoder_for_aptxhd/src/QuantiseDifference.c
+++ b/system/embdrv/encoder_for_aptxhd/src/QuantiseDifference.c
@@ -13,7 +13,8 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-#include "AptxParameters.h"
+
+#include "Quantiser.h"
 
 XBT_INLINE_ int32_t BsearchLL(const int32_t absDiffSignalShifted,
                               const int32_t delta,
@@ -232,7 +233,7 @@ void quantiseDifference_HDHL(const int32_t diffSignal, const int32_t ditherVal,
   if (tmp_round0 == 0x40000000L) acc--;
   _delta = -delta << 8;
 
-  acc = acc << 4;
+  acc = (int32_t)((uint32_t)acc << 4);
 
   /* Form (absDiffSignal * 0.125) - (acc * delta), which is the final distance
    * signal used to determine if dithering alters the quantised code value or
@@ -375,7 +376,7 @@ void quantiseDifference_HDHH(const int32_t diffSignal, const int32_t ditherVal,
   if (tmp_round0 == 0x40000000L) acc--;
   _delta = -delta << 8;
 
-  acc = acc << 4;
+  acc = (int32_t)((uint32_t)acc << 4);
 
   /* Form (absDiffSignal * 0.125) - (acc * delta), which is the final distance
    * signal used to determine if dithering alters the quantised code value or
@@ -443,8 +444,6 @@ void quantiseDifference_HDLL(const int32_t diffSignal, const int32_t ditherVal,
    * table index of the LARGEST threshold table value for which
    * absDiffSignalShifted >= (delta * threshold)
    */
-  index = 0;
-
   index =
       BsearchLL(absDiffSignalShifted, delta, qdata_pt->thresholdTablePtr_sl1);
 
@@ -522,7 +521,7 @@ void quantiseDifference_HDLL(const int32_t diffSignal, const int32_t ditherVal,
   if (tmp_round0 == 0x40000000L) acc--;
   _delta = -delta << 8;
 
-  acc = acc << 4;
+  acc = (int32_t)((uint32_t)acc << 4);
 
   /* Form (absDiffSignal * 0.125) - (acc * delta), which is the final distance
    * signal used to determine if dithering alters the quantised code value or
@@ -564,8 +563,8 @@ void quantiseDifference_HDLL(const int32_t diffSignal, const int32_t ditherVal,
   qdata_pt->qCode = tmp_qCode;
 }
 
-int32_t BsearchLH(const int32_t absDiffSignalShifted, const int32_t delta,
-                  const int32_t* dqbitTablePrt) {
+static int32_t BsearchLH(const int32_t absDiffSignalShifted,
+                         const int32_t delta, const int32_t* dqbitTablePrt) {
   int32_t qCode;
   reg64_t tmp_acc;
   int32_t tmp;
@@ -711,7 +710,7 @@ void quantiseDifference_HDLH(const int32_t diffSignal, const int32_t ditherVal,
   if (tmp_round0 == 0x40000000L) acc--;
   _delta = -delta << 8;
 
-  acc = acc << 4;
+  acc = (int32_t)((uint32_t)acc << 4);
 
   /* Form (absDiffSignal * 0.125) - (acc * delta), which is the final distance
    * signal used to determine if dithering alters the quantised code value or
diff --git a/system/embdrv/encoder_for_aptxhd/src/SubbandFunctions.h b/system/embdrv/encoder_for_aptxhd/src/SubbandFunctions.h
index b2ef3c28f8f..ebf8f485341 100644
--- a/system/embdrv/encoder_for_aptxhd/src/SubbandFunctions.h
+++ b/system/embdrv/encoder_for_aptxhd/src/SubbandFunctions.h
@@ -127,7 +127,7 @@ XBT_INLINE_ void updatePredictorPoleCoefficients(
    * back out. */
   acc2 = PoleCoeffDataPt->m_poleCoeff[a2] << 8;
   acc2 -= PoleCoeffDataPt->m_poleCoeff[a2] << 1;
-  Bacc <<= 8;
+  Bacc = (int32_t)((uint32_t)Bacc << 8);
   Bacc += acc2;
 
   tmp2_round0 = (int16_t)Bacc & 0x01FFL;
diff --git a/system/embdrv/encoder_for_aptxhd/src/aptXHDbtenc.c b/system/embdrv/encoder_for_aptxhd/src/aptXHDbtenc.c
index fa5128b67b6..2d4a0830953 100644
--- a/system/embdrv/encoder_for_aptxhd/src/aptXHDbtenc.c
+++ b/system/embdrv/encoder_for_aptxhd/src/aptXHDbtenc.c
@@ -50,7 +50,7 @@ static const int32_t IQuant_tableLogT[32] = {
     28160 * 256, 28776 * 256, 29408 * 256, 30048 * 256, 30704 * 256,
     31376 * 256, 32064 * 256};
 
-void clearmem_HD(void* mem, int32_t sz) {
+static void clearmem_HD(void* mem, int32_t sz) {
   int8_t* m = (int8_t*)mem;
   int32_t i = 0;
   for (; i < sz; i++) {
-- 
GitLab