TizenRT Libs&Environment  v2.0 M2
fixedmath.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright 2016 Samsung Electronics All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing,
12  * software distributed under the License is distributed on an
13  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14  * either express or implied. See the License for the specific
15  * language governing permissions and limitations under the License.
16  *
17  ****************************************************************************/
18 /****************************************************************************
19  * include/fixedmath.h
20  *
21  * Copyright (C) 2008 Gregory Nutt. All rights reserved.
22  * Author: Gregory Nutt <gnutt@nuttx.org>
23  *
24  * Redistribution and use in source and binary forms, with or without
25  * modification, are permitted provided that the following conditions
26  * are met:
27  *
28  * 1. Redistributions of source code must retain the above copyright
29  * notice, this list of conditions and the following disclaimer.
30  * 2. Redistributions in binary form must reproduce the above copyright
31  * notice, this list of conditions and the following disclaimer in
32  * the documentation and/or other materials provided with the
33  * distribution.
34  * 3. Neither the name NuttX nor the names of its contributors may be
35  * used to endorse or promote products derived from this software
36  * without specific prior written permission.
37  *
38  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
39  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
40  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
41  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
42  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
43  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
44  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
45  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
46  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
47  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
48  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49  * POSSIBILITY OF SUCH DAMAGE.
50  *
51  ****************************************************************************/
59 #ifndef __INCLUDE_FIXEDMATH_H
60 #define __INCLUDE_FIXEDMATH_H
61 
62 /****************************************************************************
63  * Included Files
64  ****************************************************************************/
65 
66 #include <stdint.h>
67 
68 /**************************************************************************
69  * Pre-processor Definitions
70  **************************************************************************/
71 
72 /* Common numbers */
73 
74 #define b8HUNDRED 0x6400 /* 100 */
75 #define b8TEN 0x0a00 /* 10 */
76 #define b8ONE 0x0100 /* 1 */
77 #define b8HALF 0x0080 /* 0.5 */
78 #define b8ONETENTH 0x001a /* 0.1 (acutally 0.1015625) */
79 #define b8ONEHUNDRTH 0x0003 /* 0.01 (actualy 0.0117198765) */
80 #define b8HALFPI 0x0192 /* 1.5703125 */
81 #define b8PI 0x0324 /* 3.1406250 */
82 #define b8TWOPI 0x0648 /* 6.2812500 */
83 
84 #define b8MAX 0x7fff /* Max value of b8_t */
85 #define ub8MAX 0xffff /* Max value of rb8_t */
86 #define b8MIN 0x8000 /* Min value of b8_t */
87 #define ub8MIN 0x0000 /* Min value of ub8_t */
88 
89 #define b16THOUSAND 0x03e80000 /* 1000 */
90 #define b16HUNDRED 0x00640000 /* 100 */
91 #define b16TEN 0x000a0000 /* 10 */
92 #define b16ONE 0x00010000 /* 1 */
93 #define b16HALF 0x00008000 /* 0.5 */
94 #define b16ONETENTH 0x0000199a /* 0.1 (actually 0.100006..) */
95 #define b16ONEHUNDRTH 0x0000028f /* 0.01 (actually 0.0099945..) */
96 #define b16ONETHOUSTH 0x00000042 /* 0.001 (actually 0.000100708..) */
97 #define b16HALFPI 0x0001921f /* 1.57078552246 */
98 #define b16PI 0x0003243f /* 3.14158630371 */
99 #define b16TWOPI 0x0006487b /* 6.28312683105 */
100 
101 #define b16MAX 0x7fffffff /* Max value of b16_t */
102 #define ub16MAX 0xffffffff /* Max value of ub16_t */
103 #define b16MIN 0x80000000 /* Min value of b16_t */
104 #define ub16MIN 0x00000000 /* Min value of ub16_t */
105 
106 #define b32MILLION 0x000f424000000000 /* 1000000 */
107 #define b32THOUSAND 0x000003e800000000 /* 1000 */
108 #define b32HUNDRED 0x0000006400000000 /* 100 */
109 #define b32TEN 0x0000000a00000000 /* 10 */
110 #define b32ONE 0x0000000100000000 /* 1 */
111 #define b32HALF 0x0000000080000000 /* 0.5 */
112 #define b32ONETENTH 0x000000001999999a /* 0.1 */
113 #define b32ONEHUNDRTH 0x00000000028f5c29 /* 0.01 */
114 #define b32ONETHOUSTH 0x0000000000418937 /* 0.001 */
115 #define b32ONETENTHOU 0x0000000000068db9 /* 0.0001 */
116 #define b32HALFPI 0x00000001921eb9ff /* 1.57078134990 */
117 #define b32PI 0x00000003243f6b4f /* 3.14159269980 */
118 #define b32TWOPI 0x00000006487ae7fd /* 6.28312539984 */
119 
120 #define b32MAX 0x7fffffffffffffff /* Max value of b16_t */
121 #define ub32MAX 0xffffffffffffffff /* Max value of ub16_t */
122 #define b32MIN 0x8000000000000000 /* Min value of b16_t */
123 #define ub32MIN 0x0000000000000000 /* Min value of ub16_t */
124 
125 /* Conversions between b32, b16, and b8 ***********************************/
126 
127 #define b8tob16(b) (((b16_t)(b)) << 8)
128 #define ub8toub16(b) (((ub16_t)(b)) << 8)
129 #define b16tob8(b) (b8_t)(((b)+0x0080)>>8)
130 #define ub16toub8(b) (ub8_t)(((b)+0x0080)>>8)
131 
132 #ifdef CONFIG_HAVE_LONG_LONG
133 #define b8tob32(b) (((b32_t)(b)) << 24)
134 #define ub8toub32(b) (((ub32_t)(b)) << 24)
135 #define b16tob32(b) (((b32_t)(b)) << 16)
136 #define ub16toub32(b) (((ub32_t)(b)) << 16)
137 #define b32tob16(b) (b16_t)(((b) + 0x0000000000008000)>>16)
138 #define ub32toub16(b) (ub16_t)(((b) + 0x0000000000008000)>>16)
139 #define b32tob8(b) (b8_t)(((b) + 0x0000000000000080)>>8)
140 #endif
141 
142 /* 16-bit values with 8 bits of precision *********************************/
143 
144 /* Conversions */
145 
146 #define b8toi(a) ((a) >> 8) /* Conversion to integer */
147 #define itob8(i) (((b8_t)(i)) << 8) /* Conversion from integer */
148 #define uitoub8(i) (((ub8_t)(i)) << 8) /* Conversion from unsigned integer */
149 #define b8tof(b) (((float)b)/256.0) /* Conversion to float */
150 #define ftob8(f) (b8_t)(((f)*256.0)) /* Conversion from float */
151 #define b8trunc(a) ((a) & 0xff00) /* Truncate to integer b8 */
152 #define b8round(a) (((a)+0x0080) & 0xff00) /* Round to integer b8 */
153 #define b8frac(a) ((a) & 0x00ff) /* Take fractional part */
154 
155 /* Operators */
156 
157 #define ub8inv(b) (0x8000/((b)>>1)) /* Inversion (b8=b15/b7) */
158 #define b8inv(b) (0x4000/((b)>>2)) /* Inversion (b8=b14/b6) */
159 #define b8addb8(a, b) ((a)+(b)) /* Addition */
160 #define b8addi(a, i) ((a)+itob8(i)) /* Add integer from b16 */
161 #define b8subb8(a, b) ((a)-(b)) /* Subtraction */
162 #define b8subi(a, i) ((a)-itob8(i)) /* Subtract integer from b8 */
163 #define b8mulb8(a, b) b16tob8((b16_t)(a)*(b16_t)(b) /* Muliplication */
164 #define ub8mulub8(a, b) ub16toub8((ub16_t)(a)*(ub16_t)(b) /* Muliplication */
165 #define b8muli(a, i) ((a)*(i)) /* Simple multiplication by integer */
166 #define b8sqr(a) b8mulb8(a, a) /* Square */
167 #define ub8sqr(a) ub8mulub8(a, a) /* Square */
168 #define b8divb8(a, b) b8tob16(a)/(b16_t)(b) /* Division */
169 #define ub8divub8(a, b) ub8toub16(a)/(ub16_t)(b) /* Division */
170 #define b8divi(a, i) ((a)/(i)) /* Simple division by integer */
171 #define b8idiv(i, j) (((i)<<8)/j) /* Division of integer, b8 result */
172 
173 /* 32-bit values with 16 bits of precision ********************************/
174 
175 /* Conversions */
176 
177 #define b16toi(a) ((a) >> 16) /* Conversion to integer */
178 #define itob16(i) (((b16_t)(i)) << 16) /* Conversion from integer */
179 #define uitoub16(i) (((ub16_t)(i)) << 16) /* Conversion from unsigned integer */
180 #define b16tof(b) (((float)b)/65536.0) /* Conversion to float */
181 #define ftob16(f) (b16_t)(((f)*65536.0)) /* Conversion from float */
182 #define b16trunc(a) ((a) & 0xffff0000) /* Truncate to integer */
183 #define b16round(a) (((a)+0x00008000) & 0xffff0000)
184 #define b16frac(a) ((a) & 0x0000ffff) /* Take fractional part */
185 
186 /* Operators */
187 
188 #define ub16inv(b) (0x80000000/((b)>>1)) /* Inversion (b16=b31/b15) */
189 #define b16inv(b) (0x40000000/((b)>>2)) /* Inversion (b16=b30/b14) */
190 #define b16addb16(a, b) ((a)+(b)) /* Addition */
191 #define b16addi(a, i) ((a)+itob16(i)) /* Add integer to b16 */
192 #define b16subb16(a, b) ((a)-(b)) /* Subtraction */
193 #define b16subi(a, i) ((a)-itob16(i)) /* Subtract integer from b16 */
194 #define b16muli(a, i) ((a)*(i)) /* Simple multiplication by integer */
195 #define b16divi(a, i) ((a)/(i)) /* Simple division by integer */
196 #define b16idiv(i, j) (((i)<<16)/j) /* Division of integer, b16 result */
197 
198 #ifdef CONFIG_HAVE_LONG_LONG
199 /* Multiplication operators */
200 
201 #define b16mulb16(a, b) b32tob16((b32_t)(a)*(b32_t)(b))
202 #define ub16mulub16(a, b) ub32toub16((ub32_t)(a)*(ub32_t)(b))
203 
204 /* Square operators */
205 
206 #define b16sqr(a) b16mulb16(a, a)
207 #define ub16sqr(a) ub16mulub16(a, a)
208 
209 /* Division operators */
210 
211 #define b16divb16(a, b) (b16_t)(b16tob32(a)/(b32_t)(b))
212 #define ub16divub16(a, b) (ub16_t)(ub16toub32(a)/(ub32_t)(b))
213 #endif
214 
215 /**************************************************************************
216  * Public Types
217  **************************************************************************/
218 
219 typedef int16_t b8_t;
220 typedef uint16_t ub8_t;
221 typedef int32_t b16_t;
222 typedef uint32_t ub16_t;
223 #ifdef CONFIG_HAVE_LONG_LONG
224 typedef int64_t b32_t;
225 typedef uint64_t ub32_t;
226 #endif
227 
228 /**************************************************************************
229  * Public Functions
230  **************************************************************************/
231 
232 #undef EXTERN
233 #if defined(__cplusplus)
234 #define EXTERN extern "C"
235 extern "C" {
236 #else
237 #define EXTERN extern
238 #endif
239 
240 #ifndef CONFIG_HAVE_LONG_LONG
241 /* Multiplication operators */
250 b16_t b16mulb16(b16_t m1, b16_t m2);
260 
261 /* Square operators */
269 b16_t b16sqr(b16_t a);
278 
279 /* Division operators */
288 b16_t b16divb16(b16_t num, b16_t denom);
297 ub16_t ub16divub16(ub16_t num, ub16_t denom);
298 #endif
299 
300 /* Trigonometric Functions */
308 b16_t b16sin(b16_t rad);
316 b16_t b16cos(b16_t rad);
326 b16_t b16atan2(b16_t y, b16_t x);
327 
328 #undef EXTERN
329 #if defined(__cplusplus)
330 }
331 #endif
332 
333 #endif /* __INCLUDE_FIXEDMATH_H */
334 
b16_t b16atan2(b16_t y, b16_t x)
Trigonometric tangent operation.
uint32_t ub16_t
Definition: fixedmath.h:222
#define b16divb16(a, b)
Definition: fixedmath.h:211
b16_t b16sin(b16_t rad)
Trigonometric sine operation.
int16_t b8_t
Definition: fixedmath.h:219
#define ub16sqr(a)
Definition: fixedmath.h:207
uint16_t ub8_t
Definition: fixedmath.h:220
#define b16sqr(a)
Definition: fixedmath.h:206
int64_t b32_t
Definition: fixedmath.h:224
#define b16mulb16(a, b)
Definition: fixedmath.h:201
int32_t b16_t
Definition: fixedmath.h:221
#define ub16mulub16(a, b)
Definition: fixedmath.h:202
uint64_t ub32_t
Definition: fixedmath.h:225
#define ub16divub16(a, b)
Definition: fixedmath.h:212
b16_t b16cos(b16_t rad)
Trigonometric cosine operation.