1 /*******************************************************************************
3 * Module Name: utmath - Integer math support routines
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2015, 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.
44 #include <contrib/dev/acpica/include/acpi.h>
45 #include <contrib/dev/acpica/include/accommon.h>
48 #define _COMPONENT ACPI_UTILITIES
49 ACPI_MODULE_NAME ("utmath")
52 * Optional support for 64-bit double-precision integer divide. This code
53 * is configurable and is implemented in order to support 32-bit kernel
54 * environments where a 64-bit double-precision math library is not available.
56 * Support for a more normal 64-bit divide/modulo (with check for a divide-
57 * by-zero) appears after this optional section of code.
59 #ifndef ACPI_USE_NATIVE_DIVIDE
61 /* Structures used only for 64-bit divide */
63 typedef struct uint64_struct
70 typedef union uint64_overlay
78 /*******************************************************************************
80 * FUNCTION: AcpiUtShortDivide
82 * PARAMETERS: Dividend - 64-bit dividend
83 * Divisor - 32-bit divisor
84 * OutQuotient - Pointer to where the quotient is returned
85 * OutRemainder - Pointer to where the remainder is returned
87 * RETURN: Status (Checks for divide-by-zero)
89 * DESCRIPTION: Perform a short (maximum 64 bits divided by 32 bits)
90 * divide and modulo. The result is a 64-bit quotient and a
93 ******************************************************************************/
100 UINT32 *OutRemainder)
102 UINT64_OVERLAY DividendOvl;
103 UINT64_OVERLAY Quotient;
107 ACPI_FUNCTION_TRACE (UtShortDivide);
110 /* Always check for a zero divisor */
114 ACPI_ERROR ((AE_INFO, "Divide by zero"));
115 return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO);
118 DividendOvl.Full = Dividend;
121 * The quotient is 64 bits, the remainder is always 32 bits,
122 * and is generated by the second divide.
124 ACPI_DIV_64_BY_32 (0, DividendOvl.Part.Hi, Divisor,
125 Quotient.Part.Hi, Remainder32);
126 ACPI_DIV_64_BY_32 (Remainder32, DividendOvl.Part.Lo, Divisor,
127 Quotient.Part.Lo, Remainder32);
129 /* Return only what was requested */
133 *OutQuotient = Quotient.Full;
137 *OutRemainder = Remainder32;
140 return_ACPI_STATUS (AE_OK);
144 /*******************************************************************************
146 * FUNCTION: AcpiUtDivide
148 * PARAMETERS: InDividend - Dividend
149 * InDivisor - Divisor
150 * OutQuotient - Pointer to where the quotient is returned
151 * OutRemainder - Pointer to where the remainder is returned
153 * RETURN: Status (Checks for divide-by-zero)
155 * DESCRIPTION: Perform a divide and modulo.
157 ******************************************************************************/
164 UINT64 *OutRemainder)
166 UINT64_OVERLAY Dividend;
167 UINT64_OVERLAY Divisor;
168 UINT64_OVERLAY Quotient;
169 UINT64_OVERLAY Remainder;
170 UINT64_OVERLAY NormalizedDividend;
171 UINT64_OVERLAY NormalizedDivisor;
173 UINT64_OVERLAY Partial2;
174 UINT64_OVERLAY Partial3;
177 ACPI_FUNCTION_TRACE (UtDivide);
180 /* Always check for a zero divisor */
184 ACPI_ERROR ((AE_INFO, "Divide by zero"));
185 return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO);
188 Divisor.Full = InDivisor;
189 Dividend.Full = InDividend;
190 if (Divisor.Part.Hi == 0)
193 * 1) Simplest case is where the divisor is 32 bits, we can
194 * just do two divides
196 Remainder.Part.Hi = 0;
199 * The quotient is 64 bits, the remainder is always 32 bits,
200 * and is generated by the second divide.
202 ACPI_DIV_64_BY_32 (0, Dividend.Part.Hi, Divisor.Part.Lo,
203 Quotient.Part.Hi, Partial1);
204 ACPI_DIV_64_BY_32 (Partial1, Dividend.Part.Lo, Divisor.Part.Lo,
205 Quotient.Part.Lo, Remainder.Part.Lo);
211 * 2) The general case where the divisor is a full 64 bits
214 Quotient.Part.Hi = 0;
215 NormalizedDividend = Dividend;
216 NormalizedDivisor = Divisor;
218 /* Normalize the operands (shift until the divisor is < 32 bits) */
222 ACPI_SHIFT_RIGHT_64 (NormalizedDivisor.Part.Hi,
223 NormalizedDivisor.Part.Lo);
224 ACPI_SHIFT_RIGHT_64 (NormalizedDividend.Part.Hi,
225 NormalizedDividend.Part.Lo);
227 } while (NormalizedDivisor.Part.Hi != 0);
231 ACPI_DIV_64_BY_32 (NormalizedDividend.Part.Hi,
232 NormalizedDividend.Part.Lo,
233 NormalizedDivisor.Part.Lo,
234 Quotient.Part.Lo, Partial1);
237 * The quotient is always 32 bits, and simply requires adjustment.
238 * The 64-bit remainder must be generated.
240 Partial1 = Quotient.Part.Lo * Divisor.Part.Hi;
241 Partial2.Full = (UINT64) Quotient.Part.Lo * Divisor.Part.Lo;
242 Partial3.Full = (UINT64) Partial2.Part.Hi + Partial1;
244 Remainder.Part.Hi = Partial3.Part.Lo;
245 Remainder.Part.Lo = Partial2.Part.Lo;
247 if (Partial3.Part.Hi == 0)
249 if (Partial3.Part.Lo >= Dividend.Part.Hi)
251 if (Partial3.Part.Lo == Dividend.Part.Hi)
253 if (Partial2.Part.Lo > Dividend.Part.Lo)
256 Remainder.Full -= Divisor.Full;
262 Remainder.Full -= Divisor.Full;
266 Remainder.Full = Remainder.Full - Dividend.Full;
267 Remainder.Part.Hi = (UINT32) -((INT32) Remainder.Part.Hi);
268 Remainder.Part.Lo = (UINT32) -((INT32) Remainder.Part.Lo);
270 if (Remainder.Part.Lo)
277 /* Return only what was requested */
281 *OutQuotient = Quotient.Full;
285 *OutRemainder = Remainder.Full;
288 return_ACPI_STATUS (AE_OK);
293 /*******************************************************************************
295 * FUNCTION: AcpiUtShortDivide, AcpiUtDivide
297 * PARAMETERS: See function headers above
299 * DESCRIPTION: Native versions of the UtDivide functions. Use these if either
300 * 1) The target is a 64-bit platform and therefore 64-bit
301 * integer math is supported directly by the machine.
302 * 2) The target is a 32-bit or 16-bit platform, and the
303 * double-precision integer math library is available to
304 * perform the divide.
306 ******************************************************************************/
313 UINT32 *OutRemainder)
316 ACPI_FUNCTION_TRACE (UtShortDivide);
319 /* Always check for a zero divisor */
323 ACPI_ERROR ((AE_INFO, "Divide by zero"));
324 return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO);
327 /* Return only what was requested */
331 *OutQuotient = InDividend / Divisor;
335 *OutRemainder = (UINT32) (InDividend % Divisor);
338 return_ACPI_STATUS (AE_OK);
346 UINT64 *OutRemainder)
348 ACPI_FUNCTION_TRACE (UtDivide);
351 /* Always check for a zero divisor */
355 ACPI_ERROR ((AE_INFO, "Divide by zero"));
356 return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO);
360 /* Return only what was requested */
364 *OutQuotient = InDividend / InDivisor;
368 *OutRemainder = InDividend % InDivisor;
371 return_ACPI_STATUS (AE_OK);