1 /*******************************************************************************
3 * Module Name: utmath - Integer math support routines
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2018, 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.
44 #include <acpi/acpi.h>
47 #define _COMPONENT ACPI_UTILITIES
48 ACPI_MODULE_NAME("utmath")
50 /* Structures used only for 64-bit divide */
51 typedef struct uint64_struct
{
57 typedef union uint64_overlay
{
59 struct uint64_struct part
;
64 * Optional support for 64-bit double-precision integer multiply and shift.
65 * This code is configurable and is implemented in order to support 32-bit
66 * kernel environments where a 64-bit double-precision math library is not
69 #ifndef ACPI_USE_NATIVE_MATH64
71 /*******************************************************************************
73 * FUNCTION: acpi_ut_short_multiply
75 * PARAMETERS: multiplicand - 64-bit multiplicand
76 * multiplier - 32-bit multiplier
77 * out_product - Pointer to where the product is returned
79 * DESCRIPTION: Perform a short multiply.
81 ******************************************************************************/
84 acpi_ut_short_multiply(u64 multiplicand
, u32 multiplier
, u64
*out_product
)
86 union uint64_overlay multiplicand_ovl
;
87 union uint64_overlay product
;
90 ACPI_FUNCTION_TRACE(ut_short_multiply
);
92 multiplicand_ovl
.full
= multiplicand
;
95 * The Product is 64 bits, the carry is always 32 bits,
96 * and is generated by the second multiply.
98 ACPI_MUL_64_BY_32(0, multiplicand_ovl
.part
.hi
, multiplier
,
99 product
.part
.hi
, carry32
);
101 ACPI_MUL_64_BY_32(0, multiplicand_ovl
.part
.lo
, multiplier
,
102 product
.part
.lo
, carry32
);
104 product
.part
.hi
+= carry32
;
106 /* Return only what was requested */
109 *out_product
= product
.full
;
112 return_ACPI_STATUS(AE_OK
);
115 /*******************************************************************************
117 * FUNCTION: acpi_ut_short_shift_left
119 * PARAMETERS: operand - 64-bit shift operand
120 * count - 32-bit shift count
121 * out_result - Pointer to where the result is returned
123 * DESCRIPTION: Perform a short left shift.
125 ******************************************************************************/
127 acpi_status
acpi_ut_short_shift_left(u64 operand
, u32 count
, u64
*out_result
)
129 union uint64_overlay operand_ovl
;
131 ACPI_FUNCTION_TRACE(ut_short_shift_left
);
133 operand_ovl
.full
= operand
;
135 if ((count
& 63) >= 32) {
136 operand_ovl
.part
.hi
= operand_ovl
.part
.lo
;
137 operand_ovl
.part
.lo
= 0;
138 count
= (count
& 63) - 32;
140 ACPI_SHIFT_LEFT_64_BY_32(operand_ovl
.part
.hi
,
141 operand_ovl
.part
.lo
, count
);
143 /* Return only what was requested */
146 *out_result
= operand_ovl
.full
;
149 return_ACPI_STATUS(AE_OK
);
152 /*******************************************************************************
154 * FUNCTION: acpi_ut_short_shift_right
156 * PARAMETERS: operand - 64-bit shift operand
157 * count - 32-bit shift count
158 * out_result - Pointer to where the result is returned
160 * DESCRIPTION: Perform a short right shift.
162 ******************************************************************************/
164 acpi_status
acpi_ut_short_shift_right(u64 operand
, u32 count
, u64
*out_result
)
166 union uint64_overlay operand_ovl
;
168 ACPI_FUNCTION_TRACE(ut_short_shift_right
);
170 operand_ovl
.full
= operand
;
172 if ((count
& 63) >= 32) {
173 operand_ovl
.part
.lo
= operand_ovl
.part
.hi
;
174 operand_ovl
.part
.hi
= 0;
175 count
= (count
& 63) - 32;
177 ACPI_SHIFT_RIGHT_64_BY_32(operand_ovl
.part
.hi
,
178 operand_ovl
.part
.lo
, count
);
180 /* Return only what was requested */
183 *out_result
= operand_ovl
.full
;
186 return_ACPI_STATUS(AE_OK
);
190 /*******************************************************************************
192 * FUNCTION: acpi_ut_short_multiply
194 * PARAMETERS: See function headers above
196 * DESCRIPTION: Native version of the ut_short_multiply function.
198 ******************************************************************************/
201 acpi_ut_short_multiply(u64 multiplicand
, u32 multiplier
, u64
*out_product
)
204 ACPI_FUNCTION_TRACE(ut_short_multiply
);
206 /* Return only what was requested */
209 *out_product
= multiplicand
* multiplier
;
212 return_ACPI_STATUS(AE_OK
);
215 /*******************************************************************************
217 * FUNCTION: acpi_ut_short_shift_left
219 * PARAMETERS: See function headers above
221 * DESCRIPTION: Native version of the ut_short_shift_left function.
223 ******************************************************************************/
225 acpi_status
acpi_ut_short_shift_left(u64 operand
, u32 count
, u64
*out_result
)
228 ACPI_FUNCTION_TRACE(ut_short_shift_left
);
230 /* Return only what was requested */
233 *out_result
= operand
<< count
;
236 return_ACPI_STATUS(AE_OK
);
239 /*******************************************************************************
241 * FUNCTION: acpi_ut_short_shift_right
243 * PARAMETERS: See function headers above
245 * DESCRIPTION: Native version of the ut_short_shift_right function.
247 ******************************************************************************/
249 acpi_status
acpi_ut_short_shift_right(u64 operand
, u32 count
, u64
*out_result
)
252 ACPI_FUNCTION_TRACE(ut_short_shift_right
);
254 /* Return only what was requested */
257 *out_result
= operand
>> count
;
260 return_ACPI_STATUS(AE_OK
);
265 * Optional support for 64-bit double-precision integer divide. This code
266 * is configurable and is implemented in order to support 32-bit kernel
267 * environments where a 64-bit double-precision math library is not available.
269 * Support for a more normal 64-bit divide/modulo (with check for a divide-
270 * by-zero) appears after this optional section of code.
272 #ifndef ACPI_USE_NATIVE_DIVIDE
274 /*******************************************************************************
276 * FUNCTION: acpi_ut_short_divide
278 * PARAMETERS: dividend - 64-bit dividend
279 * divisor - 32-bit divisor
280 * out_quotient - Pointer to where the quotient is returned
281 * out_remainder - Pointer to where the remainder is returned
283 * RETURN: Status (Checks for divide-by-zero)
285 * DESCRIPTION: Perform a short (maximum 64 bits divided by 32 bits)
286 * divide and modulo. The result is a 64-bit quotient and a
289 ******************************************************************************/
292 acpi_ut_short_divide(u64 dividend
,
293 u32 divisor
, u64
*out_quotient
, u32
*out_remainder
)
295 union uint64_overlay dividend_ovl
;
296 union uint64_overlay quotient
;
299 ACPI_FUNCTION_TRACE(ut_short_divide
);
301 /* Always check for a zero divisor */
304 ACPI_ERROR((AE_INFO
, "Divide by zero"));
305 return_ACPI_STATUS(AE_AML_DIVIDE_BY_ZERO
);
308 dividend_ovl
.full
= dividend
;
311 * The quotient is 64 bits, the remainder is always 32 bits,
312 * and is generated by the second divide.
314 ACPI_DIV_64_BY_32(0, dividend_ovl
.part
.hi
, divisor
,
315 quotient
.part
.hi
, remainder32
);
317 ACPI_DIV_64_BY_32(remainder32
, dividend_ovl
.part
.lo
, divisor
,
318 quotient
.part
.lo
, remainder32
);
320 /* Return only what was requested */
323 *out_quotient
= quotient
.full
;
326 *out_remainder
= remainder32
;
329 return_ACPI_STATUS(AE_OK
);
332 /*******************************************************************************
334 * FUNCTION: acpi_ut_divide
336 * PARAMETERS: in_dividend - Dividend
337 * in_divisor - Divisor
338 * out_quotient - Pointer to where the quotient is returned
339 * out_remainder - Pointer to where the remainder is returned
341 * RETURN: Status (Checks for divide-by-zero)
343 * DESCRIPTION: Perform a divide and modulo.
345 ******************************************************************************/
348 acpi_ut_divide(u64 in_dividend
,
349 u64 in_divisor
, u64
*out_quotient
, u64
*out_remainder
)
351 union uint64_overlay dividend
;
352 union uint64_overlay divisor
;
353 union uint64_overlay quotient
;
354 union uint64_overlay remainder
;
355 union uint64_overlay normalized_dividend
;
356 union uint64_overlay normalized_divisor
;
358 union uint64_overlay partial2
;
359 union uint64_overlay partial3
;
361 ACPI_FUNCTION_TRACE(ut_divide
);
363 /* Always check for a zero divisor */
365 if (in_divisor
== 0) {
366 ACPI_ERROR((AE_INFO
, "Divide by zero"));
367 return_ACPI_STATUS(AE_AML_DIVIDE_BY_ZERO
);
370 divisor
.full
= in_divisor
;
371 dividend
.full
= in_dividend
;
372 if (divisor
.part
.hi
== 0) {
374 * 1) Simplest case is where the divisor is 32 bits, we can
375 * just do two divides
377 remainder
.part
.hi
= 0;
380 * The quotient is 64 bits, the remainder is always 32 bits,
381 * and is generated by the second divide.
383 ACPI_DIV_64_BY_32(0, dividend
.part
.hi
, divisor
.part
.lo
,
384 quotient
.part
.hi
, partial1
);
386 ACPI_DIV_64_BY_32(partial1
, dividend
.part
.lo
, divisor
.part
.lo
,
387 quotient
.part
.lo
, remainder
.part
.lo
);
392 * 2) The general case where the divisor is a full 64 bits
395 quotient
.part
.hi
= 0;
396 normalized_dividend
= dividend
;
397 normalized_divisor
= divisor
;
399 /* Normalize the operands (shift until the divisor is < 32 bits) */
402 ACPI_SHIFT_RIGHT_64(normalized_divisor
.part
.hi
,
403 normalized_divisor
.part
.lo
);
404 ACPI_SHIFT_RIGHT_64(normalized_dividend
.part
.hi
,
405 normalized_dividend
.part
.lo
);
407 } while (normalized_divisor
.part
.hi
!= 0);
411 ACPI_DIV_64_BY_32(normalized_dividend
.part
.hi
,
412 normalized_dividend
.part
.lo
,
413 normalized_divisor
.part
.lo
, quotient
.part
.lo
,
417 * The quotient is always 32 bits, and simply requires
418 * adjustment. The 64-bit remainder must be generated.
420 partial1
= quotient
.part
.lo
* divisor
.part
.hi
;
421 partial2
.full
= (u64
) quotient
.part
.lo
* divisor
.part
.lo
;
422 partial3
.full
= (u64
) partial2
.part
.hi
+ partial1
;
424 remainder
.part
.hi
= partial3
.part
.lo
;
425 remainder
.part
.lo
= partial2
.part
.lo
;
427 if (partial3
.part
.hi
== 0) {
428 if (partial3
.part
.lo
>= dividend
.part
.hi
) {
429 if (partial3
.part
.lo
== dividend
.part
.hi
) {
430 if (partial2
.part
.lo
> dividend
.part
.lo
) {
432 remainder
.full
-= divisor
.full
;
436 remainder
.full
-= divisor
.full
;
440 remainder
.full
= remainder
.full
- dividend
.full
;
441 remainder
.part
.hi
= (u32
)-((s32
)remainder
.part
.hi
);
442 remainder
.part
.lo
= (u32
)-((s32
)remainder
.part
.lo
);
444 if (remainder
.part
.lo
) {
450 /* Return only what was requested */
453 *out_quotient
= quotient
.full
;
456 *out_remainder
= remainder
.full
;
459 return_ACPI_STATUS(AE_OK
);
464 /*******************************************************************************
466 * FUNCTION: acpi_ut_short_divide, acpi_ut_divide
468 * PARAMETERS: See function headers above
470 * DESCRIPTION: Native versions of the ut_divide functions. Use these if either
471 * 1) The target is a 64-bit platform and therefore 64-bit
472 * integer math is supported directly by the machine.
473 * 2) The target is a 32-bit or 16-bit platform, and the
474 * double-precision integer math library is available to
475 * perform the divide.
477 ******************************************************************************/
480 acpi_ut_short_divide(u64 in_dividend
,
481 u32 divisor
, u64
*out_quotient
, u32
*out_remainder
)
484 ACPI_FUNCTION_TRACE(ut_short_divide
);
486 /* Always check for a zero divisor */
489 ACPI_ERROR((AE_INFO
, "Divide by zero"));
490 return_ACPI_STATUS(AE_AML_DIVIDE_BY_ZERO
);
493 /* Return only what was requested */
496 *out_quotient
= in_dividend
/ divisor
;
499 *out_remainder
= (u32
) (in_dividend
% divisor
);
502 return_ACPI_STATUS(AE_OK
);
506 acpi_ut_divide(u64 in_dividend
,
507 u64 in_divisor
, u64
*out_quotient
, u64
*out_remainder
)
509 ACPI_FUNCTION_TRACE(ut_divide
);
511 /* Always check for a zero divisor */
513 if (in_divisor
== 0) {
514 ACPI_ERROR((AE_INFO
, "Divide by zero"));
515 return_ACPI_STATUS(AE_AML_DIVIDE_BY_ZERO
);
518 /* Return only what was requested */
521 *out_quotient
= in_dividend
/ in_divisor
;
524 *out_remainder
= in_dividend
% in_divisor
;
527 return_ACPI_STATUS(AE_OK
);