Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenutmath.c
Go to the documentation of this file.
00001 /******************************************************************************* 00002 * 00003 * Module Name: utmath - Integer math support routines 00004 * 00005 ******************************************************************************/ 00006 00007 /****************************************************************************** 00008 * 00009 * 1. Copyright Notice 00010 * 00011 * Some or all of this work - Copyright (c) 1999 - 2011, Intel Corp. 00012 * All rights reserved. 00013 * 00014 * 2. License 00015 * 00016 * 2.1. This is your license from Intel Corp. under its intellectual property 00017 * rights. You may have additional license terms from the party that provided 00018 * you this software, covering your right to use that party's intellectual 00019 * property rights. 00020 * 00021 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a 00022 * copy of the source code appearing in this file ("Covered Code") an 00023 * irrevocable, perpetual, worldwide license under Intel's copyrights in the 00024 * base code distributed originally by Intel ("Original Intel Code") to copy, 00025 * make derivatives, distribute, use and display any portion of the Covered 00026 * Code in any form, with the right to sublicense such rights; and 00027 * 00028 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent 00029 * license (with the right to sublicense), under only those claims of Intel 00030 * patents that are infringed by the Original Intel Code, to make, use, sell, 00031 * offer to sell, and import the Covered Code and derivative works thereof 00032 * solely to the minimum extent necessary to exercise the above copyright 00033 * license, and in no event shall the patent license extend to any additions 00034 * to or modifications of the Original Intel Code. No other license or right 00035 * is granted directly or by implication, estoppel or otherwise; 00036 * 00037 * The above copyright and patent license is granted only if the following 00038 * conditions are met: 00039 * 00040 * 3. Conditions 00041 * 00042 * 3.1. Redistribution of Source with Rights to Further Distribute Source. 00043 * Redistribution of source code of any substantial portion of the Covered 00044 * Code or modification with rights to further distribute source must include 00045 * the above Copyright Notice, the above License, this list of Conditions, 00046 * and the following Disclaimer and Export Compliance provision. In addition, 00047 * Licensee must cause all Covered Code to which Licensee contributes to 00048 * contain a file documenting the changes Licensee made to create that Covered 00049 * Code and the date of any change. Licensee must include in that file the 00050 * documentation of any changes made by any predecessor Licensee. Licensee 00051 * must include a prominent statement that the modification is derived, 00052 * directly or indirectly, from Original Intel Code. 00053 * 00054 * 3.2. Redistribution of Source with no Rights to Further Distribute Source. 00055 * Redistribution of source code of any substantial portion of the Covered 00056 * Code or modification without rights to further distribute source must 00057 * include the following Disclaimer and Export Compliance provision in the 00058 * documentation and/or other materials provided with distribution. In 00059 * addition, Licensee may not authorize further sublicense of source of any 00060 * portion of the Covered Code, and must include terms to the effect that the 00061 * license from Licensee to its licensee is limited to the intellectual 00062 * property embodied in the software Licensee provides to its licensee, and 00063 * not to intellectual property embodied in modifications its licensee may 00064 * make. 00065 * 00066 * 3.3. Redistribution of Executable. Redistribution in executable form of any 00067 * substantial portion of the Covered Code or modification must reproduce the 00068 * above Copyright Notice, and the following Disclaimer and Export Compliance 00069 * provision in the documentation and/or other materials provided with the 00070 * distribution. 00071 * 00072 * 3.4. Intel retains all right, title, and interest in and to the Original 00073 * Intel Code. 00074 * 00075 * 3.5. Neither the name Intel nor any other trademark owned or controlled by 00076 * Intel shall be used in advertising or otherwise to promote the sale, use or 00077 * other dealings in products derived from or relating to the Covered Code 00078 * without prior written authorization from Intel. 00079 * 00080 * 4. Disclaimer and Export Compliance 00081 * 00082 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED 00083 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE 00084 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE, 00085 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY 00086 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY 00087 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A 00088 * PARTICULAR PURPOSE. 00089 * 00090 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES 00091 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR 00092 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT, 00093 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY 00094 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL 00095 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS 00096 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY 00097 * LIMITED REMEDY. 00098 * 00099 * 4.3. Licensee shall not export, either directly or indirectly, any of this 00100 * software or system incorporating such software without first obtaining any 00101 * required license or other approval from the U. S. Department of Commerce or 00102 * any other agency or department of the United States Government. In the 00103 * event Licensee exports any such software from the United States or 00104 * re-exports any such software from a foreign destination, Licensee shall 00105 * ensure that the distribution and export/re-export of the software is in 00106 * compliance with all laws, regulations, orders, or other restrictions of the 00107 * U.S. Export Administration Regulations. Licensee agrees that neither it nor 00108 * any of its subsidiaries will export/re-export any technical data, process, 00109 * software, or service, directly or indirectly, to any country for which the 00110 * United States government or any agency thereof requires an export license, 00111 * other governmental approval, or letter of assurance, without first obtaining 00112 * such license, approval or letter. 00113 * 00114 *****************************************************************************/ 00115 00116 00117 #define __UTMATH_C__ 00118 00119 #include "acpi.h" 00120 #include "accommon.h" 00121 00122 00123 #define _COMPONENT ACPI_UTILITIES 00124 ACPI_MODULE_NAME ("utmath") 00125 00126 /* 00127 * Optional support for 64-bit double-precision integer divide. This code 00128 * is configurable and is implemented in order to support 32-bit kernel 00129 * environments where a 64-bit double-precision math library is not available. 00130 * 00131 * Support for a more normal 64-bit divide/modulo (with check for a divide- 00132 * by-zero) appears after this optional section of code. 00133 */ 00134 #ifndef ACPI_USE_NATIVE_DIVIDE 00135 00136 /* Structures used only for 64-bit divide */ 00137 00138 typedef struct uint64_struct 00139 { 00140 UINT32 Lo; 00141 UINT32 Hi; 00142 00143 } UINT64_STRUCT; 00144 00145 typedef union uint64_overlay 00146 { 00147 UINT64 Full; 00148 UINT64_STRUCT Part; 00149 00150 } UINT64_OVERLAY; 00151 00152 00153 /******************************************************************************* 00154 * 00155 * FUNCTION: AcpiUtShortDivide 00156 * 00157 * PARAMETERS: Dividend - 64-bit dividend 00158 * Divisor - 32-bit divisor 00159 * OutQuotient - Pointer to where the quotient is returned 00160 * OutRemainder - Pointer to where the remainder is returned 00161 * 00162 * RETURN: Status (Checks for divide-by-zero) 00163 * 00164 * DESCRIPTION: Perform a short (maximum 64 bits divided by 32 bits) 00165 * divide and modulo. The result is a 64-bit quotient and a 00166 * 32-bit remainder. 00167 * 00168 ******************************************************************************/ 00169 00170 ACPI_STATUS 00171 AcpiUtShortDivide ( 00172 UINT64 Dividend, 00173 UINT32 Divisor, 00174 UINT64 *OutQuotient, 00175 UINT32 *OutRemainder) 00176 { 00177 UINT64_OVERLAY DividendOvl; 00178 UINT64_OVERLAY Quotient; 00179 UINT32 Remainder32; 00180 00181 00182 ACPI_FUNCTION_TRACE (UtShortDivide); 00183 00184 00185 /* Always check for a zero divisor */ 00186 00187 if (Divisor == 0) 00188 { 00189 ACPI_ERROR ((AE_INFO, "Divide by zero")); 00190 return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO); 00191 } 00192 00193 DividendOvl.Full = Dividend; 00194 00195 /* 00196 * The quotient is 64 bits, the remainder is always 32 bits, 00197 * and is generated by the second divide. 00198 */ 00199 ACPI_DIV_64_BY_32 (0, DividendOvl.Part.Hi, Divisor, 00200 Quotient.Part.Hi, Remainder32); 00201 ACPI_DIV_64_BY_32 (Remainder32, DividendOvl.Part.Lo, Divisor, 00202 Quotient.Part.Lo, Remainder32); 00203 00204 /* Return only what was requested */ 00205 00206 if (OutQuotient) 00207 { 00208 *OutQuotient = Quotient.Full; 00209 } 00210 if (OutRemainder) 00211 { 00212 *OutRemainder = Remainder32; 00213 } 00214 00215 return_ACPI_STATUS (AE_OK); 00216 } 00217 00218 00219 /******************************************************************************* 00220 * 00221 * FUNCTION: AcpiUtDivide 00222 * 00223 * PARAMETERS: InDividend - Dividend 00224 * InDivisor - Divisor 00225 * OutQuotient - Pointer to where the quotient is returned 00226 * OutRemainder - Pointer to where the remainder is returned 00227 * 00228 * RETURN: Status (Checks for divide-by-zero) 00229 * 00230 * DESCRIPTION: Perform a divide and modulo. 00231 * 00232 ******************************************************************************/ 00233 00234 ACPI_STATUS 00235 AcpiUtDivide ( 00236 UINT64 InDividend, 00237 UINT64 InDivisor, 00238 UINT64 *OutQuotient, 00239 UINT64 *OutRemainder) 00240 { 00241 UINT64_OVERLAY Dividend; 00242 UINT64_OVERLAY Divisor; 00243 UINT64_OVERLAY Quotient; 00244 UINT64_OVERLAY Remainder; 00245 UINT64_OVERLAY NormalizedDividend; 00246 UINT64_OVERLAY NormalizedDivisor; 00247 UINT32 Partial1; 00248 UINT64_OVERLAY Partial2; 00249 UINT64_OVERLAY Partial3; 00250 00251 00252 ACPI_FUNCTION_TRACE (UtDivide); 00253 00254 00255 /* Always check for a zero divisor */ 00256 00257 if (InDivisor == 0) 00258 { 00259 ACPI_ERROR ((AE_INFO, "Divide by zero")); 00260 return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO); 00261 } 00262 00263 Divisor.Full = InDivisor; 00264 Dividend.Full = InDividend; 00265 if (Divisor.Part.Hi == 0) 00266 { 00267 /* 00268 * 1) Simplest case is where the divisor is 32 bits, we can 00269 * just do two divides 00270 */ 00271 Remainder.Part.Hi = 0; 00272 00273 /* 00274 * The quotient is 64 bits, the remainder is always 32 bits, 00275 * and is generated by the second divide. 00276 */ 00277 ACPI_DIV_64_BY_32 (0, Dividend.Part.Hi, Divisor.Part.Lo, 00278 Quotient.Part.Hi, Partial1); 00279 ACPI_DIV_64_BY_32 (Partial1, Dividend.Part.Lo, Divisor.Part.Lo, 00280 Quotient.Part.Lo, Remainder.Part.Lo); 00281 } 00282 00283 else 00284 { 00285 /* 00286 * 2) The general case where the divisor is a full 64 bits 00287 * is more difficult 00288 */ 00289 Quotient.Part.Hi = 0; 00290 NormalizedDividend = Dividend; 00291 NormalizedDivisor = Divisor; 00292 00293 /* Normalize the operands (shift until the divisor is < 32 bits) */ 00294 00295 do 00296 { 00297 ACPI_SHIFT_RIGHT_64 (NormalizedDivisor.Part.Hi, 00298 NormalizedDivisor.Part.Lo); 00299 ACPI_SHIFT_RIGHT_64 (NormalizedDividend.Part.Hi, 00300 NormalizedDividend.Part.Lo); 00301 00302 } while (NormalizedDivisor.Part.Hi != 0); 00303 00304 /* Partial divide */ 00305 00306 ACPI_DIV_64_BY_32 (NormalizedDividend.Part.Hi, 00307 NormalizedDividend.Part.Lo, 00308 NormalizedDivisor.Part.Lo, 00309 Quotient.Part.Lo, Partial1); 00310 00311 /* 00312 * The quotient is always 32 bits, and simply requires adjustment. 00313 * The 64-bit remainder must be generated. 00314 */ 00315 Partial1 = Quotient.Part.Lo * Divisor.Part.Hi; 00316 Partial2.Full = (UINT64) Quotient.Part.Lo * Divisor.Part.Lo; 00317 Partial3.Full = (UINT64) Partial2.Part.Hi + Partial1; 00318 00319 Remainder.Part.Hi = Partial3.Part.Lo; 00320 Remainder.Part.Lo = Partial2.Part.Lo; 00321 00322 if (Partial3.Part.Hi == 0) 00323 { 00324 if (Partial3.Part.Lo >= Dividend.Part.Hi) 00325 { 00326 if (Partial3.Part.Lo == Dividend.Part.Hi) 00327 { 00328 if (Partial2.Part.Lo > Dividend.Part.Lo) 00329 { 00330 Quotient.Part.Lo--; 00331 Remainder.Full -= Divisor.Full; 00332 } 00333 } 00334 else 00335 { 00336 Quotient.Part.Lo--; 00337 Remainder.Full -= Divisor.Full; 00338 } 00339 } 00340 00341 Remainder.Full = Remainder.Full - Dividend.Full; 00342 Remainder.Part.Hi = (UINT32) -((INT32) Remainder.Part.Hi); 00343 Remainder.Part.Lo = (UINT32) -((INT32) Remainder.Part.Lo); 00344 00345 if (Remainder.Part.Lo) 00346 { 00347 Remainder.Part.Hi--; 00348 } 00349 } 00350 } 00351 00352 /* Return only what was requested */ 00353 00354 if (OutQuotient) 00355 { 00356 *OutQuotient = Quotient.Full; 00357 } 00358 if (OutRemainder) 00359 { 00360 *OutRemainder = Remainder.Full; 00361 } 00362 00363 return_ACPI_STATUS (AE_OK); 00364 } 00365 00366 #else 00367 00368 /******************************************************************************* 00369 * 00370 * FUNCTION: AcpiUtShortDivide, AcpiUtDivide 00371 * 00372 * PARAMETERS: See function headers above 00373 * 00374 * DESCRIPTION: Native versions of the UtDivide functions. Use these if either 00375 * 1) The target is a 64-bit platform and therefore 64-bit 00376 * integer math is supported directly by the machine. 00377 * 2) The target is a 32-bit or 16-bit platform, and the 00378 * double-precision integer math library is available to 00379 * perform the divide. 00380 * 00381 ******************************************************************************/ 00382 00383 ACPI_STATUS 00384 AcpiUtShortDivide ( 00385 UINT64 InDividend, 00386 UINT32 Divisor, 00387 UINT64 *OutQuotient, 00388 UINT32 *OutRemainder) 00389 { 00390 00391 ACPI_FUNCTION_TRACE (UtShortDivide); 00392 00393 00394 /* Always check for a zero divisor */ 00395 00396 if (Divisor == 0) 00397 { 00398 ACPI_ERROR ((AE_INFO, "Divide by zero")); 00399 return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO); 00400 } 00401 00402 /* Return only what was requested */ 00403 00404 if (OutQuotient) 00405 { 00406 *OutQuotient = InDividend / Divisor; 00407 } 00408 if (OutRemainder) 00409 { 00410 *OutRemainder = (UINT32) (InDividend % Divisor); 00411 } 00412 00413 return_ACPI_STATUS (AE_OK); 00414 } 00415 00416 ACPI_STATUS 00417 AcpiUtDivide ( 00418 UINT64 InDividend, 00419 UINT64 InDivisor, 00420 UINT64 *OutQuotient, 00421 UINT64 *OutRemainder) 00422 { 00423 ACPI_FUNCTION_TRACE (UtDivide); 00424 00425 00426 /* Always check for a zero divisor */ 00427 00428 if (InDivisor == 0) 00429 { 00430 ACPI_ERROR ((AE_INFO, "Divide by zero")); 00431 return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO); 00432 } 00433 00434 00435 /* Return only what was requested */ 00436 00437 if (OutQuotient) 00438 { 00439 *OutQuotient = InDividend / InDivisor; 00440 } 00441 if (OutRemainder) 00442 { 00443 *OutRemainder = InDividend % InDivisor; 00444 } 00445 00446 return_ACPI_STATUS (AE_OK); 00447 } 00448 00449 #endif 00450 00451 Generated on Thu May 24 2012 04:27:58 for ReactOS by
1.7.6.1
|