utmath.c 9.4 KB

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