1433d6423SLionel Sambuc /*******************************************************************************
2433d6423SLionel Sambuc *
3433d6423SLionel Sambuc * Module Name: utmath - Integer math support routines
4433d6423SLionel Sambuc *
5433d6423SLionel Sambuc ******************************************************************************/
6433d6423SLionel Sambuc
7*29492bb7SDavid van Moolenbroek /*
8*29492bb7SDavid van Moolenbroek * Copyright (C) 2000 - 2014, Intel Corp.
9433d6423SLionel Sambuc * All rights reserved.
10433d6423SLionel Sambuc *
11*29492bb7SDavid van Moolenbroek * Redistribution and use in source and binary forms, with or without
12*29492bb7SDavid van Moolenbroek * modification, are permitted provided that the following conditions
13*29492bb7SDavid van Moolenbroek * are met:
14*29492bb7SDavid van Moolenbroek * 1. Redistributions of source code must retain the above copyright
15*29492bb7SDavid van Moolenbroek * notice, this list of conditions, and the following disclaimer,
16*29492bb7SDavid van Moolenbroek * without modification.
17*29492bb7SDavid van Moolenbroek * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18*29492bb7SDavid van Moolenbroek * substantially similar to the "NO WARRANTY" disclaimer below
19*29492bb7SDavid van Moolenbroek * ("Disclaimer") and any redistribution must be conditioned upon
20*29492bb7SDavid van Moolenbroek * including a substantially similar Disclaimer requirement for further
21*29492bb7SDavid van Moolenbroek * binary redistribution.
22*29492bb7SDavid van Moolenbroek * 3. Neither the names of the above-listed copyright holders nor the names
23*29492bb7SDavid van Moolenbroek * of any contributors may be used to endorse or promote products derived
24*29492bb7SDavid van Moolenbroek * from this software without specific prior written permission.
25433d6423SLionel Sambuc *
26*29492bb7SDavid van Moolenbroek * Alternatively, this software may be distributed under the terms of the
27*29492bb7SDavid van Moolenbroek * GNU General Public License ("GPL") version 2 as published by the Free
28*29492bb7SDavid van Moolenbroek * Software Foundation.
29433d6423SLionel Sambuc *
30*29492bb7SDavid van Moolenbroek * NO WARRANTY
31*29492bb7SDavid van Moolenbroek * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32*29492bb7SDavid van Moolenbroek * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33*29492bb7SDavid van Moolenbroek * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34*29492bb7SDavid van Moolenbroek * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35*29492bb7SDavid van Moolenbroek * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36*29492bb7SDavid van Moolenbroek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37*29492bb7SDavid van Moolenbroek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38*29492bb7SDavid van Moolenbroek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39*29492bb7SDavid van Moolenbroek * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40*29492bb7SDavid van Moolenbroek * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41*29492bb7SDavid van Moolenbroek * POSSIBILITY OF SUCH DAMAGES.
42*29492bb7SDavid van Moolenbroek */
43433d6423SLionel Sambuc
44433d6423SLionel Sambuc #include "acpi.h"
45433d6423SLionel Sambuc #include "accommon.h"
46433d6423SLionel Sambuc
47433d6423SLionel Sambuc
48433d6423SLionel Sambuc #define _COMPONENT ACPI_UTILITIES
49433d6423SLionel Sambuc ACPI_MODULE_NAME ("utmath")
50433d6423SLionel Sambuc
51433d6423SLionel Sambuc /*
52*29492bb7SDavid van Moolenbroek * Optional support for 64-bit double-precision integer divide. This code
53*29492bb7SDavid van Moolenbroek * is configurable and is implemented in order to support 32-bit kernel
54*29492bb7SDavid van Moolenbroek * environments where a 64-bit double-precision math library is not available.
55*29492bb7SDavid van Moolenbroek *
56*29492bb7SDavid van Moolenbroek * Support for a more normal 64-bit divide/modulo (with check for a divide-
57*29492bb7SDavid van Moolenbroek * by-zero) appears after this optional section of code.
58433d6423SLionel Sambuc */
59433d6423SLionel Sambuc #ifndef ACPI_USE_NATIVE_DIVIDE
60*29492bb7SDavid van Moolenbroek
61*29492bb7SDavid van Moolenbroek /* Structures used only for 64-bit divide */
62*29492bb7SDavid van Moolenbroek
63*29492bb7SDavid van Moolenbroek typedef struct uint64_struct
64*29492bb7SDavid van Moolenbroek {
65*29492bb7SDavid van Moolenbroek UINT32 Lo;
66*29492bb7SDavid van Moolenbroek UINT32 Hi;
67*29492bb7SDavid van Moolenbroek
68*29492bb7SDavid van Moolenbroek } UINT64_STRUCT;
69*29492bb7SDavid van Moolenbroek
70*29492bb7SDavid van Moolenbroek typedef union uint64_overlay
71*29492bb7SDavid van Moolenbroek {
72*29492bb7SDavid van Moolenbroek UINT64 Full;
73*29492bb7SDavid van Moolenbroek UINT64_STRUCT Part;
74*29492bb7SDavid van Moolenbroek
75*29492bb7SDavid van Moolenbroek } UINT64_OVERLAY;
76*29492bb7SDavid van Moolenbroek
77*29492bb7SDavid van Moolenbroek
78433d6423SLionel Sambuc /*******************************************************************************
79433d6423SLionel Sambuc *
80433d6423SLionel Sambuc * FUNCTION: AcpiUtShortDivide
81433d6423SLionel Sambuc *
82433d6423SLionel Sambuc * PARAMETERS: Dividend - 64-bit dividend
83433d6423SLionel Sambuc * Divisor - 32-bit divisor
84433d6423SLionel Sambuc * OutQuotient - Pointer to where the quotient is returned
85433d6423SLionel Sambuc * OutRemainder - Pointer to where the remainder is returned
86433d6423SLionel Sambuc *
87433d6423SLionel Sambuc * RETURN: Status (Checks for divide-by-zero)
88433d6423SLionel Sambuc *
89433d6423SLionel Sambuc * DESCRIPTION: Perform a short (maximum 64 bits divided by 32 bits)
90433d6423SLionel Sambuc * divide and modulo. The result is a 64-bit quotient and a
91433d6423SLionel Sambuc * 32-bit remainder.
92433d6423SLionel Sambuc *
93433d6423SLionel Sambuc ******************************************************************************/
94433d6423SLionel Sambuc
95433d6423SLionel Sambuc ACPI_STATUS
AcpiUtShortDivide(UINT64 Dividend,UINT32 Divisor,UINT64 * OutQuotient,UINT32 * OutRemainder)96433d6423SLionel Sambuc AcpiUtShortDivide (
97433d6423SLionel Sambuc UINT64 Dividend,
98433d6423SLionel Sambuc UINT32 Divisor,
99433d6423SLionel Sambuc UINT64 *OutQuotient,
100433d6423SLionel Sambuc UINT32 *OutRemainder)
101433d6423SLionel Sambuc {
102433d6423SLionel Sambuc UINT64_OVERLAY DividendOvl;
103433d6423SLionel Sambuc UINT64_OVERLAY Quotient;
104433d6423SLionel Sambuc UINT32 Remainder32;
105433d6423SLionel Sambuc
106433d6423SLionel Sambuc
107433d6423SLionel Sambuc ACPI_FUNCTION_TRACE (UtShortDivide);
108433d6423SLionel Sambuc
109433d6423SLionel Sambuc
110433d6423SLionel Sambuc /* Always check for a zero divisor */
111433d6423SLionel Sambuc
112433d6423SLionel Sambuc if (Divisor == 0)
113433d6423SLionel Sambuc {
114433d6423SLionel Sambuc ACPI_ERROR ((AE_INFO, "Divide by zero"));
115433d6423SLionel Sambuc return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO);
116433d6423SLionel Sambuc }
117433d6423SLionel Sambuc
118433d6423SLionel Sambuc DividendOvl.Full = Dividend;
119433d6423SLionel Sambuc
120433d6423SLionel Sambuc /*
121433d6423SLionel Sambuc * The quotient is 64 bits, the remainder is always 32 bits,
122433d6423SLionel Sambuc * and is generated by the second divide.
123433d6423SLionel Sambuc */
124433d6423SLionel Sambuc ACPI_DIV_64_BY_32 (0, DividendOvl.Part.Hi, Divisor,
125433d6423SLionel Sambuc Quotient.Part.Hi, Remainder32);
126433d6423SLionel Sambuc ACPI_DIV_64_BY_32 (Remainder32, DividendOvl.Part.Lo, Divisor,
127433d6423SLionel Sambuc Quotient.Part.Lo, Remainder32);
128433d6423SLionel Sambuc
129433d6423SLionel Sambuc /* Return only what was requested */
130433d6423SLionel Sambuc
131433d6423SLionel Sambuc if (OutQuotient)
132433d6423SLionel Sambuc {
133433d6423SLionel Sambuc *OutQuotient = Quotient.Full;
134433d6423SLionel Sambuc }
135433d6423SLionel Sambuc if (OutRemainder)
136433d6423SLionel Sambuc {
137433d6423SLionel Sambuc *OutRemainder = Remainder32;
138433d6423SLionel Sambuc }
139433d6423SLionel Sambuc
140433d6423SLionel Sambuc return_ACPI_STATUS (AE_OK);
141433d6423SLionel Sambuc }
142433d6423SLionel Sambuc
143433d6423SLionel Sambuc
144433d6423SLionel Sambuc /*******************************************************************************
145433d6423SLionel Sambuc *
146433d6423SLionel Sambuc * FUNCTION: AcpiUtDivide
147433d6423SLionel Sambuc *
148433d6423SLionel Sambuc * PARAMETERS: InDividend - Dividend
149433d6423SLionel Sambuc * InDivisor - Divisor
150433d6423SLionel Sambuc * OutQuotient - Pointer to where the quotient is returned
151433d6423SLionel Sambuc * OutRemainder - Pointer to where the remainder is returned
152433d6423SLionel Sambuc *
153433d6423SLionel Sambuc * RETURN: Status (Checks for divide-by-zero)
154433d6423SLionel Sambuc *
155433d6423SLionel Sambuc * DESCRIPTION: Perform a divide and modulo.
156433d6423SLionel Sambuc *
157433d6423SLionel Sambuc ******************************************************************************/
158433d6423SLionel Sambuc
159433d6423SLionel Sambuc ACPI_STATUS
AcpiUtDivide(UINT64 InDividend,UINT64 InDivisor,UINT64 * OutQuotient,UINT64 * OutRemainder)160433d6423SLionel Sambuc AcpiUtDivide (
161433d6423SLionel Sambuc UINT64 InDividend,
162433d6423SLionel Sambuc UINT64 InDivisor,
163433d6423SLionel Sambuc UINT64 *OutQuotient,
164433d6423SLionel Sambuc UINT64 *OutRemainder)
165433d6423SLionel Sambuc {
166433d6423SLionel Sambuc UINT64_OVERLAY Dividend;
167433d6423SLionel Sambuc UINT64_OVERLAY Divisor;
168433d6423SLionel Sambuc UINT64_OVERLAY Quotient;
169433d6423SLionel Sambuc UINT64_OVERLAY Remainder;
170433d6423SLionel Sambuc UINT64_OVERLAY NormalizedDividend;
171433d6423SLionel Sambuc UINT64_OVERLAY NormalizedDivisor;
172433d6423SLionel Sambuc UINT32 Partial1;
173433d6423SLionel Sambuc UINT64_OVERLAY Partial2;
174433d6423SLionel Sambuc UINT64_OVERLAY Partial3;
175433d6423SLionel Sambuc
176433d6423SLionel Sambuc
177433d6423SLionel Sambuc ACPI_FUNCTION_TRACE (UtDivide);
178433d6423SLionel Sambuc
179433d6423SLionel Sambuc
180433d6423SLionel Sambuc /* Always check for a zero divisor */
181433d6423SLionel Sambuc
182433d6423SLionel Sambuc if (InDivisor == 0)
183433d6423SLionel Sambuc {
184433d6423SLionel Sambuc ACPI_ERROR ((AE_INFO, "Divide by zero"));
185433d6423SLionel Sambuc return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO);
186433d6423SLionel Sambuc }
187433d6423SLionel Sambuc
188433d6423SLionel Sambuc Divisor.Full = InDivisor;
189433d6423SLionel Sambuc Dividend.Full = InDividend;
190433d6423SLionel Sambuc if (Divisor.Part.Hi == 0)
191433d6423SLionel Sambuc {
192433d6423SLionel Sambuc /*
193433d6423SLionel Sambuc * 1) Simplest case is where the divisor is 32 bits, we can
194433d6423SLionel Sambuc * just do two divides
195433d6423SLionel Sambuc */
196433d6423SLionel Sambuc Remainder.Part.Hi = 0;
197433d6423SLionel Sambuc
198433d6423SLionel Sambuc /*
199433d6423SLionel Sambuc * The quotient is 64 bits, the remainder is always 32 bits,
200433d6423SLionel Sambuc * and is generated by the second divide.
201433d6423SLionel Sambuc */
202433d6423SLionel Sambuc ACPI_DIV_64_BY_32 (0, Dividend.Part.Hi, Divisor.Part.Lo,
203433d6423SLionel Sambuc Quotient.Part.Hi, Partial1);
204433d6423SLionel Sambuc ACPI_DIV_64_BY_32 (Partial1, Dividend.Part.Lo, Divisor.Part.Lo,
205433d6423SLionel Sambuc Quotient.Part.Lo, Remainder.Part.Lo);
206433d6423SLionel Sambuc }
207433d6423SLionel Sambuc
208433d6423SLionel Sambuc else
209433d6423SLionel Sambuc {
210433d6423SLionel Sambuc /*
211433d6423SLionel Sambuc * 2) The general case where the divisor is a full 64 bits
212433d6423SLionel Sambuc * is more difficult
213433d6423SLionel Sambuc */
214433d6423SLionel Sambuc Quotient.Part.Hi = 0;
215433d6423SLionel Sambuc NormalizedDividend = Dividend;
216433d6423SLionel Sambuc NormalizedDivisor = Divisor;
217433d6423SLionel Sambuc
218433d6423SLionel Sambuc /* Normalize the operands (shift until the divisor is < 32 bits) */
219433d6423SLionel Sambuc
220433d6423SLionel Sambuc do
221433d6423SLionel Sambuc {
222433d6423SLionel Sambuc ACPI_SHIFT_RIGHT_64 (NormalizedDivisor.Part.Hi,
223433d6423SLionel Sambuc NormalizedDivisor.Part.Lo);
224433d6423SLionel Sambuc ACPI_SHIFT_RIGHT_64 (NormalizedDividend.Part.Hi,
225433d6423SLionel Sambuc NormalizedDividend.Part.Lo);
226433d6423SLionel Sambuc
227433d6423SLionel Sambuc } while (NormalizedDivisor.Part.Hi != 0);
228433d6423SLionel Sambuc
229433d6423SLionel Sambuc /* Partial divide */
230433d6423SLionel Sambuc
231433d6423SLionel Sambuc ACPI_DIV_64_BY_32 (NormalizedDividend.Part.Hi,
232433d6423SLionel Sambuc NormalizedDividend.Part.Lo,
233433d6423SLionel Sambuc NormalizedDivisor.Part.Lo,
234433d6423SLionel Sambuc Quotient.Part.Lo, Partial1);
235433d6423SLionel Sambuc
236433d6423SLionel Sambuc /*
237433d6423SLionel Sambuc * The quotient is always 32 bits, and simply requires adjustment.
238433d6423SLionel Sambuc * The 64-bit remainder must be generated.
239433d6423SLionel Sambuc */
240433d6423SLionel Sambuc Partial1 = Quotient.Part.Lo * Divisor.Part.Hi;
241433d6423SLionel Sambuc Partial2.Full = (UINT64) Quotient.Part.Lo * Divisor.Part.Lo;
242433d6423SLionel Sambuc Partial3.Full = (UINT64) Partial2.Part.Hi + Partial1;
243433d6423SLionel Sambuc
244433d6423SLionel Sambuc Remainder.Part.Hi = Partial3.Part.Lo;
245433d6423SLionel Sambuc Remainder.Part.Lo = Partial2.Part.Lo;
246433d6423SLionel Sambuc
247433d6423SLionel Sambuc if (Partial3.Part.Hi == 0)
248433d6423SLionel Sambuc {
249433d6423SLionel Sambuc if (Partial3.Part.Lo >= Dividend.Part.Hi)
250433d6423SLionel Sambuc {
251433d6423SLionel Sambuc if (Partial3.Part.Lo == Dividend.Part.Hi)
252433d6423SLionel Sambuc {
253433d6423SLionel Sambuc if (Partial2.Part.Lo > Dividend.Part.Lo)
254433d6423SLionel Sambuc {
255433d6423SLionel Sambuc Quotient.Part.Lo--;
256433d6423SLionel Sambuc Remainder.Full -= Divisor.Full;
257433d6423SLionel Sambuc }
258433d6423SLionel Sambuc }
259433d6423SLionel Sambuc else
260433d6423SLionel Sambuc {
261433d6423SLionel Sambuc Quotient.Part.Lo--;
262433d6423SLionel Sambuc Remainder.Full -= Divisor.Full;
263433d6423SLionel Sambuc }
264433d6423SLionel Sambuc }
265433d6423SLionel Sambuc
266433d6423SLionel Sambuc Remainder.Full = Remainder.Full - Dividend.Full;
267433d6423SLionel Sambuc Remainder.Part.Hi = (UINT32) -((INT32) Remainder.Part.Hi);
268433d6423SLionel Sambuc Remainder.Part.Lo = (UINT32) -((INT32) Remainder.Part.Lo);
269433d6423SLionel Sambuc
270433d6423SLionel Sambuc if (Remainder.Part.Lo)
271433d6423SLionel Sambuc {
272433d6423SLionel Sambuc Remainder.Part.Hi--;
273433d6423SLionel Sambuc }
274433d6423SLionel Sambuc }
275433d6423SLionel Sambuc }
276433d6423SLionel Sambuc
277433d6423SLionel Sambuc /* Return only what was requested */
278433d6423SLionel Sambuc
279433d6423SLionel Sambuc if (OutQuotient)
280433d6423SLionel Sambuc {
281433d6423SLionel Sambuc *OutQuotient = Quotient.Full;
282433d6423SLionel Sambuc }
283433d6423SLionel Sambuc if (OutRemainder)
284433d6423SLionel Sambuc {
285433d6423SLionel Sambuc *OutRemainder = Remainder.Full;
286433d6423SLionel Sambuc }
287433d6423SLionel Sambuc
288433d6423SLionel Sambuc return_ACPI_STATUS (AE_OK);
289433d6423SLionel Sambuc }
290433d6423SLionel Sambuc
291433d6423SLionel Sambuc #else
292433d6423SLionel Sambuc
293433d6423SLionel Sambuc /*******************************************************************************
294433d6423SLionel Sambuc *
295433d6423SLionel Sambuc * FUNCTION: AcpiUtShortDivide, AcpiUtDivide
296433d6423SLionel Sambuc *
297433d6423SLionel Sambuc * PARAMETERS: See function headers above
298433d6423SLionel Sambuc *
299433d6423SLionel Sambuc * DESCRIPTION: Native versions of the UtDivide functions. Use these if either
300433d6423SLionel Sambuc * 1) The target is a 64-bit platform and therefore 64-bit
301433d6423SLionel Sambuc * integer math is supported directly by the machine.
302433d6423SLionel Sambuc * 2) The target is a 32-bit or 16-bit platform, and the
303433d6423SLionel Sambuc * double-precision integer math library is available to
304433d6423SLionel Sambuc * perform the divide.
305433d6423SLionel Sambuc *
306433d6423SLionel Sambuc ******************************************************************************/
307433d6423SLionel Sambuc
308433d6423SLionel Sambuc ACPI_STATUS
309433d6423SLionel Sambuc AcpiUtShortDivide (
310433d6423SLionel Sambuc UINT64 InDividend,
311433d6423SLionel Sambuc UINT32 Divisor,
312433d6423SLionel Sambuc UINT64 *OutQuotient,
313433d6423SLionel Sambuc UINT32 *OutRemainder)
314433d6423SLionel Sambuc {
315433d6423SLionel Sambuc
316433d6423SLionel Sambuc ACPI_FUNCTION_TRACE (UtShortDivide);
317433d6423SLionel Sambuc
318433d6423SLionel Sambuc
319433d6423SLionel Sambuc /* Always check for a zero divisor */
320433d6423SLionel Sambuc
321433d6423SLionel Sambuc if (Divisor == 0)
322433d6423SLionel Sambuc {
323433d6423SLionel Sambuc ACPI_ERROR ((AE_INFO, "Divide by zero"));
324433d6423SLionel Sambuc return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO);
325433d6423SLionel Sambuc }
326433d6423SLionel Sambuc
327433d6423SLionel Sambuc /* Return only what was requested */
328433d6423SLionel Sambuc
329433d6423SLionel Sambuc if (OutQuotient)
330433d6423SLionel Sambuc {
331433d6423SLionel Sambuc *OutQuotient = InDividend / Divisor;
332433d6423SLionel Sambuc }
333433d6423SLionel Sambuc if (OutRemainder)
334433d6423SLionel Sambuc {
335433d6423SLionel Sambuc *OutRemainder = (UINT32) (InDividend % Divisor);
336433d6423SLionel Sambuc }
337433d6423SLionel Sambuc
338433d6423SLionel Sambuc return_ACPI_STATUS (AE_OK);
339433d6423SLionel Sambuc }
340433d6423SLionel Sambuc
341433d6423SLionel Sambuc ACPI_STATUS
342433d6423SLionel Sambuc AcpiUtDivide (
343433d6423SLionel Sambuc UINT64 InDividend,
344433d6423SLionel Sambuc UINT64 InDivisor,
345433d6423SLionel Sambuc UINT64 *OutQuotient,
346433d6423SLionel Sambuc UINT64 *OutRemainder)
347433d6423SLionel Sambuc {
348433d6423SLionel Sambuc ACPI_FUNCTION_TRACE (UtDivide);
349433d6423SLionel Sambuc
350433d6423SLionel Sambuc
351433d6423SLionel Sambuc /* Always check for a zero divisor */
352433d6423SLionel Sambuc
353433d6423SLionel Sambuc if (InDivisor == 0)
354433d6423SLionel Sambuc {
355433d6423SLionel Sambuc ACPI_ERROR ((AE_INFO, "Divide by zero"));
356433d6423SLionel Sambuc return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO);
357433d6423SLionel Sambuc }
358433d6423SLionel Sambuc
359433d6423SLionel Sambuc
360433d6423SLionel Sambuc /* Return only what was requested */
361433d6423SLionel Sambuc
362433d6423SLionel Sambuc if (OutQuotient)
363433d6423SLionel Sambuc {
364433d6423SLionel Sambuc *OutQuotient = InDividend / InDivisor;
365433d6423SLionel Sambuc }
366433d6423SLionel Sambuc if (OutRemainder)
367433d6423SLionel Sambuc {
368433d6423SLionel Sambuc *OutRemainder = InDividend % InDivisor;
369433d6423SLionel Sambuc }
370433d6423SLionel Sambuc
371433d6423SLionel Sambuc return_ACPI_STATUS (AE_OK);
372433d6423SLionel Sambuc }
373433d6423SLionel Sambuc
374433d6423SLionel Sambuc #endif
375