1 /*******************************************************************************
3 * Module Name: utmath - Integer math support routines
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2014, 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.
46 #include <contrib/dev/acpica/include/acpi.h>
47 #include <contrib/dev/acpica/include/accommon.h>
50 #define _COMPONENT ACPI_UTILITIES
51 ACPI_MODULE_NAME ("utmath")
54 * Optional support for 64-bit double-precision integer divide. This code
55 * is configurable and is implemented in order to support 32-bit kernel
56 * environments where a 64-bit double-precision math library is not available.
58 * Support for a more normal 64-bit divide/modulo (with check for a divide-
59 * by-zero) appears after this optional section of code.
61 #ifndef ACPI_USE_NATIVE_DIVIDE
63 /* Structures used only for 64-bit divide */
65 typedef struct uint64_struct
72 typedef union uint64_overlay
80 /*******************************************************************************
82 * FUNCTION: AcpiUtShortDivide
84 * PARAMETERS: Dividend - 64-bit dividend
85 * Divisor - 32-bit divisor
86 * OutQuotient - Pointer to where the quotient is returned
87 * OutRemainder - Pointer to where the remainder is returned
89 * RETURN: Status (Checks for divide-by-zero)
91 * DESCRIPTION: Perform a short (maximum 64 bits divided by 32 bits)
92 * divide and modulo. The result is a 64-bit quotient and a
95 ******************************************************************************/
102 UINT32 *OutRemainder)
104 UINT64_OVERLAY DividendOvl;
105 UINT64_OVERLAY Quotient;
109 ACPI_FUNCTION_TRACE (UtShortDivide);
112 /* Always check for a zero divisor */
116 ACPI_ERROR ((AE_INFO, "Divide by zero"));
117 return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO);
120 DividendOvl.Full = Dividend;
123 * The quotient is 64 bits, the remainder is always 32 bits,
124 * and is generated by the second divide.
126 ACPI_DIV_64_BY_32 (0, DividendOvl.Part.Hi, Divisor,
127 Quotient.Part.Hi, Remainder32);
128 ACPI_DIV_64_BY_32 (Remainder32, DividendOvl.Part.Lo, Divisor,
129 Quotient.Part.Lo, Remainder32);
131 /* Return only what was requested */
135 *OutQuotient = Quotient.Full;
139 *OutRemainder = Remainder32;
142 return_ACPI_STATUS (AE_OK);
146 /*******************************************************************************
148 * FUNCTION: AcpiUtDivide
150 * PARAMETERS: InDividend - Dividend
151 * InDivisor - Divisor
152 * OutQuotient - Pointer to where the quotient is returned
153 * OutRemainder - Pointer to where the remainder is returned
155 * RETURN: Status (Checks for divide-by-zero)
157 * DESCRIPTION: Perform a divide and modulo.
159 ******************************************************************************/
166 UINT64 *OutRemainder)
168 UINT64_OVERLAY Dividend;
169 UINT64_OVERLAY Divisor;
170 UINT64_OVERLAY Quotient;
171 UINT64_OVERLAY Remainder;
172 UINT64_OVERLAY NormalizedDividend;
173 UINT64_OVERLAY NormalizedDivisor;
175 UINT64_OVERLAY Partial2;
176 UINT64_OVERLAY Partial3;
179 ACPI_FUNCTION_TRACE (UtDivide);
182 /* Always check for a zero divisor */
186 ACPI_ERROR ((AE_INFO, "Divide by zero"));
187 return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO);
190 Divisor.Full = InDivisor;
191 Dividend.Full = InDividend;
192 if (Divisor.Part.Hi == 0)
195 * 1) Simplest case is where the divisor is 32 bits, we can
196 * just do two divides
198 Remainder.Part.Hi = 0;
201 * The quotient is 64 bits, the remainder is always 32 bits,
202 * and is generated by the second divide.
204 ACPI_DIV_64_BY_32 (0, Dividend.Part.Hi, Divisor.Part.Lo,
205 Quotient.Part.Hi, Partial1);
206 ACPI_DIV_64_BY_32 (Partial1, Dividend.Part.Lo, Divisor.Part.Lo,
207 Quotient.Part.Lo, Remainder.Part.Lo);
213 * 2) The general case where the divisor is a full 64 bits
216 Quotient.Part.Hi = 0;
217 NormalizedDividend = Dividend;
218 NormalizedDivisor = Divisor;
220 /* Normalize the operands (shift until the divisor is < 32 bits) */
224 ACPI_SHIFT_RIGHT_64 (NormalizedDivisor.Part.Hi,
225 NormalizedDivisor.Part.Lo);
226 ACPI_SHIFT_RIGHT_64 (NormalizedDividend.Part.Hi,
227 NormalizedDividend.Part.Lo);
229 } while (NormalizedDivisor.Part.Hi != 0);
233 ACPI_DIV_64_BY_32 (NormalizedDividend.Part.Hi,
234 NormalizedDividend.Part.Lo,
235 NormalizedDivisor.Part.Lo,
236 Quotient.Part.Lo, Partial1);
239 * The quotient is always 32 bits, and simply requires adjustment.
240 * The 64-bit remainder must be generated.
242 Partial1 = Quotient.Part.Lo * Divisor.Part.Hi;
243 Partial2.Full = (UINT64) Quotient.Part.Lo * Divisor.Part.Lo;
244 Partial3.Full = (UINT64) Partial2.Part.Hi + Partial1;
246 Remainder.Part.Hi = Partial3.Part.Lo;
247 Remainder.Part.Lo = Partial2.Part.Lo;
249 if (Partial3.Part.Hi == 0)
251 if (Partial3.Part.Lo >= Dividend.Part.Hi)
253 if (Partial3.Part.Lo == Dividend.Part.Hi)
255 if (Partial2.Part.Lo > Dividend.Part.Lo)
258 Remainder.Full -= Divisor.Full;
264 Remainder.Full -= Divisor.Full;
268 Remainder.Full = Remainder.Full - Dividend.Full;
269 Remainder.Part.Hi = (UINT32) -((INT32) Remainder.Part.Hi);
270 Remainder.Part.Lo = (UINT32) -((INT32) Remainder.Part.Lo);
272 if (Remainder.Part.Lo)
279 /* Return only what was requested */
283 *OutQuotient = Quotient.Full;
287 *OutRemainder = Remainder.Full;
290 return_ACPI_STATUS (AE_OK);
295 /*******************************************************************************
297 * FUNCTION: AcpiUtShortDivide, AcpiUtDivide
299 * PARAMETERS: See function headers above
301 * DESCRIPTION: Native versions of the UtDivide functions. Use these if either
302 * 1) The target is a 64-bit platform and therefore 64-bit
303 * integer math is supported directly by the machine.
304 * 2) The target is a 32-bit or 16-bit platform, and the
305 * double-precision integer math library is available to
306 * perform the divide.
308 ******************************************************************************/
315 UINT32 *OutRemainder)
318 ACPI_FUNCTION_TRACE (UtShortDivide);
321 /* Always check for a zero divisor */
325 ACPI_ERROR ((AE_INFO, "Divide by zero"));
326 return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO);
329 /* Return only what was requested */
333 *OutQuotient = InDividend / Divisor;
337 *OutRemainder = (UINT32) (InDividend % Divisor);
340 return_ACPI_STATUS (AE_OK);
348 UINT64 *OutRemainder)
350 ACPI_FUNCTION_TRACE (UtDivide);
353 /* Always check for a zero divisor */
357 ACPI_ERROR ((AE_INFO, "Divide by zero"));
358 return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO);
362 /* Return only what was requested */
366 *OutQuotient = InDividend / InDivisor;
370 *OutRemainder = InDividend % InDivisor;
373 return_ACPI_STATUS (AE_OK);