2 * Copyright Droids Corporation, Microb Technology, Eirbot (2005)
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.
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.
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
18 * Revision : $Id: f16.h,v 1.6.4.3 2008-05-10 15:06:26 zer0 Exp $
22 /* Here is an example of what is fixed point. This is the f16 type :
24 * int dec 1/256 = 0.00390625
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
31 * FF FF : -1 + 0xFF*0.00390625 = -0.0039625
33 * 7F FF : +127 + 0xFF*0.00390625 = 127.99609375
36 * For f16 the structure is composed by 2 integer of 8 bits. */
43 typedef struct fixed_16 {
52 #define f16_decimal u.s.decimal
53 #define f16_integer u.s.integer
69 #define F16_IS_GT(x,y) (f16_to_s16(x) > f16_to_s16(y))
70 #define F16_IS_LT(x,y) (f16_to_s16(x) < f16_to_s16(y))
71 #define F16_IS_GE(x,y) (f16_to_s16(x) >= f16_to_s16(y))
72 #define F16_IS_LE(x,y) (f16_to_s16(x) <= f16_to_s16(y))
73 #define F16_IS_EQ(x,y) (f16_to_s16(x) == f16_to_s16(y))
74 #define F16_IS_NE(x,y) (f16_to_s16(x) != f16_to_s16(y))
75 #define F16_IS_NEG(x) ((x).f16_integer < 0)
76 #define F16_IS_ZERO(x) ((x).f16_integer == 0 && (x).f16_decimal == 0)
79 /** convert a double to a f16 */
80 f16 f16_from_double(double f);
82 /** convert a f16 to a double */
83 double f16_to_double(f16 fix);
85 /** convert 2 integer (int8_t and uint8_t) to a f16 */
86 f16 f16_from_integer(int8_t i, uint8_t d);
88 /** convert msb integer (int8_t) to a f16 */
89 f16 f16_from_msb(int8_t i);
91 /** convert lsb integer (int8_t) to a f16
92 * ( -0.5 < ret < 0.5 )
94 f16 f16_from_lsb(int8_t i);
96 /** return opposite of the number (=-f) */
99 /** add a with b (=a+b) */
100 f16 f16_add(f16 a, f16 b);
102 /** add a with b (=a-b) */
103 f16 f16_sub(f16 a, f16 b);
105 /** return opposite of the number (=1/f) */
108 /** mul a with b (=a*b) */
109 f16 f16_mul(f16 a, f16 b);
111 /** mul a with b (=a*b), but return only the msb */
112 f16 f16_mul_msb(f16 a, f16 b);
114 /** div a with b (=a/b) */
115 f16 f16_div(f16 a, f16 b);
120 /** function that display a f16 to the standard output */
121 void f16_print(f16 fix);