Main Page   Data Structures   File List   Data Fields   Globals  

/fixedpt.c

Go to the documentation of this file.
00001 /*! \file fixedpt.c \brief Fixed-point math function library. */
00002 //*****************************************************************************
00003 //
00004 // File Name    : 'fixedpt.c'
00005 // Title        : Fixed-point math function library
00006 // Author       : Pascal Stang - Copyright (C) 2003
00007 // Created      : 2003.01.26
00008 // Revised      : 2003.02.02
00009 // Version      : 0.1
00010 // Target MCU   : Atmel AVR Series
00011 // Editor Tabs  : 4
00012 //
00013 // NOTE: This code is currently below version 1.0, and therefore is considered
00014 // to be lacking in some functionality or documentation, or may not be fully
00015 // tested.  Nonetheless, you can expect most functions to work.
00016 //
00017 // This code is distributed under the GNU Public License
00018 //      which can be found at http://www.gnu.org/licenses/gpl.txt
00019 //
00020 //*****************************************************************************
00021 
00022 
00023 #include "fixedpt.h"
00024 
00025 // Program ROM constants
00026 
00027 // Global variables
00028 u08 FixedPtBits;
00029 
00030 // Functions
00031 
00032 // fixedptInit() initializes fixed-point math function library
00033 void fixedptInit(u08 fixedPtBits)
00034 {
00035     // set the number of bits to use behind the point
00036     FixedPtBits = fixedPtBits;
00037 }
00038 
00039 s32 fixedptConvertFromInt(s32 int_number)
00040 {
00041     // convert integer to fixed-point number
00042     return (int_number<<FixedPtBits);
00043 }
00044 
00045 s32 fixedptConvertToInt(s32 fp_number)
00046 {
00047     // convert fixed-point number to integer
00048     // do rounding
00049     if( fp_number & 1<<(FixedPtBits-1) )
00050     {
00051         // bit behind the point was a '1'
00052         // round up to next higher integer
00053         return (fp_number>>FixedPtBits)+1;
00054     }
00055     else
00056     {
00057         // bit behind the point was a '0'
00058         // round down (truncate) to next lower integer
00059         return (fp_number>>FixedPtBits);
00060     }
00061 }
00062 
00063 s32 fixedptAdd(s32 a, s32 b)
00064 {
00065     // add a and b (a+b) with fixed-point math
00066     return a+b;
00067 }
00068 
00069 s32 fixedptSubtract(s32 a, s32 b)
00070 {
00071     // subtract a and b (a-b) with fixed-point math
00072     return a-b;
00073 }
00074 
00075 s32 fixedptMultiply(s32 a, s32 b)
00076 {
00077     // multiply a and b (a*b) with fixed-point math
00078     return (a*b)>>FixedPtBits;
00079 }
00080 
00081 s32 fixedptDivide(s32 numer, s32 denom)
00082 {
00083     // divide numer by denom (numer/denom) with fixed-point math
00084     return (numer<<FixedPtBits)/denom;
00085 }

Generated on Fri Aug 1 10:42:41 2003 for Procyon AVRlib by doxygen1.2.18