fixpt31_32.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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. #include "dm_services.h"
  26. #include "include/fixed31_32.h"
  27. static inline uint64_t abs_i64(
  28. int64_t arg)
  29. {
  30. if (arg > 0)
  31. return (uint64_t)arg;
  32. else
  33. return (uint64_t)(-arg);
  34. }
  35. /*
  36. * @brief
  37. * result = dividend / divisor
  38. * *remainder = dividend % divisor
  39. */
  40. static inline uint64_t complete_integer_division_u64(
  41. uint64_t dividend,
  42. uint64_t divisor,
  43. uint64_t *remainder)
  44. {
  45. uint64_t result;
  46. ASSERT(divisor);
  47. result = div64_u64_rem(dividend, divisor, remainder);
  48. return result;
  49. }
  50. #define FRACTIONAL_PART_MASK \
  51. ((1ULL << FIXED31_32_BITS_PER_FRACTIONAL_PART) - 1)
  52. #define GET_INTEGER_PART(x) \
  53. ((x) >> FIXED31_32_BITS_PER_FRACTIONAL_PART)
  54. #define GET_FRACTIONAL_PART(x) \
  55. (FRACTIONAL_PART_MASK & (x))
  56. struct fixed31_32 dal_fixed31_32_from_fraction(
  57. int64_t numerator,
  58. int64_t denominator)
  59. {
  60. struct fixed31_32 res;
  61. bool arg1_negative = numerator < 0;
  62. bool arg2_negative = denominator < 0;
  63. uint64_t arg1_value = arg1_negative ? -numerator : numerator;
  64. uint64_t arg2_value = arg2_negative ? -denominator : denominator;
  65. uint64_t remainder;
  66. /* determine integer part */
  67. uint64_t res_value = complete_integer_division_u64(
  68. arg1_value, arg2_value, &remainder);
  69. ASSERT(res_value <= LONG_MAX);
  70. /* determine fractional part */
  71. {
  72. uint32_t i = FIXED31_32_BITS_PER_FRACTIONAL_PART;
  73. do {
  74. remainder <<= 1;
  75. res_value <<= 1;
  76. if (remainder >= arg2_value) {
  77. res_value |= 1;
  78. remainder -= arg2_value;
  79. }
  80. } while (--i != 0);
  81. }
  82. /* round up LSB */
  83. {
  84. uint64_t summand = (remainder << 1) >= arg2_value;
  85. ASSERT(res_value <= LLONG_MAX - summand);
  86. res_value += summand;
  87. }
  88. res.value = (int64_t)res_value;
  89. if (arg1_negative ^ arg2_negative)
  90. res.value = -res.value;
  91. return res;
  92. }
  93. struct fixed31_32 dal_fixed31_32_from_int_nonconst(
  94. int64_t arg)
  95. {
  96. struct fixed31_32 res;
  97. ASSERT((LONG_MIN <= arg) && (arg <= LONG_MAX));
  98. res.value = arg << FIXED31_32_BITS_PER_FRACTIONAL_PART;
  99. return res;
  100. }
  101. struct fixed31_32 dal_fixed31_32_shl(
  102. struct fixed31_32 arg,
  103. uint8_t shift)
  104. {
  105. struct fixed31_32 res;
  106. ASSERT(((arg.value >= 0) && (arg.value <= LLONG_MAX >> shift)) ||
  107. ((arg.value < 0) && (arg.value >= LLONG_MIN >> shift)));
  108. res.value = arg.value << shift;
  109. return res;
  110. }
  111. struct fixed31_32 dal_fixed31_32_add(
  112. struct fixed31_32 arg1,
  113. struct fixed31_32 arg2)
  114. {
  115. struct fixed31_32 res;
  116. ASSERT(((arg1.value >= 0) && (LLONG_MAX - arg1.value >= arg2.value)) ||
  117. ((arg1.value < 0) && (LLONG_MIN - arg1.value <= arg2.value)));
  118. res.value = arg1.value + arg2.value;
  119. return res;
  120. }
  121. struct fixed31_32 dal_fixed31_32_sub(
  122. struct fixed31_32 arg1,
  123. struct fixed31_32 arg2)
  124. {
  125. struct fixed31_32 res;
  126. ASSERT(((arg2.value >= 0) && (LLONG_MIN + arg2.value <= arg1.value)) ||
  127. ((arg2.value < 0) && (LLONG_MAX + arg2.value >= arg1.value)));
  128. res.value = arg1.value - arg2.value;
  129. return res;
  130. }
  131. struct fixed31_32 dal_fixed31_32_mul(
  132. struct fixed31_32 arg1,
  133. struct fixed31_32 arg2)
  134. {
  135. struct fixed31_32 res;
  136. bool arg1_negative = arg1.value < 0;
  137. bool arg2_negative = arg2.value < 0;
  138. uint64_t arg1_value = arg1_negative ? -arg1.value : arg1.value;
  139. uint64_t arg2_value = arg2_negative ? -arg2.value : arg2.value;
  140. uint64_t arg1_int = GET_INTEGER_PART(arg1_value);
  141. uint64_t arg2_int = GET_INTEGER_PART(arg2_value);
  142. uint64_t arg1_fra = GET_FRACTIONAL_PART(arg1_value);
  143. uint64_t arg2_fra = GET_FRACTIONAL_PART(arg2_value);
  144. uint64_t tmp;
  145. res.value = arg1_int * arg2_int;
  146. ASSERT(res.value <= LONG_MAX);
  147. res.value <<= FIXED31_32_BITS_PER_FRACTIONAL_PART;
  148. tmp = arg1_int * arg2_fra;
  149. ASSERT(tmp <= (uint64_t)(LLONG_MAX - res.value));
  150. res.value += tmp;
  151. tmp = arg2_int * arg1_fra;
  152. ASSERT(tmp <= (uint64_t)(LLONG_MAX - res.value));
  153. res.value += tmp;
  154. tmp = arg1_fra * arg2_fra;
  155. tmp = (tmp >> FIXED31_32_BITS_PER_FRACTIONAL_PART) +
  156. (tmp >= (uint64_t)dal_fixed31_32_half.value);
  157. ASSERT(tmp <= (uint64_t)(LLONG_MAX - res.value));
  158. res.value += tmp;
  159. if (arg1_negative ^ arg2_negative)
  160. res.value = -res.value;
  161. return res;
  162. }
  163. struct fixed31_32 dal_fixed31_32_sqr(
  164. struct fixed31_32 arg)
  165. {
  166. struct fixed31_32 res;
  167. uint64_t arg_value = abs_i64(arg.value);
  168. uint64_t arg_int = GET_INTEGER_PART(arg_value);
  169. uint64_t arg_fra = GET_FRACTIONAL_PART(arg_value);
  170. uint64_t tmp;
  171. res.value = arg_int * arg_int;
  172. ASSERT(res.value <= LONG_MAX);
  173. res.value <<= FIXED31_32_BITS_PER_FRACTIONAL_PART;
  174. tmp = arg_int * arg_fra;
  175. ASSERT(tmp <= (uint64_t)(LLONG_MAX - res.value));
  176. res.value += tmp;
  177. ASSERT(tmp <= (uint64_t)(LLONG_MAX - res.value));
  178. res.value += tmp;
  179. tmp = arg_fra * arg_fra;
  180. tmp = (tmp >> FIXED31_32_BITS_PER_FRACTIONAL_PART) +
  181. (tmp >= (uint64_t)dal_fixed31_32_half.value);
  182. ASSERT(tmp <= (uint64_t)(LLONG_MAX - res.value));
  183. res.value += tmp;
  184. return res;
  185. }
  186. struct fixed31_32 dal_fixed31_32_recip(
  187. struct fixed31_32 arg)
  188. {
  189. /*
  190. * @note
  191. * Good idea to use Newton's method
  192. */
  193. ASSERT(arg.value);
  194. return dal_fixed31_32_from_fraction(
  195. dal_fixed31_32_one.value,
  196. arg.value);
  197. }
  198. struct fixed31_32 dal_fixed31_32_sinc(
  199. struct fixed31_32 arg)
  200. {
  201. struct fixed31_32 square;
  202. struct fixed31_32 res = dal_fixed31_32_one;
  203. int32_t n = 27;
  204. struct fixed31_32 arg_norm = arg;
  205. if (dal_fixed31_32_le(
  206. dal_fixed31_32_two_pi,
  207. dal_fixed31_32_abs(arg))) {
  208. arg_norm = dal_fixed31_32_sub(
  209. arg_norm,
  210. dal_fixed31_32_mul_int(
  211. dal_fixed31_32_two_pi,
  212. (int32_t)div64_s64(
  213. arg_norm.value,
  214. dal_fixed31_32_two_pi.value)));
  215. }
  216. square = dal_fixed31_32_sqr(arg_norm);
  217. do {
  218. res = dal_fixed31_32_sub(
  219. dal_fixed31_32_one,
  220. dal_fixed31_32_div_int(
  221. dal_fixed31_32_mul(
  222. square,
  223. res),
  224. n * (n - 1)));
  225. n -= 2;
  226. } while (n > 2);
  227. if (arg.value != arg_norm.value)
  228. res = dal_fixed31_32_div(
  229. dal_fixed31_32_mul(res, arg_norm),
  230. arg);
  231. return res;
  232. }
  233. struct fixed31_32 dal_fixed31_32_sin(
  234. struct fixed31_32 arg)
  235. {
  236. return dal_fixed31_32_mul(
  237. arg,
  238. dal_fixed31_32_sinc(arg));
  239. }
  240. struct fixed31_32 dal_fixed31_32_cos(
  241. struct fixed31_32 arg)
  242. {
  243. /* TODO implement argument normalization */
  244. const struct fixed31_32 square = dal_fixed31_32_sqr(arg);
  245. struct fixed31_32 res = dal_fixed31_32_one;
  246. int32_t n = 26;
  247. do {
  248. res = dal_fixed31_32_sub(
  249. dal_fixed31_32_one,
  250. dal_fixed31_32_div_int(
  251. dal_fixed31_32_mul(
  252. square,
  253. res),
  254. n * (n - 1)));
  255. n -= 2;
  256. } while (n != 0);
  257. return res;
  258. }
  259. /*
  260. * @brief
  261. * result = exp(arg),
  262. * where abs(arg) < 1
  263. *
  264. * Calculated as Taylor series.
  265. */
  266. static struct fixed31_32 fixed31_32_exp_from_taylor_series(
  267. struct fixed31_32 arg)
  268. {
  269. uint32_t n = 9;
  270. struct fixed31_32 res = dal_fixed31_32_from_fraction(
  271. n + 2,
  272. n + 1);
  273. /* TODO find correct res */
  274. ASSERT(dal_fixed31_32_lt(arg, dal_fixed31_32_one));
  275. do
  276. res = dal_fixed31_32_add(
  277. dal_fixed31_32_one,
  278. dal_fixed31_32_div_int(
  279. dal_fixed31_32_mul(
  280. arg,
  281. res),
  282. n));
  283. while (--n != 1);
  284. return dal_fixed31_32_add(
  285. dal_fixed31_32_one,
  286. dal_fixed31_32_mul(
  287. arg,
  288. res));
  289. }
  290. struct fixed31_32 dal_fixed31_32_exp(
  291. struct fixed31_32 arg)
  292. {
  293. /*
  294. * @brief
  295. * Main equation is:
  296. * exp(x) = exp(r + m * ln(2)) = (1 << m) * exp(r),
  297. * where m = round(x / ln(2)), r = x - m * ln(2)
  298. */
  299. if (dal_fixed31_32_le(
  300. dal_fixed31_32_ln2_div_2,
  301. dal_fixed31_32_abs(arg))) {
  302. int32_t m = dal_fixed31_32_round(
  303. dal_fixed31_32_div(
  304. arg,
  305. dal_fixed31_32_ln2));
  306. struct fixed31_32 r = dal_fixed31_32_sub(
  307. arg,
  308. dal_fixed31_32_mul_int(
  309. dal_fixed31_32_ln2,
  310. m));
  311. ASSERT(m != 0);
  312. ASSERT(dal_fixed31_32_lt(
  313. dal_fixed31_32_abs(r),
  314. dal_fixed31_32_one));
  315. if (m > 0)
  316. return dal_fixed31_32_shl(
  317. fixed31_32_exp_from_taylor_series(r),
  318. (uint8_t)m);
  319. else
  320. return dal_fixed31_32_div_int(
  321. fixed31_32_exp_from_taylor_series(r),
  322. 1LL << -m);
  323. } else if (arg.value != 0)
  324. return fixed31_32_exp_from_taylor_series(arg);
  325. else
  326. return dal_fixed31_32_one;
  327. }
  328. struct fixed31_32 dal_fixed31_32_log(
  329. struct fixed31_32 arg)
  330. {
  331. struct fixed31_32 res = dal_fixed31_32_neg(dal_fixed31_32_one);
  332. /* TODO improve 1st estimation */
  333. struct fixed31_32 error;
  334. ASSERT(arg.value > 0);
  335. /* TODO if arg is negative, return NaN */
  336. /* TODO if arg is zero, return -INF */
  337. do {
  338. struct fixed31_32 res1 = dal_fixed31_32_add(
  339. dal_fixed31_32_sub(
  340. res,
  341. dal_fixed31_32_one),
  342. dal_fixed31_32_div(
  343. arg,
  344. dal_fixed31_32_exp(res)));
  345. error = dal_fixed31_32_sub(
  346. res,
  347. res1);
  348. res = res1;
  349. /* TODO determine max_allowed_error based on quality of exp() */
  350. } while (abs_i64(error.value) > 100ULL);
  351. return res;
  352. }
  353. struct fixed31_32 dal_fixed31_32_pow(
  354. struct fixed31_32 arg1,
  355. struct fixed31_32 arg2)
  356. {
  357. return dal_fixed31_32_exp(
  358. dal_fixed31_32_mul(
  359. dal_fixed31_32_log(arg1),
  360. arg2));
  361. }
  362. int32_t dal_fixed31_32_floor(
  363. struct fixed31_32 arg)
  364. {
  365. uint64_t arg_value = abs_i64(arg.value);
  366. if (arg.value >= 0)
  367. return (int32_t)GET_INTEGER_PART(arg_value);
  368. else
  369. return -(int32_t)GET_INTEGER_PART(arg_value);
  370. }
  371. int32_t dal_fixed31_32_round(
  372. struct fixed31_32 arg)
  373. {
  374. uint64_t arg_value = abs_i64(arg.value);
  375. const int64_t summand = dal_fixed31_32_half.value;
  376. ASSERT(LLONG_MAX - (int64_t)arg_value >= summand);
  377. arg_value += summand;
  378. if (arg.value >= 0)
  379. return (int32_t)GET_INTEGER_PART(arg_value);
  380. else
  381. return -(int32_t)GET_INTEGER_PART(arg_value);
  382. }
  383. int32_t dal_fixed31_32_ceil(
  384. struct fixed31_32 arg)
  385. {
  386. uint64_t arg_value = abs_i64(arg.value);
  387. const int64_t summand = dal_fixed31_32_one.value -
  388. dal_fixed31_32_epsilon.value;
  389. ASSERT(LLONG_MAX - (int64_t)arg_value >= summand);
  390. arg_value += summand;
  391. if (arg.value >= 0)
  392. return (int32_t)GET_INTEGER_PART(arg_value);
  393. else
  394. return -(int32_t)GET_INTEGER_PART(arg_value);
  395. }
  396. /* this function is a generic helper to translate fixed point value to
  397. * specified integer format that will consist of integer_bits integer part and
  398. * fractional_bits fractional part. For example it is used in
  399. * dal_fixed31_32_u2d19 to receive 2 bits integer part and 19 bits fractional
  400. * part in 32 bits. It is used in hw programming (scaler)
  401. */
  402. static inline uint32_t ux_dy(
  403. int64_t value,
  404. uint32_t integer_bits,
  405. uint32_t fractional_bits)
  406. {
  407. /* 1. create mask of integer part */
  408. uint32_t result = (1 << integer_bits) - 1;
  409. /* 2. mask out fractional part */
  410. uint32_t fractional_part = FRACTIONAL_PART_MASK & value;
  411. /* 3. shrink fixed point integer part to be of integer_bits width*/
  412. result &= GET_INTEGER_PART(value);
  413. /* 4. make space for fractional part to be filled in after integer */
  414. result <<= fractional_bits;
  415. /* 5. shrink fixed point fractional part to of fractional_bits width*/
  416. fractional_part >>= FIXED31_32_BITS_PER_FRACTIONAL_PART - fractional_bits;
  417. /* 6. merge the result */
  418. return result | fractional_part;
  419. }
  420. uint32_t dal_fixed31_32_u2d19(
  421. struct fixed31_32 arg)
  422. {
  423. return ux_dy(arg.value, 2, 19);
  424. }
  425. uint32_t dal_fixed31_32_u0d19(
  426. struct fixed31_32 arg)
  427. {
  428. return ux_dy(arg.value, 0, 19);
  429. }