1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
/*!The Treasure Box Library
*
* TBox is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* TBox is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with TBox;
* If not, see <a href="http://www.gnu.org/licenses/"> http://www.gnu.org/licenses/</a>
*
* Copyright (C) 2009 - 2015, ruki All rights reserved.
*
* @author ruki
* @file fixed6.h
* @ingroup math
*
*/
#ifndef TB_MATH_FIXED6_H
#define TB_MATH_FIXED6_H
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
#include "fixed16.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* extern
*/
__tb_extern_c_enter__
/* //////////////////////////////////////////////////////////////////////////////////////
* macros
*/
// constant
#define TB_FIXED6_ONE (64)
#define TB_FIXED6_HALF (32)
#define TB_FIXED6_MAX (TB_MAXS32)
#define TB_FIXED6_MIN (TB_MINS32)
#define TB_FIXED6_NAN ((tb_int_t)0x80000000)
#define TB_FIXED6_INF (TB_MAXS32)
#define TB_FIXED6_PI (0xc9)
#define TB_FIXED6_SQRT2 (0x5a)
#define TB_FIXED6_NEAR0 (0)
// conversion
#ifdef TB_CONFIG_TYPE_HAVE_FLOAT
# ifndef tb_fixed6_to_float
# define tb_fixed6_to_float(x) (((x) * 0.015625))
# endif
# ifndef tb_float_to_fixed6
# define tb_float_to_fixed6(x) ((tb_fixed6_t)((x) * TB_FIXED6_ONE))
# endif
#endif
#ifdef __tb_debug__
# define tb_int_to_fixed6(x) tb_long_to_fixed6_check(x)
# define tb_fixed6_to_int(x) tb_fixed6_to_long_check(x)
# define tb_long_to_fixed6(x) tb_long_to_fixed6_check(x)
# define tb_fixed6_to_long(x) tb_fixed6_to_long_check(x)
#else
# define tb_int_to_fixed6(x) (tb_fixed6_t)((x) << 6)
# define tb_fixed6_to_int(x) (tb_int_t)((x) >> 6)
# define tb_long_to_fixed6(x) (tb_fixed6_t)((x) << 6)
# define tb_fixed6_to_long(x) (tb_long_t)((x) >> 6)
#endif
#define tb_fixed6_to_fixed16(x) ((x) << 10)
#define tb_fixed16_to_fixed6(x) ((x) >> 10)
// round
#define tb_fixed6_round(x) (((x) + TB_FIXED6_HALF) >> 6)
// ceil
#define tb_fixed6_ceil(x) (((x) + TB_FIXED6_ONE - 1) >> 6)
// floor
#define tb_fixed6_floor(x) ((x) >> 6)
// abs
#define tb_fixed6_abs(x) tb_abs(x)
// avg
#define tb_fixed6_avg(x, y) (((x) + (y)) >> 1)
// nearly equal?
#define tb_fixed6_near_eq(x, y) (tb_fixed6_abs((x) - (y)) <= TB_FIXED6_NEAR0)
// mul
#ifndef tb_fixed6_mul
# define tb_fixed6_mul(x, y) tb_fixed6_mul_inline(x, y)
#endif
// div
#ifndef tb_fixed6_div
# define tb_fixed6_div(x, y) tb_fixed6_div_inline(x, y)
#endif
// imul
#ifndef tb_fixed6_imul
# define tb_fixed6_imul(x, y) tb_fixed16_imul(x, y)
#endif
// idiv
#ifndef tb_fixed6_idiv
# define tb_fixed6_idiv(x, y) tb_fixed16_idiv(x, y)
#endif
// imuldiv
#ifndef tb_fixed6_imuldiv
# define tb_fixed6_imuldiv(x, y, z) tb_fixed16_imuldiv(x, y, z)
#endif
// imulsub
#ifndef tb_fixed6_imulsub
# define tb_fixed6_imulsub(x, y, z) tb_fixed16_imulsub(x, y, z)
#endif
// lsh
#ifndef tb_fixed6_lsh
# define tb_fixed6_lsh(x, y) tb_fixed16_lsh(x, y)
#endif
// rsh
#ifndef tb_fixed6_rsh
# define tb_fixed6_rsh(x, y) tb_fixed16_rsh(x, y)
#endif
/* //////////////////////////////////////////////////////////////////////////////////////
* inlines
*/
#ifdef __tb_debug__
static __tb_inline__ tb_fixed6_t tb_long_to_fixed6_check(tb_long_t x)
{
// check overflow
tb_assert(x >= (TB_MINS32 >> 10) && x <= (TB_MAXS32 >> 10));
// ok
return (tb_fixed6_t)(x << 6);
}
static __tb_inline__ tb_long_t tb_fixed6_to_int_check(tb_fixed6_t x)
{
// check overflow
tb_assert(x >= TB_FIXED6_MIN && x <= TB_FIXED6_MAX);
// ok
return (tb_fixed6_t)(x >> 6);
}
#endif
static __tb_inline__ tb_fixed6_t tb_fixed6_mul_inline(tb_fixed6_t x, tb_fixed6_t y)
{
// done
tb_hong_t v = (((tb_hong_t)x * y) >> 6);
// check overflow
tb_assert(v == (tb_int32_t)v);
// ok
return (tb_fixed16_t)v;
}
static __tb_inline__ tb_fixed16_t tb_fixed6_div_inline(tb_fixed6_t x, tb_fixed6_t y)
{
// check
tb_assert(y);
// no overflow? compute it fastly
if (x == (tb_int16_t)x) return (x << 16) / y;
// done
return tb_fixed16_div(x, y);
}
/* //////////////////////////////////////////////////////////////////////////////////////
* extern
*/
__tb_extern_c_leave__
#endif
|