Chris@0: /* Chris@0: * libmad - MPEG audio decoder library Chris@0: * Copyright (C) 2000-2004 Underbit Technologies, Inc. Chris@0: * Chris@0: * This program is free software; you can redistribute it and/or modify Chris@0: * it under the terms of the GNU General Public License as published by Chris@0: * the Free Software Foundation; either version 2 of the License, or Chris@0: * (at your option) any later version. Chris@0: * Chris@0: * This program is distributed in the hope that it will be useful, Chris@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: * GNU General Public License for more details. Chris@0: * Chris@0: * You should have received a copy of the GNU General Public License Chris@0: * along with this program; if not, write to the Free Software Chris@0: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Chris@0: * Chris@0: * $Id: fixed.c,v 1.13 2004/01/23 09:41:32 rob Exp $ Chris@0: */ Chris@0: Chris@0: # ifdef HAVE_CONFIG_H Chris@0: # include "config.h" Chris@0: # endif Chris@0: Chris@0: # include "global.h" Chris@0: Chris@0: # include "fixed.h" Chris@0: Chris@0: /* Chris@0: * NAME: fixed->abs() Chris@0: * DESCRIPTION: return absolute value of a fixed-point number Chris@0: */ Chris@0: mad_fixed_t mad_f_abs(mad_fixed_t x) Chris@0: { Chris@0: return x < 0 ? -x : x; Chris@0: } Chris@0: Chris@0: /* Chris@0: * NAME: fixed->div() Chris@0: * DESCRIPTION: perform division using fixed-point math Chris@0: */ Chris@0: mad_fixed_t mad_f_div(mad_fixed_t x, mad_fixed_t y) Chris@0: { Chris@0: mad_fixed_t q, r; Chris@0: unsigned int bits; Chris@0: Chris@0: q = mad_f_abs(x / y); Chris@0: Chris@0: if (x < 0) { Chris@0: x = -x; Chris@0: y = -y; Chris@0: } Chris@0: Chris@0: r = x % y; Chris@0: Chris@0: if (y < 0) { Chris@0: x = -x; Chris@0: y = -y; Chris@0: } Chris@0: Chris@0: if (q > mad_f_intpart(MAD_F_MAX) && Chris@0: !(q == -mad_f_intpart(MAD_F_MIN) && r == 0 && (x < 0) != (y < 0))) Chris@0: return 0; Chris@0: Chris@0: for (bits = MAD_F_FRACBITS; bits && r; --bits) { Chris@0: q <<= 1, r <<= 1; Chris@0: if (r >= y) Chris@0: r -= y, ++q; Chris@0: } Chris@0: Chris@0: /* round */ Chris@0: if (2 * r >= y) Chris@0: ++q; Chris@0: Chris@0: /* fix sign */ Chris@0: if ((x < 0) != (y < 0)) Chris@0: q = -q; Chris@0: Chris@0: return q << bits; Chris@0: }