custom_float.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright 2017 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 "custom_float.h"
  27. static bool build_custom_float(
  28. struct fixed31_32 value,
  29. const struct custom_float_format *format,
  30. bool *negative,
  31. uint32_t *mantissa,
  32. uint32_t *exponenta)
  33. {
  34. uint32_t exp_offset = (1 << (format->exponenta_bits - 1)) - 1;
  35. const struct fixed31_32 mantissa_constant_plus_max_fraction =
  36. dal_fixed31_32_from_fraction(
  37. (1LL << (format->mantissa_bits + 1)) - 1,
  38. 1LL << format->mantissa_bits);
  39. struct fixed31_32 mantiss;
  40. if (dal_fixed31_32_eq(
  41. value,
  42. dal_fixed31_32_zero)) {
  43. *negative = false;
  44. *mantissa = 0;
  45. *exponenta = 0;
  46. return true;
  47. }
  48. if (dal_fixed31_32_lt(
  49. value,
  50. dal_fixed31_32_zero)) {
  51. *negative = format->sign;
  52. value = dal_fixed31_32_neg(value);
  53. } else {
  54. *negative = false;
  55. }
  56. if (dal_fixed31_32_lt(
  57. value,
  58. dal_fixed31_32_one)) {
  59. uint32_t i = 1;
  60. do {
  61. value = dal_fixed31_32_shl(value, 1);
  62. ++i;
  63. } while (dal_fixed31_32_lt(
  64. value,
  65. dal_fixed31_32_one));
  66. --i;
  67. if (exp_offset <= i) {
  68. *mantissa = 0;
  69. *exponenta = 0;
  70. return true;
  71. }
  72. *exponenta = exp_offset - i;
  73. } else if (dal_fixed31_32_le(
  74. mantissa_constant_plus_max_fraction,
  75. value)) {
  76. uint32_t i = 1;
  77. do {
  78. value = dal_fixed31_32_shr(value, 1);
  79. ++i;
  80. } while (dal_fixed31_32_lt(
  81. mantissa_constant_plus_max_fraction,
  82. value));
  83. *exponenta = exp_offset + i - 1;
  84. } else {
  85. *exponenta = exp_offset;
  86. }
  87. mantiss = dal_fixed31_32_sub(
  88. value,
  89. dal_fixed31_32_one);
  90. if (dal_fixed31_32_lt(
  91. mantiss,
  92. dal_fixed31_32_zero) ||
  93. dal_fixed31_32_lt(
  94. dal_fixed31_32_one,
  95. mantiss))
  96. mantiss = dal_fixed31_32_zero;
  97. else
  98. mantiss = dal_fixed31_32_shl(
  99. mantiss,
  100. format->mantissa_bits);
  101. *mantissa = dal_fixed31_32_floor(mantiss);
  102. return true;
  103. }
  104. static bool setup_custom_float(
  105. const struct custom_float_format *format,
  106. bool negative,
  107. uint32_t mantissa,
  108. uint32_t exponenta,
  109. uint32_t *result)
  110. {
  111. uint32_t i = 0;
  112. uint32_t j = 0;
  113. uint32_t value = 0;
  114. /* verification code:
  115. * once calculation is ok we can remove it
  116. */
  117. const uint32_t mantissa_mask =
  118. (1 << (format->mantissa_bits + 1)) - 1;
  119. const uint32_t exponenta_mask =
  120. (1 << (format->exponenta_bits + 1)) - 1;
  121. if (mantissa & ~mantissa_mask) {
  122. BREAK_TO_DEBUGGER();
  123. mantissa = mantissa_mask;
  124. }
  125. if (exponenta & ~exponenta_mask) {
  126. BREAK_TO_DEBUGGER();
  127. exponenta = exponenta_mask;
  128. }
  129. /* end of verification code */
  130. while (i < format->mantissa_bits) {
  131. uint32_t mask = 1 << i;
  132. if (mantissa & mask)
  133. value |= mask;
  134. ++i;
  135. }
  136. while (j < format->exponenta_bits) {
  137. uint32_t mask = 1 << j;
  138. if (exponenta & mask)
  139. value |= mask << i;
  140. ++j;
  141. }
  142. if (negative && format->sign)
  143. value |= 1 << (i + j);
  144. *result = value;
  145. return true;
  146. }
  147. bool convert_to_custom_float_format(
  148. struct fixed31_32 value,
  149. const struct custom_float_format *format,
  150. uint32_t *result)
  151. {
  152. uint32_t mantissa;
  153. uint32_t exponenta;
  154. bool negative;
  155. return build_custom_float(
  156. value, format, &negative, &mantissa, &exponenta) &&
  157. setup_custom_float(
  158. format, negative, mantissa, exponenta, result);
  159. }