sw_math.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright (c) 2020 Actions Technology Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief Utilities of sw math API
  9. */
  10. #ifndef ZEPHYR_FRAMEWORK_INCLUDE_DISPLAY_SW_MATH_H_
  11. #define ZEPHYR_FRAMEWORK_INCLUDE_DISPLAY_SW_MATH_H_
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. /**
  15. * @defgroup display-util Display Utilities
  16. * @ingroup display_libraries
  17. * @{
  18. */
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. /* fixedpoint with 16-bit fraction */
  23. typedef int32_t fixedpoint16_t;
  24. /* convert integer to fixedpoint-1 */
  25. #define FIXEDPOINT1(x) to_fixedpoint(x, 1)
  26. #define FIXEDPOINT1_0_5 (FIXEDPOINT1(1) >> 1)
  27. /* convert pixel coordinate to fixedpoint-1 coordinate */
  28. #define PX_FIXEDPOINT1(x) (FIXEDPOINT1(x) + (FIXEDPOINT1(1) >> 1))
  29. /* convert fixedpoint-1 to integer */
  30. #define FLOOR_FIXEDPOINT1(x) floor_fixedpoint(x, 1)
  31. #define ROUND_FIXEDPOINT1(x) round_fixedpoint(x, 1)
  32. /* convert integer to fixedpoint-12 */
  33. #define FIXEDPOINT12(x) to_fixedpoint(x, 12)
  34. #define FIXEDPOINT12_0_5 (FIXEDPOINT12(1) >> 1)
  35. /* convert pixel coordinate to fixedpoint-12 coordinate */
  36. #define PX_FIXEDPOINT12(x) (FIXEDPOINT12(x) + (FIXEDPOINT12(1) >> 1))
  37. /* convert fixedpoint-12 to integer */
  38. #define FLOOR_FIXEDPOINT12(x) floor_fixedpoint(x, 12)
  39. #define ROUND_FIXEDPOINT12(x) round_fixedpoint(x, 12)
  40. /* convert integer to fixedpoint-16 */
  41. #define FIXEDPOINT16(x) to_fixedpoint(x, 16)
  42. #define FIXEDPOINT16_0_5 (FIXEDPOINT16(1) >> 1)
  43. /* convert pixel coordinate to fixedpoint-16 coordinate */
  44. #define PX_FIXEDPOINT16(x) (FIXEDPOINT16(x) + (FIXEDPOINT16(1) >> 1))
  45. /* convert fixedpoint-16 to integer */
  46. #define FLOOR_FIXEDPOINT16(x) floor_fixedpoint(x, 16)
  47. #define ROUND_FIXEDPOINT16(x) round_fixedpoint(x, 16)
  48. /* Convert to fixedpoint */
  49. static inline int32_t to_fixedpoint(int32_t x, int fraction_bits)
  50. {
  51. return x << fraction_bits;
  52. }
  53. /* Round fixedpoint towards nearest integer (round to positive infinity). */
  54. static inline int32_t round_fixedpoint(int32_t x, int fraction_bits)
  55. {
  56. return (x + (1 << (fraction_bits - 1))) >> fraction_bits;
  57. }
  58. /* Round fixedpoint towards negative infinity. */
  59. static inline int32_t floor_fixedpoint(int32_t x, int fraction_bits)
  60. {
  61. return x >> fraction_bits;
  62. }
  63. /* Round fixedpoint towards positive infinity. */
  64. static inline int32_t ceil_fixedpoint(int32_t x, int fraction_bits)
  65. {
  66. return (x + ((1 << fraction_bits) - 1)) >> fraction_bits;
  67. }
  68. /* Round fixedpoint towards zero. */
  69. static inline int32_t fix_fixedpoint(int32_t x, int fraction_bits)
  70. {
  71. if (x >= 0)
  72. return floor_fixedpoint(x, fraction_bits);
  73. else
  74. return ceil_fixedpoint(x, fraction_bits);
  75. }
  76. /* Integer part of fixedpoint */
  77. static inline int32_t fixedpoint_int(int32_t x, int fraction_bits)
  78. {
  79. //return to_fixedpoint(floor_fixedpoint(x, fraction_bits));
  80. return x & ~((1 << fraction_bits) - 1);
  81. }
  82. /* Fraction part of fixedpoint */
  83. static inline int32_t fixedpoint_frac(int32_t x, int fraction_bits)
  84. {
  85. return x - fixedpoint_int(x, fraction_bits);
  86. }
  87. /*
  88. * @brief compute cos
  89. *
  90. * @param angle angle in 0.1 degree [0, 3600]
  91. *
  92. * @retval result in fixedpoint-30
  93. */
  94. int32_t sw_cos30(uint16_t angle);
  95. /*
  96. * @brief compute sin
  97. *
  98. * @param angle angle in 0.1 degree [0, 3600]
  99. *
  100. * @retval result in fixedpoint-30
  101. */
  102. int32_t sw_sin30(uint16_t angle);
  103. /*
  104. * @brief compute cos
  105. *
  106. * @param angle angle in 0.1 degree [0, 3600]
  107. *
  108. * @retval result in fixedpoint-14
  109. */
  110. int16_t sw_cos14(uint16_t angle);
  111. /*
  112. * @brief compute sin
  113. *
  114. * @param angle angle in 0.1 degree [0, 3600]
  115. *
  116. * @retval result in fixedpoint-14
  117. */
  118. int16_t sw_sin14(uint16_t angle);
  119. /*
  120. * @brief rotate a 32-bit point
  121. *
  122. * @param dest_x x coord of the point after rotation
  123. * @param dest_y y coord of the point after rotation
  124. * @param src_x x coord of the point before rotation
  125. * @param src_y y coord of the point image before rotation
  126. * @param pivot_x x coord of rotation pivot
  127. * @param pivot_y y coord of rotation pivot
  128. * @param angle rotation angle in 0.1 degree [0, 3600]
  129. *
  130. * @retval N/A
  131. */
  132. void sw_rotate_point32(int32_t *dest_x, int32_t *dest_y,
  133. int32_t src_x, int32_t src_y, int32_t pivot_x, int32_t pivot_y, uint16_t angle);
  134. /*
  135. * @brief rotate a 16-bit point
  136. *
  137. * @param dest_x x coord of the point after rotation
  138. * @param dest_y y coord of the point after rotation
  139. * @param src_x x coord of the point before rotation
  140. * @param src_y y coord of the point image before rotation
  141. * @param pivot_x x coord of rotation pivot
  142. * @param pivot_y y coord of rotation pivot
  143. * @param angle rotation angle in 0.1 degree [0, 3600]
  144. *
  145. * @retval N/A
  146. */
  147. void sw_rotate_point16(int16_t *dest_x, int16_t *dest_y,
  148. int16_t src_x, int16_t src_y, int16_t pivot_x, int16_t pivot_y, uint16_t angle);
  149. /*
  150. * @brief rotate a 16-bit area
  151. *
  152. * [x1, x2] x [y1, y2] define the pixel coordinates of a area
  153. *
  154. * @param dest_x1 x1 coord of rect after rotation
  155. * @param dest_y1 y1 coord of rect after rotation
  156. * @param dest_x2 x2 coord of rect after rotation
  157. * @param dest_y2 y2 coord of rect after rotation
  158. * @param src_x1 x1 coord of rect before rotation
  159. * @param src_y1 y1 coord of rect before rotation
  160. * @param src_x2 x2 coord of rect before rotation
  161. * @param src_y2 y2 coord of rect before rotation
  162. * @param pivot_x x coord of rotation pivot
  163. * @param pivot_y y coord of rotation pivot
  164. * @param angle rotation angle in 0.1 degree [0, 3600]
  165. *
  166. * @retval N/A
  167. */
  168. void sw_rotate_area16(
  169. int16_t *dest_x1, int16_t *dest_y1, int16_t *dest_x2, int16_t *dest_y2,
  170. int16_t src_x1, int16_t src_y1, int16_t src_x2, int16_t src_y2,
  171. int16_t pivot_x, int16_t pivot_y, uint16_t angle);
  172. #ifdef __cplusplus
  173. }
  174. #endif
  175. /**
  176. * @}
  177. */
  178. #endif /* ZEPHYR_FRAMEWORK_INCLUDE_DISPLAY_SW_MATH_H_ */