fixed31_32.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * Copyright 2012-15 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: AMD
  23. *
  24. */
  25. #ifndef __DAL_FIXED31_32_H__
  26. #define __DAL_FIXED31_32_H__
  27. #include "os_types.h"
  28. /*
  29. * @brief
  30. * Arithmetic operations on real numbers
  31. * represented as fixed-point numbers.
  32. * There are: 1 bit for sign,
  33. * 31 bit for integer part,
  34. * 32 bits for fractional part.
  35. *
  36. * @note
  37. * Currently, overflows and underflows are asserted;
  38. * no special result returned.
  39. */
  40. struct fixed31_32 {
  41. int64_t value;
  42. };
  43. /*
  44. * @brief
  45. * Useful constants
  46. */
  47. static const struct fixed31_32 dal_fixed31_32_zero = { 0 };
  48. static const struct fixed31_32 dal_fixed31_32_epsilon = { 1LL };
  49. static const struct fixed31_32 dal_fixed31_32_half = { 0x80000000LL };
  50. static const struct fixed31_32 dal_fixed31_32_one = { 0x100000000LL };
  51. static const struct fixed31_32 dal_fixed31_32_pi = { 13493037705LL };
  52. static const struct fixed31_32 dal_fixed31_32_two_pi = { 26986075409LL };
  53. static const struct fixed31_32 dal_fixed31_32_e = { 11674931555LL };
  54. static const struct fixed31_32 dal_fixed31_32_ln2 = { 2977044471LL };
  55. static const struct fixed31_32 dal_fixed31_32_ln2_div_2 = { 1488522236LL };
  56. /*
  57. * @brief
  58. * Initialization routines
  59. */
  60. /*
  61. * @brief
  62. * result = numerator / denominator
  63. */
  64. struct fixed31_32 dal_fixed31_32_from_fraction(
  65. int64_t numerator,
  66. int64_t denominator);
  67. /*
  68. * @brief
  69. * result = arg
  70. */
  71. struct fixed31_32 dal_fixed31_32_from_int(
  72. int64_t arg);
  73. /*
  74. * @brief
  75. * Unary operators
  76. */
  77. /*
  78. * @brief
  79. * result = -arg
  80. */
  81. struct fixed31_32 dal_fixed31_32_neg(
  82. struct fixed31_32 arg);
  83. /*
  84. * @brief
  85. * result = abs(arg) := (arg >= 0) ? arg : -arg
  86. */
  87. struct fixed31_32 dal_fixed31_32_abs(
  88. struct fixed31_32 arg);
  89. /*
  90. * @brief
  91. * Binary relational operators
  92. */
  93. /*
  94. * @brief
  95. * result = arg1 < arg2
  96. */
  97. bool dal_fixed31_32_lt(
  98. struct fixed31_32 arg1,
  99. struct fixed31_32 arg2);
  100. /*
  101. * @brief
  102. * result = arg1 <= arg2
  103. */
  104. bool dal_fixed31_32_le(
  105. struct fixed31_32 arg1,
  106. struct fixed31_32 arg2);
  107. /*
  108. * @brief
  109. * result = arg1 == arg2
  110. */
  111. bool dal_fixed31_32_eq(
  112. struct fixed31_32 arg1,
  113. struct fixed31_32 arg2);
  114. /*
  115. * @brief
  116. * result = min(arg1, arg2) := (arg1 <= arg2) ? arg1 : arg2
  117. */
  118. struct fixed31_32 dal_fixed31_32_min(
  119. struct fixed31_32 arg1,
  120. struct fixed31_32 arg2);
  121. /*
  122. * @brief
  123. * result = max(arg1, arg2) := (arg1 <= arg2) ? arg2 : arg1
  124. */
  125. struct fixed31_32 dal_fixed31_32_max(
  126. struct fixed31_32 arg1,
  127. struct fixed31_32 arg2);
  128. /*
  129. * @brief
  130. * | min_value, when arg <= min_value
  131. * result = | arg, when min_value < arg < max_value
  132. * | max_value, when arg >= max_value
  133. */
  134. struct fixed31_32 dal_fixed31_32_clamp(
  135. struct fixed31_32 arg,
  136. struct fixed31_32 min_value,
  137. struct fixed31_32 max_value);
  138. /*
  139. * @brief
  140. * Binary shift operators
  141. */
  142. /*
  143. * @brief
  144. * result = arg << shift
  145. */
  146. struct fixed31_32 dal_fixed31_32_shl(
  147. struct fixed31_32 arg,
  148. uint8_t shift);
  149. /*
  150. * @brief
  151. * result = arg >> shift
  152. */
  153. struct fixed31_32 dal_fixed31_32_shr(
  154. struct fixed31_32 arg,
  155. uint8_t shift);
  156. /*
  157. * @brief
  158. * Binary additive operators
  159. */
  160. /*
  161. * @brief
  162. * result = arg1 + arg2
  163. */
  164. struct fixed31_32 dal_fixed31_32_add(
  165. struct fixed31_32 arg1,
  166. struct fixed31_32 arg2);
  167. /*
  168. * @brief
  169. * result = arg1 - arg2
  170. */
  171. struct fixed31_32 dal_fixed31_32_sub_int(
  172. struct fixed31_32 arg1,
  173. int32_t arg2);
  174. /*
  175. * @brief
  176. * result = arg1 - arg2
  177. */
  178. struct fixed31_32 dal_fixed31_32_sub(
  179. struct fixed31_32 arg1,
  180. struct fixed31_32 arg2);
  181. /*
  182. * @brief
  183. * Binary multiplicative operators
  184. */
  185. /*
  186. * @brief
  187. * result = arg1 * arg2
  188. */
  189. struct fixed31_32 dal_fixed31_32_mul_int(
  190. struct fixed31_32 arg1,
  191. int32_t arg2);
  192. /*
  193. * @brief
  194. * result = arg1 * arg2
  195. */
  196. struct fixed31_32 dal_fixed31_32_mul(
  197. struct fixed31_32 arg1,
  198. struct fixed31_32 arg2);
  199. /*
  200. * @brief
  201. * result = square(arg) := arg * arg
  202. */
  203. struct fixed31_32 dal_fixed31_32_sqr(
  204. struct fixed31_32 arg);
  205. /*
  206. * @brief
  207. * result = arg1 / arg2
  208. */
  209. struct fixed31_32 dal_fixed31_32_div_int(
  210. struct fixed31_32 arg1,
  211. int64_t arg2);
  212. /*
  213. * @brief
  214. * result = arg1 / arg2
  215. */
  216. struct fixed31_32 dal_fixed31_32_div(
  217. struct fixed31_32 arg1,
  218. struct fixed31_32 arg2);
  219. /*
  220. * @brief
  221. * Reciprocal function
  222. */
  223. /*
  224. * @brief
  225. * result = reciprocal(arg) := 1 / arg
  226. *
  227. * @note
  228. * No special actions taken in case argument is zero.
  229. */
  230. struct fixed31_32 dal_fixed31_32_recip(
  231. struct fixed31_32 arg);
  232. /*
  233. * @brief
  234. * Trigonometric functions
  235. */
  236. /*
  237. * @brief
  238. * result = sinc(arg) := sin(arg) / arg
  239. *
  240. * @note
  241. * Argument specified in radians,
  242. * internally it's normalized to [-2pi...2pi] range.
  243. */
  244. struct fixed31_32 dal_fixed31_32_sinc(
  245. struct fixed31_32 arg);
  246. /*
  247. * @brief
  248. * result = sin(arg)
  249. *
  250. * @note
  251. * Argument specified in radians,
  252. * internally it's normalized to [-2pi...2pi] range.
  253. */
  254. struct fixed31_32 dal_fixed31_32_sin(
  255. struct fixed31_32 arg);
  256. /*
  257. * @brief
  258. * result = cos(arg)
  259. *
  260. * @note
  261. * Argument specified in radians
  262. * and should be in [-2pi...2pi] range -
  263. * passing arguments outside that range
  264. * will cause incorrect result!
  265. */
  266. struct fixed31_32 dal_fixed31_32_cos(
  267. struct fixed31_32 arg);
  268. /*
  269. * @brief
  270. * Transcendent functions
  271. */
  272. /*
  273. * @brief
  274. * result = exp(arg)
  275. *
  276. * @note
  277. * Currently, function is verified for abs(arg) <= 1.
  278. */
  279. struct fixed31_32 dal_fixed31_32_exp(
  280. struct fixed31_32 arg);
  281. /*
  282. * @brief
  283. * result = log(arg)
  284. *
  285. * @note
  286. * Currently, abs(arg) should be less than 1.
  287. * No normalization is done.
  288. * Currently, no special actions taken
  289. * in case of invalid argument(s). Take care!
  290. */
  291. struct fixed31_32 dal_fixed31_32_log(
  292. struct fixed31_32 arg);
  293. /*
  294. * @brief
  295. * Power function
  296. */
  297. /*
  298. * @brief
  299. * result = pow(arg1, arg2)
  300. *
  301. * @note
  302. * Currently, abs(arg1) should be less than 1. Take care!
  303. */
  304. struct fixed31_32 dal_fixed31_32_pow(
  305. struct fixed31_32 arg1,
  306. struct fixed31_32 arg2);
  307. /*
  308. * @brief
  309. * Rounding functions
  310. */
  311. /*
  312. * @brief
  313. * result = floor(arg) := greatest integer lower than or equal to arg
  314. */
  315. int32_t dal_fixed31_32_floor(
  316. struct fixed31_32 arg);
  317. /*
  318. * @brief
  319. * result = round(arg) := integer nearest to arg
  320. */
  321. int32_t dal_fixed31_32_round(
  322. struct fixed31_32 arg);
  323. /*
  324. * @brief
  325. * result = ceil(arg) := lowest integer greater than or equal to arg
  326. */
  327. int32_t dal_fixed31_32_ceil(
  328. struct fixed31_32 arg);
  329. /* the following two function are used in scaler hw programming to convert fixed
  330. * point value to format 2 bits from integer part and 19 bits from fractional
  331. * part. The same applies for u0d19, 0 bits from integer part and 19 bits from
  332. * fractional
  333. */
  334. uint32_t dal_fixed31_32_u2d19(
  335. struct fixed31_32 arg);
  336. uint32_t dal_fixed31_32_u0d19(
  337. struct fixed31_32 arg);
  338. #endif