fixed31_32.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. #define FIXED31_32_BITS_PER_FRACTIONAL_PART 32
  29. /*
  30. * @brief
  31. * Arithmetic operations on real numbers
  32. * represented as fixed-point numbers.
  33. * There are: 1 bit for sign,
  34. * 31 bit for integer part,
  35. * 32 bits for fractional part.
  36. *
  37. * @note
  38. * Currently, overflows and underflows are asserted;
  39. * no special result returned.
  40. */
  41. struct fixed31_32 {
  42. int64_t value;
  43. };
  44. /*
  45. * @brief
  46. * Useful constants
  47. */
  48. static const struct fixed31_32 dal_fixed31_32_zero = { 0 };
  49. static const struct fixed31_32 dal_fixed31_32_epsilon = { 1LL };
  50. static const struct fixed31_32 dal_fixed31_32_half = { 0x80000000LL };
  51. static const struct fixed31_32 dal_fixed31_32_one = { 0x100000000LL };
  52. static const struct fixed31_32 dal_fixed31_32_pi = { 13493037705LL };
  53. static const struct fixed31_32 dal_fixed31_32_two_pi = { 26986075409LL };
  54. static const struct fixed31_32 dal_fixed31_32_e = { 11674931555LL };
  55. static const struct fixed31_32 dal_fixed31_32_ln2 = { 2977044471LL };
  56. static const struct fixed31_32 dal_fixed31_32_ln2_div_2 = { 1488522236LL };
  57. /*
  58. * @brief
  59. * Initialization routines
  60. */
  61. /*
  62. * @brief
  63. * result = numerator / denominator
  64. */
  65. struct fixed31_32 dal_fixed31_32_from_fraction(
  66. int64_t numerator,
  67. int64_t denominator);
  68. /*
  69. * @brief
  70. * result = arg
  71. */
  72. struct fixed31_32 dal_fixed31_32_from_int_nonconst(int64_t arg);
  73. static inline struct fixed31_32 dal_fixed31_32_from_int(int64_t arg)
  74. {
  75. if (__builtin_constant_p(arg)) {
  76. struct fixed31_32 res;
  77. BUILD_BUG_ON((LONG_MIN > arg) || (arg > LONG_MAX));
  78. res.value = arg << FIXED31_32_BITS_PER_FRACTIONAL_PART;
  79. return res;
  80. } else
  81. return dal_fixed31_32_from_int_nonconst(arg);
  82. }
  83. /*
  84. * @brief
  85. * Unary operators
  86. */
  87. /*
  88. * @brief
  89. * result = -arg
  90. */
  91. static inline struct fixed31_32 dal_fixed31_32_neg(struct fixed31_32 arg)
  92. {
  93. struct fixed31_32 res;
  94. res.value = -arg.value;
  95. return res;
  96. }
  97. /*
  98. * @brief
  99. * result = abs(arg) := (arg >= 0) ? arg : -arg
  100. */
  101. static inline struct fixed31_32 dal_fixed31_32_abs(struct fixed31_32 arg)
  102. {
  103. if (arg.value < 0)
  104. return dal_fixed31_32_neg(arg);
  105. else
  106. return arg;
  107. }
  108. /*
  109. * @brief
  110. * Binary relational operators
  111. */
  112. /*
  113. * @brief
  114. * result = arg1 < arg2
  115. */
  116. static inline bool dal_fixed31_32_lt(struct fixed31_32 arg1,
  117. struct fixed31_32 arg2)
  118. {
  119. return arg1.value < arg2.value;
  120. }
  121. /*
  122. * @brief
  123. * result = arg1 <= arg2
  124. */
  125. static inline bool dal_fixed31_32_le(struct fixed31_32 arg1,
  126. struct fixed31_32 arg2)
  127. {
  128. return arg1.value <= arg2.value;
  129. }
  130. /*
  131. * @brief
  132. * result = arg1 == arg2
  133. */
  134. static inline bool dal_fixed31_32_eq(struct fixed31_32 arg1,
  135. struct fixed31_32 arg2)
  136. {
  137. return arg1.value == arg2.value;
  138. }
  139. /*
  140. * @brief
  141. * result = min(arg1, arg2) := (arg1 <= arg2) ? arg1 : arg2
  142. */
  143. static inline struct fixed31_32 dal_fixed31_32_min(struct fixed31_32 arg1,
  144. struct fixed31_32 arg2)
  145. {
  146. if (arg1.value <= arg2.value)
  147. return arg1;
  148. else
  149. return arg2;
  150. }
  151. /*
  152. * @brief
  153. * result = max(arg1, arg2) := (arg1 <= arg2) ? arg2 : arg1
  154. */
  155. static inline struct fixed31_32 dal_fixed31_32_max(struct fixed31_32 arg1,
  156. struct fixed31_32 arg2)
  157. {
  158. if (arg1.value <= arg2.value)
  159. return arg2;
  160. else
  161. return arg1;
  162. }
  163. /*
  164. * @brief
  165. * | min_value, when arg <= min_value
  166. * result = | arg, when min_value < arg < max_value
  167. * | max_value, when arg >= max_value
  168. */
  169. static inline struct fixed31_32 dal_fixed31_32_clamp(
  170. struct fixed31_32 arg,
  171. struct fixed31_32 min_value,
  172. struct fixed31_32 max_value)
  173. {
  174. if (dal_fixed31_32_le(arg, min_value))
  175. return min_value;
  176. else if (dal_fixed31_32_le(max_value, arg))
  177. return max_value;
  178. else
  179. return arg;
  180. }
  181. /*
  182. * @brief
  183. * Binary shift operators
  184. */
  185. /*
  186. * @brief
  187. * result = arg << shift
  188. */
  189. struct fixed31_32 dal_fixed31_32_shl(
  190. struct fixed31_32 arg,
  191. uint8_t shift);
  192. /*
  193. * @brief
  194. * result = arg >> shift
  195. */
  196. static inline struct fixed31_32 dal_fixed31_32_shr(
  197. struct fixed31_32 arg,
  198. uint8_t shift)
  199. {
  200. struct fixed31_32 res;
  201. res.value = arg.value >> shift;
  202. return res;
  203. }
  204. /*
  205. * @brief
  206. * Binary additive operators
  207. */
  208. /*
  209. * @brief
  210. * result = arg1 + arg2
  211. */
  212. struct fixed31_32 dal_fixed31_32_add(
  213. struct fixed31_32 arg1,
  214. struct fixed31_32 arg2);
  215. /*
  216. * @brief
  217. * result = arg1 + arg2
  218. */
  219. static inline struct fixed31_32 dal_fixed31_32_add_int(struct fixed31_32 arg1,
  220. int32_t arg2)
  221. {
  222. return dal_fixed31_32_add(arg1,
  223. dal_fixed31_32_from_int(arg2));
  224. }
  225. /*
  226. * @brief
  227. * result = arg1 - arg2
  228. */
  229. struct fixed31_32 dal_fixed31_32_sub(
  230. struct fixed31_32 arg1,
  231. struct fixed31_32 arg2);
  232. /*
  233. * @brief
  234. * result = arg1 - arg2
  235. */
  236. static inline struct fixed31_32 dal_fixed31_32_sub_int(struct fixed31_32 arg1,
  237. int32_t arg2)
  238. {
  239. return dal_fixed31_32_sub(arg1,
  240. dal_fixed31_32_from_int(arg2));
  241. }
  242. /*
  243. * @brief
  244. * Binary multiplicative operators
  245. */
  246. /*
  247. * @brief
  248. * result = arg1 * arg2
  249. */
  250. struct fixed31_32 dal_fixed31_32_mul(
  251. struct fixed31_32 arg1,
  252. struct fixed31_32 arg2);
  253. /*
  254. * @brief
  255. * result = arg1 * arg2
  256. */
  257. static inline struct fixed31_32 dal_fixed31_32_mul_int(struct fixed31_32 arg1,
  258. int32_t arg2)
  259. {
  260. return dal_fixed31_32_mul(arg1,
  261. dal_fixed31_32_from_int(arg2));
  262. }
  263. /*
  264. * @brief
  265. * result = square(arg) := arg * arg
  266. */
  267. struct fixed31_32 dal_fixed31_32_sqr(
  268. struct fixed31_32 arg);
  269. /*
  270. * @brief
  271. * result = arg1 / arg2
  272. */
  273. static inline struct fixed31_32 dal_fixed31_32_div_int(struct fixed31_32 arg1,
  274. int64_t arg2)
  275. {
  276. return dal_fixed31_32_from_fraction(arg1.value,
  277. dal_fixed31_32_from_int(arg2).value);
  278. }
  279. /*
  280. * @brief
  281. * result = arg1 / arg2
  282. */
  283. static inline struct fixed31_32 dal_fixed31_32_div(struct fixed31_32 arg1,
  284. struct fixed31_32 arg2)
  285. {
  286. return dal_fixed31_32_from_fraction(arg1.value,
  287. arg2.value);
  288. }
  289. /*
  290. * @brief
  291. * Reciprocal function
  292. */
  293. /*
  294. * @brief
  295. * result = reciprocal(arg) := 1 / arg
  296. *
  297. * @note
  298. * No special actions taken in case argument is zero.
  299. */
  300. struct fixed31_32 dal_fixed31_32_recip(
  301. struct fixed31_32 arg);
  302. /*
  303. * @brief
  304. * Trigonometric functions
  305. */
  306. /*
  307. * @brief
  308. * result = sinc(arg) := sin(arg) / arg
  309. *
  310. * @note
  311. * Argument specified in radians,
  312. * internally it's normalized to [-2pi...2pi] range.
  313. */
  314. struct fixed31_32 dal_fixed31_32_sinc(
  315. struct fixed31_32 arg);
  316. /*
  317. * @brief
  318. * result = sin(arg)
  319. *
  320. * @note
  321. * Argument specified in radians,
  322. * internally it's normalized to [-2pi...2pi] range.
  323. */
  324. struct fixed31_32 dal_fixed31_32_sin(
  325. struct fixed31_32 arg);
  326. /*
  327. * @brief
  328. * result = cos(arg)
  329. *
  330. * @note
  331. * Argument specified in radians
  332. * and should be in [-2pi...2pi] range -
  333. * passing arguments outside that range
  334. * will cause incorrect result!
  335. */
  336. struct fixed31_32 dal_fixed31_32_cos(
  337. struct fixed31_32 arg);
  338. /*
  339. * @brief
  340. * Transcendent functions
  341. */
  342. /*
  343. * @brief
  344. * result = exp(arg)
  345. *
  346. * @note
  347. * Currently, function is verified for abs(arg) <= 1.
  348. */
  349. struct fixed31_32 dal_fixed31_32_exp(
  350. struct fixed31_32 arg);
  351. /*
  352. * @brief
  353. * result = log(arg)
  354. *
  355. * @note
  356. * Currently, abs(arg) should be less than 1.
  357. * No normalization is done.
  358. * Currently, no special actions taken
  359. * in case of invalid argument(s). Take care!
  360. */
  361. struct fixed31_32 dal_fixed31_32_log(
  362. struct fixed31_32 arg);
  363. /*
  364. * @brief
  365. * Power function
  366. */
  367. /*
  368. * @brief
  369. * result = pow(arg1, arg2)
  370. *
  371. * @note
  372. * Currently, abs(arg1) should be less than 1. Take care!
  373. */
  374. struct fixed31_32 dal_fixed31_32_pow(
  375. struct fixed31_32 arg1,
  376. struct fixed31_32 arg2);
  377. /*
  378. * @brief
  379. * Rounding functions
  380. */
  381. /*
  382. * @brief
  383. * result = floor(arg) := greatest integer lower than or equal to arg
  384. */
  385. int32_t dal_fixed31_32_floor(
  386. struct fixed31_32 arg);
  387. /*
  388. * @brief
  389. * result = round(arg) := integer nearest to arg
  390. */
  391. int32_t dal_fixed31_32_round(
  392. struct fixed31_32 arg);
  393. /*
  394. * @brief
  395. * result = ceil(arg) := lowest integer greater than or equal to arg
  396. */
  397. int32_t dal_fixed31_32_ceil(
  398. struct fixed31_32 arg);
  399. /* the following two function are used in scaler hw programming to convert fixed
  400. * point value to format 2 bits from integer part and 19 bits from fractional
  401. * part. The same applies for u0d19, 0 bits from integer part and 19 bits from
  402. * fractional
  403. */
  404. uint32_t dal_fixed31_32_u2d19(
  405. struct fixed31_32 arg);
  406. uint32_t dal_fixed31_32_u0d19(
  407. struct fixed31_32 arg);
  408. #endif