fixed31_32.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. #define FIXED31_32_BITS_PER_FRACTIONAL_PART 32
  28. #ifndef LLONG_MIN
  29. #define LLONG_MIN (1LL<<63)
  30. #endif
  31. #ifndef LLONG_MAX
  32. #define LLONG_MAX (-1LL>>1)
  33. #endif
  34. /*
  35. * @brief
  36. * Arithmetic operations on real numbers
  37. * represented as fixed-point numbers.
  38. * There are: 1 bit for sign,
  39. * 31 bit for integer part,
  40. * 32 bits for fractional part.
  41. *
  42. * @note
  43. * Currently, overflows and underflows are asserted;
  44. * no special result returned.
  45. */
  46. struct fixed31_32 {
  47. long long value;
  48. };
  49. /*
  50. * @brief
  51. * Useful constants
  52. */
  53. static const struct fixed31_32 dc_fixpt_zero = { 0 };
  54. static const struct fixed31_32 dc_fixpt_epsilon = { 1LL };
  55. static const struct fixed31_32 dc_fixpt_half = { 0x80000000LL };
  56. static const struct fixed31_32 dc_fixpt_one = { 0x100000000LL };
  57. static const struct fixed31_32 dc_fixpt_pi = { 13493037705LL };
  58. static const struct fixed31_32 dc_fixpt_two_pi = { 26986075409LL };
  59. static const struct fixed31_32 dc_fixpt_e = { 11674931555LL };
  60. static const struct fixed31_32 dc_fixpt_ln2 = { 2977044471LL };
  61. static const struct fixed31_32 dc_fixpt_ln2_div_2 = { 1488522236LL };
  62. /*
  63. * @brief
  64. * Initialization routines
  65. */
  66. /*
  67. * @brief
  68. * result = numerator / denominator
  69. */
  70. struct fixed31_32 dc_fixpt_from_fraction(long long numerator, long long denominator);
  71. /*
  72. * @brief
  73. * result = arg
  74. */
  75. static inline struct fixed31_32 dc_fixpt_from_int(int arg)
  76. {
  77. struct fixed31_32 res;
  78. res.value = (long long) arg << FIXED31_32_BITS_PER_FRACTIONAL_PART;
  79. return res;
  80. }
  81. /*
  82. * @brief
  83. * Unary operators
  84. */
  85. /*
  86. * @brief
  87. * result = -arg
  88. */
  89. static inline struct fixed31_32 dc_fixpt_neg(struct fixed31_32 arg)
  90. {
  91. struct fixed31_32 res;
  92. res.value = -arg.value;
  93. return res;
  94. }
  95. /*
  96. * @brief
  97. * result = abs(arg) := (arg >= 0) ? arg : -arg
  98. */
  99. static inline struct fixed31_32 dc_fixpt_abs(struct fixed31_32 arg)
  100. {
  101. if (arg.value < 0)
  102. return dc_fixpt_neg(arg);
  103. else
  104. return arg;
  105. }
  106. /*
  107. * @brief
  108. * Binary relational operators
  109. */
  110. /*
  111. * @brief
  112. * result = arg1 < arg2
  113. */
  114. static inline bool dc_fixpt_lt(struct fixed31_32 arg1, struct fixed31_32 arg2)
  115. {
  116. return arg1.value < arg2.value;
  117. }
  118. /*
  119. * @brief
  120. * result = arg1 <= arg2
  121. */
  122. static inline bool dc_fixpt_le(struct fixed31_32 arg1, struct fixed31_32 arg2)
  123. {
  124. return arg1.value <= arg2.value;
  125. }
  126. /*
  127. * @brief
  128. * result = arg1 == arg2
  129. */
  130. static inline bool dc_fixpt_eq(struct fixed31_32 arg1, struct fixed31_32 arg2)
  131. {
  132. return arg1.value == arg2.value;
  133. }
  134. /*
  135. * @brief
  136. * result = min(arg1, arg2) := (arg1 <= arg2) ? arg1 : arg2
  137. */
  138. static inline struct fixed31_32 dc_fixpt_min(struct fixed31_32 arg1, struct fixed31_32 arg2)
  139. {
  140. if (arg1.value <= arg2.value)
  141. return arg1;
  142. else
  143. return arg2;
  144. }
  145. /*
  146. * @brief
  147. * result = max(arg1, arg2) := (arg1 <= arg2) ? arg2 : arg1
  148. */
  149. static inline struct fixed31_32 dc_fixpt_max(struct fixed31_32 arg1, struct fixed31_32 arg2)
  150. {
  151. if (arg1.value <= arg2.value)
  152. return arg2;
  153. else
  154. return arg1;
  155. }
  156. /*
  157. * @brief
  158. * | min_value, when arg <= min_value
  159. * result = | arg, when min_value < arg < max_value
  160. * | max_value, when arg >= max_value
  161. */
  162. static inline struct fixed31_32 dc_fixpt_clamp(
  163. struct fixed31_32 arg,
  164. struct fixed31_32 min_value,
  165. struct fixed31_32 max_value)
  166. {
  167. if (dc_fixpt_le(arg, min_value))
  168. return min_value;
  169. else if (dc_fixpt_le(max_value, arg))
  170. return max_value;
  171. else
  172. return arg;
  173. }
  174. /*
  175. * @brief
  176. * Binary shift operators
  177. */
  178. /*
  179. * @brief
  180. * result = arg << shift
  181. */
  182. static inline struct fixed31_32 dc_fixpt_shl(struct fixed31_32 arg, unsigned char shift)
  183. {
  184. ASSERT(((arg.value >= 0) && (arg.value <= LLONG_MAX >> shift)) ||
  185. ((arg.value < 0) && (arg.value >= ~(LLONG_MAX >> shift))));
  186. arg.value = arg.value << shift;
  187. return arg;
  188. }
  189. /*
  190. * @brief
  191. * result = arg >> shift
  192. */
  193. static inline struct fixed31_32 dc_fixpt_shr(struct fixed31_32 arg, unsigned char shift)
  194. {
  195. bool negative = arg.value < 0;
  196. if (negative)
  197. arg.value = -arg.value;
  198. arg.value = arg.value >> shift;
  199. if (negative)
  200. arg.value = -arg.value;
  201. return arg;
  202. }
  203. /*
  204. * @brief
  205. * Binary additive operators
  206. */
  207. /*
  208. * @brief
  209. * result = arg1 + arg2
  210. */
  211. static inline struct fixed31_32 dc_fixpt_add(struct fixed31_32 arg1, struct fixed31_32 arg2)
  212. {
  213. struct fixed31_32 res;
  214. ASSERT(((arg1.value >= 0) && (LLONG_MAX - arg1.value >= arg2.value)) ||
  215. ((arg1.value < 0) && (LLONG_MIN - arg1.value <= arg2.value)));
  216. res.value = arg1.value + arg2.value;
  217. return res;
  218. }
  219. /*
  220. * @brief
  221. * result = arg1 + arg2
  222. */
  223. static inline struct fixed31_32 dc_fixpt_add_int(struct fixed31_32 arg1, int arg2)
  224. {
  225. return dc_fixpt_add(arg1, dc_fixpt_from_int(arg2));
  226. }
  227. /*
  228. * @brief
  229. * result = arg1 - arg2
  230. */
  231. static inline struct fixed31_32 dc_fixpt_sub(struct fixed31_32 arg1, struct fixed31_32 arg2)
  232. {
  233. struct fixed31_32 res;
  234. ASSERT(((arg2.value >= 0) && (LLONG_MIN + arg2.value <= arg1.value)) ||
  235. ((arg2.value < 0) && (LLONG_MAX + arg2.value >= arg1.value)));
  236. res.value = arg1.value - arg2.value;
  237. return res;
  238. }
  239. /*
  240. * @brief
  241. * result = arg1 - arg2
  242. */
  243. static inline struct fixed31_32 dc_fixpt_sub_int(struct fixed31_32 arg1, int arg2)
  244. {
  245. return dc_fixpt_sub(arg1, dc_fixpt_from_int(arg2));
  246. }
  247. /*
  248. * @brief
  249. * Binary multiplicative operators
  250. */
  251. /*
  252. * @brief
  253. * result = arg1 * arg2
  254. */
  255. struct fixed31_32 dc_fixpt_mul(struct fixed31_32 arg1, struct fixed31_32 arg2);
  256. /*
  257. * @brief
  258. * result = arg1 * arg2
  259. */
  260. static inline struct fixed31_32 dc_fixpt_mul_int(struct fixed31_32 arg1, int arg2)
  261. {
  262. return dc_fixpt_mul(arg1, dc_fixpt_from_int(arg2));
  263. }
  264. /*
  265. * @brief
  266. * result = square(arg) := arg * arg
  267. */
  268. struct fixed31_32 dc_fixpt_sqr(struct fixed31_32 arg);
  269. /*
  270. * @brief
  271. * result = arg1 / arg2
  272. */
  273. static inline struct fixed31_32 dc_fixpt_div_int(struct fixed31_32 arg1, long long arg2)
  274. {
  275. return dc_fixpt_from_fraction(arg1.value, dc_fixpt_from_int(arg2).value);
  276. }
  277. /*
  278. * @brief
  279. * result = arg1 / arg2
  280. */
  281. static inline struct fixed31_32 dc_fixpt_div(struct fixed31_32 arg1, struct fixed31_32 arg2)
  282. {
  283. return dc_fixpt_from_fraction(arg1.value, arg2.value);
  284. }
  285. /*
  286. * @brief
  287. * Reciprocal function
  288. */
  289. /*
  290. * @brief
  291. * result = reciprocal(arg) := 1 / arg
  292. *
  293. * @note
  294. * No special actions taken in case argument is zero.
  295. */
  296. struct fixed31_32 dc_fixpt_recip(struct fixed31_32 arg);
  297. /*
  298. * @brief
  299. * Trigonometric functions
  300. */
  301. /*
  302. * @brief
  303. * result = sinc(arg) := sin(arg) / arg
  304. *
  305. * @note
  306. * Argument specified in radians,
  307. * internally it's normalized to [-2pi...2pi] range.
  308. */
  309. struct fixed31_32 dc_fixpt_sinc(struct fixed31_32 arg);
  310. /*
  311. * @brief
  312. * result = sin(arg)
  313. *
  314. * @note
  315. * Argument specified in radians,
  316. * internally it's normalized to [-2pi...2pi] range.
  317. */
  318. struct fixed31_32 dc_fixpt_sin(struct fixed31_32 arg);
  319. /*
  320. * @brief
  321. * result = cos(arg)
  322. *
  323. * @note
  324. * Argument specified in radians
  325. * and should be in [-2pi...2pi] range -
  326. * passing arguments outside that range
  327. * will cause incorrect result!
  328. */
  329. struct fixed31_32 dc_fixpt_cos(struct fixed31_32 arg);
  330. /*
  331. * @brief
  332. * Transcendent functions
  333. */
  334. /*
  335. * @brief
  336. * result = exp(arg)
  337. *
  338. * @note
  339. * Currently, function is verified for abs(arg) <= 1.
  340. */
  341. struct fixed31_32 dc_fixpt_exp(struct fixed31_32 arg);
  342. /*
  343. * @brief
  344. * result = log(arg)
  345. *
  346. * @note
  347. * Currently, abs(arg) should be less than 1.
  348. * No normalization is done.
  349. * Currently, no special actions taken
  350. * in case of invalid argument(s). Take care!
  351. */
  352. struct fixed31_32 dc_fixpt_log(struct fixed31_32 arg);
  353. /*
  354. * @brief
  355. * Power function
  356. */
  357. /*
  358. * @brief
  359. * result = pow(arg1, arg2)
  360. *
  361. * @note
  362. * Currently, abs(arg1) should be less than 1. Take care!
  363. */
  364. static inline struct fixed31_32 dc_fixpt_pow(struct fixed31_32 arg1, struct fixed31_32 arg2)
  365. {
  366. return dc_fixpt_exp(
  367. dc_fixpt_mul(
  368. dc_fixpt_log(arg1),
  369. arg2));
  370. }
  371. /*
  372. * @brief
  373. * Rounding functions
  374. */
  375. /*
  376. * @brief
  377. * result = floor(arg) := greatest integer lower than or equal to arg
  378. */
  379. static inline int dc_fixpt_floor(struct fixed31_32 arg)
  380. {
  381. unsigned long long arg_value = arg.value > 0 ? arg.value : -arg.value;
  382. if (arg.value >= 0)
  383. return (int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART);
  384. else
  385. return -(int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART);
  386. }
  387. /*
  388. * @brief
  389. * result = round(arg) := integer nearest to arg
  390. */
  391. static inline int dc_fixpt_round(struct fixed31_32 arg)
  392. {
  393. unsigned long long arg_value = arg.value > 0 ? arg.value : -arg.value;
  394. const long long summand = dc_fixpt_half.value;
  395. ASSERT(LLONG_MAX - (long long)arg_value >= summand);
  396. arg_value += summand;
  397. if (arg.value >= 0)
  398. return (int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART);
  399. else
  400. return -(int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART);
  401. }
  402. /*
  403. * @brief
  404. * result = ceil(arg) := lowest integer greater than or equal to arg
  405. */
  406. static inline int dc_fixpt_ceil(struct fixed31_32 arg)
  407. {
  408. unsigned long long arg_value = arg.value > 0 ? arg.value : -arg.value;
  409. const long long summand = dc_fixpt_one.value -
  410. dc_fixpt_epsilon.value;
  411. ASSERT(LLONG_MAX - (long long)arg_value >= summand);
  412. arg_value += summand;
  413. if (arg.value >= 0)
  414. return (int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART);
  415. else
  416. return -(int)(arg_value >> FIXED31_32_BITS_PER_FRACTIONAL_PART);
  417. }
  418. /* the following two function are used in scaler hw programming to convert fixed
  419. * point value to format 2 bits from integer part and 19 bits from fractional
  420. * part. The same applies for u0d19, 0 bits from integer part and 19 bits from
  421. * fractional
  422. */
  423. unsigned int dc_fixpt_u3d19(struct fixed31_32 arg);
  424. unsigned int dc_fixpt_u2d19(struct fixed31_32 arg);
  425. unsigned int dc_fixpt_u0d19(struct fixed31_32 arg);
  426. unsigned int dc_fixpt_clamp_u0d14(struct fixed31_32 arg);
  427. unsigned int dc_fixpt_clamp_u0d10(struct fixed31_32 arg);
  428. int dc_fixpt_s4d19(struct fixed31_32 arg);
  429. static inline struct fixed31_32 dc_fixpt_truncate(struct fixed31_32 arg, unsigned int frac_bits)
  430. {
  431. bool negative = arg.value < 0;
  432. if (frac_bits >= FIXED31_32_BITS_PER_FRACTIONAL_PART) {
  433. ASSERT(frac_bits == FIXED31_32_BITS_PER_FRACTIONAL_PART);
  434. return arg;
  435. }
  436. if (negative)
  437. arg.value = -arg.value;
  438. arg.value &= (~0LL) << (FIXED31_32_BITS_PER_FRACTIONAL_PART - frac_bits);
  439. if (negative)
  440. arg.value = -arg.value;
  441. return arg;
  442. }
  443. #endif