fixpt32_32.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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/fixed32_32.h"
  27. static uint64_t u64_div(uint64_t n, uint64_t d)
  28. {
  29. uint32_t i = 0;
  30. uint64_t r;
  31. uint64_t q = div64_u64_rem(n, d, &r);
  32. for (i = 0; i < 32; ++i) {
  33. uint64_t sbit = q & (1ULL<<63);
  34. r <<= 1;
  35. r |= sbit ? 1 : 0;
  36. q <<= 1;
  37. if (r >= d) {
  38. r -= d;
  39. q |= 1;
  40. }
  41. }
  42. if (2*r >= d)
  43. q += 1;
  44. return q;
  45. }
  46. struct fixed32_32 dal_fixed32_32_from_fraction(uint32_t n, uint32_t d)
  47. {
  48. struct fixed32_32 fx;
  49. fx.value = u64_div((uint64_t)n << 32, (uint64_t)d << 32);
  50. return fx;
  51. }
  52. struct fixed32_32 dal_fixed32_32_add(
  53. struct fixed32_32 lhs,
  54. struct fixed32_32 rhs)
  55. {
  56. struct fixed32_32 fx = {lhs.value + rhs.value};
  57. ASSERT(fx.value >= rhs.value);
  58. return fx;
  59. }
  60. struct fixed32_32 dal_fixed32_32_add_int(struct fixed32_32 lhs, uint32_t rhs)
  61. {
  62. struct fixed32_32 fx = {lhs.value + ((uint64_t)rhs << 32)};
  63. ASSERT(fx.value >= (uint64_t)rhs << 32);
  64. return fx;
  65. }
  66. struct fixed32_32 dal_fixed32_32_sub(
  67. struct fixed32_32 lhs,
  68. struct fixed32_32 rhs)
  69. {
  70. struct fixed32_32 fx;
  71. ASSERT(lhs.value >= rhs.value);
  72. fx.value = lhs.value - rhs.value;
  73. return fx;
  74. }
  75. struct fixed32_32 dal_fixed32_32_sub_int(struct fixed32_32 lhs, uint32_t rhs)
  76. {
  77. struct fixed32_32 fx;
  78. ASSERT(lhs.value >= ((uint64_t)rhs<<32));
  79. fx.value = lhs.value - ((uint64_t)rhs<<32);
  80. return fx;
  81. }
  82. struct fixed32_32 dal_fixed32_32_mul(
  83. struct fixed32_32 lhs,
  84. struct fixed32_32 rhs)
  85. {
  86. struct fixed32_32 fx;
  87. uint64_t lhs_int = lhs.value>>32;
  88. uint64_t lhs_frac = (uint32_t)lhs.value;
  89. uint64_t rhs_int = rhs.value>>32;
  90. uint64_t rhs_frac = (uint32_t)rhs.value;
  91. uint64_t ahbh = lhs_int * rhs_int;
  92. uint64_t ahbl = lhs_int * rhs_frac;
  93. uint64_t albh = lhs_frac * rhs_int;
  94. uint64_t albl = lhs_frac * rhs_frac;
  95. ASSERT((ahbh>>32) == 0);
  96. fx.value = (ahbh<<32) + ahbl + albh + (albl>>32);
  97. return fx;
  98. }
  99. struct fixed32_32 dal_fixed32_32_mul_int(struct fixed32_32 lhs, uint32_t rhs)
  100. {
  101. struct fixed32_32 fx;
  102. uint64_t lhsi = (lhs.value>>32) * (uint64_t)rhs;
  103. uint64_t lhsf;
  104. ASSERT((lhsi>>32) == 0);
  105. lhsf = ((uint32_t)lhs.value) * (uint64_t)rhs;
  106. ASSERT((lhsi<<32) + lhsf >= lhsf);
  107. fx.value = (lhsi<<32) + lhsf;
  108. return fx;
  109. }
  110. struct fixed32_32 dal_fixed32_32_div(
  111. struct fixed32_32 lhs,
  112. struct fixed32_32 rhs)
  113. {
  114. struct fixed32_32 fx;
  115. fx.value = u64_div(lhs.value, rhs.value);
  116. return fx;
  117. }
  118. struct fixed32_32 dal_fixed32_32_div_int(struct fixed32_32 lhs, uint32_t rhs)
  119. {
  120. struct fixed32_32 fx;
  121. fx.value = u64_div(lhs.value, (uint64_t)rhs << 32);
  122. return fx;
  123. }
  124. uint32_t dal_fixed32_32_ceil(struct fixed32_32 v)
  125. {
  126. ASSERT((uint32_t)v.value ? (v.value >> 32) + 1 >= 1 : true);
  127. return (v.value>>32) + ((uint32_t)v.value ? 1 : 0);
  128. }
  129. uint32_t dal_fixed32_32_round(struct fixed32_32 v)
  130. {
  131. ASSERT(v.value + (1ULL<<31) >= (1ULL<<31));
  132. return (v.value + (1ULL<<31))>>32;
  133. }