fixed-point: playing it safe. SHL32() now automatically casts input to 32-bit
[opus.git] / libcelt / vq.c
index 0fe2526..440da81 100644 (file)
@@ -33,8 +33,6 @@
 #include "config.h"
 #endif
 
-#include <math.h>
-#include <stdlib.h>
 #include "mathops.h"
 #include "cwrs.h"
 #include "vq.h"
@@ -49,7 +47,7 @@ static void mix_pitch_and_residual(int *iy, celt_norm_t *X, int N, int K, const
    int i;
    celt_word32_t Ryp, Ryy, Rpp;
    celt_word32_t g;
-   VARDECL(celt_norm_t *y);
+   VARDECL(celt_norm_ty);
 #ifdef FIXED_POINT
    int yshift;
 #endif
@@ -85,10 +83,14 @@ static void mix_pitch_and_residual(int *iy, celt_norm_t *X, int N, int K, const
       Ryy = MAC16_16(Ryy, y[i],y[i]);
 
    /* g = (sqrt(Ryp^2 + Ryy - Rpp*Ryy)-Ryp)/Ryy */
-   g = DIV32(SHL32(celt_sqrt(MULT16_16(ROUND(Ryp,14),ROUND(Ryp,14)) + Ryy - MULT16_16(ROUND(Ryy,14),ROUND(Rpp,14))) - ROUND(Ryp,14),14),ROUND(Ryy,14));
+   g = MULT16_32_Q15(
+            celt_sqrt(MULT16_16(ROUND(Ryp,14),ROUND(Ryp,14)) + Ryy -
+                      MULT16_16(ROUND(Ryy,14),ROUND(Rpp,14)))
+            - ROUND(Ryp,14),
+       celt_rcp(SHR32(Ryy,9)));
 
    for (i=0;i<N;i++)
-      X[i] = P[i] + MULT16_32_Q14(y[i], g);
+      X[i] = P[i] + ROUND(MULT16_16(y[i], g),11);
    RESTORE_STACK;
 }
 
@@ -106,21 +108,21 @@ struct NBest {
 void alg_quant(celt_norm_t *X, celt_mask_t *W, int N, int K, const celt_norm_t *P, celt_word16_t alpha, ec_enc *enc)
 {
    int L = 3;
-   VARDECL(celt_norm_t *_y);
-   VARDECL(celt_norm_t *_ny);
-   VARDECL(int *_iy);
-   VARDECL(int *_iny);
-   VARDECL(celt_norm_t **y);
-   VARDECL(celt_norm_t **ny);
-   VARDECL(int **iy);
-   VARDECL(int **iny);
+   VARDECL(celt_norm_t_y);
+   VARDECL(celt_norm_t_ny);
+   VARDECL(int_iy);
+   VARDECL(int_iny);
+   VARDECL(celt_norm_t *y);
+   VARDECL(celt_norm_t *ny);
+   VARDECL(int *iy);
+   VARDECL(int *iny);
    int i, j, k, m;
    int pulsesLeft;
-   VARDECL(celt_word32_t *xy);
-   VARDECL(celt_word32_t *yy);
-   VARDECL(celt_word32_t *yp);
-   VARDECL(struct NBest *_nbest);
-   VARDECL(struct NBest **nbest);
+   VARDECL(celt_word32_txy);
+   VARDECL(celt_word32_tyy);
+   VARDECL(celt_word32_typ);
+   VARDECL(struct NBest_nbest);
+   VARDECL(struct NBest *nbest);
    celt_word32_t Rpp=0, Rxp=0;
    int maxL = 1;
 #ifdef FIXED_POINT
@@ -218,25 +220,27 @@ void alg_quant(celt_norm_t *X, celt_mask_t *W, int N, int K, const celt_norm_t *
                if (iy[m][j]*sign < 0)
                   continue;
 
-               spj = MULT16_16_P14(s, P[j]);
-               aspj = MULT16_16_P15(alpha, spj);
+               spj = MULT16_16_Q14(s, P[j]);
+               aspj = MULT16_16_Q15(alpha, spj);
                /* Updating the sums of the new pulse(s) */
-               Rxy = xy[m] + MULT16_16(s,X[j])     - MULT16_16(MULT16_16_P15(alpha,spj),Rxp);
+               Rxy = xy[m] + MULT16_16(s,X[j])     - MULT16_16(MULT16_16_Q15(alpha,spj),Rxp);
                Ryy = yy[m] + 2*MULT16_16(s,y[m][j]) + MULT16_16(s,s)   +MULT16_16(aspj,MULT16_16_Q14(aspj,Rpp)) - 2*MULT16_32_Q14(aspj,yp[m]) - 2*MULT16_16(s,MULT16_16_Q14(aspj,P[j]));
                Ryp = yp[m] + MULT16_16(spj, SUB16(QCONST16(1.f,14),MULT16_16_Q15(alpha,Rpp)));
                
                /* Compute the gain such that ||p + g*y|| = 1 */
-               g = DIV32(SHL32(celt_sqrt(MULT16_16(ROUND(Ryp,14),ROUND(Ryp,14)) + Ryy - MULT16_16(ROUND(Ryy,14),Rpp)) - ROUND(Ryp,14),14),ROUND(Ryy,14));
-               
-               /* Knowing that gain, what the error: (x-g*y)^2 
+               g = MULT16_32_Q15(
+                        celt_sqrt(MULT16_16(ROUND(Ryp,14),ROUND(Ryp,14)) + Ryy -
+                                  MULT16_16(ROUND(Ryy,14),Rpp))
+                        - ROUND(Ryp,14),
+                   celt_rcp(SHR32(Ryy,12)));
+               /* Knowing that gain, what's the error: (x-g*y)^2 
                   (result is negated and we discard x^2 because it's constant) */
                /*score = 2.f*g*Rxy - 1.f*g*g*Ryy*NORM_SCALING_1;*/
-               score = 2*MULT16_32_Q14(ROUND(Rxy,14),g) -
-                     MULT16_32_Q14(EXTRACT16(MULT16_32_Q14(ROUND(Ryy,14),g)),g);
+               score = 2*MULT16_32_Q14(ROUND(Rxy,14),g)
+                       - MULT16_32_Q14(EXTRACT16(MULT16_32_Q14(ROUND(Ryy,14),g)),g);
 
                if (score>nbest[Lupdate-1]->score)
                {
-                  int k;
                   int id = Lupdate-1;
                   struct NBest *tmp_best;
 
@@ -340,7 +344,7 @@ void alg_quant(celt_norm_t *X, celt_mask_t *W, int N, int K, const celt_norm_t *
     the final normalised signal in the current band. */
 void alg_unquant(celt_norm_t *X, int N, int K, celt_norm_t *P, celt_word16_t alpha, ec_dec *dec)
 {
-   VARDECL(int *iy);
+   VARDECL(intiy);
    SAVE_STACK;
    ALLOC(iy, N, int);
    decode_pulses(iy, N, K, dec);
@@ -369,7 +373,6 @@ void intra_prediction(celt_norm_t *x, celt_mask_t *W, int N, int K, celt_norm_t
 
    for (i=0;i<max_pos*B;i+=B)
    {
-      int j;
       celt_word32_t xy=0, yy=0;
       celt_word32_t score;
       for (j=0;j<N;j++)