fixed31_32.h 11 KB

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