fixpt.h

Go to the documentation of this file.
00001 /*                         F I X P T . H
00002  * BRL-CAD
00003  *
00004  * Copyright (c) 2004-2006 United States Government as represented by
00005  * the U.S. Army Research Laboratory.
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License
00009  * as published by the Free Software Foundation; either version 2 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Library General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this file; see the file named COPYING for more
00019  * information.
00020  */
00021 
00022 /** \addtogroup librt */
00023 /*@{*/
00024 /** @file fixpt.h
00025  *  Data structures and macros for dealing with integer-based
00026  *  fixed point math.
00027  *
00028  *  These macros use integer instructions for
00029  *  a special "fixed point" format.
00030  *  The fractional part is stored in 28-bit integer form,
00031  *  to prevent roundoff errors.
00032  *
00033  *  Author -
00034  *      Michael John Muuss
00035  *
00036  *  Source -
00037  *      SECAD/VLD Computing Consortium, Bldg 394
00038  *      The U. S. Army Ballistic Research Laboratory
00039  *      Aberdeen Proving Ground, Maryland  21005
00040  *
00041  *
00042  *  $Header: /cvsroot/brlcad/brlcad/src/librt/fixpt.h,v 14.9 2006/01/18 06:46:17 brlcad Exp $
00043  */
00044 /*@}*/
00045 
00046 #ifndef FIXPT_H
00047 #define FIXPT_H seen
00048 
00049 #define FIXPT_SCALE     ((1<<28)-1)
00050 struct fixpt  {
00051         int     i;
00052         int     f;              /* Ranges 0 to FIXPT_SCALE-1 */
00053 };
00054 
00055 #define FIXPT_FLOAT( fp, fl )   { \
00056         register double d = fl; \
00057         fp.f = (d - (fp.i = (int)d)) * FIXPT_SCALE; \
00058         FIXPT_NORMALIZE(fp); }
00059 
00060 #define FLOAT_FIXPT( fp )  ( fp.i + ((double)fp.f)/FIXPT_SCALE )
00061 
00062 #define FIXPT_NORMALIZE(fp)     { \
00063         if( fp.f < 0 )  { \
00064                 do {  \
00065                         fp.i--; \
00066                         fp.f += FIXPT_SCALE; \
00067                 } while( fp.f < 0 ); \
00068         } else if( fp.f >= FIXPT_SCALE )  { \
00069                 do { \
00070                         fp.i++; \
00071                         fp.f -= FIXPT_SCALE; \
00072                 } while( fp.f >= FIXPT_SCALE ); \
00073         } }
00074 
00075 #define FIXPT_ROUND(fp)         { \
00076         if( fp.f > FIXPT_SCALE/2 )  { \
00077                 if( fp.i >= 0 ) fp.i++; \
00078                 else fp.i--; \
00079         }  fp.f = 0; }
00080 
00081 #define FIXPT_ADD2( o, a, b )   {\
00082         o.i = a.i + b.i; \
00083         o.f = a.f + b.f; \
00084         FIXPT_NORMALIZE(o); }
00085 
00086 #define PR_FIX( str, fp )       bu_log("%s = %2d+x%8.8x\n", str, fp.i, fp.f )
00087 
00088 #define PR_FIX2( str, fp )      bu_log("%s = (%2d+x%8.8x,%2d+x%8.8x)\n", \
00089                                 str, fp[0].i, fp[0].f, fp[1].i, fp[1].f )
00090 
00091 #endif /* FIXPT_H */
00092 
00093 /*
00094  * Local Variables:
00095  * mode: C
00096  * tab-width: 8
00097  * c-basic-offset: 4
00098  * indent-tabs-mode: t
00099  * End:
00100  * ex: shiftwidth=4 tabstop=8
00101  */

Generated on Mon Sep 18 01:24:49 2006 for BRL-CAD by  doxygen 1.4.6