bw_fixed.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright 2015 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 "bw_fixed.h"
  27. #define MIN_I64 \
  28. (int64_t)(-(1LL << 63))
  29. #define MAX_I64 \
  30. (int64_t)((1ULL << 63) - 1)
  31. #define FRACTIONAL_PART_MASK \
  32. ((1ULL << BW_FIXED_BITS_PER_FRACTIONAL_PART) - 1)
  33. #define GET_FRACTIONAL_PART(x) \
  34. (FRACTIONAL_PART_MASK & (x))
  35. static uint64_t abs_i64(int64_t arg)
  36. {
  37. if (arg >= 0)
  38. return (uint64_t)(arg);
  39. else
  40. return (uint64_t)(-arg);
  41. }
  42. struct bw_fixed bw_int_to_fixed_nonconst(int64_t value)
  43. {
  44. struct bw_fixed res;
  45. ASSERT(value < BW_FIXED_MAX_I32 && value > BW_FIXED_MIN_I32);
  46. res.value = value << BW_FIXED_BITS_PER_FRACTIONAL_PART;
  47. return res;
  48. }
  49. struct bw_fixed bw_frc_to_fixed(int64_t numerator, int64_t denominator)
  50. {
  51. struct bw_fixed res;
  52. bool arg1_negative = numerator < 0;
  53. bool arg2_negative = denominator < 0;
  54. uint64_t arg1_value;
  55. uint64_t arg2_value;
  56. uint64_t remainder;
  57. /* determine integer part */
  58. uint64_t res_value;
  59. ASSERT(denominator != 0);
  60. arg1_value = abs_i64(numerator);
  61. arg2_value = abs_i64(denominator);
  62. res_value = div64_u64_rem(arg1_value, arg2_value, &remainder);
  63. ASSERT(res_value <= BW_FIXED_MAX_I32);
  64. /* determine fractional part */
  65. {
  66. uint32_t i = BW_FIXED_BITS_PER_FRACTIONAL_PART;
  67. do
  68. {
  69. remainder <<= 1;
  70. res_value <<= 1;
  71. if (remainder >= arg2_value)
  72. {
  73. res_value |= 1;
  74. remainder -= arg2_value;
  75. }
  76. } while (--i != 0);
  77. }
  78. /* round up LSB */
  79. {
  80. uint64_t summand = (remainder << 1) >= arg2_value;
  81. ASSERT(res_value <= MAX_I64 - summand);
  82. res_value += summand;
  83. }
  84. res.value = (int64_t)(res_value);
  85. if (arg1_negative ^ arg2_negative)
  86. res.value = -res.value;
  87. return res;
  88. }
  89. struct bw_fixed bw_floor2(
  90. const struct bw_fixed arg,
  91. const struct bw_fixed significance)
  92. {
  93. struct bw_fixed result;
  94. int64_t multiplicand;
  95. multiplicand = div64_s64(arg.value, abs_i64(significance.value));
  96. result.value = abs_i64(significance.value) * multiplicand;
  97. ASSERT(abs_i64(result.value) <= abs_i64(arg.value));
  98. return result;
  99. }
  100. struct bw_fixed bw_ceil2(
  101. const struct bw_fixed arg,
  102. const struct bw_fixed significance)
  103. {
  104. struct bw_fixed result;
  105. int64_t multiplicand;
  106. multiplicand = div64_s64(arg.value, abs_i64(significance.value));
  107. result.value = abs_i64(significance.value) * multiplicand;
  108. if (abs_i64(result.value) < abs_i64(arg.value)) {
  109. if (arg.value < 0)
  110. result.value -= abs_i64(significance.value);
  111. else
  112. result.value += abs_i64(significance.value);
  113. }
  114. return result;
  115. }
  116. struct bw_fixed bw_mul(const struct bw_fixed arg1, const struct bw_fixed arg2)
  117. {
  118. struct bw_fixed res;
  119. bool arg1_negative = arg1.value < 0;
  120. bool arg2_negative = arg2.value < 0;
  121. uint64_t arg1_value = abs_i64(arg1.value);
  122. uint64_t arg2_value = abs_i64(arg2.value);
  123. uint64_t arg1_int = BW_FIXED_GET_INTEGER_PART(arg1_value);
  124. uint64_t arg2_int = BW_FIXED_GET_INTEGER_PART(arg2_value);
  125. uint64_t arg1_fra = GET_FRACTIONAL_PART(arg1_value);
  126. uint64_t arg2_fra = GET_FRACTIONAL_PART(arg2_value);
  127. uint64_t tmp;
  128. res.value = arg1_int * arg2_int;
  129. ASSERT(res.value <= BW_FIXED_MAX_I32);
  130. res.value <<= BW_FIXED_BITS_PER_FRACTIONAL_PART;
  131. tmp = arg1_int * arg2_fra;
  132. ASSERT(tmp <= (uint64_t)(MAX_I64 - res.value));
  133. res.value += tmp;
  134. tmp = arg2_int * arg1_fra;
  135. ASSERT(tmp <= (uint64_t)(MAX_I64 - res.value));
  136. res.value += tmp;
  137. tmp = arg1_fra * arg2_fra;
  138. tmp = (tmp >> BW_FIXED_BITS_PER_FRACTIONAL_PART) +
  139. (tmp >= (uint64_t)(bw_frc_to_fixed(1, 2).value));
  140. ASSERT(tmp <= (uint64_t)(MAX_I64 - res.value));
  141. res.value += tmp;
  142. if (arg1_negative ^ arg2_negative)
  143. res.value = -res.value;
  144. return res;
  145. }