~ubuntu-branches/ubuntu/vivid/linphone/vivid

« back to all changes in this revision

Viewing changes to speex/libspeex/fixed_debug.h

  • Committer: Bazaar Package Importer
  • Author(s): Samuel Mimram
  • Date: 2006-11-15 10:34:50 UTC
  • mfrom: (1.2.1 upstream) (2.1.8 feisty)
  • Revision ID: james.westby@ubuntu.com-20061115103450-qgafwcks2lkhctlj
* New upstream release.
* Enable video support.
* Fix mismatched #endif in mscommon.h, closes: #398307.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2003 Jean-Marc Valin */
2
 
/**
3
 
   @file fixed_debug.h
4
 
   @brief Fixed-point operations with debugging
5
 
*/
6
 
/*
7
 
   Redistribution and use in source and binary forms, with or without
8
 
   modification, are permitted provided that the following conditions
9
 
   are met:
10
 
   
11
 
   - Redistributions of source code must retain the above copyright
12
 
   notice, this list of conditions and the following disclaimer.
13
 
   
14
 
   - Redistributions in binary form must reproduce the above copyright
15
 
   notice, this list of conditions and the following disclaimer in the
16
 
   documentation and/or other materials provided with the distribution.
17
 
   
18
 
   - Neither the name of the Xiph.org Foundation nor the names of its
19
 
   contributors may be used to endorse or promote products derived from
20
 
   this software without specific prior written permission.
21
 
   
22
 
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23
 
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24
 
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25
 
   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
26
 
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27
 
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28
 
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29
 
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30
 
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31
 
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32
 
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
 
*/
34
 
 
35
 
#ifndef FIXED_DEBUG_H
36
 
#define FIXED_DEBUG_H
37
 
 
38
 
#include <stdio.h>
39
 
 
40
 
extern long long spx_mips;
41
 
#define MIPS_INC spx_mips++,
42
 
 
43
 
#define VERIFY_SHORT(x) ((x)<=32767&&(x)>=-32768)
44
 
#define VERIFY_INT(x) ((x)<=2147483647LL&&(x)>=-2147483648LL)
45
 
 
46
 
#define SHR(a,shift) ((a) >> (shift))
47
 
#define SHL(a,shift) ((a) << (shift))
48
 
 
49
 
static inline short ADD16(int a, int b) 
50
 
{
51
 
   int res;
52
 
   if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
53
 
   {
54
 
      fprintf (stderr, "ADD16: inputs are not short: %d %d\n", a, b);
55
 
   }
56
 
   res = a+b;
57
 
   if (!VERIFY_SHORT(res))
58
 
      fprintf (stderr, "ADD16: output is not short: %d\n", res);
59
 
   spx_mips++;
60
 
   return res;
61
 
}
62
 
static inline short SUB16(int a, int b) 
63
 
{
64
 
   int res;
65
 
   if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
66
 
   {
67
 
      fprintf (stderr, "SUB16: inputs are not short: %d %d\n", a, b);
68
 
   }
69
 
   res = a-b;
70
 
   if (!VERIFY_SHORT(res))
71
 
      fprintf (stderr, "SUB16: output is not short: %d\n", res);
72
 
   spx_mips++;
73
 
   return res;
74
 
}
75
 
 
76
 
static inline int ADD32(long long a, long long b) 
77
 
{
78
 
   long long res;
79
 
   if (!VERIFY_INT(a) || !VERIFY_INT(b))
80
 
   {
81
 
      fprintf (stderr, "ADD32: inputs are not int: %d %d\n", (int)a, (int)b);
82
 
   }
83
 
   res = a+b;
84
 
   if (!VERIFY_INT(res))
85
 
      fprintf (stderr, "ADD32: output is not int: %d\n", (int)res);
86
 
   spx_mips++;
87
 
   return res;
88
 
}
89
 
 
90
 
static inline int SUB32(long long a, long long b) 
91
 
{
92
 
   long long res;
93
 
   if (!VERIFY_INT(a) || !VERIFY_INT(b))
94
 
   {
95
 
      fprintf (stderr, "SUB32: inputs are not int: %d %d\n", (int)a, (int)b);
96
 
   }
97
 
   res = a-b;
98
 
   if (!VERIFY_INT(res))
99
 
      fprintf (stderr, "SUB32: output is not int: %d\n", (int)res);
100
 
   spx_mips++;
101
 
   return res;
102
 
}
103
 
 
104
 
#define ADD64(a,b) (MIPS_INC(a)+(b))
105
 
 
106
 
#define PSHR(a,shift) (SHR((a)+(1<<((shift)-1)),shift))
107
 
 
108
 
/* result fits in 16 bits */
109
 
static inline short MULT16_16_16(int a, int b) 
110
 
{
111
 
   int res;
112
 
   if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
113
 
   {
114
 
      fprintf (stderr, "MULT16_16_16: inputs are not short: %d %d\n", a, b);
115
 
   }
116
 
   res = a*b;
117
 
   if (!VERIFY_SHORT(res))
118
 
      fprintf (stderr, "MULT16_16_16: output is not short: %d\n", res);
119
 
   spx_mips++;
120
 
   return res;
121
 
}
122
 
 
123
 
static inline int MULT16_16(int a, int b) 
124
 
{
125
 
   long long res;
126
 
   if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
127
 
   {
128
 
      fprintf (stderr, "MULT16_16: inputs are not short: %d %d\n", a, b);
129
 
   }
130
 
   res = ((long long)a)*b;
131
 
   if (!VERIFY_INT(res))
132
 
      fprintf (stderr, "MULT16_16: output is not int: %d\n", (int)res);
133
 
   spx_mips++;
134
 
   return res;
135
 
}
136
 
#define MULT16_16B(a,b)     (((short)(a))*((short)(b)))
137
 
 
138
 
#define MAC16_16(c,a,b)     (ADD32((c),MULT16_16((a),(b))))
139
 
#define MAC16_16_Q11(c,a,b)     (ADD32((c),SHR(MULT16_16((a),(b)),11)))
140
 
 
141
 
#define MULT16_32_Q12(a,b) ADD32(MULT16_16((a),SHR((b),12)), SHR(MULT16_16((a),((b)&0x00000fff)),12))
142
 
#define MULT16_32_Q13(a,b) ADD32(MULT16_16((a),SHR((b),13)), SHR(MULT16_16((a),((b)&0x00001fff)),13))
143
 
#define MULT16_32_Q14(a,b) ADD32(MULT16_16((a),SHR((b),14)), SHR(MULT16_16((a),((b)&0x00003fff)),14))
144
 
 
145
 
#define MULT16_32_Q11(a,b) ADD32(MULT16_16((a),SHR((b),11)), SHR(MULT16_16((a),((b)&0x000007ff)),11))
146
 
#define MAC16_32_Q11(c,a,b) ADD32(c,ADD32(MULT16_16((a),SHR((b),11)), SHR(MULT16_16((a),((b)&0x000007ff)),11)))
147
 
 
148
 
#define MULT16_32_Q15(a,b) ADD32(MULT16_16((a),SHR((b),15)), SHR(MULT16_16((a),((b)&0x00007fff)),15))
149
 
#define MAC16_32_Q15(c,a,b) ADD32(c,ADD32(MULT16_16((a),SHR((b),15)), SHR(MULT16_16((a),((b)&0x00007fff)),15)))
150
 
 
151
 
static inline int SATURATE(int a, int b)
152
 
{
153
 
   if (a>b)
154
 
      a=b;
155
 
   if (a<-b)
156
 
      a = -b;
157
 
   return a;
158
 
}
159
 
 
160
 
static inline short MULT16_16_Q11(int a, int b) 
161
 
{
162
 
   long long res;
163
 
   if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
164
 
   {
165
 
      fprintf (stderr, "MULT16_16_Q11: inputs are not short: %d %d\n", a, b);
166
 
   }
167
 
   res = ((long long)a)*b;
168
 
   res >>= 11;
169
 
   if (!VERIFY_SHORT(res))
170
 
      fprintf (stderr, "MULT16_16_Q11: output is not short: %d*%d=%d\n", (int)a, (int)b, (int)res);
171
 
   spx_mips++;
172
 
   return res;
173
 
}
174
 
static inline short MULT16_16_Q13(int a, int b) 
175
 
{
176
 
   long long res;
177
 
   if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
178
 
   {
179
 
      fprintf (stderr, "MULT16_16_Q13: inputs are not short: %d %d\n", a, b);
180
 
   }
181
 
   res = ((long long)a)*b;
182
 
   res >>= 13;
183
 
   if (!VERIFY_SHORT(res))
184
 
      fprintf (stderr, "MULT16_16_Q13: output is not short: %d*%d=%d\n", a, b, (int)res);
185
 
   spx_mips++;
186
 
   return res;
187
 
}
188
 
static inline short MULT16_16_Q14(int a, int b) 
189
 
{
190
 
   long long res;
191
 
   if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
192
 
   {
193
 
      fprintf (stderr, "MULT16_16_Q14: inputs are not short: %d %d\n", a, b);
194
 
   }
195
 
   res = ((long long)a)*b;
196
 
   res >>= 14;
197
 
   if (!VERIFY_SHORT(res))
198
 
      fprintf (stderr, "MULT16_16_Q14: output is not short: %d\n", (int)res);
199
 
   spx_mips++;
200
 
   return res;
201
 
}
202
 
static inline short MULT16_16_Q15(int a, int b) 
203
 
{
204
 
   long long res;
205
 
   if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
206
 
   {
207
 
      fprintf (stderr, "MULT16_16_Q15: inputs are not short: %d %d\n", a, b);
208
 
   }
209
 
   res = ((long long)a)*b;
210
 
   res >>= 15;
211
 
   if (!VERIFY_SHORT(res))
212
 
      fprintf (stderr, "MULT16_16_Q15: output is not short: %d\n", (int)res);
213
 
   spx_mips++;
214
 
   return res;
215
 
}
216
 
 
217
 
static inline short MULT16_16_P13(int a, int b) 
218
 
{
219
 
   long long res;
220
 
   if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
221
 
   {
222
 
      fprintf (stderr, "MULT16_16_P13: inputs are not short: %d %d\n", a, b);
223
 
   }
224
 
   res = ((long long)a)*b;
225
 
   res += 4096;
226
 
   if (!VERIFY_INT(res))
227
 
      fprintf (stderr, "MULT16_16_P13: overflow: %d*%d=%d\n", a, b, (int)res);
228
 
   res >>= 13;
229
 
   if (!VERIFY_SHORT(res))
230
 
      fprintf (stderr, "MULT16_16_P13: output is not short: %d*%d=%d\n", a, b, (int)res);
231
 
   spx_mips++;
232
 
   return res;
233
 
}
234
 
static inline short MULT16_16_P14(int a, int b) 
235
 
{
236
 
   long long res;
237
 
   if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
238
 
   {
239
 
      fprintf (stderr, "MULT16_16_P14: inputs are not short: %d %d\n", a, b);
240
 
   }
241
 
   res = ((long long)a)*b;
242
 
   res += 8192;
243
 
   if (!VERIFY_INT(res))
244
 
      fprintf (stderr, "MULT16_16_P14: overflow: %d*%d=%d\n", a, b, (int)res);
245
 
   res >>= 14;
246
 
   if (!VERIFY_SHORT(res))
247
 
      fprintf (stderr, "MULT16_16_P14: output is not short: %d*%d=%d\n", a, b, (int)res);
248
 
   spx_mips++;
249
 
   return res;
250
 
}
251
 
static inline short MULT16_16_P15(int a, int b) 
252
 
{
253
 
   long long res;
254
 
   if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b))
255
 
   {
256
 
      fprintf (stderr, "MULT16_16_P15: inputs are not short: %d %d\n", a, b);
257
 
   }
258
 
   res = ((long long)a)*b;
259
 
   res += 16384;
260
 
   if (!VERIFY_INT(res))
261
 
      fprintf (stderr, "MULT16_16_P15: overflow: %d*%d=%d\n", a, b, (int)res);
262
 
   res >>= 15;
263
 
   if (!VERIFY_SHORT(res))
264
 
      fprintf (stderr, "MULT16_16_P15: output is not short: %d*%d=%d\n", a, b, (int)res);
265
 
   spx_mips++;
266
 
   return res;
267
 
}
268
 
 
269
 
#define MUL_16_32_R15(a,bh,bl) ADD32(MULT16_16((a),(bh)), SHR(MULT16_16((a),(bl)),15))
270
 
 
271
 
 
272
 
static inline int DIV32_16(long long a, long long b) 
273
 
{
274
 
   long long res;
275
 
   if (b==0)
276
 
   {
277
 
      fprintf(stderr, "DIV32_16: divide by zero: %d/%d\n", (int)a, (int)b);
278
 
      return 0;
279
 
   }
280
 
   if (!VERIFY_INT(a) || !VERIFY_SHORT(b))
281
 
   {
282
 
      fprintf (stderr, "DIV32_16: inputs are not int/short: %d %d\n", (int)a, (int)b);
283
 
   }
284
 
   res = a/b;
285
 
   if (!VERIFY_SHORT(res))
286
 
   {
287
 
      fprintf (stderr, "DIV32_16: output is not short: %d / %d = %d\n", (int)a,(int)b,(int)res);
288
 
      if (res>32767)
289
 
         res = 32767;
290
 
      if (res<-32768)
291
 
         res = -32768;
292
 
   }
293
 
   spx_mips++;
294
 
   return res;
295
 
}
296
 
static inline int DIV32(long long a, long long b) 
297
 
{
298
 
   long long res;
299
 
   if (b==0)
300
 
   {
301
 
      fprintf(stderr, "DIV32: divide by zero: %d/%d\n", (int)a, (int)b);
302
 
      return 0;
303
 
   }
304
 
 
305
 
   if (!VERIFY_INT(a) || !VERIFY_INT(b))
306
 
   {
307
 
      fprintf (stderr, "DIV32: inputs are not int/short: %d %d\n", (int)a, (int)b);
308
 
   }
309
 
   res = a/b;
310
 
   if (!VERIFY_INT(res))
311
 
      fprintf (stderr, "DIV32: output is not int: %d\n", (int)res);
312
 
   spx_mips++;
313
 
   return res;
314
 
}
315
 
 
316
 
 
317
 
 
318
 
#endif