2
* libmad - MPEG audio decoder library
3
* Copyright (C) 2000-2004 Underbit Technologies, Inc.
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
* $Id: fixed.h,v 1.38 2004/02/17 02:02:03 rob Exp $
22
# ifndef LIBMAD_FIXED_H
23
# define LIBMAD_FIXED_H
24
#include "../ADM_library/default.h"
25
#define mad_fixed_t int32_t
26
#define mad_fixed64hi_t int32_t
27
#define mad_fixed64lo_t uint32_t
31
typedef signed int mad_fixed_t;
33
typedef signed int mad_fixed64hi_t;
34
typedef unsigned int mad_fixed64lo_t;
36
typedef signed long mad_fixed_t;
38
typedef signed long mad_fixed64hi_t;
39
typedef unsigned long mad_fixed64lo_t;
43
# if defined(_MSC_VER)
44
# define mad_fixed64_t signed __int64
45
# elif 1 || defined(__GNUC__)
46
# define mad_fixed64_t signed long long
49
# if defined(FPM_FLOAT)
50
typedef double mad_sample_t;
52
typedef mad_fixed_t mad_sample_t;
56
* Fixed-point format: 0xABBBBBBB
57
* A == whole part (sign + 3 bits)
58
* B == fractional part (28 bits)
60
* Values are signed two's complement, so the effective range is:
61
* 0x80000000 to 0x7fffffff
62
* -8.0 to +7.9999999962747097015380859375
64
* The smallest representable value is:
65
* 0x00000001 == 0.0000000037252902984619140625 (i.e. about 3.725e-9)
67
* 28 bits of fractional accuracy represent about
68
* 8.6 digits of decimal accuracy.
70
* Fixed-point numbers can be added or subtracted as normal
71
* integers, but multiplication requires shifting the 64-bit result
72
* from 56 fractional bits back to 28 (and rounding.)
74
* Changing the definition of MAD_F_FRACBITS is only partially
75
* supported, and must be done with care.
78
# define MAD_F_FRACBITS 28
80
# if MAD_F_FRACBITS == 28
81
# define MAD_F(x) ((mad_fixed_t) (x##L))
83
# if MAD_F_FRACBITS < 28
84
# warning "MAD_F_FRACBITS < 28"
85
# define MAD_F(x) ((mad_fixed_t) \
87
(1L << (28 - MAD_F_FRACBITS - 1))) >> \
88
(28 - MAD_F_FRACBITS)))
89
# elif MAD_F_FRACBITS > 28
90
# error "MAD_F_FRACBITS > 28 not currently supported"
91
# define MAD_F(x) ((mad_fixed_t) \
92
((x##L) << (MAD_F_FRACBITS - 28)))
96
# define MAD_F_MIN ((mad_fixed_t) -0x80000000L)
97
# define MAD_F_MAX ((mad_fixed_t) +0x7fffffffL)
99
# define MAD_F_ONE MAD_F(0x10000000)
101
# define mad_f_tofixed(x) ((mad_fixed_t) \
102
((x) * (double) (1L << MAD_F_FRACBITS) + 0.5))
103
# define mad_f_todouble(x) ((double) \
104
((x) / (double) (1L << MAD_F_FRACBITS)))
106
# define mad_f_intpart(x) ((x) >> MAD_F_FRACBITS)
107
# define mad_f_fracpart(x) ((x) & ((1L << MAD_F_FRACBITS) - 1))
108
/* (x should be positive) */
110
# define mad_f_fromint(x) ((x) << MAD_F_FRACBITS)
112
# define mad_f_add(x, y) ((x) + (y))
113
# define mad_f_sub(x, y) ((x) - (y))
115
# if defined(FPM_FLOAT)
116
# error "FPM_FLOAT not yet supported"
119
# define MAD_F(x) mad_f_todouble(x)
121
# define mad_f_mul(x, y) ((x) * (y))
122
# define mad_f_scale64
124
# undef ASO_ZEROCHECK
126
# elif defined(FPM_64BIT)
129
* This version should be the most accurate if 64-bit types are supported by
130
* the compiler, although it may not be the most efficient.
132
# if defined(OPT_ACCURACY)
133
# define mad_f_mul(x, y) \
135
((((mad_fixed64_t) (x) * (y)) + \
136
(1L << (MAD_F_SCALEBITS - 1))) >> MAD_F_SCALEBITS))
138
# define mad_f_mul(x, y) \
139
((mad_fixed_t) (((mad_fixed64_t) (x) * (y)) >> MAD_F_SCALEBITS))
142
# define MAD_F_SCALEBITS MAD_F_FRACBITS
144
/* --- Intel --------------------------------------------------------------- */
146
# elif defined(FPM_INTEL)
148
# if defined(_MSC_VER)
149
# pragma warning(push)
150
# pragma warning(disable: 4035) /* no return value */
152
mad_fixed_t mad_f_mul_inline(mad_fixed_t x, mad_fixed_t y)
155
fracbits = MAD_F_FRACBITS
161
shrd eax, edx, fracbits
164
/* implicit return of eax */
166
# pragma warning(pop)
168
# define mad_f_mul mad_f_mul_inline
169
# define mad_f_scale64
172
* This Intel version is fast and accurate; the disposition of the least
173
* significant bit depends on OPT_ACCURACY via mad_f_scale64().
175
# define MAD_F_MLX(hi, lo, x, y) \
177
: "=a" (lo), "=d" (hi) \
178
: "%a" (x), "rm" (y) \
181
# if defined(OPT_ACCURACY)
183
* This gives best accuracy but is not very fast.
185
# define MAD_F_MLA(hi, lo, x, y) \
186
({ mad_fixed64hi_t __hi; \
187
mad_fixed64lo_t __lo; \
188
MAD_F_MLX(__hi, __lo, (x), (y)); \
189
asm ("addl %2,%0\n\t" \
191
: "=rm" (lo), "=rm" (hi) \
192
: "r" (__lo), "r" (__hi), "0" (lo), "1" (hi) \
195
# endif /* OPT_ACCURACY */
197
# if defined(OPT_ACCURACY)
199
* Surprisingly, this is faster than SHRD followed by ADC.
201
# define mad_f_scale64(hi, lo) \
202
({ mad_fixed64hi_t __hi_; \
203
mad_fixed64lo_t __lo_; \
204
mad_fixed_t __result; \
205
asm ("addl %4,%2\n\t" \
207
: "=rm" (__lo_), "=rm" (__hi_) \
208
: "0" (lo), "1" (hi), \
209
"ir" (1L << (MAD_F_SCALEBITS - 1)), "ir" (0) \
211
asm ("shrdl %3,%2,%1" \
213
: "0" (__lo_), "r" (__hi_), "I" (MAD_F_SCALEBITS) \
217
# elif defined(OPT_INTEL)
219
* Alternate Intel scaling that may or may not perform better.
221
# define mad_f_scale64(hi, lo) \
222
({ mad_fixed_t __result; \
223
asm ("shrl %3,%1\n\t" \
227
: "0" (lo), "r" (hi), \
228
"I" (MAD_F_SCALEBITS), "I" (32 - MAD_F_SCALEBITS) \
233
# define mad_f_scale64(hi, lo) \
234
({ mad_fixed_t __result; \
235
asm ("shrdl %3,%2,%1" \
237
: "0" (lo), "r" (hi), "I" (MAD_F_SCALEBITS) \
241
# endif /* OPT_ACCURACY */
243
# define MAD_F_SCALEBITS MAD_F_FRACBITS
246
/* --- ARM ----------------------------------------------------------------- */
248
# elif defined(FPM_ARM)
251
* This ARM V4 version is as accurate as FPM_64BIT but much faster. The
252
* least significant bit is properly rounded at no CPU cycle cost!
256
* This is faster than the default implementation via MAD_F_MLX() and
259
# define mad_f_mul(x, y) \
260
({ mad_fixed64hi_t __hi; \
261
mad_fixed64lo_t __lo; \
262
mad_fixed_t __result; \
263
asm ("smull %0, %1, %3, %4\n\t" \
264
"movs %0, %0, lsr %5\n\t" \
265
"adc %2, %0, %1, lsl %6" \
266
: "=&r" (__lo), "=&r" (__hi), "=r" (__result) \
267
: "%r" (x), "r" (y), \
268
"M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS) \
274
# define MAD_F_MLX(hi, lo, x, y) \
275
asm ("smull %0, %1, %2, %3" \
276
: "=&r" (lo), "=&r" (hi) \
279
# define MAD_F_MLA(hi, lo, x, y) \
280
asm ("smlal %0, %1, %2, %3" \
281
: "+r" (lo), "+r" (hi) \
284
# define MAD_F_MLN(hi, lo) \
285
asm ("rsbs %0, %2, #0\n\t" \
287
: "=r" (lo), "=r" (hi) \
288
: "0" (lo), "1" (hi) \
291
# define mad_f_scale64(hi, lo) \
292
({ mad_fixed_t __result; \
293
asm ("movs %0, %1, lsr %3\n\t" \
294
"adc %0, %0, %2, lsl %4" \
296
: "r" (lo), "r" (hi), \
297
"M" (MAD_F_SCALEBITS), "M" (32 - MAD_F_SCALEBITS) \
302
# define MAD_F_SCALEBITS MAD_F_FRACBITS
304
/* --- MIPS ---------------------------------------------------------------- */
306
# elif defined(FPM_MIPS)
309
* This MIPS version is fast and accurate; the disposition of the least
310
* significant bit depends on OPT_ACCURACY via mad_f_scale64().
312
# define MAD_F_MLX(hi, lo, x, y) \
314
: "=l" (lo), "=h" (hi) \
317
# if defined(HAVE_MADD_ASM)
318
# define MAD_F_MLA(hi, lo, x, y) \
320
: "+l" (lo), "+h" (hi) \
322
# elif defined(HAVE_MADD16_ASM)
324
* This loses significant accuracy due to the 16-bit integer limit in the
325
* multiply/accumulate instruction.
327
# define MAD_F_ML0(hi, lo, x, y) \
329
: "=l" (lo), "=h" (hi) \
330
: "%r" ((x) >> 12), "r" ((y) >> 16))
331
# define MAD_F_MLA(hi, lo, x, y) \
332
asm ("madd16 %2,%3" \
333
: "+l" (lo), "+h" (hi) \
334
: "%r" ((x) >> 12), "r" ((y) >> 16))
335
# define MAD_F_MLZ(hi, lo) ((mad_fixed_t) (lo))
338
# if defined(OPT_SPEED)
339
# define mad_f_scale64(hi, lo) \
340
((mad_fixed_t) ((hi) << (32 - MAD_F_SCALEBITS)))
341
# define MAD_F_SCALEBITS MAD_F_FRACBITS
344
/* --- SPARC --------------------------------------------------------------- */
346
# elif defined(FPM_SPARC)
349
* This SPARC V8 version is fast and accurate; the disposition of the least
350
* significant bit depends on OPT_ACCURACY via mad_f_scale64().
352
# define MAD_F_MLX(hi, lo, x, y) \
353
asm ("smul %2, %3, %0\n\t" \
355
: "=r" (lo), "=r" (hi) \
356
: "%r" (x), "rI" (y))
358
/* --- PowerPC ------------------------------------------------------------- */
360
# elif defined(FPM_PPC)
363
* This PowerPC version is fast and accurate; the disposition of the least
364
* significant bit depends on OPT_ACCURACY via mad_f_scale64().
366
# define MAD_F_MLX(hi, lo, x, y) \
368
asm ("mullw %0,%1,%2" \
370
: "%r" (x), "r" (y)); \
371
asm ("mulhw %0,%1,%2" \
373
: "%r" (x), "r" (y)); \
377
# if defined(OPT_ACCURACY)
379
* This gives best accuracy but is not very fast.
381
# define MAD_F_MLA(hi, lo, x, y) \
382
({ mad_fixed64hi_t __hi; \
383
mad_fixed64lo_t __lo; \
384
MAD_F_MLX(__hi, __lo, (x), (y)); \
385
asm ("addc %0,%2,%3\n\t" \
387
: "=r" (lo), "=r" (hi) \
388
: "%r" (lo), "r" (__lo), \
389
"%r" (hi), "r" (__hi) \
394
# if defined(OPT_ACCURACY)
396
* This is slower than the truncating version below it.
398
# define mad_f_scale64(hi, lo) \
399
({ mad_fixed_t __result, __round; \
400
asm ("rotrwi %0,%1,%2" \
402
: "r" (lo), "i" (MAD_F_SCALEBITS)); \
403
asm ("extrwi %0,%1,1,0" \
406
asm ("insrwi %0,%1,%2,0" \
408
: "r" (hi), "i" (MAD_F_SCALEBITS)); \
409
asm ("add %0,%1,%2" \
411
: "%r" (__result), "r" (__round)); \
415
# define mad_f_scale64(hi, lo) \
416
({ mad_fixed_t __result; \
417
asm ("rotrwi %0,%1,%2" \
419
: "r" (lo), "i" (MAD_F_SCALEBITS)); \
420
asm ("insrwi %0,%1,%2,0" \
422
: "r" (hi), "i" (MAD_F_SCALEBITS)); \
427
# define MAD_F_SCALEBITS MAD_F_FRACBITS
429
/* --- Default ------------------------------------------------------------- */
431
# elif defined(FPM_DEFAULT)
434
* This version is the most portable but it loses significant accuracy.
435
* Furthermore, accuracy is biased against the second argument, so care
436
* should be taken when ordering operands.
438
* The scale factors are constant as this is not used with SSO.
440
* Pre-rounding is required to stay within the limits of compliance.
442
# if defined(OPT_SPEED)
443
# define mad_f_mul(x, y) (((x) >> 12) * ((y) >> 16))
445
# define mad_f_mul(x, y) ((((x) + (1L << 11)) >> 12) * \
446
(((y) + (1L << 15)) >> 16))
449
/* ------------------------------------------------------------------------- */
452
# error "no FPM selected"
455
/* default implementations */
457
# if !defined(mad_f_mul)
458
# define mad_f_mul(x, y) \
459
({ register mad_fixed64hi_t __hi; \
460
register mad_fixed64lo_t __lo; \
461
MAD_F_MLX(__hi, __lo, (x), (y)); \
462
mad_f_scale64(__hi, __lo); \
466
# if !defined(MAD_F_MLA)
467
# define MAD_F_ML0(hi, lo, x, y) ((lo) = mad_f_mul((x), (y)))
468
# define MAD_F_MLA(hi, lo, x, y) ((lo) += mad_f_mul((x), (y)))
469
# define MAD_F_MLN(hi, lo) ((lo) = -(lo))
470
# define MAD_F_MLZ(hi, lo) ((void) (hi), (mad_fixed_t) (lo))
473
# if !defined(MAD_F_ML0)
474
# define MAD_F_ML0(hi, lo, x, y) MAD_F_MLX((hi), (lo), (x), (y))
477
# if !defined(MAD_F_MLN)
478
# define MAD_F_MLN(hi, lo) ((hi) = ((lo) = -(lo)) ? ~(hi) : -(hi))
481
# if !defined(MAD_F_MLZ)
482
# define MAD_F_MLZ(hi, lo) mad_f_scale64((hi), (lo))
485
# if !defined(mad_f_scale64)
486
# if defined(OPT_ACCURACY)
487
# define mad_f_scale64(hi, lo) \
489
(((hi) << (32 - (MAD_F_SCALEBITS - 1))) | \
490
((lo) >> (MAD_F_SCALEBITS - 1)))) + 1) >> 1)
492
# define mad_f_scale64(hi, lo) \
494
(((hi) << (32 - MAD_F_SCALEBITS)) | \
495
((lo) >> MAD_F_SCALEBITS)))
497
# define MAD_F_SCALEBITS MAD_F_FRACBITS
502
mad_fixed_t mad_f_abs(mad_fixed_t);
503
mad_fixed_t mad_f_div(mad_fixed_t, mad_fixed_t);