utmath.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*******************************************************************************
  2. *
  3. * Module Name: utmath - Integer math support routines
  4. *
  5. ******************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2008, Intel Corp.
  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. #include "accommon.h"
  44. #define _COMPONENT ACPI_UTILITIES
  45. ACPI_MODULE_NAME("utmath")
  46. /*
  47. * Support for double-precision integer divide. This code is included here
  48. * in order to support kernel environments where the double-precision math
  49. * library is not available.
  50. */
  51. #ifndef ACPI_USE_NATIVE_DIVIDE
  52. /*******************************************************************************
  53. *
  54. * FUNCTION: acpi_ut_short_divide
  55. *
  56. * PARAMETERS: Dividend - 64-bit dividend
  57. * Divisor - 32-bit divisor
  58. * out_quotient - Pointer to where the quotient is returned
  59. * out_remainder - Pointer to where the remainder is returned
  60. *
  61. * RETURN: Status (Checks for divide-by-zero)
  62. *
  63. * DESCRIPTION: Perform a short (maximum 64 bits divided by 32 bits)
  64. * divide and modulo. The result is a 64-bit quotient and a
  65. * 32-bit remainder.
  66. *
  67. ******************************************************************************/
  68. acpi_status
  69. acpi_ut_short_divide(acpi_integer dividend,
  70. u32 divisor,
  71. acpi_integer * out_quotient, u32 * out_remainder)
  72. {
  73. union uint64_overlay dividend_ovl;
  74. union uint64_overlay quotient;
  75. u32 remainder32;
  76. ACPI_FUNCTION_TRACE(ut_short_divide);
  77. /* Always check for a zero divisor */
  78. if (divisor == 0) {
  79. ACPI_ERROR((AE_INFO, "Divide by zero"));
  80. return_ACPI_STATUS(AE_AML_DIVIDE_BY_ZERO);
  81. }
  82. dividend_ovl.full = dividend;
  83. /*
  84. * The quotient is 64 bits, the remainder is always 32 bits,
  85. * and is generated by the second divide.
  86. */
  87. ACPI_DIV_64_BY_32(0, dividend_ovl.part.hi, divisor,
  88. quotient.part.hi, remainder32);
  89. ACPI_DIV_64_BY_32(remainder32, dividend_ovl.part.lo, divisor,
  90. quotient.part.lo, remainder32);
  91. /* Return only what was requested */
  92. if (out_quotient) {
  93. *out_quotient = quotient.full;
  94. }
  95. if (out_remainder) {
  96. *out_remainder = remainder32;
  97. }
  98. return_ACPI_STATUS(AE_OK);
  99. }
  100. /*******************************************************************************
  101. *
  102. * FUNCTION: acpi_ut_divide
  103. *
  104. * PARAMETERS: in_dividend - Dividend
  105. * in_divisor - Divisor
  106. * out_quotient - Pointer to where the quotient is returned
  107. * out_remainder - Pointer to where the remainder is returned
  108. *
  109. * RETURN: Status (Checks for divide-by-zero)
  110. *
  111. * DESCRIPTION: Perform a divide and modulo.
  112. *
  113. ******************************************************************************/
  114. acpi_status
  115. acpi_ut_divide(acpi_integer in_dividend,
  116. acpi_integer in_divisor,
  117. acpi_integer * out_quotient, acpi_integer * out_remainder)
  118. {
  119. union uint64_overlay dividend;
  120. union uint64_overlay divisor;
  121. union uint64_overlay quotient;
  122. union uint64_overlay remainder;
  123. union uint64_overlay normalized_dividend;
  124. union uint64_overlay normalized_divisor;
  125. u32 partial1;
  126. union uint64_overlay partial2;
  127. union uint64_overlay partial3;
  128. ACPI_FUNCTION_TRACE(ut_divide);
  129. /* Always check for a zero divisor */
  130. if (in_divisor == 0) {
  131. ACPI_ERROR((AE_INFO, "Divide by zero"));
  132. return_ACPI_STATUS(AE_AML_DIVIDE_BY_ZERO);
  133. }
  134. divisor.full = in_divisor;
  135. dividend.full = in_dividend;
  136. if (divisor.part.hi == 0) {
  137. /*
  138. * 1) Simplest case is where the divisor is 32 bits, we can
  139. * just do two divides
  140. */
  141. remainder.part.hi = 0;
  142. /*
  143. * The quotient is 64 bits, the remainder is always 32 bits,
  144. * and is generated by the second divide.
  145. */
  146. ACPI_DIV_64_BY_32(0, dividend.part.hi, divisor.part.lo,
  147. quotient.part.hi, partial1);
  148. ACPI_DIV_64_BY_32(partial1, dividend.part.lo, divisor.part.lo,
  149. quotient.part.lo, remainder.part.lo);
  150. }
  151. else {
  152. /*
  153. * 2) The general case where the divisor is a full 64 bits
  154. * is more difficult
  155. */
  156. quotient.part.hi = 0;
  157. normalized_dividend = dividend;
  158. normalized_divisor = divisor;
  159. /* Normalize the operands (shift until the divisor is < 32 bits) */
  160. do {
  161. ACPI_SHIFT_RIGHT_64(normalized_divisor.part.hi,
  162. normalized_divisor.part.lo);
  163. ACPI_SHIFT_RIGHT_64(normalized_dividend.part.hi,
  164. normalized_dividend.part.lo);
  165. } while (normalized_divisor.part.hi != 0);
  166. /* Partial divide */
  167. ACPI_DIV_64_BY_32(normalized_dividend.part.hi,
  168. normalized_dividend.part.lo,
  169. normalized_divisor.part.lo,
  170. quotient.part.lo, partial1);
  171. /*
  172. * The quotient is always 32 bits, and simply requires adjustment.
  173. * The 64-bit remainder must be generated.
  174. */
  175. partial1 = quotient.part.lo * divisor.part.hi;
  176. partial2.full =
  177. (acpi_integer) quotient.part.lo * divisor.part.lo;
  178. partial3.full = (acpi_integer) partial2.part.hi + partial1;
  179. remainder.part.hi = partial3.part.lo;
  180. remainder.part.lo = partial2.part.lo;
  181. if (partial3.part.hi == 0) {
  182. if (partial3.part.lo >= dividend.part.hi) {
  183. if (partial3.part.lo == dividend.part.hi) {
  184. if (partial2.part.lo > dividend.part.lo) {
  185. quotient.part.lo--;
  186. remainder.full -= divisor.full;
  187. }
  188. } else {
  189. quotient.part.lo--;
  190. remainder.full -= divisor.full;
  191. }
  192. }
  193. remainder.full = remainder.full - dividend.full;
  194. remainder.part.hi = (u32) - ((s32) remainder.part.hi);
  195. remainder.part.lo = (u32) - ((s32) remainder.part.lo);
  196. if (remainder.part.lo) {
  197. remainder.part.hi--;
  198. }
  199. }
  200. }
  201. /* Return only what was requested */
  202. if (out_quotient) {
  203. *out_quotient = quotient.full;
  204. }
  205. if (out_remainder) {
  206. *out_remainder = remainder.full;
  207. }
  208. return_ACPI_STATUS(AE_OK);
  209. }
  210. #else
  211. /*******************************************************************************
  212. *
  213. * FUNCTION: acpi_ut_short_divide, acpi_ut_divide
  214. *
  215. * PARAMETERS: See function headers above
  216. *
  217. * DESCRIPTION: Native versions of the ut_divide functions. Use these if either
  218. * 1) The target is a 64-bit platform and therefore 64-bit
  219. * integer math is supported directly by the machine.
  220. * 2) The target is a 32-bit or 16-bit platform, and the
  221. * double-precision integer math library is available to
  222. * perform the divide.
  223. *
  224. ******************************************************************************/
  225. acpi_status
  226. acpi_ut_short_divide(acpi_integer in_dividend,
  227. u32 divisor,
  228. acpi_integer * out_quotient, u32 * out_remainder)
  229. {
  230. ACPI_FUNCTION_TRACE(ut_short_divide);
  231. /* Always check for a zero divisor */
  232. if (divisor == 0) {
  233. ACPI_ERROR((AE_INFO, "Divide by zero"));
  234. return_ACPI_STATUS(AE_AML_DIVIDE_BY_ZERO);
  235. }
  236. /* Return only what was requested */
  237. if (out_quotient) {
  238. *out_quotient = in_dividend / divisor;
  239. }
  240. if (out_remainder) {
  241. *out_remainder = (u32) (in_dividend % divisor);
  242. }
  243. return_ACPI_STATUS(AE_OK);
  244. }
  245. acpi_status
  246. acpi_ut_divide(acpi_integer in_dividend,
  247. acpi_integer in_divisor,
  248. acpi_integer * out_quotient, acpi_integer * out_remainder)
  249. {
  250. ACPI_FUNCTION_TRACE(ut_divide);
  251. /* Always check for a zero divisor */
  252. if (in_divisor == 0) {
  253. ACPI_ERROR((AE_INFO, "Divide by zero"));
  254. return_ACPI_STATUS(AE_AML_DIVIDE_BY_ZERO);
  255. }
  256. /* Return only what was requested */
  257. if (out_quotient) {
  258. *out_quotient = in_dividend / in_divisor;
  259. }
  260. if (out_remainder) {
  261. *out_remainder = in_dividend % in_divisor;
  262. }
  263. return_ACPI_STATUS(AE_OK);
  264. }
  265. #endif