[xiph-cvs] cvs commit: speex/libspeex arch.h cb_search.c filters.c fixed_arm.h fixed_debug.h fixed_generic.h ltp.c nb_celp.c

Jean-Marc Valin jm at xiph.org
Sat Nov 29 21:46:04 PST 2003



jm          03/11/30 00:46:04

  Modified:    libspeex arch.h cb_search.c filters.c fixed_arm.h
                        fixed_debug.h fixed_generic.h ltp.c nb_celp.c
  Log:
  fixed-point: Fixed several overflows. Added an explicit saturation function

Revision  Changes    Path
1.7       +4 -0      speex/libspeex/arch.h

Index: arch.h
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/arch.h,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- arch.h	29 Nov 2003 07:03:58 -0000	1.6
+++ arch.h	30 Nov 2003 05:46:02 -0000	1.7
@@ -95,6 +95,8 @@
 #define PSHR(a,shift)       (a)
 #define SHR(a,shift)       (a)
 #define SHL(a,shift)       (a)
+#define SATURATE(x,a) (x)
+
 #define ADD16(a,b) ((a)+(b))
 #define SUB16(a,b) ((a)-(b))
 #define ADD32(a,b) ((a)+(b))
@@ -112,6 +114,8 @@
 #define MAC16_32_Q11(c,a,b)     ((c)+(a)*(b))
 #define MAC16_32_Q15(c,a,b)     ((c)+(a)*(b))
 
+#define MAC16_16_Q11(c,a,b)     ((c)+(a)*(b))
+#define MULT16_16_Q11(a,b)     ((a)*(b))
 #define MULT16_16_Q13(a,b)     ((a)*(b))
 #define MULT16_16_Q14(a,b)     ((a)*(b))
 #define MULT16_16_Q15(a,b)     ((a)*(b))

<p><p>1.103     +2 -2      speex/libspeex/cb_search.c

Index: cb_search.c
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/cb_search.c,v
retrieving revision 1.102
retrieving revision 1.103
diff -u -r1.102 -r1.103
--- cb_search.c	12 Nov 2003 17:16:38 -0000	1.102
+++ cb_search.c	30 Nov 2003 05:46:03 -0000	1.103
@@ -143,7 +143,7 @@
       {
          spx_word32_t resj=0;
          for (k=0;k<=j;k++)
-            resj = MAC16_32_Q11(resj,shape[k],r[j-k]);
+            resj = MAC16_16_Q11(resj,shape[k],r[j-k]);
 #ifndef FIXED_POINT
          resj *= 0.03125;
 #endif
@@ -236,7 +236,7 @@
 #ifdef FIXED_POINT
                   g=sign*shape_cb[rind*subvect_size+m];
                   for (n=subvect_size*(i+1);n<nsf;n++,q++)
-                     t[n] = SUB32(t[n],MULT16_32_Q11(g,r[q]));
+                     t[n] = SUB32(t[n],MULT16_16_Q11(g,r[q]));
 #else
                   g=sign*0.03125*shape_cb[rind*subvect_size+m];
                   for (n=subvect_size*(i+1);n<nsf;n++,q++)

<p><p>1.66      +6 -6      speex/libspeex/filters.c

Index: filters.c
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/filters.c,v
retrieving revision 1.65
retrieving revision 1.66
diff -u -r1.65 -r1.66
--- filters.c	28 Nov 2003 05:39:57 -0000	1.65
+++ filters.c	30 Nov 2003 05:46:03 -0000	1.66
@@ -193,8 +193,8 @@
    for (i=0;i<N;i++)
    {
       int xh,xl,yh,yl;
-      xi=x[i];
-      yi = xi + (mem[0]<<2);
+      xi=SATURATE(x[i],805306368);
+      yi = SATURATE(ADD32(xi, SHL(mem[0],2)),805306368);
       nyi = -yi;
       xh = xi>>15; xl=xi&0x00007fff; yh = yi>>15; yl=yi&0x00007fff; 
       for (j=0;j<ord-1;j++)
@@ -214,8 +214,8 @@
    for (i=0;i<N;i++)
    {
       int yh,yl;
-      xi=x[i];
-      yi = xi + (mem[0]<<2);
+      xi=SATURATE(x[i],805306368);
+      yi = SATURATE(xi + (mem[0]<<2),805306368);
       nyi = -yi;
       yh = yi>>15; yl=yi&0x00007fff; 
       for (j=0;j<ord-1;j++)
@@ -236,7 +236,7 @@
    for (i=0;i<N;i++)
    {
       int xh,xl;
-      xi=x[i];
+      xi=SATURATE(x[i],805306368);
       yi = xi + (mem[0]<<2);
       xh = xi>>15; xl=xi&0x00007fff;
       for (j=0;j<ord-1;j++)
@@ -244,7 +244,7 @@
          mem[j] = MAC16_32_Q15(mem[j+1], num[j+1],xi);
       }
       mem[ord-1] = MULT16_32_Q15(num[ord],xi);
-      y[i] = yi;
+      y[i] = SATURATE(yi,805306368);
    }
 
 }

<p><p>1.2       +5 -0      speex/libspeex/fixed_arm.h

Index: fixed_arm.h
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/fixed_arm.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- fixed_arm.h	29 Nov 2003 07:03:58 -0000	1.1
+++ fixed_arm.h	30 Nov 2003 05:46:03 -0000	1.2
@@ -38,6 +38,8 @@
 #define SHR(a,shift) ((a) >> (shift))
 #define SHL(a,shift) ((a) << (shift))
 
+#define SATURATE(x,a) ((x)>(a) ? (a) : (x)<-(a) ? -(a) : (x))
+
 
 #define ADD16(a,b) ((short)((short)(a)+(short)(b)))
 #define SUB16(a,b) ((a)-(b))
@@ -99,6 +101,9 @@
   return(res);
 }
 
+#define MAC16_16_Q11(c,a,b)     (ADD32((c),SHR(MULT16_16((a),(b)),11)))
+
+#define MULT16_16_Q11(a,b) (SHR(MULT16_16((a),(b)),11))
 #define MULT16_16_Q13(a,b) (SHR(MULT16_16((a),(b)),13))
 #define MULT16_16_Q14(a,b) (SHR(MULT16_16((a),(b)),14))
 #define MULT16_16_Q15(a,b) (SHR(MULT16_16((a),(b)),15))

<p><p>1.5       +24 -0     speex/libspeex/fixed_debug.h

Index: fixed_debug.h
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/fixed_debug.h,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- fixed_debug.h	29 Nov 2003 19:25:48 -0000	1.4
+++ fixed_debug.h	30 Nov 2003 05:46:03 -0000	1.5
@@ -133,8 +133,10 @@
    spx_mips++;
    return res;
 }
+#define MULT16_16B(a,b)     (((short)(a))*((short)(b)))
 
 #define MAC16_16(c,a,b)     (ADD32((c),MULT16_16((a),(b))))
+#define MAC16_16_Q11(c,a,b)     (ADD32((c),SHR(MULT16_16((a),(b)),11)))
 
 #define MULT16_32_Q12(a,b) ADD32(MULT16_16((a),SHR((b),12)), SHR(MULT16_16((a),((b)&0x00000fff)),12))
 #define MULT16_32_Q13(a,b) ADD32(MULT16_16((a),SHR((b),13)), SHR(MULT16_16((a),((b)&0x00001fff)),13))
@@ -146,7 +148,29 @@
 #define MULT16_32_Q15(a,b) ADD32(MULT16_16((a),SHR((b),15)), SHR(MULT16_16((a),((b)&0x00007fff)),15))
 #define MAC16_32_Q15(c,a,b) ADD32(c,ADD32(MULT16_16((a),SHR((b),15)), SHR(MULT16_16((a),((b)&0x00007fff)),15)))
 
+static inline int SATURATE(int a, int b)
+{
+   if (a>b)
+      a=b;
+   if (a<-b)
+      a = -b;
+   return a;
+}
 
+static inline short MULT16_16_Q11(int a, int b) 
+{
+   long long res;
+   if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
+   {
+      fprintf (stderr, "MULT16_16_Q11: inputs are not short: %d %d\n", a, b);
+   }
+   res = ((long long)a)*b;
+   res >>= 11;
+   if (!VERIFY_SHORT(res))
+      fprintf (stderr, "MULT16_16_Q11: output is not short: %d*%d=%d\n", a, b, res);
+   spx_mips++;
+   return res;
+}
 static inline short MULT16_16_Q13(int a, int b) 
 {
    long long res;

<p><p>1.2       +4 -0      speex/libspeex/fixed_generic.h

Index: fixed_generic.h
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/fixed_generic.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- fixed_generic.h	29 Nov 2003 07:03:58 -0000	1.1
+++ fixed_generic.h	30 Nov 2003 05:46:03 -0000	1.2
@@ -38,6 +38,7 @@
 #define SHR(a,shift) ((a) >> (shift))
 #define SHL(a,shift) ((a) << (shift))
 
+#define SATURATE(x,a) ((x)>(a) ? (a) : (x)<-(a) ? -(a) : (x))
 
 #define ADD16(a,b) ((short)((short)(a)+(short)(b)))
 #define SUB16(a,b) ((a)-(b))
@@ -67,6 +68,9 @@
 #define MAC16_32_Q15(c,a,b) ADD32(c,ADD32(MULT16_16((a),SHR((b),15)), SHR(MULT16_16((a),((b)&0x00007fff)),15)))
 
 
+#define MAC16_16_Q11(c,a,b)     (ADD32((c),SHR(MULT16_16((a),(b)),11)))
+
+#define MULT16_16_Q11(a,b) (SHR(MULT16_16((a),(b)),11))
 #define MULT16_16_Q13(a,b) (SHR(MULT16_16((a),(b)),13))
 #define MULT16_16_Q14(a,b) (SHR(MULT16_16((a),(b)),14))
 #define MULT16_16_Q15(a,b) (SHR(MULT16_16((a),(b)),15))

<p><p>1.104     +5 -6      speex/libspeex/ltp.c

Index: ltp.c
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/ltp.c,v
retrieving revision 1.103
retrieving revision 1.104
diff -u -r1.103 -r1.104
--- ltp.c	29 Nov 2003 19:25:48 -0000	1.103
+++ ltp.c	30 Nov 2003 05:46:03 -0000	1.104
@@ -180,7 +180,6 @@
 }
 
 
-
 /** Finds the best quantized 3-tap pitch predictor by analysis by synthesis */
 static spx_word64_t pitch_gain_search_3tap(
 spx_sig_t target[],                 /* Target vector */
@@ -225,7 +224,6 @@
    e[0]=tmp2;
    e[1]=tmp2+nsf;
    e[2]=tmp2+2*nsf;
-   
    for (i=2;i>=0;i--)
    {
       int pp=pitch+1-i;
@@ -249,7 +247,7 @@
          {
             /* FIXME: Check for overflows */
             /*x[i][j]+=e[i][0]*r[j]/SIG_SCALING;*/
-            x[i][j]+=MULT16_32_Q13(SHR(r[j],1), e[i][0]);
+            x[i][j]+=SHL(MULT16_32_Q15(r[j], e[i][0]),1);
             /*printf ("%d\n", (int)r[j]);*/
          }
       }
@@ -386,14 +384,15 @@
 
 #ifdef FIXED_POINT
    for (i=0;i<nsf;i++)
-     exc[i]=SHL(MULT16_32_Q14(SHL(gain[0],7),e[2][i])+MULT16_32_Q14(SHL(gain[1],7),e[1][i])+MULT16_32_Q14(SHL(gain[2],7),e[0][i]),1);
+     exc[i]=SHL(MULT16_32_Q15(SHL(gain[0],7),e[2][i])+MULT16_32_Q15(SHL(gain[1],7),e[1][i])+MULT16_32_Q15(SHL(gain[2],7),e[0][i]),2);
    
    err=0;
    for (i=0;i<nsf;i++)
    {
-      spx_sig_t perr=target[i]-SHL((MULT16_32_Q14(SHL(gain[0],7),x[2][i])+MULT16_32_Q14(SHL(gain[1],7),x[1][i])+MULT16_32_Q14(SHL(gain[2],7),x[0][i])),1);
+      spx_sig_t perr=target[i]-SHL((MULT16_32_Q15(SHL(gain[0],7),x[2][i])+MULT16_32_Q15(SHL(gain[1],7),x[1][i])+MULT16_32_Q15(SHL(gain[2],7),x[0][i])),2);
       spx_word16_t perr2 = PSHR(perr,15);
       err = ADD64(err,MULT16_16(perr2,perr2));
+      
    }
 #else
    for (i=0;i<nsf;i++)
@@ -624,7 +623,7 @@
 #ifdef FIXED_POINT
       {
          for (i=0;i<nsf;i++)
-            exc[i]=SHL(MULT16_32_Q14(SHL(sgain[0],7),e[2][i])+MULT16_32_Q14(SHL(sgain[1],7),e[1][i])+MULT16_32_Q14(SHL(sgain[2],7),e[0][i]),1);
+            exc[i]=SHL(MULT16_32_Q15(SHL(sgain[0],7),e[2][i])+MULT16_32_Q15(SHL(sgain[1],7),e[1][i])+MULT16_32_Q15(SHL(sgain[2],7),e[0][i]),2);
       }
 #else
       for (i=0;i<nsf;i++)

<p><p>1.161     +1 -1      speex/libspeex/nb_celp.c

Index: nb_celp.c
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/nb_celp.c,v
retrieving revision 1.160
retrieving revision 1.161
diff -u -r1.160 -r1.161
--- nb_celp.c	29 Nov 2003 08:12:51 -0000	1.160
+++ nb_celp.c	30 Nov 2003 05:46:03 -0000	1.161
@@ -780,7 +780,7 @@
       /* Update target for adaptive codebook contribution */
       syn_percep_zero(exc, st->interp_qlpc, st->bw_lpc1, st->bw_lpc2, res, st->subframeSize, st->lpcSize, stack);
       for (i=0;i<st->subframeSize;i++)
-         target[i]-=res[i];
+         target[i]=SATURATE(SUB32(target[i],res[i]),805306368);
 
 
       /* Quantization of innovation */

<p><p>--- >8 ----
List archives:  http://www.xiph.org/archives/
Ogg project homepage: http://www.xiph.org/ogg/
To unsubscribe from this list, send a message to 'cvs-request at xiph.org'
containing only the word 'unsubscribe' in the body.  No subject is needed.
Unsubscribe messages sent to the list will be ignored/filtered.



More information about the commits mailing list