1 /*******************************************************************************
3 * Module Name: utmath - Integer math support routines
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2017, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
48 #define _COMPONENT ACPI_UTILITIES
49 ACPI_MODULE_NAME ("utmath")
51 /* Structures used only for 64-bit divide */
53 typedef struct uint64_struct
60 typedef union uint64_overlay
68 * Optional support for 64-bit double-precision integer multiply and shift.
69 * This code is configurable and is implemented in order to support 32-bit
70 * kernel environments where a 64-bit double-precision math library is not
73 #ifndef ACPI_USE_NATIVE_MATH64
75 /*******************************************************************************
77 * FUNCTION: AcpiUtShortMultiply
79 * PARAMETERS: Multiplicand - 64-bit multiplicand
80 * Multiplier - 32-bit multiplier
81 * OutProduct - Pointer to where the product is returned
83 * DESCRIPTION: Perform a short multiply.
85 ******************************************************************************/
93 UINT64_OVERLAY MultiplicandOvl
;
94 UINT64_OVERLAY Product
;
98 ACPI_FUNCTION_TRACE (UtShortMultiply
);
101 MultiplicandOvl
.Full
= Multiplicand
;
104 * The Product is 64 bits, the carry is always 32 bits,
105 * and is generated by the second multiply.
107 ACPI_MUL_64_BY_32 (0, MultiplicandOvl
.Part
.Hi
, Multiplier
,
108 Product
.Part
.Hi
, Carry32
);
110 ACPI_MUL_64_BY_32 (0, MultiplicandOvl
.Part
.Lo
, Multiplier
,
111 Product
.Part
.Lo
, Carry32
);
113 Product
.Part
.Hi
+= Carry32
;
115 /* Return only what was requested */
119 *OutProduct
= Product
.Full
;
122 return_ACPI_STATUS (AE_OK
);
126 /*******************************************************************************
128 * FUNCTION: AcpiUtShortShiftLeft
130 * PARAMETERS: Operand - 64-bit shift operand
131 * Count - 32-bit shift count
132 * OutResult - Pointer to where the result is returned
134 * DESCRIPTION: Perform a short left shift.
136 ******************************************************************************/
139 AcpiUtShortShiftLeft (
144 UINT64_OVERLAY OperandOvl
;
147 ACPI_FUNCTION_TRACE (UtShortShiftLeft
);
150 OperandOvl
.Full
= Operand
;
152 if ((Count
& 63) >= 32)
154 OperandOvl
.Part
.Hi
= OperandOvl
.Part
.Lo
;
155 OperandOvl
.Part
.Lo
= 0;
156 Count
= (Count
& 63) - 32;
158 ACPI_SHIFT_LEFT_64_BY_32 (OperandOvl
.Part
.Hi
,
159 OperandOvl
.Part
.Lo
, Count
);
161 /* Return only what was requested */
165 *OutResult
= OperandOvl
.Full
;
168 return_ACPI_STATUS (AE_OK
);
171 /*******************************************************************************
173 * FUNCTION: AcpiUtShortShiftRight
175 * PARAMETERS: Operand - 64-bit shift operand
176 * Count - 32-bit shift count
177 * OutResult - Pointer to where the result is returned
179 * DESCRIPTION: Perform a short right shift.
181 ******************************************************************************/
184 AcpiUtShortShiftRight (
189 UINT64_OVERLAY OperandOvl
;
192 ACPI_FUNCTION_TRACE (UtShortShiftRight
);
195 OperandOvl
.Full
= Operand
;
197 if ((Count
& 63) >= 32)
199 OperandOvl
.Part
.Lo
= OperandOvl
.Part
.Hi
;
200 OperandOvl
.Part
.Hi
= 0;
201 Count
= (Count
& 63) - 32;
203 ACPI_SHIFT_RIGHT_64_BY_32 (OperandOvl
.Part
.Hi
,
204 OperandOvl
.Part
.Lo
, Count
);
206 /* Return only what was requested */
210 *OutResult
= OperandOvl
.Full
;
213 return_ACPI_STATUS (AE_OK
);
217 /*******************************************************************************
219 * FUNCTION: AcpiUtShortMultiply
221 * PARAMETERS: See function headers above
223 * DESCRIPTION: Native version of the UtShortMultiply function.
225 ******************************************************************************/
228 AcpiUtShortMultiply (
234 ACPI_FUNCTION_TRACE (UtShortMultiply
);
237 /* Return only what was requested */
241 *OutProduct
= Multiplicand
* Multiplier
;
244 return_ACPI_STATUS (AE_OK
);
247 /*******************************************************************************
249 * FUNCTION: AcpiUtShortShiftLeft
251 * PARAMETERS: See function headers above
253 * DESCRIPTION: Native version of the UtShortShiftLeft function.
255 ******************************************************************************/
258 AcpiUtShortShiftLeft (
264 ACPI_FUNCTION_TRACE (UtShortShiftLeft
);
267 /* Return only what was requested */
271 *OutResult
= Operand
<< Count
;
274 return_ACPI_STATUS (AE_OK
);
277 /*******************************************************************************
279 * FUNCTION: AcpiUtShortShiftRight
281 * PARAMETERS: See function headers above
283 * DESCRIPTION: Native version of the UtShortShiftRight function.
285 ******************************************************************************/
288 AcpiUtShortShiftRight (
294 ACPI_FUNCTION_TRACE (UtShortShiftRight
);
297 /* Return only what was requested */
301 *OutResult
= Operand
>> Count
;
304 return_ACPI_STATUS (AE_OK
);
309 * Optional support for 64-bit double-precision integer divide. This code
310 * is configurable and is implemented in order to support 32-bit kernel
311 * environments where a 64-bit double-precision math library is not available.
313 * Support for a more normal 64-bit divide/modulo (with check for a divide-
314 * by-zero) appears after this optional section of code.
316 #ifndef ACPI_USE_NATIVE_DIVIDE
319 /*******************************************************************************
321 * FUNCTION: AcpiUtShortDivide
323 * PARAMETERS: Dividend - 64-bit dividend
324 * Divisor - 32-bit divisor
325 * OutQuotient - Pointer to where the quotient is returned
326 * OutRemainder - Pointer to where the remainder is returned
328 * RETURN: Status (Checks for divide-by-zero)
330 * DESCRIPTION: Perform a short (maximum 64 bits divided by 32 bits)
331 * divide and modulo. The result is a 64-bit quotient and a
334 ******************************************************************************/
341 UINT32
*OutRemainder
)
343 UINT64_OVERLAY DividendOvl
;
344 UINT64_OVERLAY Quotient
;
348 ACPI_FUNCTION_TRACE (UtShortDivide
);
351 /* Always check for a zero divisor */
355 ACPI_ERROR ((AE_INFO
, "Divide by zero"));
356 return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO
);
359 DividendOvl
.Full
= Dividend
;
362 * The quotient is 64 bits, the remainder is always 32 bits,
363 * and is generated by the second divide.
365 ACPI_DIV_64_BY_32 (0, DividendOvl
.Part
.Hi
, Divisor
,
366 Quotient
.Part
.Hi
, Remainder32
);
368 ACPI_DIV_64_BY_32 (Remainder32
, DividendOvl
.Part
.Lo
, Divisor
,
369 Quotient
.Part
.Lo
, Remainder32
);
371 /* Return only what was requested */
375 *OutQuotient
= Quotient
.Full
;
379 *OutRemainder
= Remainder32
;
382 return_ACPI_STATUS (AE_OK
);
386 /*******************************************************************************
388 * FUNCTION: AcpiUtDivide
390 * PARAMETERS: InDividend - Dividend
391 * InDivisor - Divisor
392 * OutQuotient - Pointer to where the quotient is returned
393 * OutRemainder - Pointer to where the remainder is returned
395 * RETURN: Status (Checks for divide-by-zero)
397 * DESCRIPTION: Perform a divide and modulo.
399 ******************************************************************************/
406 UINT64
*OutRemainder
)
408 UINT64_OVERLAY Dividend
;
409 UINT64_OVERLAY Divisor
;
410 UINT64_OVERLAY Quotient
;
411 UINT64_OVERLAY Remainder
;
412 UINT64_OVERLAY NormalizedDividend
;
413 UINT64_OVERLAY NormalizedDivisor
;
415 UINT64_OVERLAY Partial2
;
416 UINT64_OVERLAY Partial3
;
419 ACPI_FUNCTION_TRACE (UtDivide
);
422 /* Always check for a zero divisor */
426 ACPI_ERROR ((AE_INFO
, "Divide by zero"));
427 return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO
);
430 Divisor
.Full
= InDivisor
;
431 Dividend
.Full
= InDividend
;
432 if (Divisor
.Part
.Hi
== 0)
435 * 1) Simplest case is where the divisor is 32 bits, we can
436 * just do two divides
438 Remainder
.Part
.Hi
= 0;
441 * The quotient is 64 bits, the remainder is always 32 bits,
442 * and is generated by the second divide.
444 ACPI_DIV_64_BY_32 (0, Dividend
.Part
.Hi
, Divisor
.Part
.Lo
,
445 Quotient
.Part
.Hi
, Partial1
);
447 ACPI_DIV_64_BY_32 (Partial1
, Dividend
.Part
.Lo
, Divisor
.Part
.Lo
,
448 Quotient
.Part
.Lo
, Remainder
.Part
.Lo
);
454 * 2) The general case where the divisor is a full 64 bits
457 Quotient
.Part
.Hi
= 0;
458 NormalizedDividend
= Dividend
;
459 NormalizedDivisor
= Divisor
;
461 /* Normalize the operands (shift until the divisor is < 32 bits) */
465 ACPI_SHIFT_RIGHT_64 (
466 NormalizedDivisor
.Part
.Hi
, NormalizedDivisor
.Part
.Lo
);
467 ACPI_SHIFT_RIGHT_64 (
468 NormalizedDividend
.Part
.Hi
, NormalizedDividend
.Part
.Lo
);
470 } while (NormalizedDivisor
.Part
.Hi
!= 0);
475 NormalizedDividend
.Part
.Hi
, NormalizedDividend
.Part
.Lo
,
476 NormalizedDivisor
.Part
.Lo
, Quotient
.Part
.Lo
, Partial1
);
479 * The quotient is always 32 bits, and simply requires
480 * adjustment. The 64-bit remainder must be generated.
482 Partial1
= Quotient
.Part
.Lo
* Divisor
.Part
.Hi
;
483 Partial2
.Full
= (UINT64
) Quotient
.Part
.Lo
* Divisor
.Part
.Lo
;
484 Partial3
.Full
= (UINT64
) Partial2
.Part
.Hi
+ Partial1
;
486 Remainder
.Part
.Hi
= Partial3
.Part
.Lo
;
487 Remainder
.Part
.Lo
= Partial2
.Part
.Lo
;
489 if (Partial3
.Part
.Hi
== 0)
491 if (Partial3
.Part
.Lo
>= Dividend
.Part
.Hi
)
493 if (Partial3
.Part
.Lo
== Dividend
.Part
.Hi
)
495 if (Partial2
.Part
.Lo
> Dividend
.Part
.Lo
)
498 Remainder
.Full
-= Divisor
.Full
;
504 Remainder
.Full
-= Divisor
.Full
;
508 Remainder
.Full
= Remainder
.Full
- Dividend
.Full
;
509 Remainder
.Part
.Hi
= (UINT32
) -((INT32
) Remainder
.Part
.Hi
);
510 Remainder
.Part
.Lo
= (UINT32
) -((INT32
) Remainder
.Part
.Lo
);
512 if (Remainder
.Part
.Lo
)
519 /* Return only what was requested */
523 *OutQuotient
= Quotient
.Full
;
527 *OutRemainder
= Remainder
.Full
;
530 return_ACPI_STATUS (AE_OK
);
535 /*******************************************************************************
537 * FUNCTION: AcpiUtShortDivide, AcpiUtDivide
539 * PARAMETERS: See function headers above
541 * DESCRIPTION: Native versions of the UtDivide functions. Use these if either
542 * 1) The target is a 64-bit platform and therefore 64-bit
543 * integer math is supported directly by the machine.
544 * 2) The target is a 32-bit or 16-bit platform, and the
545 * double-precision integer math library is available to
546 * perform the divide.
548 ******************************************************************************/
555 UINT32
*OutRemainder
)
558 ACPI_FUNCTION_TRACE (UtShortDivide
);
561 /* Always check for a zero divisor */
565 ACPI_ERROR ((AE_INFO
, "Divide by zero"));
566 return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO
);
569 /* Return only what was requested */
573 *OutQuotient
= InDividend
/ Divisor
;
577 *OutRemainder
= (UINT32
) (InDividend
% Divisor
);
580 return_ACPI_STATUS (AE_OK
);
588 UINT64
*OutRemainder
)
590 ACPI_FUNCTION_TRACE (UtDivide
);
593 /* Always check for a zero divisor */
597 ACPI_ERROR ((AE_INFO
, "Divide by zero"));
598 return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO
);
602 /* Return only what was requested */
606 *OutQuotient
= InDividend
/ InDivisor
;
610 *OutRemainder
= InDividend
% InDivisor
;
613 return_ACPI_STATUS (AE_OK
);