Chris@0
|
1 /*
|
Chris@0
|
2 * libmad - MPEG audio decoder library
|
Chris@0
|
3 * Copyright (C) 2000-2004 Underbit Technologies, Inc.
|
Chris@0
|
4 *
|
Chris@0
|
5 * This program is free software; you can redistribute it and/or modify
|
Chris@0
|
6 * it under the terms of the GNU General Public License as published by
|
Chris@0
|
7 * the Free Software Foundation; either version 2 of the License, or
|
Chris@0
|
8 * (at your option) any later version.
|
Chris@0
|
9 *
|
Chris@0
|
10 * This program is distributed in the hope that it will be useful,
|
Chris@0
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
13 * GNU General Public License for more details.
|
Chris@0
|
14 *
|
Chris@0
|
15 * You should have received a copy of the GNU General Public License
|
Chris@0
|
16 * along with this program; if not, write to the Free Software
|
Chris@0
|
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
Chris@0
|
18 *
|
Chris@0
|
19 * $Id: fixed.c,v 1.13 2004/01/23 09:41:32 rob Exp $
|
Chris@0
|
20 */
|
Chris@0
|
21
|
Chris@0
|
22 # ifdef HAVE_CONFIG_H
|
Chris@0
|
23 # include "config.h"
|
Chris@0
|
24 # endif
|
Chris@0
|
25
|
Chris@0
|
26 # include "global.h"
|
Chris@0
|
27
|
Chris@0
|
28 # include "fixed.h"
|
Chris@0
|
29
|
Chris@0
|
30 /*
|
Chris@0
|
31 * NAME: fixed->abs()
|
Chris@0
|
32 * DESCRIPTION: return absolute value of a fixed-point number
|
Chris@0
|
33 */
|
Chris@0
|
34 mad_fixed_t mad_f_abs(mad_fixed_t x)
|
Chris@0
|
35 {
|
Chris@0
|
36 return x < 0 ? -x : x;
|
Chris@0
|
37 }
|
Chris@0
|
38
|
Chris@0
|
39 /*
|
Chris@0
|
40 * NAME: fixed->div()
|
Chris@0
|
41 * DESCRIPTION: perform division using fixed-point math
|
Chris@0
|
42 */
|
Chris@0
|
43 mad_fixed_t mad_f_div(mad_fixed_t x, mad_fixed_t y)
|
Chris@0
|
44 {
|
Chris@0
|
45 mad_fixed_t q, r;
|
Chris@0
|
46 unsigned int bits;
|
Chris@0
|
47
|
Chris@0
|
48 q = mad_f_abs(x / y);
|
Chris@0
|
49
|
Chris@0
|
50 if (x < 0) {
|
Chris@0
|
51 x = -x;
|
Chris@0
|
52 y = -y;
|
Chris@0
|
53 }
|
Chris@0
|
54
|
Chris@0
|
55 r = x % y;
|
Chris@0
|
56
|
Chris@0
|
57 if (y < 0) {
|
Chris@0
|
58 x = -x;
|
Chris@0
|
59 y = -y;
|
Chris@0
|
60 }
|
Chris@0
|
61
|
Chris@0
|
62 if (q > mad_f_intpart(MAD_F_MAX) &&
|
Chris@0
|
63 !(q == -mad_f_intpart(MAD_F_MIN) && r == 0 && (x < 0) != (y < 0)))
|
Chris@0
|
64 return 0;
|
Chris@0
|
65
|
Chris@0
|
66 for (bits = MAD_F_FRACBITS; bits && r; --bits) {
|
Chris@0
|
67 q <<= 1, r <<= 1;
|
Chris@0
|
68 if (r >= y)
|
Chris@0
|
69 r -= y, ++q;
|
Chris@0
|
70 }
|
Chris@0
|
71
|
Chris@0
|
72 /* round */
|
Chris@0
|
73 if (2 * r >= y)
|
Chris@0
|
74 ++q;
|
Chris@0
|
75
|
Chris@0
|
76 /* fix sign */
|
Chris@0
|
77 if ((x < 0) != (y < 0))
|
Chris@0
|
78 q = -q;
|
Chris@0
|
79
|
Chris@0
|
80 return q << bits;
|
Chris@0
|
81 }
|