ini
[aversive.git] / modules / base / math / fixed_point / f32.h
1 /*  
2  *  Copyright Droids Corporation, Microb Technology, Eirbot (2005)
3  * 
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  *  Revision : $Id: f32.h,v 1.6.4.3 2008-05-10 15:06:26 zer0 Exp $
19  *
20  */
21
22 /* Here is an example of what is fixed point. This is the f16 type : 
23  *
24  *   int dec   1/256 = 0.00390625 
25  *
26  *    07 01 : 7 + 0x01*0.00390625 = 7.0039625
27  *    07 80 : 7 + 0x80*0.00390625 = 7.5
28  *    07 FF : 7 + 0xFF*0.00390625 = 7.99609375
29  *    00 00 : 0
30  *    FF 00 : -1
31  *    FF FF : -1 + 0xFF*0.00390625 = -0.0039625
32  *    7F 00 : +127
33  *    7F FF : +127 + 0xFF*0.00390625 = 127.99609375
34  *    80 00 : -128
35  *
36  * For f32 the structure is composed by 2 integer of 16 bits. */
37
38 #ifndef _F32_H_
39 #define _F32_H_
40
41 #include <aversive.h>
42
43 typedef struct fixed_32 {
44         union {
45                 struct {
46                         uint16_t decimal;
47                         int16_t integer;
48                 } s;
49                 int32_t s32;
50         } u;
51 } f32;
52 #define f32_decimal u.s.decimal
53 #define f32_integer u.s.integer
54
55 #define F32_ZERO (    \
56 {                     \
57     f32 __f;          \
58     __f.u.s32 = 0;    \
59     __f;              \
60 })
61
62 #define F32_NAN (              \
63 {                              \
64     f32 __f;                   \
65     __f.u.s32 = 0xFFFFFFFF;    \
66     __f;                       \
67 })
68
69 #define F32_IS_GT(x,y) (f32_to_s32(x) >  f32_to_s32(y))
70 #define F32_IS_LT(x,y) (f32_to_s32(x) <  f32_to_s32(y))
71 #define F32_IS_GE(x,y) (f32_to_s32(x) >= f32_to_s32(y))
72 #define F32_IS_LE(x,y) (f32_to_s32(x) <= f32_to_s32(y))
73 #define F32_IS_EQ(x,y) (f32_to_s32(x) == f32_to_s32(y))
74 #define F32_IS_NE(x,y) (f32_to_s32(x) != f32_to_s32(y))
75 #define F32_IS_NEG(x)  ((x).f32_integer < 0)
76 #define F32_IS_ZERO(x)  ((x).f32_integer == 0 && (x).f32_decimal == 0)
77
78 /** convert a double to a f32 */
79 f32 f32_from_double(double f);
80
81 /** convert a f32 to a double */
82 double f32_to_double(f32 fix);
83
84 /** convert 2 integer (int16_t and uint16_t) to a f32 */
85 f32 f32_from_integer(int16_t i, uint16_t d);
86
87 /** convert msb integer (int16_t) to a f32 */
88 f32 f32_from_msb(int16_t i);
89
90 /** convert lsb integer (int16_t) to a f32 
91  *      ( -0.5 < ret < 0.5 ) 
92  */
93 f32 f32_from_lsb(int16_t i);
94
95 /** return opposite of the number (=-f) */
96 f32 f32_neg(f32 f);
97
98 /** add a with b (=a+b) */
99 f32 f32_add(f32 a, f32 b);
100
101 /** add a with b (=a-b) */
102 f32 f32_sub(f32 a, f32 b);
103
104 /** return opposite of the number (=1/f) */
105 f32 f32_inv(f32 f);
106
107 /** mul a with b (=a*b) */
108 f32 f32_mul(f32 a, f32 b);
109
110 /** mul a with b (=a*b), but return only the msb */
111 f32 f32_mul_msb(f32 a, f32 b);
112
113 /** div a with b (=a/b) */
114 f32 f32_div(f32 a, f32 b);
115
116 /** sqrt of f */
117 f32 f32_sqrt(f32 f);
118
119 /** function that display a f32 to the standard output */
120 void f32_print(f32 fix);
121
122 #endif