ini
[aversive.git] / modules / base / math / fixed_point / f64.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: f64.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 f64 the structure is composed by 2 integer of 32 bits. */
37
38 #include <aversive.h>
39
40 #ifndef _F64_H_
41 #define _F64_H_
42
43 typedef struct fixed_64 {
44         union {
45                 struct {
46                         uint32_t decimal;
47                         int32_t integer;
48                 } s;
49                 int64_t s64;
50         } u;
51 } f64;
52 #define f64_integer u.s.integer
53 #define f64_decimal u.s.decimal
54
55 #define F64_ZERO (    \
56 {                     \
57     f64 __f;          \
58     __f.u.s64 = 0;    \
59     __f;              \
60 })
61
62 #define F64_NAN (                      \
63 {                                      \
64     f64 __f;                           \
65     __f.u.s64 = 0xFFFFFFFFFFFFFFFF;    \
66     __f;                               \
67 })
68
69 #define F64_IS_GT(x,y) (f64_to_s64(x) >  f64_to_s64(y))
70 #define F64_IS_LT(x,y) (f64_to_s64(x) <  f64_to_s64(y))
71 #define F64_IS_GE(x,y) (f64_to_s64(x) >= f64_to_s64(y))
72 #define F64_IS_LE(x,y) (f64_to_s64(x) <= f64_to_s64(y))
73 #define F64_IS_EQ(x,y) (f64_to_s64(x) == f64_to_s64(y))
74 #define F64_IS_NE(x,y) (f64_to_s64(x) != f64_to_s64(y))
75 #define F64_IS_NEG(x)  ((x).f64_integer < 0)
76 #define F64_IS_ZERO(x)  ((x).f64_integer == 0 && (x).f64_decimal == 0)
77
78
79 /** convert a double to a f64 */
80 f64 f64_from_double(double f);
81
82 /** convert a f64 to a double */
83 double f64_to_double(f64 fix);
84
85 /** convert 2 integer (int32_t and uint32_t) to a f64 */
86 f64 f64_from_integer(int32_t i, uint32_t d);
87
88 /** convert msb integer (int32_t) to a f64 */
89 f64 f64_from_msb(int32_t i);
90
91 /** convert lsb integer (int32_t) to a f64 
92  *      ( -0.5 < ret < 0.5 ) 
93  */
94 f64 f64_from_lsb(int32_t i);
95
96 /** return opposite of the number (=-f) */
97 f64 f64_neg(f64 f);
98
99 /** add a with b (=a+b) */
100 f64 f64_add(f64 a, f64 b);
101
102 /** add a with b (=a-b) */
103 f64 f64_sub(f64 a, f64 b);
104
105 /** return opposite of the number (=1/f) */
106 f64 f64_inv(f64 f);
107
108 /** mul a with b (=a*b) */
109 f64 f64_mul(f64 a, f64 b);
110
111 /** mul a with b (=a*b), but return only the msb */
112 int32_t f64_msb_mul(f64 a, f64 b);
113
114 /** div a with b (=a/b) */
115 f64 f64_div(f64 a, f64 b);
116
117 /** sqrt of f */
118 f64 f64_sqrt(f64 f);
119
120 /** function that display a f64 to the standard output */
121 void f64_print(f64 fix);
122
123 #endif