fixedmath.h 12 KB

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