1 /*******************************************************************************
3 * Module Name: utmath - Integer math support routines
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2013, 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.
51 #define _COMPONENT ACPI_UTILITIES
52 ACPI_MODULE_NAME ("utmath")
55 * Optional support for 64-bit double-precision integer divide. This code
56 * is configurable and is implemented in order to support 32-bit kernel
57 * environments where a 64-bit double-precision math library is not available.
59 * Support for a more normal 64-bit divide/modulo (with check for a divide-
60 * by-zero) appears after this optional section of code.
62 #ifndef ACPI_USE_NATIVE_DIVIDE
64 /* Structures used only for 64-bit divide */
66 typedef struct uint64_struct
73 typedef union uint64_overlay
81 /*******************************************************************************
83 * FUNCTION: AcpiUtShortDivide
85 * PARAMETERS: Dividend - 64-bit dividend
86 * Divisor - 32-bit divisor
87 * OutQuotient - Pointer to where the quotient is returned
88 * OutRemainder - Pointer to where the remainder is returned
90 * RETURN: Status (Checks for divide-by-zero)
92 * DESCRIPTION: Perform a short (maximum 64 bits divided by 32 bits)
93 * divide and modulo. The result is a 64-bit quotient and a
96 ******************************************************************************/
103 UINT32
*OutRemainder
)
105 UINT64_OVERLAY DividendOvl
;
106 UINT64_OVERLAY Quotient
;
110 ACPI_FUNCTION_TRACE (UtShortDivide
);
113 /* Always check for a zero divisor */
117 ACPI_ERROR ((AE_INFO
, "Divide by zero"));
118 return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO
);
121 DividendOvl
.Full
= Dividend
;
124 * The quotient is 64 bits, the remainder is always 32 bits,
125 * and is generated by the second divide.
127 ACPI_DIV_64_BY_32 (0, DividendOvl
.Part
.Hi
, Divisor
,
128 Quotient
.Part
.Hi
, Remainder32
);
129 ACPI_DIV_64_BY_32 (Remainder32
, DividendOvl
.Part
.Lo
, Divisor
,
130 Quotient
.Part
.Lo
, Remainder32
);
132 /* Return only what was requested */
136 *OutQuotient
= Quotient
.Full
;
140 *OutRemainder
= Remainder32
;
143 return_ACPI_STATUS (AE_OK
);
147 /*******************************************************************************
149 * FUNCTION: AcpiUtDivide
151 * PARAMETERS: InDividend - Dividend
152 * InDivisor - Divisor
153 * OutQuotient - Pointer to where the quotient is returned
154 * OutRemainder - Pointer to where the remainder is returned
156 * RETURN: Status (Checks for divide-by-zero)
158 * DESCRIPTION: Perform a divide and modulo.
160 ******************************************************************************/
167 UINT64
*OutRemainder
)
169 UINT64_OVERLAY Dividend
;
170 UINT64_OVERLAY Divisor
;
171 UINT64_OVERLAY Quotient
;
172 UINT64_OVERLAY Remainder
;
173 UINT64_OVERLAY NormalizedDividend
;
174 UINT64_OVERLAY NormalizedDivisor
;
176 UINT64_OVERLAY Partial2
;
177 UINT64_OVERLAY Partial3
;
180 ACPI_FUNCTION_TRACE (UtDivide
);
183 /* Always check for a zero divisor */
187 ACPI_ERROR ((AE_INFO
, "Divide by zero"));
188 return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO
);
191 Divisor
.Full
= InDivisor
;
192 Dividend
.Full
= InDividend
;
193 if (Divisor
.Part
.Hi
== 0)
196 * 1) Simplest case is where the divisor is 32 bits, we can
197 * just do two divides
199 Remainder
.Part
.Hi
= 0;
202 * The quotient is 64 bits, the remainder is always 32 bits,
203 * and is generated by the second divide.
205 ACPI_DIV_64_BY_32 (0, Dividend
.Part
.Hi
, Divisor
.Part
.Lo
,
206 Quotient
.Part
.Hi
, Partial1
);
207 ACPI_DIV_64_BY_32 (Partial1
, Dividend
.Part
.Lo
, Divisor
.Part
.Lo
,
208 Quotient
.Part
.Lo
, Remainder
.Part
.Lo
);
214 * 2) The general case where the divisor is a full 64 bits
217 Quotient
.Part
.Hi
= 0;
218 NormalizedDividend
= Dividend
;
219 NormalizedDivisor
= Divisor
;
221 /* Normalize the operands (shift until the divisor is < 32 bits) */
225 ACPI_SHIFT_RIGHT_64 (NormalizedDivisor
.Part
.Hi
,
226 NormalizedDivisor
.Part
.Lo
);
227 ACPI_SHIFT_RIGHT_64 (NormalizedDividend
.Part
.Hi
,
228 NormalizedDividend
.Part
.Lo
);
230 } while (NormalizedDivisor
.Part
.Hi
!= 0);
234 ACPI_DIV_64_BY_32 (NormalizedDividend
.Part
.Hi
,
235 NormalizedDividend
.Part
.Lo
,
236 NormalizedDivisor
.Part
.Lo
,
237 Quotient
.Part
.Lo
, Partial1
);
240 * The quotient is always 32 bits, and simply requires adjustment.
241 * The 64-bit remainder must be generated.
243 Partial1
= Quotient
.Part
.Lo
* Divisor
.Part
.Hi
;
244 Partial2
.Full
= (UINT64
) Quotient
.Part
.Lo
* Divisor
.Part
.Lo
;
245 Partial3
.Full
= (UINT64
) Partial2
.Part
.Hi
+ Partial1
;
247 Remainder
.Part
.Hi
= Partial3
.Part
.Lo
;
248 Remainder
.Part
.Lo
= Partial2
.Part
.Lo
;
250 if (Partial3
.Part
.Hi
== 0)
252 if (Partial3
.Part
.Lo
>= Dividend
.Part
.Hi
)
254 if (Partial3
.Part
.Lo
== Dividend
.Part
.Hi
)
256 if (Partial2
.Part
.Lo
> Dividend
.Part
.Lo
)
259 Remainder
.Full
-= Divisor
.Full
;
265 Remainder
.Full
-= Divisor
.Full
;
269 Remainder
.Full
= Remainder
.Full
- Dividend
.Full
;
270 Remainder
.Part
.Hi
= (UINT32
) -((INT32
) Remainder
.Part
.Hi
);
271 Remainder
.Part
.Lo
= (UINT32
) -((INT32
) Remainder
.Part
.Lo
);
273 if (Remainder
.Part
.Lo
)
280 /* Return only what was requested */
284 *OutQuotient
= Quotient
.Full
;
288 *OutRemainder
= Remainder
.Full
;
291 return_ACPI_STATUS (AE_OK
);
296 /*******************************************************************************
298 * FUNCTION: AcpiUtShortDivide, AcpiUtDivide
300 * PARAMETERS: See function headers above
302 * DESCRIPTION: Native versions of the UtDivide functions. Use these if either
303 * 1) The target is a 64-bit platform and therefore 64-bit
304 * integer math is supported directly by the machine.
305 * 2) The target is a 32-bit or 16-bit platform, and the
306 * double-precision integer math library is available to
307 * perform the divide.
309 ******************************************************************************/
316 UINT32
*OutRemainder
)
319 ACPI_FUNCTION_TRACE (UtShortDivide
);
322 /* Always check for a zero divisor */
326 ACPI_ERROR ((AE_INFO
, "Divide by zero"));
327 return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO
);
330 /* Return only what was requested */
334 *OutQuotient
= InDividend
/ Divisor
;
338 *OutRemainder
= (UINT32
) (InDividend
% Divisor
);
341 return_ACPI_STATUS (AE_OK
);
349 UINT64
*OutRemainder
)
351 ACPI_FUNCTION_TRACE (UtDivide
);
354 /* Always check for a zero divisor */
358 ACPI_ERROR ((AE_INFO
, "Divide by zero"));
359 return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO
);
363 /* Return only what was requested */
367 *OutQuotient
= InDividend
/ InDivisor
;
371 *OutRemainder
= InDividend
% InDivisor
;
374 return_ACPI_STATUS (AE_OK
);