fixed31_32.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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_add_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_int(
  179. struct fixed31_32 arg1,
  180. int32_t arg2);
  181. /*
  182. * @brief
  183. * result = arg1 - arg2
  184. */
  185. struct fixed31_32 dal_fixed31_32_sub(
  186. struct fixed31_32 arg1,
  187. struct fixed31_32 arg2);
  188. /*
  189. * @brief
  190. * Binary multiplicative operators
  191. */
  192. /*
  193. * @brief
  194. * result = arg1 * arg2
  195. */
  196. struct fixed31_32 dal_fixed31_32_mul_int(
  197. struct fixed31_32 arg1,
  198. int32_t arg2);
  199. /*
  200. * @brief
  201. * result = arg1 * arg2
  202. */
  203. struct fixed31_32 dal_fixed31_32_mul(
  204. struct fixed31_32 arg1,
  205. struct fixed31_32 arg2);
  206. /*
  207. * @brief
  208. * result = square(arg) := arg * arg
  209. */
  210. struct fixed31_32 dal_fixed31_32_sqr(
  211. struct fixed31_32 arg);
  212. /*
  213. * @brief
  214. * result = arg1 / arg2
  215. */
  216. struct fixed31_32 dal_fixed31_32_div_int(
  217. struct fixed31_32 arg1,
  218. int64_t arg2);
  219. /*
  220. * @brief
  221. * result = arg1 / arg2
  222. */
  223. struct fixed31_32 dal_fixed31_32_div(
  224. struct fixed31_32 arg1,
  225. struct fixed31_32 arg2);
  226. /*
  227. * @brief
  228. * Reciprocal function
  229. */
  230. /*
  231. * @brief
  232. * result = reciprocal(arg) := 1 / arg
  233. *
  234. * @note
  235. * No special actions taken in case argument is zero.
  236. */
  237. struct fixed31_32 dal_fixed31_32_recip(
  238. struct fixed31_32 arg);
  239. /*
  240. * @brief
  241. * Trigonometric functions
  242. */
  243. /*
  244. * @brief
  245. * result = sinc(arg) := sin(arg) / arg
  246. *
  247. * @note
  248. * Argument specified in radians,
  249. * internally it's normalized to [-2pi...2pi] range.
  250. */
  251. struct fixed31_32 dal_fixed31_32_sinc(
  252. struct fixed31_32 arg);
  253. /*
  254. * @brief
  255. * result = sin(arg)
  256. *
  257. * @note
  258. * Argument specified in radians,
  259. * internally it's normalized to [-2pi...2pi] range.
  260. */
  261. struct fixed31_32 dal_fixed31_32_sin(
  262. struct fixed31_32 arg);
  263. /*
  264. * @brief
  265. * result = cos(arg)
  266. *
  267. * @note
  268. * Argument specified in radians
  269. * and should be in [-2pi...2pi] range -
  270. * passing arguments outside that range
  271. * will cause incorrect result!
  272. */
  273. struct fixed31_32 dal_fixed31_32_cos(
  274. struct fixed31_32 arg);
  275. /*
  276. * @brief
  277. * Transcendent functions
  278. */
  279. /*
  280. * @brief
  281. * result = exp(arg)
  282. *
  283. * @note
  284. * Currently, function is verified for abs(arg) <= 1.
  285. */
  286. struct fixed31_32 dal_fixed31_32_exp(
  287. struct fixed31_32 arg);
  288. /*
  289. * @brief
  290. * result = log(arg)
  291. *
  292. * @note
  293. * Currently, abs(arg) should be less than 1.
  294. * No normalization is done.
  295. * Currently, no special actions taken
  296. * in case of invalid argument(s). Take care!
  297. */
  298. struct fixed31_32 dal_fixed31_32_log(
  299. struct fixed31_32 arg);
  300. /*
  301. * @brief
  302. * Power function
  303. */
  304. /*
  305. * @brief
  306. * result = pow(arg1, arg2)
  307. *
  308. * @note
  309. * Currently, abs(arg1) should be less than 1. Take care!
  310. */
  311. struct fixed31_32 dal_fixed31_32_pow(
  312. struct fixed31_32 arg1,
  313. struct fixed31_32 arg2);
  314. /*
  315. * @brief
  316. * Rounding functions
  317. */
  318. /*
  319. * @brief
  320. * result = floor(arg) := greatest integer lower than or equal to arg
  321. */
  322. int32_t dal_fixed31_32_floor(
  323. struct fixed31_32 arg);
  324. /*
  325. * @brief
  326. * result = round(arg) := integer nearest to arg
  327. */
  328. int32_t dal_fixed31_32_round(
  329. struct fixed31_32 arg);
  330. /*
  331. * @brief
  332. * result = ceil(arg) := lowest integer greater than or equal to arg
  333. */
  334. int32_t dal_fixed31_32_ceil(
  335. struct fixed31_32 arg);
  336. /* the following two function are used in scaler hw programming to convert fixed
  337. * point value to format 2 bits from integer part and 19 bits from fractional
  338. * part. The same applies for u0d19, 0 bits from integer part and 19 bits from
  339. * fractional
  340. */
  341. uint32_t dal_fixed31_32_u2d19(
  342. struct fixed31_32 arg);
  343. uint32_t dal_fixed31_32_u0d19(
  344. struct fixed31_32 arg);
  345. #endif