utmath.c 13 KB

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