fixed31_32.h 11 KB

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