blob: 16dbf665927b2596602af021b68622ac75ad0ce1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*******************************************************************************
2 *
3 * Module Name: utmath - Integer math support routines
4 *
5 ******************************************************************************/
6
7/*
Bob Moore6c9deb72007-02-02 19:48:24 +03008 * Copyright (C) 2000 - 2007, R. Byron Moore
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * All rights reserved.
10 *
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.
25 *
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.
29 *
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.
42 */
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <acpi/acpi.h>
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define _COMPONENT ACPI_UTILITIES
Len Brown4be44fc2005-08-05 00:44:28 -040047ACPI_MODULE_NAME("utmath")
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/*
50 * Support for double-precision integer divide. This code is included here
51 * in order to support kernel environments where the double-precision math
52 * library is not available.
53 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#ifndef ACPI_USE_NATIVE_DIVIDE
55/*******************************************************************************
56 *
57 * FUNCTION: acpi_ut_short_divide
58 *
59 * PARAMETERS: Dividend - 64-bit dividend
60 * Divisor - 32-bit divisor
61 * out_quotient - Pointer to where the quotient is returned
62 * out_remainder - Pointer to where the remainder is returned
63 *
64 * RETURN: Status (Checks for divide-by-zero)
65 *
66 * DESCRIPTION: Perform a short (maximum 64 bits divided by 32 bits)
67 * divide and modulo. The result is a 64-bit quotient and a
68 * 32-bit remainder.
69 *
70 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070071acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -040072acpi_ut_short_divide(acpi_integer dividend,
73 u32 divisor,
74 acpi_integer * out_quotient, u32 * out_remainder)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Len Brown4be44fc2005-08-05 00:44:28 -040076 union uint64_overlay dividend_ovl;
77 union uint64_overlay quotient;
78 u32 remainder32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Bob Mooreb229cf92006-04-21 17:15:00 -040080 ACPI_FUNCTION_TRACE(ut_short_divide);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 /* Always check for a zero divisor */
83
84 if (divisor == 0) {
Bob Mooreb8e4d892006-01-27 16:43:00 -050085 ACPI_ERROR((AE_INFO, "Divide by zero"));
Len Brown4be44fc2005-08-05 00:44:28 -040086 return_ACPI_STATUS(AE_AML_DIVIDE_BY_ZERO);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 }
88
89 dividend_ovl.full = dividend;
90
91 /*
92 * The quotient is 64 bits, the remainder is always 32 bits,
93 * and is generated by the second divide.
94 */
Len Brown4be44fc2005-08-05 00:44:28 -040095 ACPI_DIV_64_BY_32(0, dividend_ovl.part.hi, divisor,
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 quotient.part.hi, remainder32);
Len Brown4be44fc2005-08-05 00:44:28 -040097 ACPI_DIV_64_BY_32(remainder32, dividend_ovl.part.lo, divisor,
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 quotient.part.lo, remainder32);
99
100 /* Return only what was requested */
101
102 if (out_quotient) {
103 *out_quotient = quotient.full;
104 }
105 if (out_remainder) {
106 *out_remainder = remainder32;
107 }
108
Len Brown4be44fc2005-08-05 00:44:28 -0400109 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/*******************************************************************************
113 *
114 * FUNCTION: acpi_ut_divide
115 *
116 * PARAMETERS: in_dividend - Dividend
117 * in_divisor - Divisor
118 * out_quotient - Pointer to where the quotient is returned
119 * out_remainder - Pointer to where the remainder is returned
120 *
121 * RETURN: Status (Checks for divide-by-zero)
122 *
123 * DESCRIPTION: Perform a divide and modulo.
124 *
125 ******************************************************************************/
126
127acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400128acpi_ut_divide(acpi_integer in_dividend,
129 acpi_integer in_divisor,
130 acpi_integer * out_quotient, acpi_integer * out_remainder)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Len Brown4be44fc2005-08-05 00:44:28 -0400132 union uint64_overlay dividend;
133 union uint64_overlay divisor;
134 union uint64_overlay quotient;
135 union uint64_overlay remainder;
136 union uint64_overlay normalized_dividend;
137 union uint64_overlay normalized_divisor;
138 u32 partial1;
139 union uint64_overlay partial2;
140 union uint64_overlay partial3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Bob Mooreb229cf92006-04-21 17:15:00 -0400142 ACPI_FUNCTION_TRACE(ut_divide);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 /* Always check for a zero divisor */
145
146 if (in_divisor == 0) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500147 ACPI_ERROR((AE_INFO, "Divide by zero"));
Len Brown4be44fc2005-08-05 00:44:28 -0400148 return_ACPI_STATUS(AE_AML_DIVIDE_BY_ZERO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
150
Len Brown4be44fc2005-08-05 00:44:28 -0400151 divisor.full = in_divisor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 dividend.full = in_dividend;
153 if (divisor.part.hi == 0) {
154 /*
155 * 1) Simplest case is where the divisor is 32 bits, we can
156 * just do two divides
157 */
158 remainder.part.hi = 0;
159
160 /*
161 * The quotient is 64 bits, the remainder is always 32 bits,
162 * and is generated by the second divide.
163 */
Len Brown4be44fc2005-08-05 00:44:28 -0400164 ACPI_DIV_64_BY_32(0, dividend.part.hi, divisor.part.lo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 quotient.part.hi, partial1);
Len Brown4be44fc2005-08-05 00:44:28 -0400166 ACPI_DIV_64_BY_32(partial1, dividend.part.lo, divisor.part.lo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 quotient.part.lo, remainder.part.lo);
168 }
169
170 else {
171 /*
172 * 2) The general case where the divisor is a full 64 bits
173 * is more difficult
174 */
Len Brown4be44fc2005-08-05 00:44:28 -0400175 quotient.part.hi = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 normalized_dividend = dividend;
177 normalized_divisor = divisor;
178
179 /* Normalize the operands (shift until the divisor is < 32 bits) */
180
181 do {
Len Brown4be44fc2005-08-05 00:44:28 -0400182 ACPI_SHIFT_RIGHT_64(normalized_divisor.part.hi,
183 normalized_divisor.part.lo);
184 ACPI_SHIFT_RIGHT_64(normalized_dividend.part.hi,
185 normalized_dividend.part.lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 } while (normalized_divisor.part.hi != 0);
188
189 /* Partial divide */
190
Len Brown4be44fc2005-08-05 00:44:28 -0400191 ACPI_DIV_64_BY_32(normalized_dividend.part.hi,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 normalized_dividend.part.lo,
193 normalized_divisor.part.lo,
194 quotient.part.lo, partial1);
195
196 /*
197 * The quotient is always 32 bits, and simply requires adjustment.
198 * The 64-bit remainder must be generated.
199 */
Len Brown4be44fc2005-08-05 00:44:28 -0400200 partial1 = quotient.part.lo * divisor.part.hi;
201 partial2.full =
202 (acpi_integer) quotient.part.lo * divisor.part.lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 partial3.full = (acpi_integer) partial2.part.hi + partial1;
204
205 remainder.part.hi = partial3.part.lo;
206 remainder.part.lo = partial2.part.lo;
207
208 if (partial3.part.hi == 0) {
209 if (partial3.part.lo >= dividend.part.hi) {
210 if (partial3.part.lo == dividend.part.hi) {
211 if (partial2.part.lo > dividend.part.lo) {
212 quotient.part.lo--;
213 remainder.full -= divisor.full;
214 }
Len Brown4be44fc2005-08-05 00:44:28 -0400215 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 quotient.part.lo--;
217 remainder.full -= divisor.full;
218 }
219 }
220
Len Brown4be44fc2005-08-05 00:44:28 -0400221 remainder.full = remainder.full - dividend.full;
222 remainder.part.hi = (u32) - ((s32) remainder.part.hi);
223 remainder.part.lo = (u32) - ((s32) remainder.part.lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 if (remainder.part.lo) {
226 remainder.part.hi--;
227 }
228 }
229 }
230
231 /* Return only what was requested */
232
233 if (out_quotient) {
234 *out_quotient = quotient.full;
235 }
236 if (out_remainder) {
237 *out_remainder = remainder.full;
238 }
239
Len Brown4be44fc2005-08-05 00:44:28 -0400240 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
243#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244/*******************************************************************************
245 *
246 * FUNCTION: acpi_ut_short_divide, acpi_ut_divide
247 *
Robert Moore44f6c012005-04-18 22:49:35 -0400248 * PARAMETERS: See function headers above
249 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 * DESCRIPTION: Native versions of the ut_divide functions. Use these if either
251 * 1) The target is a 64-bit platform and therefore 64-bit
252 * integer math is supported directly by the machine.
253 * 2) The target is a 32-bit or 16-bit platform, and the
254 * double-precision integer math library is available to
255 * perform the divide.
256 *
257 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400259acpi_ut_short_divide(acpi_integer in_dividend,
260 u32 divisor,
261 acpi_integer * out_quotient, u32 * out_remainder)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263
Bob Mooreb229cf92006-04-21 17:15:00 -0400264 ACPI_FUNCTION_TRACE(ut_short_divide);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 /* Always check for a zero divisor */
267
268 if (divisor == 0) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500269 ACPI_ERROR((AE_INFO, "Divide by zero"));
Len Brown4be44fc2005-08-05 00:44:28 -0400270 return_ACPI_STATUS(AE_AML_DIVIDE_BY_ZERO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
272
273 /* Return only what was requested */
274
275 if (out_quotient) {
276 *out_quotient = in_dividend / divisor;
277 }
278 if (out_remainder) {
Bob Moore1f549a22008-04-10 19:06:40 +0400279 *out_remainder = (u32) (in_dividend % divisor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
281
Len Brown4be44fc2005-08-05 00:44:28 -0400282 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
285acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400286acpi_ut_divide(acpi_integer in_dividend,
287 acpi_integer in_divisor,
288 acpi_integer * out_quotient, acpi_integer * out_remainder)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Bob Mooreb229cf92006-04-21 17:15:00 -0400290 ACPI_FUNCTION_TRACE(ut_divide);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 /* Always check for a zero divisor */
293
294 if (in_divisor == 0) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500295 ACPI_ERROR((AE_INFO, "Divide by zero"));
Len Brown4be44fc2005-08-05 00:44:28 -0400296 return_ACPI_STATUS(AE_AML_DIVIDE_BY_ZERO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 /* Return only what was requested */
300
301 if (out_quotient) {
302 *out_quotient = in_dividend / in_divisor;
303 }
304 if (out_remainder) {
305 *out_remainder = in_dividend % in_divisor;
306 }
307
Len Brown4be44fc2005-08-05 00:44:28 -0400308 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
311#endif