[xiph-cvs] cvs commit: speex/libspeex fixed_debug.h nb_celp.c
Jean-Marc Valin
jm at xiph.org
Sat Nov 29 00:12:51 PST 2003
jm 03/11/29 03:12:51
Modified: libspeex fixed_debug.h nb_celp.c
Log:
fixed-point: fixed some overflows
Revision Changes Path
1.3 +94 -6 speex/libspeex/fixed_debug.h
Index: fixed_debug.h
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/fixed_debug.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- fixed_debug.h 29 Nov 2003 07:38:24 -0000 1.2
+++ fixed_debug.h 29 Nov 2003 08:12:51 -0000 1.3
@@ -190,16 +190,104 @@
return res;
}
-#define MULT16_16_P13(a,b) (SHR(ADD32(4096,MULT16_16((a),(b))),13))
-#define MULT16_16_P14(a,b) (SHR(ADD32(8192,MULT16_16((a),(b))),14))
-#define MULT16_16_P15(a,b) (SHR(ADD32(16384,MULT16_16((a),(b))),15))
+static inline short MULT16_16_P13(int a, int b)
+{
+ long long res;
+ if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
+ {
+ fprintf (stderr, "MULT16_16_P13: inputs are not short: %d %d\n", a, b);
+ }
+ res = ((long long)a)*b;
+ res += 4096;
+ if (!VERIFY_INT(res))
+ fprintf (stderr, "MULT16_16_P13: overflow: %d*%d=%d\n", a, b, res);
+ res >>= 13;
+ if (!VERIFY_SHORT(res))
+ fprintf (stderr, "MULT16_16_P13: output is not short: %d*%d=%d\n", a, b, res);
+ spx_mips++;
+ return res;
+}
+static inline short MULT16_16_P14(int a, int b)
+{
+ long long res;
+ if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
+ {
+ fprintf (stderr, "MULT16_16_P14: inputs are not short: %d %d\n", a, b);
+ }
+ res = ((long long)a)*b;
+ res += 8192;
+ if (!VERIFY_INT(res))
+ fprintf (stderr, "MULT16_16_P14: overflow: %d*%d=%d\n", a, b, res);
+ res >>= 14;
+ if (!VERIFY_SHORT(res))
+ fprintf (stderr, "MULT16_16_P14: output is not short: %d*%d=%d\n", a, b, res);
+ spx_mips++;
+ return res;
+}
+static inline short MULT16_16_P15(int a, int b)
+{
+ long long res;
+ if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
+ {
+ fprintf (stderr, "MULT16_16_P15: inputs are not short: %d %d\n", a, b);
+ }
+ res = ((long long)a)*b;
+ res += 16384;
+ if (!VERIFY_INT(res))
+ fprintf (stderr, "MULT16_16_P15: overflow: %d*%d=%d\n", a, b, res);
+ res >>= 15;
+ if (!VERIFY_SHORT(res))
+ fprintf (stderr, "MULT16_16_P15: output is not short: %d*%d=%d\n", a, b, res);
+ spx_mips++;
+ return res;
+}
#define MUL_16_32_R15(a,bh,bl) ADD32(MULT16_16((a),(bh)), SHR(MULT16_16((a),(bl)),15))
-
-#define DIV32_16(a,b) ((short)(((signed int)(a))/((short)(b))))
-#define DIV32(a,b) (((signed int)(a))/((signed int)(b)))
+static inline int DIV32_16(long long a, long long b)
+{
+ long long res;
+ if (b==0)
+ {
+ fprintf(stderr, "DIV32_16: divide by zero: %d/%d\n", a, b);
+ return 0;
+ }
+ if (!VERIFY_INT(a) || !VERIFY_SHORT(b))
+ {
+ fprintf (stderr, "DIV32_16: inputs are not int/short: %d %d\n", a, b);
+ }
+ res = a/b;
+ if (!VERIFY_SHORT(res))
+ {
+ fprintf (stderr, "DIV32_16: output is not short: %d / %d = %d\n", (int)a,(int)b,(int)res);
+ if (res>32767)
+ res = 32767;
+ if (res<-32768)
+ res = -32768;
+ }
+ spx_mips++;
+ return res;
+}
+static inline int DIV32(long long a, long long b)
+{
+ long long res;
+ if (b==0)
+ {
+ fprintf(stderr, "DIV32: divide by zero: %d/%d\n", a, b);
+ return 0;
+ }
+
+ if (!VERIFY_INT(a) || !VERIFY_INT(b))
+ {
+ fprintf (stderr, "DIV32: inputs are not int/short: %d %d\n", a, b);
+ }
+ res = a/b;
+ if (!VERIFY_INT(res))
+ fprintf (stderr, "DIV32: output is not int: %d\n", res);
+ spx_mips++;
+ return res;
+}
<p><p>1.160 +12 -2 speex/libspeex/nb_celp.c
Index: nb_celp.c
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/nb_celp.c,v
retrieving revision 1.159
retrieving revision 1.160
diff -u -r1.159 -r1.160
--- nb_celp.c 29 Nov 2003 05:17:31 -0000 1.159
+++ nb_celp.c 29 Nov 2003 08:12:51 -0000 1.160
@@ -801,8 +801,18 @@
printf ("%f\n", st->buf2[i]/ener);
*/
- fine_gain = DIV32_16(ener,SHR(ol_gain,SIG_SHIFT));
-
+ /*FIXME: Should use DIV32_16 and make sure result fits in 16 bits */
+#ifdef FIXED_POINT
+ {
+ spx_word32_t f = DIV32(ener,PSHR(ol_gain,SIG_SHIFT));
+ if (f<32768)
+ fine_gain = f;
+ else
+ fine_gain = 32767;
+ }
+#else
+ fine_gain = DIV32_16(ener,PSHR(ol_gain,SIG_SHIFT));
+#endif
/* Calculate gain correction for the sub-frame (if any) */
if (SUBMODE(have_subframe_gain))
{
<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