fixedmath.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /****************************************************************************
  2. * include/fixedmath.h
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. #ifndef __INCLUDE_FIXEDMATH_H
  21. #define __INCLUDE_FIXEDMATH_H
  22. /****************************************************************************
  23. * Included Files
  24. ****************************************************************************/
  25. #include <nuttx/compiler.h>
  26. #include <stdint.h>
  27. /****************************************************************************
  28. * Pre-processor Definitions
  29. ****************************************************************************/
  30. /* Common numbers */
  31. #define b8HUNDRED 0x6400 /* 100 */
  32. #define b8TEN 0x0a00 /* 10 */
  33. #define b8ONE 0x0100 /* 1 */
  34. #define b8HALF 0x0080 /* 0.5 */
  35. #define b8ONETENTH 0x001a /* 0.1 (actually 0.1015625) */
  36. #define b8ONEHUNDRTH 0x0003 /* 0.01 (actually 0.0117198765) */
  37. #define b8HALFPI 0x0192 /* 1.5703125 */
  38. #define b8PI 0x0324 /* 3.1406250 */
  39. #define b8TWOPI 0x0648 /* 6.2812500 */
  40. #define b8MAX 0x7fff /* Max value of b8_t */
  41. #define ub8MAX 0xffff /* Max value of rb8_t */
  42. #define b8MIN 0x8000 /* Min value of b8_t */
  43. #define ub8MIN 0x0000 /* Min value of ub8_t */
  44. #define b16THOUSAND 0x03e80000 /* 1000 */
  45. #define b16HUNDRED 0x00640000 /* 100 */
  46. #define b16TEN 0x000a0000 /* 10 */
  47. #define b16ONE 0x00010000 /* 1 */
  48. #define b16HALF 0x00008000 /* 0.5 */
  49. #define b16ONETENTH 0x0000199a /* 0.1 (actually 0.100006..) */
  50. #define b16ONEHUNDRTH 0x0000028f /* 0.01 (actually 0.0099945..) */
  51. #define b16ONETHOUSTH 0x00000042 /* 0.001 (actually 0.000100708..)*/
  52. #define b16HALFPI 0x0001921f /* 1.57078552246 */
  53. #define b16PI 0x0003243f /* 3.14158630371 */
  54. #define b16TWOPI 0x0006487b /* 6.28312683105 */
  55. #define b16MAX 0x7fffffff /* Max value of b16_t */
  56. #define ub16MAX 0xffffffff /* Max value of ub16_t */
  57. #define b16MIN 0x80000000 /* Min value of b16_t */
  58. #define ub16MIN 0x00000000 /* Min value of ub16_t */
  59. #define b32MILLION 0x000f424000000000LL /* 1000000 */
  60. #define b32THOUSAND 0x000003e800000000LL /* 1000 */
  61. #define b32HUNDRED 0x0000006400000000LL /* 100 */
  62. #define b32TEN 0x0000000a00000000LL /* 10 */
  63. #define b32ONE 0x0000000100000000LL /* 1 */
  64. #define b32HALF 0x0000000080000000LL /* 0.5 */
  65. #define b32ONETENTH 0x000000001999999aLL /* 0.1 */
  66. #define b32ONEHUNDRTH 0x00000000028f5c29LL /* 0.01 */
  67. #define b32ONETHOUSTH 0x0000000000418937LL /* 0.001 */
  68. #define b32ONETENTHOU 0x0000000000068db9LL /* 0.0001 */
  69. #define b32HALFPI 0x00000001921eb9ffLL /* 1.57078134990 */
  70. #define b32PI 0x00000003243f6b4fLL /* 3.14159269980 */
  71. #define b32TWOPI 0x00000006487ae7fdLL /* 6.28312539984 */
  72. #define b32MAX 0x7fffffffffffffffLL /* Max value of b32_t */
  73. #define ub32MAX 0xffffffffffffffffLL /* Max value of ub32_t */
  74. #define b32MIN 0x8000000000000000LL /* Min value of b32_t */
  75. #define ub32MIN 0x0000000000000000LL /* Min value of ub32_t */
  76. /* Conversions between b32, b16, and b8 *************************************/
  77. #define b8tob16(b) (((b16_t)(b)) << 8)
  78. #define ub8toub16(b) (((ub16_t)(b)) << 8)
  79. #define b16tob8(b) (b8_t)(((b)+0x0080)>>8)
  80. #define ub16toub8(b) (ub8_t)(((b)+0x0080)>>8)
  81. #ifdef CONFIG_HAVE_LONG_LONG
  82. # define b8tob32(b) (((b32_t)(b)) << 24)
  83. # define ub8toub32(b) (((ub32_t)(b)) << 24)
  84. # define b16tob32(b) (((b32_t)(b)) << 16)
  85. # define ub16toub32(b) (((ub32_t)(b)) << 16)
  86. # define b32tob16(b) (b16_t)(((b) + 0x0000000000008000)>>16)
  87. # define ub32toub16(b) (ub16_t)(((b) + 0x0000000000008000)>>16)
  88. # define b32tob8(b) (b8_t)(((b) + 0x0000000000000080)>>8)
  89. #endif
  90. /* 16-bit values with 8 bits of precision ***********************************/
  91. /* Conversions */
  92. #define b8toi(a) ((a) >> 8) /* Conversion to integer */
  93. #define ub8toi(a) ((a) >> 8) /* Conversion to unsigned integer */
  94. #define itob8(i) (((b8_t)(i)) << 8) /* Conversion from integer */
  95. #define uitoub8(i) (((ub8_t)(i)) << 8) /* Conversion from unsigned integer */
  96. #define b8tof(b) (((float)(b))/256.0f) /* Conversion to float */
  97. #define ftob8(f) (b8_t)(((f)*256.0f)) /* Conversion from float */
  98. #define b8trunc(a) ((a) & 0xff00) /* Truncate to integer b8 */
  99. #define b8round(a) (((a)+0x0080) & 0xff00) /* Round to integer b8 */
  100. #define b8frac(a) ((a) & 0x00ff) /* Take fractional part */
  101. /* Operators */
  102. #define ub8inv(b) (0x8000/((b)>>1)) /* Inversion (b8=b15/b7) */
  103. #define b8inv(b) (0x4000/((b)>>2)) /* Inversion (b8=b14/b6) */
  104. #define b8addb8(a,b) ((a)+(b)) /* Addition */
  105. #define b8addi(a,i) ((a)+itob8(i)) /* Add integer from b16 */
  106. #define b8subb8(a,b) ((a)-(b)) /* Subtraction */
  107. #define b8subi(a,i) ((a)-itob8(i)) /* Subtract integer from b8 */
  108. #define b8mulb8(a,b) (b16tob8((b16_t)(a)*(b16_t)(b)) /* Muliplication */
  109. #define ub8mulub8(a,b) (ub16toub8((ub16_t)(a)*(ub16_t)(b)) /* Muliplication */
  110. #define b8muli(a,i) ((a)*(i)) /* Simple multiplication by integer */
  111. #define b8sqr(a) b8mulb8(a,a) /* Square */
  112. #define ub8sqr(a) ub8mulub8(a,a) /* Square */
  113. #define b8divb8(a,b) (b8tob16(a)/(b16_t)(b)) /* Division */
  114. #define ub8divub8(a,b) (ub8toub16(a)/(ub16_t)(b)) /* Division */
  115. #define b8divi(a,i) ((a)/(i)) /* Simple division by integer */
  116. #define b8idiv(i,j) (((i)<<8)/j) /* Division of integer, b8 result */
  117. /* 32-bit values with 16 bits of precision **********************************/
  118. /* Conversions */
  119. #define b16toi(a) ((a) >> 16) /* Conversion to integer */
  120. #define ub16toi(a) ((a) >> 16) /* Conversion to unsgined integer */
  121. #define itob16(i) (((b16_t)(i)) << 16) /* Conversion from integer */
  122. #define uitoub16(i) (((ub16_t)(i)) << 16) /* Conversion from unsigned integer */
  123. #define b16tof(b) (((float)(b))/65536.0f) /* Conversion to float */
  124. #define ftob16(f) (b16_t)(((f)*65536.0f)) /* Conversion from float */
  125. #define b16tod(b) (((double)(b))/65536.0) /* Conversion to double */
  126. #define dtob16(f) (b16_t)(((f)*65536.0)) /* Conversion from double */
  127. #define b16trunc(a) ((a) & 0xffff0000) /* Truncate to integer */
  128. #define b16round(a) (((a)+0x00008000) & 0xffff0000)
  129. #define b16frac(a) ((a) & 0x0000ffff) /* Take fractional part */
  130. /* Operators */
  131. #define ub16inv(b) (0x80000000/((b)>>1)) /* Inversion (b16=b31/b15) */
  132. #define b16inv(b) (0x40000000/((b)>>2)) /* Inversion (b16=b30/b14) */
  133. #define b16addb16(a,b) ((a)+(b)) /* Addition */
  134. #define b16addi(a,i) ((a)+itob16(i)) /* Add integer to b16 */
  135. #define b16subb16(a,b) ((a)-(b)) /* Subtraction */
  136. #define b16subi(a,i) ((a)-itob16(i)) /* Subtract integer from b16 */
  137. #define b16muli(a,i) ((a)*(i)) /* Simple multiplication by integer */
  138. #define b16divi(a,i) ((a)/(i)) /* Simple division by integer*/
  139. #define b16idiv(i,j) (((i)<<16)/j) /* Division of integer, b16 result */
  140. #ifdef CONFIG_HAVE_LONG_LONG
  141. /* Multiplication operators */
  142. # define b16mulb16(a,b) b32tob16((b32_t)(a)*(b32_t)(b))
  143. # define ub16mulub16(a,b) ub32toub16((ub32_t)(a)*(ub32_t)(b))
  144. /* Square operators */
  145. # define b16sqr(a) b16mulb16(a,a)
  146. # define ub16sqr(a) ub16mulub16(a,a)
  147. /* Division operators */
  148. # define b16divb16(a,b) (b16_t)(b16tob32(a)/(b32_t)(b))
  149. # define ub16divub16(a,b) (ub16_t)(ub16toub32(a)/(ub32_t)(b))
  150. /* Square root operators */
  151. # define ub16sqrtub16(a) ub32sqrtub16(ub16toub32(a))
  152. #else
  153. # define ub16sqrtub16(a) ub8toub16(ub16sqrtub8(a))
  154. #endif
  155. /* 64-bit values with 32 bits of precision **********************************/
  156. #ifdef CONFIG_HAVE_LONG_LONG
  157. /* Conversions */
  158. #define b32toi(a) ((a) >> 32) /* Conversion to integer */
  159. #define itob32(i) (((b32_t)(i)) << 32) /* Conversion from integer */
  160. #define uitoub32(i) (((ub32_t)(i)) << 32) /* Conversion from unsigned integer */
  161. #define b32tod(b) (((double)(b))/b32ONE) /* Conversion to double */
  162. #define dtob32(f) (b32_t)(((f)*(double)b32ONE)) /* Conversion from double */
  163. #define b32trunc(a) ((a) & 0xffffffff00000000) /* Truncate to integer */
  164. #define b32round(a) (((a)+0x0000000080000000) & 0xffffffff00000000)
  165. #define b32frac(a) ((a) & 0x00000000ffffffff) /* Take fractional part */
  166. #endif
  167. /****************************************************************************
  168. * Public Types
  169. ****************************************************************************/
  170. typedef int16_t b8_t;
  171. typedef uint16_t ub8_t;
  172. typedef int32_t b16_t;
  173. typedef uint32_t ub16_t;
  174. #ifdef CONFIG_HAVE_LONG_LONG
  175. typedef int64_t b32_t;
  176. typedef uint64_t ub32_t;
  177. #endif
  178. /****************************************************************************
  179. * Public Function Prototypes
  180. ****************************************************************************/
  181. #undef EXTERN
  182. #if defined(__cplusplus)
  183. #define EXTERN extern "C"
  184. extern "C"
  185. {
  186. #else
  187. #define EXTERN extern
  188. #endif
  189. #ifndef CONFIG_HAVE_LONG_LONG
  190. /* Multiplication operators */
  191. b16_t b16mulb16(b16_t m1, b16_t m2);
  192. ub16_t ub16mulub16(ub16_t m1, ub16_t m2);
  193. /* Square operators */
  194. b16_t b16sqr(b16_t a);
  195. ub16_t ub16sqr(ub16_t a);
  196. /* Division operators */
  197. b16_t b16divb16(b16_t num, b16_t denom);
  198. ub16_t ub16divub16(ub16_t num, ub16_t denom);
  199. #endif
  200. /* Trigonometric Functions */
  201. b16_t b16sin(b16_t rad);
  202. b16_t b16cos(b16_t rad);
  203. b16_t b16atan2(b16_t y, b16_t x);
  204. /* Square root operators */
  205. #ifdef CONFIG_HAVE_LONG_LONG
  206. ub16_t ub32sqrtub16(ub32_t a);
  207. #endif
  208. ub8_t ub16sqrtub8(ub16_t a);
  209. #undef EXTERN
  210. #if defined(__cplusplus)
  211. }
  212. #endif
  213. #endif /* __INCLUDE_FIXEDMATH_H */