utmath.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /*******************************************************************************
  2. *
  3. * Module Name: utmath - Integer math support routines
  4. *
  5. ******************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2017, 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. /* Structures used only for 64-bit divide */
  47. typedef struct uint64_struct {
  48. u32 lo;
  49. u32 hi;
  50. } uint64_struct;
  51. typedef union uint64_overlay {
  52. u64 full;
  53. struct uint64_struct part;
  54. } uint64_overlay;
  55. /*
  56. * Optional support for 64-bit double-precision integer multiply and shift.
  57. * This code is configurable and is implemented in order to support 32-bit
  58. * kernel environments where a 64-bit double-precision math library is not
  59. * available.
  60. */
  61. #ifndef ACPI_USE_NATIVE_MATH64
  62. /*******************************************************************************
  63. *
  64. * FUNCTION: acpi_ut_short_multiply
  65. *
  66. * PARAMETERS: multiplicand - 64-bit multiplicand
  67. * multiplier - 32-bit multiplier
  68. * out_product - Pointer to where the product is returned
  69. *
  70. * DESCRIPTION: Perform a short multiply.
  71. *
  72. ******************************************************************************/
  73. acpi_status
  74. acpi_ut_short_multiply(u64 multiplicand, u32 multiplier, u64 *out_product)
  75. {
  76. union uint64_overlay multiplicand_ovl;
  77. union uint64_overlay product;
  78. u32 carry32;
  79. ACPI_FUNCTION_TRACE(ut_short_multiply);
  80. multiplicand_ovl.full = multiplicand;
  81. /*
  82. * The Product is 64 bits, the carry is always 32 bits,
  83. * and is generated by the second multiply.
  84. */
  85. ACPI_MUL_64_BY_32(0, multiplicand_ovl.part.hi, multiplier,
  86. product.part.hi, carry32);
  87. ACPI_MUL_64_BY_32(0, multiplicand_ovl.part.lo, multiplier,
  88. product.part.lo, carry32);
  89. product.part.hi += carry32;
  90. /* Return only what was requested */
  91. if (out_product) {
  92. *out_product = product.full;
  93. }
  94. return_ACPI_STATUS(AE_OK);
  95. }
  96. /*******************************************************************************
  97. *
  98. * FUNCTION: acpi_ut_short_shift_left
  99. *
  100. * PARAMETERS: operand - 64-bit shift operand
  101. * count - 32-bit shift count
  102. * out_result - Pointer to where the result is returned
  103. *
  104. * DESCRIPTION: Perform a short left shift.
  105. *
  106. ******************************************************************************/
  107. acpi_status acpi_ut_short_shift_left(u64 operand, u32 count, u64 *out_result)
  108. {
  109. union uint64_overlay operand_ovl;
  110. ACPI_FUNCTION_TRACE(ut_short_shift_left);
  111. operand_ovl.full = operand;
  112. if ((count & 63) >= 32) {
  113. operand_ovl.part.hi = operand_ovl.part.lo;
  114. operand_ovl.part.lo ^= operand_ovl.part.lo;
  115. count = (count & 63) - 32;
  116. }
  117. ACPI_SHIFT_LEFT_64_BY_32(operand_ovl.part.hi,
  118. operand_ovl.part.lo, count);
  119. /* Return only what was requested */
  120. if (out_result) {
  121. *out_result = operand_ovl.full;
  122. }
  123. return_ACPI_STATUS(AE_OK);
  124. }
  125. /*******************************************************************************
  126. *
  127. * FUNCTION: acpi_ut_short_shift_right
  128. *
  129. * PARAMETERS: operand - 64-bit shift operand
  130. * count - 32-bit shift count
  131. * out_result - Pointer to where the result is returned
  132. *
  133. * DESCRIPTION: Perform a short right shift.
  134. *
  135. ******************************************************************************/
  136. acpi_status acpi_ut_short_shift_right(u64 operand, u32 count, u64 *out_result)
  137. {
  138. union uint64_overlay operand_ovl;
  139. ACPI_FUNCTION_TRACE(ut_short_shift_right);
  140. operand_ovl.full = operand;
  141. if ((count & 63) >= 32) {
  142. operand_ovl.part.lo = operand_ovl.part.hi;
  143. operand_ovl.part.hi ^= operand_ovl.part.hi;
  144. count = (count & 63) - 32;
  145. }
  146. ACPI_SHIFT_RIGHT_64_BY_32(operand_ovl.part.hi,
  147. operand_ovl.part.lo, count);
  148. /* Return only what was requested */
  149. if (out_result) {
  150. *out_result = operand_ovl.full;
  151. }
  152. return_ACPI_STATUS(AE_OK);
  153. }
  154. #else
  155. /*******************************************************************************
  156. *
  157. * FUNCTION: acpi_ut_short_multiply
  158. *
  159. * PARAMETERS: See function headers above
  160. *
  161. * DESCRIPTION: Native version of the ut_short_multiply function.
  162. *
  163. ******************************************************************************/
  164. acpi_status
  165. acpi_ut_short_multiply(u64 multiplicand, u32 multiplier, u64 *out_product)
  166. {
  167. ACPI_FUNCTION_TRACE(ut_short_multiply);
  168. /* Return only what was requested */
  169. if (out_product) {
  170. *out_product = multiplicand * multiplier;
  171. }
  172. return_ACPI_STATUS(AE_OK);
  173. }
  174. /*******************************************************************************
  175. *
  176. * FUNCTION: acpi_ut_short_shift_left
  177. *
  178. * PARAMETERS: See function headers above
  179. *
  180. * DESCRIPTION: Native version of the ut_short_shift_left function.
  181. *
  182. ******************************************************************************/
  183. acpi_status acpi_ut_short_shift_left(u64 operand, u32 count, u64 *out_result)
  184. {
  185. ACPI_FUNCTION_TRACE(ut_short_shift_left);
  186. /* Return only what was requested */
  187. if (out_result) {
  188. *out_result = operand << count;
  189. }
  190. return_ACPI_STATUS(AE_OK);
  191. }
  192. /*******************************************************************************
  193. *
  194. * FUNCTION: acpi_ut_short_shift_right
  195. *
  196. * PARAMETERS: See function headers above
  197. *
  198. * DESCRIPTION: Native version of the ut_short_shift_right function.
  199. *
  200. ******************************************************************************/
  201. acpi_status acpi_ut_short_shift_right(u64 operand, u32 count, u64 *out_result)
  202. {
  203. ACPI_FUNCTION_TRACE(ut_short_shift_right);
  204. /* Return only what was requested */
  205. if (out_result) {
  206. *out_result = operand >> count;
  207. }
  208. return_ACPI_STATUS(AE_OK);
  209. }
  210. #endif
  211. /*
  212. * Optional support for 64-bit double-precision integer divide. This code
  213. * is configurable and is implemented in order to support 32-bit kernel
  214. * environments where a 64-bit double-precision math library is not available.
  215. *
  216. * Support for a more normal 64-bit divide/modulo (with check for a divide-
  217. * by-zero) appears after this optional section of code.
  218. */
  219. #ifndef ACPI_USE_NATIVE_DIVIDE
  220. /*******************************************************************************
  221. *
  222. * FUNCTION: acpi_ut_short_divide
  223. *
  224. * PARAMETERS: dividend - 64-bit dividend
  225. * divisor - 32-bit divisor
  226. * out_quotient - Pointer to where the quotient is returned
  227. * out_remainder - Pointer to where the remainder is returned
  228. *
  229. * RETURN: Status (Checks for divide-by-zero)
  230. *
  231. * DESCRIPTION: Perform a short (maximum 64 bits divided by 32 bits)
  232. * divide and modulo. The result is a 64-bit quotient and a
  233. * 32-bit remainder.
  234. *
  235. ******************************************************************************/
  236. acpi_status
  237. acpi_ut_short_divide(u64 dividend,
  238. u32 divisor, u64 *out_quotient, u32 *out_remainder)
  239. {
  240. union uint64_overlay dividend_ovl;
  241. union uint64_overlay quotient;
  242. u32 remainder32;
  243. ACPI_FUNCTION_TRACE(ut_short_divide);
  244. /* Always check for a zero divisor */
  245. if (divisor == 0) {
  246. ACPI_ERROR((AE_INFO, "Divide by zero"));
  247. return_ACPI_STATUS(AE_AML_DIVIDE_BY_ZERO);
  248. }
  249. dividend_ovl.full = dividend;
  250. /*
  251. * The quotient is 64 bits, the remainder is always 32 bits,
  252. * and is generated by the second divide.
  253. */
  254. ACPI_DIV_64_BY_32(0, dividend_ovl.part.hi, divisor,
  255. quotient.part.hi, remainder32);
  256. ACPI_DIV_64_BY_32(remainder32, dividend_ovl.part.lo, divisor,
  257. quotient.part.lo, remainder32);
  258. /* Return only what was requested */
  259. if (out_quotient) {
  260. *out_quotient = quotient.full;
  261. }
  262. if (out_remainder) {
  263. *out_remainder = remainder32;
  264. }
  265. return_ACPI_STATUS(AE_OK);
  266. }
  267. /*******************************************************************************
  268. *
  269. * FUNCTION: acpi_ut_divide
  270. *
  271. * PARAMETERS: in_dividend - Dividend
  272. * in_divisor - Divisor
  273. * out_quotient - Pointer to where the quotient is returned
  274. * out_remainder - Pointer to where the remainder is returned
  275. *
  276. * RETURN: Status (Checks for divide-by-zero)
  277. *
  278. * DESCRIPTION: Perform a divide and modulo.
  279. *
  280. ******************************************************************************/
  281. acpi_status
  282. acpi_ut_divide(u64 in_dividend,
  283. u64 in_divisor, u64 *out_quotient, u64 *out_remainder)
  284. {
  285. union uint64_overlay dividend;
  286. union uint64_overlay divisor;
  287. union uint64_overlay quotient;
  288. union uint64_overlay remainder;
  289. union uint64_overlay normalized_dividend;
  290. union uint64_overlay normalized_divisor;
  291. u32 partial1;
  292. union uint64_overlay partial2;
  293. union uint64_overlay partial3;
  294. ACPI_FUNCTION_TRACE(ut_divide);
  295. /* Always check for a zero divisor */
  296. if (in_divisor == 0) {
  297. ACPI_ERROR((AE_INFO, "Divide by zero"));
  298. return_ACPI_STATUS(AE_AML_DIVIDE_BY_ZERO);
  299. }
  300. divisor.full = in_divisor;
  301. dividend.full = in_dividend;
  302. if (divisor.part.hi == 0) {
  303. /*
  304. * 1) Simplest case is where the divisor is 32 bits, we can
  305. * just do two divides
  306. */
  307. remainder.part.hi = 0;
  308. /*
  309. * The quotient is 64 bits, the remainder is always 32 bits,
  310. * and is generated by the second divide.
  311. */
  312. ACPI_DIV_64_BY_32(0, dividend.part.hi, divisor.part.lo,
  313. quotient.part.hi, partial1);
  314. ACPI_DIV_64_BY_32(partial1, dividend.part.lo, divisor.part.lo,
  315. quotient.part.lo, remainder.part.lo);
  316. }
  317. else {
  318. /*
  319. * 2) The general case where the divisor is a full 64 bits
  320. * is more difficult
  321. */
  322. quotient.part.hi = 0;
  323. normalized_dividend = dividend;
  324. normalized_divisor = divisor;
  325. /* Normalize the operands (shift until the divisor is < 32 bits) */
  326. do {
  327. ACPI_SHIFT_RIGHT_64(normalized_divisor.part.hi,
  328. normalized_divisor.part.lo);
  329. ACPI_SHIFT_RIGHT_64(normalized_dividend.part.hi,
  330. normalized_dividend.part.lo);
  331. } while (normalized_divisor.part.hi != 0);
  332. /* Partial divide */
  333. ACPI_DIV_64_BY_32(normalized_dividend.part.hi,
  334. normalized_dividend.part.lo,
  335. normalized_divisor.part.lo, quotient.part.lo,
  336. partial1);
  337. /*
  338. * The quotient is always 32 bits, and simply requires
  339. * adjustment. The 64-bit remainder must be generated.
  340. */
  341. partial1 = quotient.part.lo * divisor.part.hi;
  342. partial2.full = (u64) quotient.part.lo * divisor.part.lo;
  343. partial3.full = (u64) partial2.part.hi + partial1;
  344. remainder.part.hi = partial3.part.lo;
  345. remainder.part.lo = partial2.part.lo;
  346. if (partial3.part.hi == 0) {
  347. if (partial3.part.lo >= dividend.part.hi) {
  348. if (partial3.part.lo == dividend.part.hi) {
  349. if (partial2.part.lo > dividend.part.lo) {
  350. quotient.part.lo--;
  351. remainder.full -= divisor.full;
  352. }
  353. } else {
  354. quotient.part.lo--;
  355. remainder.full -= divisor.full;
  356. }
  357. }
  358. remainder.full = remainder.full - dividend.full;
  359. remainder.part.hi = (u32)-((s32)remainder.part.hi);
  360. remainder.part.lo = (u32)-((s32)remainder.part.lo);
  361. if (remainder.part.lo) {
  362. remainder.part.hi--;
  363. }
  364. }
  365. }
  366. /* Return only what was requested */
  367. if (out_quotient) {
  368. *out_quotient = quotient.full;
  369. }
  370. if (out_remainder) {
  371. *out_remainder = remainder.full;
  372. }
  373. return_ACPI_STATUS(AE_OK);
  374. }
  375. #else
  376. /*******************************************************************************
  377. *
  378. * FUNCTION: acpi_ut_short_divide, acpi_ut_divide
  379. *
  380. * PARAMETERS: See function headers above
  381. *
  382. * DESCRIPTION: Native versions of the ut_divide functions. Use these if either
  383. * 1) The target is a 64-bit platform and therefore 64-bit
  384. * integer math is supported directly by the machine.
  385. * 2) The target is a 32-bit or 16-bit platform, and the
  386. * double-precision integer math library is available to
  387. * perform the divide.
  388. *
  389. ******************************************************************************/
  390. acpi_status
  391. acpi_ut_short_divide(u64 in_dividend,
  392. u32 divisor, u64 *out_quotient, u32 *out_remainder)
  393. {
  394. ACPI_FUNCTION_TRACE(ut_short_divide);
  395. /* Always check for a zero divisor */
  396. if (divisor == 0) {
  397. ACPI_ERROR((AE_INFO, "Divide by zero"));
  398. return_ACPI_STATUS(AE_AML_DIVIDE_BY_ZERO);
  399. }
  400. /* Return only what was requested */
  401. if (out_quotient) {
  402. *out_quotient = in_dividend / divisor;
  403. }
  404. if (out_remainder) {
  405. *out_remainder = (u32) (in_dividend % divisor);
  406. }
  407. return_ACPI_STATUS(AE_OK);
  408. }
  409. acpi_status
  410. acpi_ut_divide(u64 in_dividend,
  411. u64 in_divisor, u64 *out_quotient, u64 *out_remainder)
  412. {
  413. ACPI_FUNCTION_TRACE(ut_divide);
  414. /* Always check for a zero divisor */
  415. if (in_divisor == 0) {
  416. ACPI_ERROR((AE_INFO, "Divide by zero"));
  417. return_ACPI_STATUS(AE_AML_DIVIDE_BY_ZERO);
  418. }
  419. /* Return only what was requested */
  420. if (out_quotient) {
  421. *out_quotient = in_dividend / in_divisor;
  422. }
  423. if (out_remainder) {
  424. *out_remainder = in_dividend % in_divisor;
  425. }
  426. return_ACPI_STATUS(AE_OK);
  427. }
  428. #endif