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