dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / acpi / common / utmath.c
blobaa3d762c0ddb6fc8918f5da25546fbcfe9c7e5ba
1 /*******************************************************************************
3 * Module Name: utmath - Integer math support routines
5 ******************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2016, 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.h"
45 #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
65 UINT32 Lo;
66 UINT32 Hi;
68 } UINT64_STRUCT;
70 typedef union uint64_overlay
72 UINT64 Full;
73 UINT64_STRUCT Part;
75 } 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
91 * 32-bit remainder.
93 ******************************************************************************/
95 ACPI_STATUS
96 AcpiUtShortDivide (
97 UINT64 Dividend,
98 UINT32 Divisor,
99 UINT64 *OutQuotient,
100 UINT32 *OutRemainder)
102 UINT64_OVERLAY DividendOvl;
103 UINT64_OVERLAY Quotient;
104 UINT32 Remainder32;
107 ACPI_FUNCTION_TRACE (UtShortDivide);
110 /* Always check for a zero divisor */
112 if (Divisor == 0)
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);
127 ACPI_DIV_64_BY_32 (Remainder32, DividendOvl.Part.Lo, Divisor,
128 Quotient.Part.Lo, Remainder32);
130 /* Return only what was requested */
132 if (OutQuotient)
134 *OutQuotient = Quotient.Full;
136 if (OutRemainder)
138 *OutRemainder = Remainder32;
141 return_ACPI_STATUS (AE_OK);
145 /*******************************************************************************
147 * FUNCTION: AcpiUtDivide
149 * PARAMETERS: InDividend - Dividend
150 * InDivisor - Divisor
151 * OutQuotient - Pointer to where the quotient is returned
152 * OutRemainder - Pointer to where the remainder is returned
154 * RETURN: Status (Checks for divide-by-zero)
156 * DESCRIPTION: Perform a divide and modulo.
158 ******************************************************************************/
160 ACPI_STATUS
161 AcpiUtDivide (
162 UINT64 InDividend,
163 UINT64 InDivisor,
164 UINT64 *OutQuotient,
165 UINT64 *OutRemainder)
167 UINT64_OVERLAY Dividend;
168 UINT64_OVERLAY Divisor;
169 UINT64_OVERLAY Quotient;
170 UINT64_OVERLAY Remainder;
171 UINT64_OVERLAY NormalizedDividend;
172 UINT64_OVERLAY NormalizedDivisor;
173 UINT32 Partial1;
174 UINT64_OVERLAY Partial2;
175 UINT64_OVERLAY Partial3;
178 ACPI_FUNCTION_TRACE (UtDivide);
181 /* Always check for a zero divisor */
183 if (InDivisor == 0)
185 ACPI_ERROR ((AE_INFO, "Divide by zero"));
186 return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO);
189 Divisor.Full = InDivisor;
190 Dividend.Full = InDividend;
191 if (Divisor.Part.Hi == 0)
194 * 1) Simplest case is where the divisor is 32 bits, we can
195 * just do two divides
197 Remainder.Part.Hi = 0;
200 * The quotient is 64 bits, the remainder is always 32 bits,
201 * and is generated by the second divide.
203 ACPI_DIV_64_BY_32 (0, Dividend.Part.Hi, Divisor.Part.Lo,
204 Quotient.Part.Hi, Partial1);
206 ACPI_DIV_64_BY_32 (Partial1, Dividend.Part.Lo, Divisor.Part.Lo,
207 Quotient.Part.Lo, Remainder.Part.Lo);
210 else
213 * 2) The general case where the divisor is a full 64 bits
214 * is more difficult
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 (
225 NormalizedDivisor.Part.Hi, NormalizedDivisor.Part.Lo);
226 ACPI_SHIFT_RIGHT_64 (
227 NormalizedDividend.Part.Hi, NormalizedDividend.Part.Lo);
229 } while (NormalizedDivisor.Part.Hi != 0);
231 /* Partial divide */
233 ACPI_DIV_64_BY_32 (
234 NormalizedDividend.Part.Hi, NormalizedDividend.Part.Lo,
235 NormalizedDivisor.Part.Lo, Quotient.Part.Lo, Partial1);
238 * The quotient is always 32 bits, and simply requires
239 * adjustment. The 64-bit remainder must be generated.
241 Partial1 = Quotient.Part.Lo * Divisor.Part.Hi;
242 Partial2.Full = (UINT64) Quotient.Part.Lo * Divisor.Part.Lo;
243 Partial3.Full = (UINT64) Partial2.Part.Hi + Partial1;
245 Remainder.Part.Hi = Partial3.Part.Lo;
246 Remainder.Part.Lo = Partial2.Part.Lo;
248 if (Partial3.Part.Hi == 0)
250 if (Partial3.Part.Lo >= Dividend.Part.Hi)
252 if (Partial3.Part.Lo == Dividend.Part.Hi)
254 if (Partial2.Part.Lo > Dividend.Part.Lo)
256 Quotient.Part.Lo--;
257 Remainder.Full -= Divisor.Full;
260 else
262 Quotient.Part.Lo--;
263 Remainder.Full -= Divisor.Full;
267 Remainder.Full = Remainder.Full - Dividend.Full;
268 Remainder.Part.Hi = (UINT32) -((INT32) Remainder.Part.Hi);
269 Remainder.Part.Lo = (UINT32) -((INT32) Remainder.Part.Lo);
271 if (Remainder.Part.Lo)
273 Remainder.Part.Hi--;
278 /* Return only what was requested */
280 if (OutQuotient)
282 *OutQuotient = Quotient.Full;
284 if (OutRemainder)
286 *OutRemainder = Remainder.Full;
289 return_ACPI_STATUS (AE_OK);
292 #else
294 /*******************************************************************************
296 * FUNCTION: AcpiUtShortDivide, AcpiUtDivide
298 * PARAMETERS: See function headers above
300 * DESCRIPTION: Native versions of the UtDivide functions. Use these if either
301 * 1) The target is a 64-bit platform and therefore 64-bit
302 * integer math is supported directly by the machine.
303 * 2) The target is a 32-bit or 16-bit platform, and the
304 * double-precision integer math library is available to
305 * perform the divide.
307 ******************************************************************************/
309 ACPI_STATUS
310 AcpiUtShortDivide (
311 UINT64 InDividend,
312 UINT32 Divisor,
313 UINT64 *OutQuotient,
314 UINT32 *OutRemainder)
317 ACPI_FUNCTION_TRACE (UtShortDivide);
320 /* Always check for a zero divisor */
322 if (Divisor == 0)
324 ACPI_ERROR ((AE_INFO, "Divide by zero"));
325 return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO);
328 /* Return only what was requested */
330 if (OutQuotient)
332 *OutQuotient = InDividend / Divisor;
334 if (OutRemainder)
336 *OutRemainder = (UINT32) (InDividend % Divisor);
339 return_ACPI_STATUS (AE_OK);
342 ACPI_STATUS
343 AcpiUtDivide (
344 UINT64 InDividend,
345 UINT64 InDivisor,
346 UINT64 *OutQuotient,
347 UINT64 *OutRemainder)
349 ACPI_FUNCTION_TRACE (UtDivide);
352 /* Always check for a zero divisor */
354 if (InDivisor == 0)
356 ACPI_ERROR ((AE_INFO, "Divide by zero"));
357 return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO);
361 /* Return only what was requested */
363 if (OutQuotient)
365 *OutQuotient = InDividend / InDivisor;
367 if (OutRemainder)
369 *OutRemainder = InDividend % InDivisor;
372 return_ACPI_STATUS (AE_OK);
375 #endif