fixed.c

00001 /*
00002  * libmad - MPEG audio decoder library
00003  * Copyright (C) 2000-2003 Underbit Technologies, Inc.
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018  *
00019  * $Id: fixed.c,v 1.1 2003/08/31 18:59:46 gabest Exp $
00020  */
00021 
00022 # ifdef HAVE_CONFIG_H
00023 #  include "config.h"
00024 # endif
00025 
00026 # include "global.h"
00027 
00028 # include "fixed.h"
00029 
00030 /*
00031  * NAME:        fixed->abs()
00032  * DESCRIPTION: return absolute value of a fixed-point number
00033  */
00034 mad_fixed_t mad_f_abs(mad_fixed_t x)
00035 {
00036   return x < 0 ? -x : x;
00037 }
00038 
00039 /*
00040  * NAME:        fixed->div()
00041  * DESCRIPTION: perform division using fixed-point math
00042  */
00043 mad_fixed_t mad_f_div(mad_fixed_t x, mad_fixed_t y)
00044 {
00045   mad_fixed_t q, r;
00046   unsigned int bits;
00047 
00048   q = mad_f_abs(x / y);
00049 
00050   if (x < 0) {
00051     x = -x;
00052     y = -y;
00053   }
00054 
00055   r = x % y;
00056 
00057   if (y < 0) {
00058     x = -x;
00059     y = -y;
00060   }
00061 
00062   if (q > mad_f_intpart(MAD_F_MAX) &&
00063       !(q == -mad_f_intpart(MAD_F_MIN) && r == 0 && (x < 0) != (y < 0)))
00064     return 0;
00065 
00066   for (bits = MAD_F_FRACBITS; bits && r; --bits) {
00067     q <<= 1, r <<= 1;
00068     if (r >= y)
00069       r -= y, ++q;
00070   }
00071 
00072   /* round */
00073   if (2 * r >= y)
00074     ++q;
00075 
00076   /* fix sign */
00077   if ((x < 0) != (y < 0))
00078     q = -q;
00079 
00080   return q << bits;
00081 }

Generated on Tue Dec 13 14:47:48 2005 for guliverkli by  doxygen 1.4.5