utmath.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*******************************************************************************
  2. *
  3. * Module Name: utmath - Integer math support routines
  4. *
  5. ******************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2005, R. Byron Moore
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <acpi/acpi.h>
  43. #define _COMPONENT ACPI_UTILITIES
  44. ACPI_MODULE_NAME("utmath")
  45. /*
  46. * Support for double-precision integer divide. This code is included here
  47. * in order to support kernel environments where the double-precision math
  48. * library is not available.
  49. */
  50. #ifndef ACPI_USE_NATIVE_DIVIDE
  51. /*******************************************************************************
  52. *
  53. * FUNCTION: acpi_ut_short_divide
  54. *
  55. * PARAMETERS: Dividend - 64-bit dividend
  56. * Divisor - 32-bit divisor
  57. * out_quotient - Pointer to where the quotient is returned
  58. * out_remainder - Pointer to where the remainder is returned
  59. *
  60. * RETURN: Status (Checks for divide-by-zero)
  61. *
  62. * DESCRIPTION: Perform a short (maximum 64 bits divided by 32 bits)
  63. * divide and modulo. The result is a 64-bit quotient and a
  64. * 32-bit remainder.
  65. *
  66. ******************************************************************************/
  67. acpi_status
  68. acpi_ut_short_divide(acpi_integer dividend,
  69. u32 divisor,
  70. acpi_integer * out_quotient, u32 * out_remainder)
  71. {
  72. union uint64_overlay dividend_ovl;
  73. union uint64_overlay quotient;
  74. u32 remainder32;
  75. ACPI_FUNCTION_TRACE("ut_short_divide");
  76. /* Always check for a zero divisor */
  77. if (divisor == 0) {
  78. ACPI_REPORT_ERROR(("acpi_ut_short_divide: Divide by zero\n"));
  79. return_ACPI_STATUS(AE_AML_DIVIDE_BY_ZERO);
  80. }
  81. dividend_ovl.full = dividend;
  82. /*
  83. * The quotient is 64 bits, the remainder is always 32 bits,
  84. * and is generated by the second divide.
  85. */
  86. ACPI_DIV_64_BY_32(0, dividend_ovl.part.hi, divisor,
  87. quotient.part.hi, remainder32);
  88. ACPI_DIV_64_BY_32(remainder32, dividend_ovl.part.lo, divisor,
  89. quotient.part.lo, remainder32);
  90. /* Return only what was requested */
  91. if (out_quotient) {
  92. *out_quotient = quotient.full;
  93. }
  94. if (out_remainder) {
  95. *out_remainder = remainder32;
  96. }
  97. return_ACPI_STATUS(AE_OK);
  98. }
  99. /*******************************************************************************
  100. *
  101. * FUNCTION: acpi_ut_divide
  102. *
  103. * PARAMETERS: in_dividend - Dividend
  104. * in_divisor - Divisor
  105. * out_quotient - Pointer to where the quotient is returned
  106. * out_remainder - Pointer to where the remainder is returned
  107. *
  108. * RETURN: Status (Checks for divide-by-zero)
  109. *
  110. * DESCRIPTION: Perform a divide and modulo.
  111. *
  112. ******************************************************************************/
  113. acpi_status
  114. acpi_ut_divide(acpi_integer in_dividend,
  115. acpi_integer in_divisor,
  116. acpi_integer * out_quotient, acpi_integer * out_remainder)
  117. {
  118. union uint64_overlay dividend;
  119. union uint64_overlay divisor;
  120. union uint64_overlay quotient;
  121. union uint64_overlay remainder;
  122. union uint64_overlay normalized_dividend;
  123. union uint64_overlay normalized_divisor;
  124. u32 partial1;
  125. union uint64_overlay partial2;
  126. union uint64_overlay partial3;
  127. ACPI_FUNCTION_TRACE("ut_divide");
  128. /* Always check for a zero divisor */
  129. if (in_divisor == 0) {
  130. ACPI_REPORT_ERROR(("acpi_ut_divide: Divide by zero\n"));
  131. return_ACPI_STATUS(AE_AML_DIVIDE_BY_ZERO);
  132. }
  133. divisor.full = in_divisor;
  134. dividend.full = in_dividend;
  135. if (divisor.part.hi == 0) {
  136. /*
  137. * 1) Simplest case is where the divisor is 32 bits, we can
  138. * just do two divides
  139. */
  140. remainder.part.hi = 0;
  141. /*
  142. * The quotient is 64 bits, the remainder is always 32 bits,
  143. * and is generated by the second divide.
  144. */
  145. ACPI_DIV_64_BY_32(0, dividend.part.hi, divisor.part.lo,
  146. quotient.part.hi, partial1);
  147. ACPI_DIV_64_BY_32(partial1, dividend.part.lo, divisor.part.lo,
  148. quotient.part.lo, remainder.part.lo);
  149. }
  150. else {
  151. /*
  152. * 2) The general case where the divisor is a full 64 bits
  153. * is more difficult
  154. */
  155. quotient.part.hi = 0;
  156. normalized_dividend = dividend;
  157. normalized_divisor = divisor;
  158. /* Normalize the operands (shift until the divisor is < 32 bits) */
  159. do {
  160. ACPI_SHIFT_RIGHT_64(normalized_divisor.part.hi,
  161. normalized_divisor.part.lo);
  162. ACPI_SHIFT_RIGHT_64(normalized_dividend.part.hi,
  163. normalized_dividend.part.lo);
  164. } while (normalized_divisor.part.hi != 0);
  165. /* Partial divide */
  166. ACPI_DIV_64_BY_32(normalized_dividend.part.hi,
  167. normalized_dividend.part.lo,
  168. normalized_divisor.part.lo,
  169. quotient.part.lo, partial1);
  170. /*
  171. * The quotient is always 32 bits, and simply requires adjustment.
  172. * The 64-bit remainder must be generated.
  173. */
  174. partial1 = quotient.part.lo * divisor.part.hi;
  175. partial2.full =
  176. (acpi_integer) quotient.part.lo * divisor.part.lo;
  177. partial3.full = (acpi_integer) partial2.part.hi + partial1;
  178. remainder.part.hi = partial3.part.lo;
  179. remainder.part.lo = partial2.part.lo;
  180. if (partial3.part.hi == 0) {
  181. if (partial3.part.lo >= dividend.part.hi) {
  182. if (partial3.part.lo == dividend.part.hi) {
  183. if (partial2.part.lo > dividend.part.lo) {
  184. quotient.part.lo--;
  185. remainder.full -= divisor.full;
  186. }
  187. } else {
  188. quotient.part.lo--;
  189. remainder.full -= divisor.full;
  190. }
  191. }
  192. remainder.full = remainder.full - dividend.full;
  193. remainder.part.hi = (u32) - ((s32) remainder.part.hi);
  194. remainder.part.lo = (u32) - ((s32) remainder.part.lo);
  195. if (remainder.part.lo) {
  196. remainder.part.hi--;
  197. }
  198. }
  199. }
  200. /* Return only what was requested */
  201. if (out_quotient) {
  202. *out_quotient = quotient.full;
  203. }
  204. if (out_remainder) {
  205. *out_remainder = remainder.full;
  206. }
  207. return_ACPI_STATUS(AE_OK);
  208. }
  209. #else
  210. /*******************************************************************************
  211. *
  212. * FUNCTION: acpi_ut_short_divide, acpi_ut_divide
  213. *
  214. * PARAMETERS: See function headers above
  215. *
  216. * DESCRIPTION: Native versions of the ut_divide functions. Use these if either
  217. * 1) The target is a 64-bit platform and therefore 64-bit
  218. * integer math is supported directly by the machine.
  219. * 2) The target is a 32-bit or 16-bit platform, and the
  220. * double-precision integer math library is available to
  221. * perform the divide.
  222. *
  223. ******************************************************************************/
  224. acpi_status
  225. acpi_ut_short_divide(acpi_integer in_dividend,
  226. u32 divisor,
  227. acpi_integer * out_quotient, u32 * out_remainder)
  228. {
  229. ACPI_FUNCTION_TRACE("ut_short_divide");
  230. /* Always check for a zero divisor */
  231. if (divisor == 0) {
  232. ACPI_REPORT_ERROR(("acpi_ut_short_divide: Divide by zero\n"));
  233. return_ACPI_STATUS(AE_AML_DIVIDE_BY_ZERO);
  234. }
  235. /* Return only what was requested */
  236. if (out_quotient) {
  237. *out_quotient = in_dividend / divisor;
  238. }
  239. if (out_remainder) {
  240. *out_remainder = (u32) in_dividend % divisor;
  241. }
  242. return_ACPI_STATUS(AE_OK);
  243. }
  244. acpi_status
  245. acpi_ut_divide(acpi_integer in_dividend,
  246. acpi_integer in_divisor,
  247. acpi_integer * out_quotient, acpi_integer * out_remainder)
  248. {
  249. ACPI_FUNCTION_TRACE("ut_divide");
  250. /* Always check for a zero divisor */
  251. if (in_divisor == 0) {
  252. ACPI_REPORT_ERROR(("acpi_ut_divide: Divide by zero\n"));
  253. return_ACPI_STATUS(AE_AML_DIVIDE_BY_ZERO);
  254. }
  255. /* Return only what was requested */
  256. if (out_quotient) {
  257. *out_quotient = in_dividend / in_divisor;
  258. }
  259. if (out_remainder) {
  260. *out_remainder = in_dividend % in_divisor;
  261. }
  262. return_ACPI_STATUS(AE_OK);
  263. }
  264. #endif