From 06181733a6c304bd152caadfc5cd2aaf492016b2 Mon Sep 17 00:00:00 2001 From: Karl Tomlinson Date: Tue, 1 Sep 2020 11:53:25 +1200 Subject: fixed-point: introduce MULT16_32_32 to handle unexpected types in MULT16_32_Q15 --- libspeexdsp/arch.h | 1 + libspeexdsp/fixed_debug.h | 15 +++++++++++++++ libspeexdsp/fixed_generic.h | 10 ++++++---- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/libspeexdsp/arch.h b/libspeexdsp/arch.h index 0b2accb..4d953c6 100644 --- a/libspeexdsp/arch.h +++ b/libspeexdsp/arch.h @@ -177,6 +177,7 @@ typedef float spx_word32_t; #define ADD32(a,b) ((a)+(b)) #define SUB32(a,b) ((a)-(b)) #define MULT16_16_16(a,b) ((a)*(b)) +#define MULT16_32_32(a,b) ((a)*(b)) #define MULT16_16(a,b) ((spx_word32_t)(a)*(spx_word32_t)(b)) #define MAC16_16(c,a,b) ((c)+(spx_word32_t)(a)*(spx_word32_t)(b)) diff --git a/libspeexdsp/fixed_debug.h b/libspeexdsp/fixed_debug.h index 25d4ea7..dbf02f1 100644 --- a/libspeexdsp/fixed_debug.h +++ b/libspeexdsp/fixed_debug.h @@ -250,6 +250,21 @@ static inline short MULT16_16_16(int a, int b) return res; } +/* result fits in 32 bits */ +static inline int MULT16_32_32(int a, long long b) +{ + long long res; + if (!VERIFY_SHORT(a) || !VERIFY_INT(b)) + { + fprintf (stderr, "MULT16_32_32: inputs are not short+int: %d %d\n", a, (int)b); + } + res = a*b; + if (!VERIFY_INT(res)) + fprintf (stderr, "MULT16_32_32: output is not int: %d\n", (int)res); + spx_mips++; + return res; +} + #define MULT16_16(a, b) _MULT16_16(a, b, __FILE__, __LINE__) static inline int _MULT16_16(int a, int b, char *file, int line) { diff --git a/libspeexdsp/fixed_generic.h b/libspeexdsp/fixed_generic.h index e5d1ce5..09366c3 100644 --- a/libspeexdsp/fixed_generic.h +++ b/libspeexdsp/fixed_generic.h @@ -69,16 +69,18 @@ /* result fits in 16 bits */ -#define MULT16_16_16(a,b) ((((spx_word16_t)(a))*((spx_word16_t)(b)))) +#define MULT16_16_16(a,b) (((spx_word16_t)(a))*((spx_word16_t)(b))) +/* result fits in 32 bits */ +#define MULT16_32_32(a,b) (((spx_word16_t)(a))*((spx_word32_t)(b))) /* (spx_word32_t)(spx_word16_t) gives TI compiler a hint that it's 16x16->32 multiply */ #define MULT16_16(a,b) (((spx_word32_t)(spx_word16_t)(a))*((spx_word32_t)(spx_word16_t)(b))) #define MAC16_16(c,a,b) (ADD32((c),MULT16_16((a),(b)))) -#define MULT16_32_P15(a,b) ADD32((a)*SHR((b),15), PSHR(MULT16_16((a),((b)&0x00007fff)),15)) -#define MULT16_32_Q15(a,b) ADD32((a)*SHR((b),15), SHR(MULT16_16((a),((b)&0x00007fff)),15)) -#define MAC16_32_Q15(c,a,b) ADD32(c,ADD32((a)*SHR((b),15), SHR(MULT16_16((a),((b)&0x00007fff)),15))) +#define MULT16_32_P15(a,b) ADD32(MULT16_32_32(a,SHR((b),15)), PSHR(MULT16_16((a),((b)&0x00007fff)),15)) +#define MULT16_32_Q15(a,b) ADD32(MULT16_32_32(a,SHR((b),15)), SHR(MULT16_16((a),((b)&0x00007fff)),15)) +#define MAC16_32_Q15(c,a,b) ADD32(c,MULT16_32_Q15(a,b)) #define MAC16_16_Q11(c,a,b) (ADD32((c),SHR(MULT16_16((a),(b)),11))) -- cgit v1.2.3