/drivers/acpi/utilities/utmath.c

https://bitbucket.org/evzijst/gittest · C · 333 lines · 152 code · 63 blank · 118 comment · 29 complexity · fea1775731ecaa1508e78af14b660169 MD5 · raw file

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