Line No. | Rev | Author | Line |
---|---|---|---|
1 | 6 | kaklik | /*! \file fixedpt.c \brief Fixed-point math function library. */ |
2 | //***************************************************************************** |
||
3 | // |
||
4 | // File Name : 'fixedpt.c' |
||
5 | // Title : Fixed-point math function library |
||
6 | // Author : Pascal Stang - Copyright (C) 2003 |
||
7 | // Created : 2003.01.26 |
||
8 | // Revised : 2003.02.02 |
||
9 | // Version : 0.1 |
||
10 | // Target MCU : Atmel AVR Series |
||
11 | // Editor Tabs : 4 |
||
12 | // |
||
13 | // NOTE: This code is currently below version 1.0, and therefore is considered |
||
14 | // to be lacking in some functionality or documentation, or may not be fully |
||
15 | // tested. Nonetheless, you can expect most functions to work. |
||
16 | // |
||
17 | // This code is distributed under the GNU Public License |
||
18 | // which can be found at http://www.gnu.org/licenses/gpl.txt |
||
19 | // |
||
20 | //***************************************************************************** |
||
21 | |||
22 | |||
23 | #include "fixedpt.h" |
||
24 | |||
25 | // Program ROM constants |
||
26 | |||
27 | // Global variables |
||
28 | u08 FixedPtBits; |
||
29 | |||
30 | // Functions |
||
31 | |||
32 | // fixedptInit() initializes fixed-point math function library |
||
33 | void fixedptInit(u08 fixedPtBits) |
||
34 | { |
||
35 | // set the number of bits to use behind the point |
||
36 | FixedPtBits = fixedPtBits; |
||
37 | } |
||
38 | |||
39 | s32 fixedptConvertFromInt(s32 int_number) |
||
40 | { |
||
41 | // convert integer to fixed-point number |
||
42 | return (int_number<<FixedPtBits); |
||
43 | } |
||
44 | |||
45 | s32 fixedptConvertToInt(s32 fp_number) |
||
46 | { |
||
47 | // convert fixed-point number to integer |
||
48 | // do rounding |
||
49 | if( fp_number & 1<<(FixedPtBits-1) ) |
||
50 | { |
||
51 | // bit behind the point was a '1' |
||
52 | // round up to next higher integer |
||
53 | return (fp_number>>FixedPtBits)+1; |
||
54 | } |
||
55 | else |
||
56 | { |
||
57 | // bit behind the point was a '0' |
||
58 | // round down (truncate) to next lower integer |
||
59 | return (fp_number>>FixedPtBits); |
||
60 | } |
||
61 | } |
||
62 | |||
63 | s32 fixedptAdd(s32 a, s32 b) |
||
64 | { |
||
65 | // add a and b (a+b) with fixed-point math |
||
66 | return a+b; |
||
67 | } |
||
68 | |||
69 | s32 fixedptSubtract(s32 a, s32 b) |
||
70 | { |
||
71 | // subtract a and b (a-b) with fixed-point math |
||
72 | return a-b; |
||
73 | } |
||
74 | |||
75 | s32 fixedptMultiply(s32 a, s32 b) |
||
76 | { |
||
77 | // multiply a and b (a*b) with fixed-point math |
||
78 | return (a*b)>>FixedPtBits; |
||
79 | } |
||
80 | |||
81 | s32 fixedptDivide(s32 numer, s32 denom) |
||
82 | { |
||
83 | // divide numer by denom (numer/denom) with fixed-point math |
||
84 | return (numer<<FixedPtBits)/denom; |
||
85 | } |
Powered by WebSVN v2.8.3