Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * |
| 3 | * Module Name: utmath - Integer math support routines |
| 4 | * |
| 5 | ******************************************************************************/ |
| 6 | |
| 7 | /* |
Bob Moore | a8357b0 | 2010-01-22 19:07:36 +0800 | [diff] [blame] | 8 | * Copyright (C) 2000 - 2010, Intel Corp. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <acpi/acpi.h> |
Len Brown | e2f7a77 | 2009-01-09 00:30:03 -0500 | [diff] [blame] | 45 | #include "accommon.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #define _COMPONENT ACPI_UTILITIES |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 48 | ACPI_MODULE_NAME("utmath") |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | acpi_status |
Bob Moore | 5df7e6c | 2010-01-21 10:06:32 +0800 | [diff] [blame] | 73 | acpi_ut_short_divide(u64 dividend, |
| 74 | u32 divisor, u64 *out_quotient, u32 *out_remainder) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 76 | union uint64_overlay dividend_ovl; |
| 77 | union uint64_overlay quotient; |
| 78 | u32 remainder32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
Bob Moore | b229cf9 | 2006-04-21 17:15:00 -0400 | [diff] [blame] | 80 | ACPI_FUNCTION_TRACE(ut_short_divide); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
| 82 | /* Always check for a zero divisor */ |
| 83 | |
| 84 | if (divisor == 0) { |
Bob Moore | b8e4d89 | 2006-01-27 16:43:00 -0500 | [diff] [blame] | 85 | ACPI_ERROR((AE_INFO, "Divide by zero")); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 86 | return_ACPI_STATUS(AE_AML_DIVIDE_BY_ZERO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | } |
| 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 Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 95 | ACPI_DIV_64_BY_32(0, dividend_ovl.part.hi, divisor, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | quotient.part.hi, remainder32); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 97 | ACPI_DIV_64_BY_32(remainder32, dividend_ovl.part.lo, divisor, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | 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 Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 109 | return_ACPI_STATUS(AE_OK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | /******************************************************************************* |
| 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 | |
| 127 | acpi_status |
Bob Moore | 5df7e6c | 2010-01-21 10:06:32 +0800 | [diff] [blame] | 128 | acpi_ut_divide(u64 in_dividend, |
| 129 | u64 in_divisor, u64 *out_quotient, u64 *out_remainder) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 131 | union uint64_overlay dividend; |
| 132 | union uint64_overlay divisor; |
| 133 | union uint64_overlay quotient; |
| 134 | union uint64_overlay remainder; |
| 135 | union uint64_overlay normalized_dividend; |
| 136 | union uint64_overlay normalized_divisor; |
| 137 | u32 partial1; |
| 138 | union uint64_overlay partial2; |
| 139 | union uint64_overlay partial3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
Bob Moore | b229cf9 | 2006-04-21 17:15:00 -0400 | [diff] [blame] | 141 | ACPI_FUNCTION_TRACE(ut_divide); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
| 143 | /* Always check for a zero divisor */ |
| 144 | |
| 145 | if (in_divisor == 0) { |
Bob Moore | b8e4d89 | 2006-01-27 16:43:00 -0500 | [diff] [blame] | 146 | ACPI_ERROR((AE_INFO, "Divide by zero")); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 147 | return_ACPI_STATUS(AE_AML_DIVIDE_BY_ZERO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 150 | divisor.full = in_divisor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | dividend.full = in_dividend; |
| 152 | if (divisor.part.hi == 0) { |
| 153 | /* |
| 154 | * 1) Simplest case is where the divisor is 32 bits, we can |
| 155 | * just do two divides |
| 156 | */ |
| 157 | remainder.part.hi = 0; |
| 158 | |
| 159 | /* |
| 160 | * The quotient is 64 bits, the remainder is always 32 bits, |
| 161 | * and is generated by the second divide. |
| 162 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 163 | ACPI_DIV_64_BY_32(0, dividend.part.hi, divisor.part.lo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | quotient.part.hi, partial1); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 165 | ACPI_DIV_64_BY_32(partial1, dividend.part.lo, divisor.part.lo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | quotient.part.lo, remainder.part.lo); |
| 167 | } |
| 168 | |
| 169 | else { |
| 170 | /* |
| 171 | * 2) The general case where the divisor is a full 64 bits |
| 172 | * is more difficult |
| 173 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 174 | quotient.part.hi = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | normalized_dividend = dividend; |
| 176 | normalized_divisor = divisor; |
| 177 | |
| 178 | /* Normalize the operands (shift until the divisor is < 32 bits) */ |
| 179 | |
| 180 | do { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 181 | ACPI_SHIFT_RIGHT_64(normalized_divisor.part.hi, |
| 182 | normalized_divisor.part.lo); |
| 183 | ACPI_SHIFT_RIGHT_64(normalized_dividend.part.hi, |
| 184 | normalized_dividend.part.lo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
| 186 | } while (normalized_divisor.part.hi != 0); |
| 187 | |
| 188 | /* Partial divide */ |
| 189 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 190 | ACPI_DIV_64_BY_32(normalized_dividend.part.hi, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | normalized_dividend.part.lo, |
| 192 | normalized_divisor.part.lo, |
| 193 | quotient.part.lo, partial1); |
| 194 | |
| 195 | /* |
| 196 | * The quotient is always 32 bits, and simply requires adjustment. |
| 197 | * The 64-bit remainder must be generated. |
| 198 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 199 | partial1 = quotient.part.lo * divisor.part.hi; |
Bob Moore | 5df7e6c | 2010-01-21 10:06:32 +0800 | [diff] [blame] | 200 | partial2.full = (u64) quotient.part.lo * divisor.part.lo; |
| 201 | partial3.full = (u64) partial2.part.hi + partial1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
| 203 | remainder.part.hi = partial3.part.lo; |
| 204 | remainder.part.lo = partial2.part.lo; |
| 205 | |
| 206 | if (partial3.part.hi == 0) { |
| 207 | if (partial3.part.lo >= dividend.part.hi) { |
| 208 | if (partial3.part.lo == dividend.part.hi) { |
| 209 | if (partial2.part.lo > dividend.part.lo) { |
| 210 | quotient.part.lo--; |
| 211 | remainder.full -= divisor.full; |
| 212 | } |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 213 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | quotient.part.lo--; |
| 215 | remainder.full -= divisor.full; |
| 216 | } |
| 217 | } |
| 218 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 219 | remainder.full = remainder.full - dividend.full; |
| 220 | remainder.part.hi = (u32) - ((s32) remainder.part.hi); |
| 221 | remainder.part.lo = (u32) - ((s32) remainder.part.lo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
| 223 | if (remainder.part.lo) { |
| 224 | remainder.part.hi--; |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /* Return only what was requested */ |
| 230 | |
| 231 | if (out_quotient) { |
| 232 | *out_quotient = quotient.full; |
| 233 | } |
| 234 | if (out_remainder) { |
| 235 | *out_remainder = remainder.full; |
| 236 | } |
| 237 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 238 | return_ACPI_STATUS(AE_OK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | /******************************************************************************* |
| 243 | * |
| 244 | * FUNCTION: acpi_ut_short_divide, acpi_ut_divide |
| 245 | * |
Robert Moore | 44f6c01 | 2005-04-18 22:49:35 -0400 | [diff] [blame] | 246 | * PARAMETERS: See function headers above |
| 247 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | * DESCRIPTION: Native versions of the ut_divide functions. Use these if either |
| 249 | * 1) The target is a 64-bit platform and therefore 64-bit |
| 250 | * integer math is supported directly by the machine. |
| 251 | * 2) The target is a 32-bit or 16-bit platform, and the |
| 252 | * double-precision integer math library is available to |
| 253 | * perform the divide. |
| 254 | * |
| 255 | ******************************************************************************/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | acpi_status |
Bob Moore | 5df7e6c | 2010-01-21 10:06:32 +0800 | [diff] [blame] | 257 | acpi_ut_short_divide(u64 in_dividend, |
| 258 | u32 divisor, u64 *out_quotient, u32 *out_remainder) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | { |
| 260 | |
Bob Moore | b229cf9 | 2006-04-21 17:15:00 -0400 | [diff] [blame] | 261 | ACPI_FUNCTION_TRACE(ut_short_divide); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
| 263 | /* Always check for a zero divisor */ |
| 264 | |
| 265 | if (divisor == 0) { |
Bob Moore | b8e4d89 | 2006-01-27 16:43:00 -0500 | [diff] [blame] | 266 | ACPI_ERROR((AE_INFO, "Divide by zero")); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 267 | return_ACPI_STATUS(AE_AML_DIVIDE_BY_ZERO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | /* Return only what was requested */ |
| 271 | |
| 272 | if (out_quotient) { |
| 273 | *out_quotient = in_dividend / divisor; |
| 274 | } |
| 275 | if (out_remainder) { |
Bob Moore | 1f549a2 | 2008-04-10 19:06:40 +0400 | [diff] [blame] | 276 | *out_remainder = (u32) (in_dividend % divisor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 279 | return_ACPI_STATUS(AE_OK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | acpi_status |
Bob Moore | 5df7e6c | 2010-01-21 10:06:32 +0800 | [diff] [blame] | 283 | acpi_ut_divide(u64 in_dividend, |
| 284 | u64 in_divisor, u64 *out_quotient, u64 *out_remainder) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | { |
Bob Moore | b229cf9 | 2006-04-21 17:15:00 -0400 | [diff] [blame] | 286 | ACPI_FUNCTION_TRACE(ut_divide); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | |
| 288 | /* Always check for a zero divisor */ |
| 289 | |
| 290 | if (in_divisor == 0) { |
Bob Moore | b8e4d89 | 2006-01-27 16:43:00 -0500 | [diff] [blame] | 291 | ACPI_ERROR((AE_INFO, "Divide by zero")); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 292 | return_ACPI_STATUS(AE_AML_DIVIDE_BY_ZERO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | /* Return only what was requested */ |
| 296 | |
| 297 | if (out_quotient) { |
| 298 | *out_quotient = in_dividend / in_divisor; |
| 299 | } |
| 300 | if (out_remainder) { |
| 301 | *out_remainder = in_dividend % in_divisor; |
| 302 | } |
| 303 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 304 | return_ACPI_STATUS(AE_OK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | #endif |