From 98c86c7885791ee1e2508ebf878a5e94f16a70ae Mon Sep 17 00:00:00 2001
From: Jean-Marc Valin <jean-marc.valin@usherbrooke.ca>
Date: Thu, 27 Mar 2008 08:40:45 +1100
Subject: [PATCH] Trying to clean up celt_ilog2() vs. EC_ILOG a bit.

---
 libcelt/cwrs.c        |  5 +++--
 libcelt/ecintrin.h    |  3 +++
 libcelt/mathops.h     | 22 +++++++++++-----------
 libcelt/quant_bands.c |  4 ++--
 libcelt/vq.c          |  4 ++--
 5 files changed, 21 insertions(+), 17 deletions(-)

diff --git a/libcelt/cwrs.c b/libcelt/cwrs.c
index 92c0eed8c..12835de55 100644
--- a/libcelt/cwrs.c
+++ b/libcelt/cwrs.c
@@ -39,6 +39,7 @@
 
 #include <stdlib.h>
 #include "cwrs.h"
+#include "mathops.h"
 
 /* Knowing ncwrs() for a fixed number of pulses m and for all vector sizes n,
    compute ncwrs() for m+1, for all n. Could also be used when m and n are
@@ -373,7 +374,7 @@ void encode_pulses(int *_y, int N, int K, ec_enc *enc)
    
    pulse2comb(N, K, comb, signs, _y);
    /* Simple heuristic to figure out whether it fits in 32 bits */
-   if((N+4)*(K+4)<250 || EC_ILOG(N)*K<31)
+   if((N+4)*(K+4)<250 || (celt_ilog2(N)+1)*K<31)
    {
       celt_uint32_t bound, id;
       id = icwrs(N, K, comb, signs, &bound);
@@ -395,7 +396,7 @@ void decode_pulses(int *_y, int N, int K, ec_dec *dec)
    ALLOC(comb, K, int);
    ALLOC(signs, K, int);
    /* Simple heuristic to figure out whether it fits in 32 bits */
-   if((N+4)*(K+4)<250 || EC_ILOG(N)*K<31)
+   if((N+4)*(K+4)<250 || (celt_ilog2(N)+1)*K<31)
    {
       cwrsi(N, K, ec_dec_uint(dec, ncwrs(N, K)), comb, signs);
       comb2pulse(N, K, _y, comb, signs);
diff --git a/libcelt/ecintrin.h b/libcelt/ecintrin.h
index 4cb4d1fdc..d2d229aea 100644
--- a/libcelt/ecintrin.h
+++ b/libcelt/ecintrin.h
@@ -70,6 +70,9 @@
   The majority of the time we can never pass it zero.
   When we need to, it can be special cased.*/
 # define EC_ILOG(_x) (EC_CLZ0-EC_CLZ(_x))
+#elif defined(ENABLE_TI_DSPLIB)
+#include "dsplib.h"
+#define EC_ILOG(x) (31 - _lnorm(x))
 #else
 # define EC_ILOG(_x) (ec_ilog(_x))
 #endif
diff --git a/libcelt/mathops.h b/libcelt/mathops.h
index 221842907..fc59e6529 100644
--- a/libcelt/mathops.h
+++ b/libcelt/mathops.h
@@ -36,6 +36,16 @@
 #define MATHOPS_H
 
 #include "arch.h"
+#include "entcode.h"
+
+#ifndef OVERRIDE_CELT_ILOG2
+/** Integer log in base2. Undefined for zero and negative numbers */
+static inline celt_int16_t celt_ilog2(celt_word32_t x)
+{
+   celt_assert2(x>0, "celt_ilog2() only defined for strictly positive numbers");
+   return EC_ILOG(x)-1;
+}
+#endif
 
 #ifndef OVERRIDE_FIND_MAX16
 static inline int find_max16(celt_word16_t *x, int len)
@@ -89,18 +99,8 @@ static inline int find_max32(celt_word32_t *x, int len)
 
 #ifdef FIXED_POINT
 
-#include "entcode.h"
 #include "os_support.h"
 
-#ifndef OVERRIDE_CELT_ILOG2
-/** Integer log in base2. Undefined for zero and negative numbers */
-static inline celt_int16_t celt_ilog2(celt_word32_t x)
-{
-   celt_assert2(x>0, "celt_ilog2() only defined for strictly positive numbers");
-   return EC_ILOG(x)-1;
-}
-#endif
-
 #ifndef OVERRIDE_CELT_MAXABS16
 static inline celt_word16_t celt_maxabs16(celt_word16_t *x, int len)
 {
@@ -115,7 +115,7 @@ static inline celt_word16_t celt_maxabs16(celt_word16_t *x, int len)
 /** Integer log in base2. Defined for zero, but not for negative numbers */
 static inline celt_int16_t celt_zlog2(celt_word32_t x)
 {
-   return EC_ILOG(x)-1;
+   return x <= 0 ? 0 : celt_ilog2(x);
 }
 
 /** Reciprocal sqrt approximation (Q30 input, Q0 output or equivalent) */
diff --git a/libcelt/quant_bands.c b/libcelt/quant_bands.c
index 29b84246b..ba76c1d6c 100644
--- a/libcelt/quant_bands.c
+++ b/libcelt/quant_bands.c
@@ -138,7 +138,7 @@ static void quant_energy_mono(const CELTMode *m, celt_ener_t *eBands, celt_word1
       int q2;
       celt_word16_t offset = (error[i]+QCONST16(.5f,8))*frac[i];
       /* FIXME: Instead of giving up without warning, we should degrade everything gracefully */
-      if (ec_enc_tell(enc, 0) - bits +EC_ILOG(frac[i])> budget)
+      if (ec_enc_tell(enc, 0) - bits + celt_ilog2(frac[i]) >= budget)
          break;
 #ifdef FIXED_POINT
       /* Has to be without rounding */
@@ -196,7 +196,7 @@ static void unquant_energy_mono(const CELTMode *m, celt_ener_t *eBands, celt_wor
    {
       int q2;
       celt_word16_t offset;
-      if (ec_dec_tell(dec, 0) - bits +EC_ILOG(frac[i])> budget)
+      if (ec_dec_tell(dec, 0) - bits + celt_ilog2(frac[i]) >= budget)
          break;
       q2 = ec_dec_uint(dec, frac[i]);
       offset = EXTRACT16(celt_div(SHL16(q2,8)+QCONST16(.5,8),frac[i])-QCONST16(.5f,8));
diff --git a/libcelt/vq.c b/libcelt/vq.c
index 3eb33903c..ce8f056f3 100644
--- a/libcelt/vq.c
+++ b/libcelt/vq.c
@@ -52,7 +52,7 @@ static void mix_pitch_and_residual(int * restrict iy, celt_norm_t * restrict X,
 #endif
    SAVE_STACK;
 #ifdef FIXED_POINT
-   yshift = 14-EC_ILOG(K);
+   yshift = 13-celt_ilog2(K);
 #endif
    ALLOC(y, N, celt_norm_t);
 
@@ -104,7 +104,7 @@ void alg_quant(celt_norm_t *X, celt_mask_t *W, int N, int K, const celt_norm_t *
    SAVE_STACK;
 
 #ifdef FIXED_POINT
-   yshift = 14-EC_ILOG(K);
+   yshift = 13-celt_ilog2(K);
 #endif
 
    ALLOC(y, N, celt_norm_t);
-- 
GitLab