utmath.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*******************************************************************************
  2. *
  3. * Module Name: utmath - Integer math support routines
  4. *
  5. ******************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2010, 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(u64 dividend,
  70. u32 divisor, u64 *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_ERROR((AE_INFO, "Divide by zero"));
  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(u64 in_dividend,
  115. u64 in_divisor, u64 *out_quotient, u64 *out_remainder)
  116. {
  117. union uint64_overlay dividend;
  118. union uint64_overlay divisor;
  119. union uint64_overlay quotient;
  120. union uint64_overlay remainder;
  121. union uint64_overlay normalized_dividend;
  122. union uint64_overlay normalized_divisor;
  123. u32 partial1;
  124. union uint64_overlay partial2;
  125. union uint64_overlay partial3;
  126. ACPI_FUNCTION_TRACE(ut_divide);
  127. /* Always check for a zero divisor */
  128. if (in_divisor == 0) {
  129. ACPI_ERROR((AE_INFO, "Divide by zero"));
  130. return_ACPI_STATUS(AE_AML_DIVIDE_BY_ZERO);
  131. }
  132. divisor.full = in_divisor;
  133. dividend.full = in_dividend;
  134. if (divisor.part.hi == 0) {
  135. /*
  136. * 1) Simplest case is where the divisor is 32 bits, we can
  137. * just do two divides
  138. */
  139. remainder.part.hi = 0;
  140. /*
  141. * The quotient is 64 bits, the remainder is always 32 bits,
  142. * and is generated by the second divide.
  143. */
  144. ACPI_DIV_64_BY_32(0, dividend.part.hi, divisor.part.lo,
  145. quotient.part.hi, partial1);
  146. ACPI_DIV_64_BY_32(partial1, dividend.part.lo, divisor.part.lo,
  147. quotient.part.lo, remainder.part.lo);
  148. }
  149. else {
  150. /*
  151. * 2) The general case where the divisor is a full 64 bits
  152. * is more difficult
  153. */
  154. quotient.part.hi = 0;
  155. normalized_dividend = dividend;
  156. normalized_divisor = divisor;
  157. /* Normalize the operands (shift until the divisor is < 32 bits) */
  158. do {
  159. ACPI_SHIFT_RIGHT_64(normalized_divisor.part.hi,
  160. normalized_divisor.part.lo);
  161. ACPI_SHIFT_RIGHT_64(normalized_dividend.part.hi,
  162. normalized_dividend.part.lo);
  163. } while (normalized_divisor.part.hi != 0);
  164. /* Partial divide */
  165. ACPI_DIV_64_BY_32(normalized_dividend.part.hi,
  166. normalized_dividend.part.lo,
  167. normalized_divisor.part.lo,
  168. quotient.part.lo, partial1);
  169. /*
  170. * The quotient is always 32 bits, and simply requires adjustment.
  171. * The 64-bit remainder must be generated.
  172. */
  173. partial1 = quotient.part.lo * divisor.part.hi;
  174. partial2.full = (u64) quotient.part.lo * divisor.part.lo;
  175. partial3.full = (u64) partial2.part.hi + partial1;
  176. remainder.part.hi = partial3.part.lo;
  177. remainder.part.lo = partial2.part.lo;
  178. if (partial3.part.hi == 0) {
  179. if (partial3.part.lo >= dividend.part.hi) {
  180. if (partial3.part.lo == dividend.part.hi) {
  181. if (partial2.part.lo > dividend.part.lo) {
  182. quotient.part.lo--;
  183. remainder.full -= divisor.full;
  184. }
  185. } else {
  186. quotient.part.lo--;
  187. remainder.full -= divisor.full;
  188. }
  189. }
  190. remainder.full = remainder.full - dividend.full;
  191. remainder.part.hi = (u32) - ((s32) remainder.part.hi);
  192. remainder.part.lo = (u32) - ((s32) remainder.part.lo);
  193. if (remainder.part.lo) {
  194. remainder.part.hi--;
  195. }
  196. }
  197. }
  198. /* Return only what was requested */
  199. if (out_quotient) {
  200. *out_quotient = quotient.full;
  201. }
  202. if (out_remainder) {
  203. *out_remainder = remainder.full;
  204. }
  205. return_ACPI_STATUS(AE_OK);
  206. }
  207. #else
  208. /*******************************************************************************
  209. *
  210. * FUNCTION: acpi_ut_short_divide, acpi_ut_divide
  211. *
  212. * PARAMETERS: See function headers above
  213. *
  214. * DESCRIPTION: Native versions of the ut_divide functions. Use these if either
  215. * 1) The target is a 64-bit platform and therefore 64-bit
  216. * integer math is supported directly by the machine.
  217. * 2) The target is a 32-bit or 16-bit platform, and the
  218. * double-precision integer math library is available to
  219. * perform the divide.
  220. *
  221. ******************************************************************************/
  222. acpi_status
  223. acpi_ut_short_divide(u64 in_dividend,
  224. u32 divisor, u64 *out_quotient, u32 *out_remainder)
  225. {
  226. ACPI_FUNCTION_TRACE(ut_short_divide);
  227. /* Always check for a zero divisor */
  228. if (divisor == 0) {
  229. ACPI_ERROR((AE_INFO, "Divide by zero"));
  230. return_ACPI_STATUS(AE_AML_DIVIDE_BY_ZERO);
  231. }
  232. /* Return only what was requested */
  233. if (out_quotient) {
  234. *out_quotient = in_dividend / divisor;
  235. }
  236. if (out_remainder) {
  237. *out_remainder = (u32) (in_dividend % divisor);
  238. }
  239. return_ACPI_STATUS(AE_OK);
  240. }
  241. acpi_status
  242. acpi_ut_divide(u64 in_dividend,
  243. u64 in_divisor, u64 *out_quotient, u64 *out_remainder)
  244. {
  245. ACPI_FUNCTION_TRACE(ut_divide);
  246. /* Always check for a zero divisor */
  247. if (in_divisor == 0) {
  248. ACPI_ERROR((AE_INFO, "Divide by zero"));
  249. return_ACPI_STATUS(AE_AML_DIVIDE_BY_ZERO);
  250. }
  251. /* Return only what was requested */
  252. if (out_quotient) {
  253. *out_quotient = in_dividend / in_divisor;
  254. }
  255. if (out_remainder) {
  256. *out_remainder = in_dividend % in_divisor;
  257. }
  258. return_ACPI_STATUS(AE_OK);
  259. }
  260. #endif