bw_fixed.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #ifndef BW_FIXED_H_
  26. #define BW_FIXED_H_
  27. #define BW_FIXED_BITS_PER_FRACTIONAL_PART 24
  28. #define BW_FIXED_GET_INTEGER_PART(x) ((x) >> BW_FIXED_BITS_PER_FRACTIONAL_PART)
  29. struct bw_fixed {
  30. int64_t value;
  31. };
  32. #define BW_FIXED_MIN_I32 \
  33. (int64_t)(-(1LL << (63 - BW_FIXED_BITS_PER_FRACTIONAL_PART)))
  34. #define BW_FIXED_MAX_I32 \
  35. (int64_t)((1ULL << (63 - BW_FIXED_BITS_PER_FRACTIONAL_PART)) - 1)
  36. static inline struct bw_fixed bw_min2(const struct bw_fixed arg1,
  37. const struct bw_fixed arg2)
  38. {
  39. return (arg1.value <= arg2.value) ? arg1 : arg2;
  40. }
  41. static inline struct bw_fixed bw_max2(const struct bw_fixed arg1,
  42. const struct bw_fixed arg2)
  43. {
  44. return (arg2.value <= arg1.value) ? arg1 : arg2;
  45. }
  46. static inline struct bw_fixed bw_min3(struct bw_fixed v1,
  47. struct bw_fixed v2,
  48. struct bw_fixed v3)
  49. {
  50. return bw_min2(bw_min2(v1, v2), v3);
  51. }
  52. static inline struct bw_fixed bw_max3(struct bw_fixed v1,
  53. struct bw_fixed v2,
  54. struct bw_fixed v3)
  55. {
  56. return bw_max2(bw_max2(v1, v2), v3);
  57. }
  58. struct bw_fixed bw_int_to_fixed_nonconst(int64_t value);
  59. static inline struct bw_fixed bw_int_to_fixed(int64_t value)
  60. {
  61. if (__builtin_constant_p(value)) {
  62. struct bw_fixed res;
  63. BUILD_BUG_ON(value > BW_FIXED_MAX_I32 || value < BW_FIXED_MIN_I32);
  64. res.value = value << BW_FIXED_BITS_PER_FRACTIONAL_PART;
  65. return res;
  66. } else
  67. return bw_int_to_fixed_nonconst(value);
  68. }
  69. static inline int32_t bw_fixed_to_int(struct bw_fixed value)
  70. {
  71. return BW_FIXED_GET_INTEGER_PART(value.value);
  72. }
  73. struct bw_fixed bw_frc_to_fixed(int64_t num, int64_t denum);
  74. static inline struct bw_fixed fixed31_32_to_bw_fixed(int64_t raw)
  75. {
  76. struct bw_fixed result = { 0 };
  77. if (raw < 0) {
  78. raw = -raw;
  79. result.value = -(raw >> (32 - BW_FIXED_BITS_PER_FRACTIONAL_PART));
  80. } else {
  81. result.value = raw >> (32 - BW_FIXED_BITS_PER_FRACTIONAL_PART);
  82. }
  83. return result;
  84. }
  85. static inline struct bw_fixed bw_add(const struct bw_fixed arg1,
  86. const struct bw_fixed arg2)
  87. {
  88. struct bw_fixed res;
  89. res.value = arg1.value + arg2.value;
  90. return res;
  91. }
  92. static inline struct bw_fixed bw_sub(const struct bw_fixed arg1, const struct bw_fixed arg2)
  93. {
  94. struct bw_fixed res;
  95. res.value = arg1.value - arg2.value;
  96. return res;
  97. }
  98. struct bw_fixed bw_mul(const struct bw_fixed arg1, const struct bw_fixed arg2);
  99. static inline struct bw_fixed bw_div(const struct bw_fixed arg1, const struct bw_fixed arg2)
  100. {
  101. return bw_frc_to_fixed(arg1.value, arg2.value);
  102. }
  103. static inline struct bw_fixed bw_mod(const struct bw_fixed arg1, const struct bw_fixed arg2)
  104. {
  105. struct bw_fixed res;
  106. div64_u64_rem(arg1.value, arg2.value, &res.value);
  107. return res;
  108. }
  109. struct bw_fixed bw_floor2(const struct bw_fixed arg, const struct bw_fixed significance);
  110. struct bw_fixed bw_ceil2(const struct bw_fixed arg, const struct bw_fixed significance);
  111. static inline bool bw_equ(const struct bw_fixed arg1, const struct bw_fixed arg2)
  112. {
  113. return arg1.value == arg2.value;
  114. }
  115. static inline bool bw_neq(const struct bw_fixed arg1, const struct bw_fixed arg2)
  116. {
  117. return arg1.value != arg2.value;
  118. }
  119. static inline bool bw_leq(const struct bw_fixed arg1, const struct bw_fixed arg2)
  120. {
  121. return arg1.value <= arg2.value;
  122. }
  123. static inline bool bw_meq(const struct bw_fixed arg1, const struct bw_fixed arg2)
  124. {
  125. return arg1.value >= arg2.value;
  126. }
  127. static inline bool bw_ltn(const struct bw_fixed arg1, const struct bw_fixed arg2)
  128. {
  129. return arg1.value < arg2.value;
  130. }
  131. static inline bool bw_mtn(const struct bw_fixed arg1, const struct bw_fixed arg2)
  132. {
  133. return arg1.value > arg2.value;
  134. }
  135. #endif //BW_FIXED_H_