Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / acpi / acpica / utmath.c
blobeddf719904331a402c3c577dbed20fbab7ab33f9
1 /*******************************************************************************
3 * Module Name: utmath - Integer math support routines
5 ******************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2018, Intel Corp.
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
30 * NO WARRANTY
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
44 #include <acpi/acpi.h>
45 #include "accommon.h"
47 #define _COMPONENT ACPI_UTILITIES
48 ACPI_MODULE_NAME("utmath")
50 /* Structures used only for 64-bit divide */
51 typedef struct uint64_struct {
52 u32 lo;
53 u32 hi;
55 } uint64_struct;
57 typedef union uint64_overlay {
58 u64 full;
59 struct uint64_struct part;
61 } uint64_overlay;
64 * Optional support for 64-bit double-precision integer multiply and shift.
65 * This code is configurable and is implemented in order to support 32-bit
66 * kernel environments where a 64-bit double-precision math library is not
67 * available.
69 #ifndef ACPI_USE_NATIVE_MATH64
71 /*******************************************************************************
73 * FUNCTION: acpi_ut_short_multiply
75 * PARAMETERS: multiplicand - 64-bit multiplicand
76 * multiplier - 32-bit multiplier
77 * out_product - Pointer to where the product is returned
79 * DESCRIPTION: Perform a short multiply.
81 ******************************************************************************/
83 acpi_status
84 acpi_ut_short_multiply(u64 multiplicand, u32 multiplier, u64 *out_product)
86 union uint64_overlay multiplicand_ovl;
87 union uint64_overlay product;
88 u32 carry32;
90 ACPI_FUNCTION_TRACE(ut_short_multiply);
92 multiplicand_ovl.full = multiplicand;
95 * The Product is 64 bits, the carry is always 32 bits,
96 * and is generated by the second multiply.
98 ACPI_MUL_64_BY_32(0, multiplicand_ovl.part.hi, multiplier,
99 product.part.hi, carry32);
101 ACPI_MUL_64_BY_32(0, multiplicand_ovl.part.lo, multiplier,
102 product.part.lo, carry32);
104 product.part.hi += carry32;
106 /* Return only what was requested */
108 if (out_product) {
109 *out_product = product.full;
112 return_ACPI_STATUS(AE_OK);
115 /*******************************************************************************
117 * FUNCTION: acpi_ut_short_shift_left
119 * PARAMETERS: operand - 64-bit shift operand
120 * count - 32-bit shift count
121 * out_result - Pointer to where the result is returned
123 * DESCRIPTION: Perform a short left shift.
125 ******************************************************************************/
127 acpi_status acpi_ut_short_shift_left(u64 operand, u32 count, u64 *out_result)
129 union uint64_overlay operand_ovl;
131 ACPI_FUNCTION_TRACE(ut_short_shift_left);
133 operand_ovl.full = operand;
135 if ((count & 63) >= 32) {
136 operand_ovl.part.hi = operand_ovl.part.lo;
137 operand_ovl.part.lo = 0;
138 count = (count & 63) - 32;
140 ACPI_SHIFT_LEFT_64_BY_32(operand_ovl.part.hi,
141 operand_ovl.part.lo, count);
143 /* Return only what was requested */
145 if (out_result) {
146 *out_result = operand_ovl.full;
149 return_ACPI_STATUS(AE_OK);
152 /*******************************************************************************
154 * FUNCTION: acpi_ut_short_shift_right
156 * PARAMETERS: operand - 64-bit shift operand
157 * count - 32-bit shift count
158 * out_result - Pointer to where the result is returned
160 * DESCRIPTION: Perform a short right shift.
162 ******************************************************************************/
164 acpi_status acpi_ut_short_shift_right(u64 operand, u32 count, u64 *out_result)
166 union uint64_overlay operand_ovl;
168 ACPI_FUNCTION_TRACE(ut_short_shift_right);
170 operand_ovl.full = operand;
172 if ((count & 63) >= 32) {
173 operand_ovl.part.lo = operand_ovl.part.hi;
174 operand_ovl.part.hi = 0;
175 count = (count & 63) - 32;
177 ACPI_SHIFT_RIGHT_64_BY_32(operand_ovl.part.hi,
178 operand_ovl.part.lo, count);
180 /* Return only what was requested */
182 if (out_result) {
183 *out_result = operand_ovl.full;
186 return_ACPI_STATUS(AE_OK);
188 #else
190 /*******************************************************************************
192 * FUNCTION: acpi_ut_short_multiply
194 * PARAMETERS: See function headers above
196 * DESCRIPTION: Native version of the ut_short_multiply function.
198 ******************************************************************************/
200 acpi_status
201 acpi_ut_short_multiply(u64 multiplicand, u32 multiplier, u64 *out_product)
204 ACPI_FUNCTION_TRACE(ut_short_multiply);
206 /* Return only what was requested */
208 if (out_product) {
209 *out_product = multiplicand * multiplier;
212 return_ACPI_STATUS(AE_OK);
215 /*******************************************************************************
217 * FUNCTION: acpi_ut_short_shift_left
219 * PARAMETERS: See function headers above
221 * DESCRIPTION: Native version of the ut_short_shift_left function.
223 ******************************************************************************/
225 acpi_status acpi_ut_short_shift_left(u64 operand, u32 count, u64 *out_result)
228 ACPI_FUNCTION_TRACE(ut_short_shift_left);
230 /* Return only what was requested */
232 if (out_result) {
233 *out_result = operand << count;
236 return_ACPI_STATUS(AE_OK);
239 /*******************************************************************************
241 * FUNCTION: acpi_ut_short_shift_right
243 * PARAMETERS: See function headers above
245 * DESCRIPTION: Native version of the ut_short_shift_right function.
247 ******************************************************************************/
249 acpi_status acpi_ut_short_shift_right(u64 operand, u32 count, u64 *out_result)
252 ACPI_FUNCTION_TRACE(ut_short_shift_right);
254 /* Return only what was requested */
256 if (out_result) {
257 *out_result = operand >> count;
260 return_ACPI_STATUS(AE_OK);
262 #endif
265 * Optional support for 64-bit double-precision integer divide. This code
266 * is configurable and is implemented in order to support 32-bit kernel
267 * environments where a 64-bit double-precision math library is not available.
269 * Support for a more normal 64-bit divide/modulo (with check for a divide-
270 * by-zero) appears after this optional section of code.
272 #ifndef ACPI_USE_NATIVE_DIVIDE
274 /*******************************************************************************
276 * FUNCTION: acpi_ut_short_divide
278 * PARAMETERS: dividend - 64-bit dividend
279 * divisor - 32-bit divisor
280 * out_quotient - Pointer to where the quotient is returned
281 * out_remainder - Pointer to where the remainder is returned
283 * RETURN: Status (Checks for divide-by-zero)
285 * DESCRIPTION: Perform a short (maximum 64 bits divided by 32 bits)
286 * divide and modulo. The result is a 64-bit quotient and a
287 * 32-bit remainder.
289 ******************************************************************************/
291 acpi_status
292 acpi_ut_short_divide(u64 dividend,
293 u32 divisor, u64 *out_quotient, u32 *out_remainder)
295 union uint64_overlay dividend_ovl;
296 union uint64_overlay quotient;
297 u32 remainder32;
299 ACPI_FUNCTION_TRACE(ut_short_divide);
301 /* Always check for a zero divisor */
303 if (divisor == 0) {
304 ACPI_ERROR((AE_INFO, "Divide by zero"));
305 return_ACPI_STATUS(AE_AML_DIVIDE_BY_ZERO);
308 dividend_ovl.full = dividend;
311 * The quotient is 64 bits, the remainder is always 32 bits,
312 * and is generated by the second divide.
314 ACPI_DIV_64_BY_32(0, dividend_ovl.part.hi, divisor,
315 quotient.part.hi, remainder32);
317 ACPI_DIV_64_BY_32(remainder32, dividend_ovl.part.lo, divisor,
318 quotient.part.lo, remainder32);
320 /* Return only what was requested */
322 if (out_quotient) {
323 *out_quotient = quotient.full;
325 if (out_remainder) {
326 *out_remainder = remainder32;
329 return_ACPI_STATUS(AE_OK);
332 /*******************************************************************************
334 * FUNCTION: acpi_ut_divide
336 * PARAMETERS: in_dividend - Dividend
337 * in_divisor - Divisor
338 * out_quotient - Pointer to where the quotient is returned
339 * out_remainder - Pointer to where the remainder is returned
341 * RETURN: Status (Checks for divide-by-zero)
343 * DESCRIPTION: Perform a divide and modulo.
345 ******************************************************************************/
347 acpi_status
348 acpi_ut_divide(u64 in_dividend,
349 u64 in_divisor, u64 *out_quotient, u64 *out_remainder)
351 union uint64_overlay dividend;
352 union uint64_overlay divisor;
353 union uint64_overlay quotient;
354 union uint64_overlay remainder;
355 union uint64_overlay normalized_dividend;
356 union uint64_overlay normalized_divisor;
357 u32 partial1;
358 union uint64_overlay partial2;
359 union uint64_overlay partial3;
361 ACPI_FUNCTION_TRACE(ut_divide);
363 /* Always check for a zero divisor */
365 if (in_divisor == 0) {
366 ACPI_ERROR((AE_INFO, "Divide by zero"));
367 return_ACPI_STATUS(AE_AML_DIVIDE_BY_ZERO);
370 divisor.full = in_divisor;
371 dividend.full = in_dividend;
372 if (divisor.part.hi == 0) {
374 * 1) Simplest case is where the divisor is 32 bits, we can
375 * just do two divides
377 remainder.part.hi = 0;
380 * The quotient is 64 bits, the remainder is always 32 bits,
381 * and is generated by the second divide.
383 ACPI_DIV_64_BY_32(0, dividend.part.hi, divisor.part.lo,
384 quotient.part.hi, partial1);
386 ACPI_DIV_64_BY_32(partial1, dividend.part.lo, divisor.part.lo,
387 quotient.part.lo, remainder.part.lo);
390 else {
392 * 2) The general case where the divisor is a full 64 bits
393 * is more difficult
395 quotient.part.hi = 0;
396 normalized_dividend = dividend;
397 normalized_divisor = divisor;
399 /* Normalize the operands (shift until the divisor is < 32 bits) */
401 do {
402 ACPI_SHIFT_RIGHT_64(normalized_divisor.part.hi,
403 normalized_divisor.part.lo);
404 ACPI_SHIFT_RIGHT_64(normalized_dividend.part.hi,
405 normalized_dividend.part.lo);
407 } while (normalized_divisor.part.hi != 0);
409 /* Partial divide */
411 ACPI_DIV_64_BY_32(normalized_dividend.part.hi,
412 normalized_dividend.part.lo,
413 normalized_divisor.part.lo, quotient.part.lo,
414 partial1);
417 * The quotient is always 32 bits, and simply requires
418 * adjustment. The 64-bit remainder must be generated.
420 partial1 = quotient.part.lo * divisor.part.hi;
421 partial2.full = (u64) quotient.part.lo * divisor.part.lo;
422 partial3.full = (u64) partial2.part.hi + partial1;
424 remainder.part.hi = partial3.part.lo;
425 remainder.part.lo = partial2.part.lo;
427 if (partial3.part.hi == 0) {
428 if (partial3.part.lo >= dividend.part.hi) {
429 if (partial3.part.lo == dividend.part.hi) {
430 if (partial2.part.lo > dividend.part.lo) {
431 quotient.part.lo--;
432 remainder.full -= divisor.full;
434 } else {
435 quotient.part.lo--;
436 remainder.full -= divisor.full;
440 remainder.full = remainder.full - dividend.full;
441 remainder.part.hi = (u32)-((s32)remainder.part.hi);
442 remainder.part.lo = (u32)-((s32)remainder.part.lo);
444 if (remainder.part.lo) {
445 remainder.part.hi--;
450 /* Return only what was requested */
452 if (out_quotient) {
453 *out_quotient = quotient.full;
455 if (out_remainder) {
456 *out_remainder = remainder.full;
459 return_ACPI_STATUS(AE_OK);
462 #else
464 /*******************************************************************************
466 * FUNCTION: acpi_ut_short_divide, acpi_ut_divide
468 * PARAMETERS: See function headers above
470 * DESCRIPTION: Native versions of the ut_divide functions. Use these if either
471 * 1) The target is a 64-bit platform and therefore 64-bit
472 * integer math is supported directly by the machine.
473 * 2) The target is a 32-bit or 16-bit platform, and the
474 * double-precision integer math library is available to
475 * perform the divide.
477 ******************************************************************************/
479 acpi_status
480 acpi_ut_short_divide(u64 in_dividend,
481 u32 divisor, u64 *out_quotient, u32 *out_remainder)
484 ACPI_FUNCTION_TRACE(ut_short_divide);
486 /* Always check for a zero divisor */
488 if (divisor == 0) {
489 ACPI_ERROR((AE_INFO, "Divide by zero"));
490 return_ACPI_STATUS(AE_AML_DIVIDE_BY_ZERO);
493 /* Return only what was requested */
495 if (out_quotient) {
496 *out_quotient = in_dividend / divisor;
498 if (out_remainder) {
499 *out_remainder = (u32) (in_dividend % divisor);
502 return_ACPI_STATUS(AE_OK);
505 acpi_status
506 acpi_ut_divide(u64 in_dividend,
507 u64 in_divisor, u64 *out_quotient, u64 *out_remainder)
509 ACPI_FUNCTION_TRACE(ut_divide);
511 /* Always check for a zero divisor */
513 if (in_divisor == 0) {
514 ACPI_ERROR((AE_INFO, "Divide by zero"));
515 return_ACPI_STATUS(AE_AML_DIVIDE_BY_ZERO);
518 /* Return only what was requested */
520 if (out_quotient) {
521 *out_quotient = in_dividend / in_divisor;
523 if (out_remainder) {
524 *out_remainder = in_dividend % in_divisor;
527 return_ACPI_STATUS(AE_OK);
530 #endif