blob: c9f682d640efb05edd8ba1b4b18116328ed3ab2b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*******************************************************************************
2 *
3 * Module Name: utmath - Integer math support routines
4 *
5 ******************************************************************************/
6
7/*
Len Brown75a44ce2008-04-23 23:00:13 -04008 * Copyright (C) 2000 - 2008, Intel Corp.
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>
Len Browne2f7a772009-01-09 00:30:03 -050045#include "accommon.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define _COMPONENT ACPI_UTILITIES
Len Brown4be44fc2005-08-05 00:44:28 -040048ACPI_MODULE_NAME("utmath")
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/*
51 * Support for double-precision integer divide. This code is included here
52 * in order to support kernel environments where the double-precision math
53 * library is not available.
54 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#ifndef ACPI_USE_NATIVE_DIVIDE
56/*******************************************************************************
57 *
58 * FUNCTION: acpi_ut_short_divide
59 *
60 * PARAMETERS: Dividend - 64-bit dividend
61 * Divisor - 32-bit divisor
62 * out_quotient - Pointer to where the quotient is returned
63 * out_remainder - Pointer to where the remainder is returned
64 *
65 * RETURN: Status (Checks for divide-by-zero)
66 *
67 * DESCRIPTION: Perform a short (maximum 64 bits divided by 32 bits)
68 * divide and modulo. The result is a 64-bit quotient and a
69 * 32-bit remainder.
70 *
71 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070072acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -040073acpi_ut_short_divide(acpi_integer dividend,
74 u32 divisor,
75 acpi_integer * out_quotient, u32 * out_remainder)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Len Brown4be44fc2005-08-05 00:44:28 -040077 union uint64_overlay dividend_ovl;
78 union uint64_overlay quotient;
79 u32 remainder32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Bob Mooreb229cf92006-04-21 17:15:00 -040081 ACPI_FUNCTION_TRACE(ut_short_divide);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 /* Always check for a zero divisor */
84
85 if (divisor == 0) {
Bob Mooreb8e4d892006-01-27 16:43:00 -050086 ACPI_ERROR((AE_INFO, "Divide by zero"));
Len Brown4be44fc2005-08-05 00:44:28 -040087 return_ACPI_STATUS(AE_AML_DIVIDE_BY_ZERO);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 }
89
90 dividend_ovl.full = dividend;
91
92 /*
93 * The quotient is 64 bits, the remainder is always 32 bits,
94 * and is generated by the second divide.
95 */
Len Brown4be44fc2005-08-05 00:44:28 -040096 ACPI_DIV_64_BY_32(0, dividend_ovl.part.hi, divisor,
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 quotient.part.hi, remainder32);
Len Brown4be44fc2005-08-05 00:44:28 -040098 ACPI_DIV_64_BY_32(remainder32, dividend_ovl.part.lo, divisor,
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 quotient.part.lo, remainder32);
100
101 /* Return only what was requested */
102
103 if (out_quotient) {
104 *out_quotient = quotient.full;
105 }
106 if (out_remainder) {
107 *out_remainder = remainder32;
108 }
109
Len Brown4be44fc2005-08-05 00:44:28 -0400110 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113/*******************************************************************************
114 *
115 * FUNCTION: acpi_ut_divide
116 *
117 * PARAMETERS: in_dividend - Dividend
118 * in_divisor - Divisor
119 * out_quotient - Pointer to where the quotient is returned
120 * out_remainder - Pointer to where the remainder is returned
121 *
122 * RETURN: Status (Checks for divide-by-zero)
123 *
124 * DESCRIPTION: Perform a divide and modulo.
125 *
126 ******************************************************************************/
127
128acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400129acpi_ut_divide(acpi_integer in_dividend,
130 acpi_integer in_divisor,
131 acpi_integer * out_quotient, acpi_integer * out_remainder)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Len Brown4be44fc2005-08-05 00:44:28 -0400133 union uint64_overlay dividend;
134 union uint64_overlay divisor;
135 union uint64_overlay quotient;
136 union uint64_overlay remainder;
137 union uint64_overlay normalized_dividend;
138 union uint64_overlay normalized_divisor;
139 u32 partial1;
140 union uint64_overlay partial2;
141 union uint64_overlay partial3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Bob Mooreb229cf92006-04-21 17:15:00 -0400143 ACPI_FUNCTION_TRACE(ut_divide);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 /* Always check for a zero divisor */
146
147 if (in_divisor == 0) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500148 ACPI_ERROR((AE_INFO, "Divide by zero"));
Len Brown4be44fc2005-08-05 00:44:28 -0400149 return_ACPI_STATUS(AE_AML_DIVIDE_BY_ZERO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
151
Len Brown4be44fc2005-08-05 00:44:28 -0400152 divisor.full = in_divisor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 dividend.full = in_dividend;
154 if (divisor.part.hi == 0) {
155 /*
156 * 1) Simplest case is where the divisor is 32 bits, we can
157 * just do two divides
158 */
159 remainder.part.hi = 0;
160
161 /*
162 * The quotient is 64 bits, the remainder is always 32 bits,
163 * and is generated by the second divide.
164 */
Len Brown4be44fc2005-08-05 00:44:28 -0400165 ACPI_DIV_64_BY_32(0, dividend.part.hi, divisor.part.lo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 quotient.part.hi, partial1);
Len Brown4be44fc2005-08-05 00:44:28 -0400167 ACPI_DIV_64_BY_32(partial1, dividend.part.lo, divisor.part.lo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 quotient.part.lo, remainder.part.lo);
169 }
170
171 else {
172 /*
173 * 2) The general case where the divisor is a full 64 bits
174 * is more difficult
175 */
Len Brown4be44fc2005-08-05 00:44:28 -0400176 quotient.part.hi = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 normalized_dividend = dividend;
178 normalized_divisor = divisor;
179
180 /* Normalize the operands (shift until the divisor is < 32 bits) */
181
182 do {
Len Brown4be44fc2005-08-05 00:44:28 -0400183 ACPI_SHIFT_RIGHT_64(normalized_divisor.part.hi,
184 normalized_divisor.part.lo);
185 ACPI_SHIFT_RIGHT_64(normalized_dividend.part.hi,
186 normalized_dividend.part.lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 } while (normalized_divisor.part.hi != 0);
189
190 /* Partial divide */
191
Len Brown4be44fc2005-08-05 00:44:28 -0400192 ACPI_DIV_64_BY_32(normalized_dividend.part.hi,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 normalized_dividend.part.lo,
194 normalized_divisor.part.lo,
195 quotient.part.lo, partial1);
196
197 /*
198 * The quotient is always 32 bits, and simply requires adjustment.
199 * The 64-bit remainder must be generated.
200 */
Len Brown4be44fc2005-08-05 00:44:28 -0400201 partial1 = quotient.part.lo * divisor.part.hi;
202 partial2.full =
203 (acpi_integer) quotient.part.lo * divisor.part.lo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 partial3.full = (acpi_integer) partial2.part.hi + partial1;
205
206 remainder.part.hi = partial3.part.lo;
207 remainder.part.lo = partial2.part.lo;
208
209 if (partial3.part.hi == 0) {
210 if (partial3.part.lo >= dividend.part.hi) {
211 if (partial3.part.lo == dividend.part.hi) {
212 if (partial2.part.lo > dividend.part.lo) {
213 quotient.part.lo--;
214 remainder.full -= divisor.full;
215 }
Len Brown4be44fc2005-08-05 00:44:28 -0400216 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 quotient.part.lo--;
218 remainder.full -= divisor.full;
219 }
220 }
221
Len Brown4be44fc2005-08-05 00:44:28 -0400222 remainder.full = remainder.full - dividend.full;
223 remainder.part.hi = (u32) - ((s32) remainder.part.hi);
224 remainder.part.lo = (u32) - ((s32) remainder.part.lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 if (remainder.part.lo) {
227 remainder.part.hi--;
228 }
229 }
230 }
231
232 /* Return only what was requested */
233
234 if (out_quotient) {
235 *out_quotient = quotient.full;
236 }
237 if (out_remainder) {
238 *out_remainder = remainder.full;
239 }
240
Len Brown4be44fc2005-08-05 00:44:28 -0400241 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
244#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245/*******************************************************************************
246 *
247 * FUNCTION: acpi_ut_short_divide, acpi_ut_divide
248 *
Robert Moore44f6c012005-04-18 22:49:35 -0400249 * PARAMETERS: See function headers above
250 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 * DESCRIPTION: Native versions of the ut_divide functions. Use these if either
252 * 1) The target is a 64-bit platform and therefore 64-bit
253 * integer math is supported directly by the machine.
254 * 2) The target is a 32-bit or 16-bit platform, and the
255 * double-precision integer math library is available to
256 * perform the divide.
257 *
258 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400260acpi_ut_short_divide(acpi_integer in_dividend,
261 u32 divisor,
262 acpi_integer * out_quotient, u32 * out_remainder)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264
Bob Mooreb229cf92006-04-21 17:15:00 -0400265 ACPI_FUNCTION_TRACE(ut_short_divide);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 /* Always check for a zero divisor */
268
269 if (divisor == 0) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500270 ACPI_ERROR((AE_INFO, "Divide by zero"));
Len Brown4be44fc2005-08-05 00:44:28 -0400271 return_ACPI_STATUS(AE_AML_DIVIDE_BY_ZERO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
273
274 /* Return only what was requested */
275
276 if (out_quotient) {
277 *out_quotient = in_dividend / divisor;
278 }
279 if (out_remainder) {
Bob Moore1f549a22008-04-10 19:06:40 +0400280 *out_remainder = (u32) (in_dividend % divisor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
282
Len Brown4be44fc2005-08-05 00:44:28 -0400283 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
286acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400287acpi_ut_divide(acpi_integer in_dividend,
288 acpi_integer in_divisor,
289 acpi_integer * out_quotient, acpi_integer * out_remainder)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Bob Mooreb229cf92006-04-21 17:15:00 -0400291 ACPI_FUNCTION_TRACE(ut_divide);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 /* Always check for a zero divisor */
294
295 if (in_divisor == 0) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500296 ACPI_ERROR((AE_INFO, "Divide by zero"));
Len Brown4be44fc2005-08-05 00:44:28 -0400297 return_ACPI_STATUS(AE_AML_DIVIDE_BY_ZERO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 /* Return only what was requested */
301
302 if (out_quotient) {
303 *out_quotient = in_dividend / in_divisor;
304 }
305 if (out_remainder) {
306 *out_remainder = in_dividend % in_divisor;
307 }
308
Len Brown4be44fc2005-08-05 00:44:28 -0400309 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
312#endif