Path: blob/main/sys/contrib/dev/acpica/components/utilities/utmath.c
48406 views
/*******************************************************************************1*2* Module Name: utmath - Integer math support routines3*4******************************************************************************/56/******************************************************************************7*8* 1. Copyright Notice9*10* Some or all of this work - Copyright (c) 1999 - 2025, Intel Corp.11* All rights reserved.12*13* 2. License14*15* 2.1. This is your license from Intel Corp. under its intellectual property16* rights. You may have additional license terms from the party that provided17* you this software, covering your right to use that party's intellectual18* property rights.19*20* 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a21* copy of the source code appearing in this file ("Covered Code") an22* irrevocable, perpetual, worldwide license under Intel's copyrights in the23* base code distributed originally by Intel ("Original Intel Code") to copy,24* make derivatives, distribute, use and display any portion of the Covered25* Code in any form, with the right to sublicense such rights; and26*27* 2.3. Intel grants Licensee a non-exclusive and non-transferable patent28* license (with the right to sublicense), under only those claims of Intel29* patents that are infringed by the Original Intel Code, to make, use, sell,30* offer to sell, and import the Covered Code and derivative works thereof31* solely to the minimum extent necessary to exercise the above copyright32* license, and in no event shall the patent license extend to any additions33* to or modifications of the Original Intel Code. No other license or right34* is granted directly or by implication, estoppel or otherwise;35*36* The above copyright and patent license is granted only if the following37* conditions are met:38*39* 3. Conditions40*41* 3.1. Redistribution of Source with Rights to Further Distribute Source.42* Redistribution of source code of any substantial portion of the Covered43* Code or modification with rights to further distribute source must include44* the above Copyright Notice, the above License, this list of Conditions,45* and the following Disclaimer and Export Compliance provision. In addition,46* Licensee must cause all Covered Code to which Licensee contributes to47* contain a file documenting the changes Licensee made to create that Covered48* Code and the date of any change. Licensee must include in that file the49* documentation of any changes made by any predecessor Licensee. Licensee50* must include a prominent statement that the modification is derived,51* directly or indirectly, from Original Intel Code.52*53* 3.2. Redistribution of Source with no Rights to Further Distribute Source.54* Redistribution of source code of any substantial portion of the Covered55* Code or modification without rights to further distribute source must56* include the following Disclaimer and Export Compliance provision in the57* documentation and/or other materials provided with distribution. In58* addition, Licensee may not authorize further sublicense of source of any59* portion of the Covered Code, and must include terms to the effect that the60* license from Licensee to its licensee is limited to the intellectual61* property embodied in the software Licensee provides to its licensee, and62* not to intellectual property embodied in modifications its licensee may63* make.64*65* 3.3. Redistribution of Executable. Redistribution in executable form of any66* substantial portion of the Covered Code or modification must reproduce the67* above Copyright Notice, and the following Disclaimer and Export Compliance68* provision in the documentation and/or other materials provided with the69* distribution.70*71* 3.4. Intel retains all right, title, and interest in and to the Original72* Intel Code.73*74* 3.5. Neither the name Intel nor any other trademark owned or controlled by75* Intel shall be used in advertising or otherwise to promote the sale, use or76* other dealings in products derived from or relating to the Covered Code77* without prior written authorization from Intel.78*79* 4. Disclaimer and Export Compliance80*81* 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED82* HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE83* IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,84* INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY85* UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY86* IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A87* PARTICULAR PURPOSE.88*89* 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES90* OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR91* COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,92* SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY93* CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL94* HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS95* SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY96* LIMITED REMEDY.97*98* 4.3. Licensee shall not export, either directly or indirectly, any of this99* software or system incorporating such software without first obtaining any100* required license or other approval from the U. S. Department of Commerce or101* any other agency or department of the United States Government. In the102* event Licensee exports any such software from the United States or103* re-exports any such software from a foreign destination, Licensee shall104* ensure that the distribution and export/re-export of the software is in105* compliance with all laws, regulations, orders, or other restrictions of the106* U.S. Export Administration Regulations. Licensee agrees that neither it nor107* any of its subsidiaries will export/re-export any technical data, process,108* software, or service, directly or indirectly, to any country for which the109* United States government or any agency thereof requires an export license,110* other governmental approval, or letter of assurance, without first obtaining111* such license, approval or letter.112*113*****************************************************************************114*115* Alternatively, you may choose to be licensed under the terms of the116* following license:117*118* Redistribution and use in source and binary forms, with or without119* modification, are permitted provided that the following conditions120* are met:121* 1. Redistributions of source code must retain the above copyright122* notice, this list of conditions, and the following disclaimer,123* without modification.124* 2. Redistributions in binary form must reproduce at minimum a disclaimer125* substantially similar to the "NO WARRANTY" disclaimer below126* ("Disclaimer") and any redistribution must be conditioned upon127* including a substantially similar Disclaimer requirement for further128* binary redistribution.129* 3. Neither the names of the above-listed copyright holders nor the names130* of any contributors may be used to endorse or promote products derived131* from this software without specific prior written permission.132*133* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS134* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT135* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR136* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT137* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,138* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT139* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,140* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY141* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT142* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE143* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.144*145* Alternatively, you may choose to be licensed under the terms of the146* GNU General Public License ("GPL") version 2 as published by the Free147* Software Foundation.148*149*****************************************************************************/150151#include <contrib/dev/acpica/include/acpi.h>152#include <contrib/dev/acpica/include/accommon.h>153154155#define _COMPONENT ACPI_UTILITIES156ACPI_MODULE_NAME ("utmath")157158/* Structures used only for 64-bit divide */159160typedef struct uint64_struct161{162UINT32 Lo;163UINT32 Hi;164165} UINT64_STRUCT;166167typedef union uint64_overlay168{169UINT64 Full;170UINT64_STRUCT Part;171172} UINT64_OVERLAY;173174/*175* Optional support for 64-bit double-precision integer multiply and shift.176* This code is configurable and is implemented in order to support 32-bit177* kernel environments where a 64-bit double-precision math library is not178* available.179*/180#ifndef ACPI_USE_NATIVE_MATH64181182/*******************************************************************************183*184* FUNCTION: AcpiUtShortMultiply185*186* PARAMETERS: Multiplicand - 64-bit multiplicand187* Multiplier - 32-bit multiplier188* OutProduct - Pointer to where the product is returned189*190* DESCRIPTION: Perform a short multiply.191*192******************************************************************************/193194ACPI_STATUS195AcpiUtShortMultiply (196UINT64 Multiplicand,197UINT32 Multiplier,198UINT64 *OutProduct)199{200UINT64_OVERLAY MultiplicandOvl;201UINT64_OVERLAY Product;202UINT32 Carry32;203204205ACPI_FUNCTION_TRACE (UtShortMultiply);206207208MultiplicandOvl.Full = Multiplicand;209210/*211* The Product is 64 bits, the carry is always 32 bits,212* and is generated by the second multiply.213*/214ACPI_MUL_64_BY_32 (0, MultiplicandOvl.Part.Hi, Multiplier,215Product.Part.Hi, Carry32);216217ACPI_MUL_64_BY_32 (0, MultiplicandOvl.Part.Lo, Multiplier,218Product.Part.Lo, Carry32);219220Product.Part.Hi += Carry32;221222/* Return only what was requested */223224if (OutProduct)225{226*OutProduct = Product.Full;227}228229return_ACPI_STATUS (AE_OK);230}231232233/*******************************************************************************234*235* FUNCTION: AcpiUtShortShiftLeft236*237* PARAMETERS: Operand - 64-bit shift operand238* Count - 32-bit shift count239* OutResult - Pointer to where the result is returned240*241* DESCRIPTION: Perform a short left shift.242*243******************************************************************************/244245ACPI_STATUS246AcpiUtShortShiftLeft (247UINT64 Operand,248UINT32 Count,249UINT64 *OutResult)250{251UINT64_OVERLAY OperandOvl;252253254ACPI_FUNCTION_TRACE (UtShortShiftLeft);255256257OperandOvl.Full = Operand;258259if ((Count & 63) >= 32)260{261OperandOvl.Part.Hi = OperandOvl.Part.Lo;262OperandOvl.Part.Lo = 0;263Count = (Count & 63) - 32;264}265ACPI_SHIFT_LEFT_64_BY_32 (OperandOvl.Part.Hi,266OperandOvl.Part.Lo, Count);267268/* Return only what was requested */269270if (OutResult)271{272*OutResult = OperandOvl.Full;273}274275return_ACPI_STATUS (AE_OK);276}277278/*******************************************************************************279*280* FUNCTION: AcpiUtShortShiftRight281*282* PARAMETERS: Operand - 64-bit shift operand283* Count - 32-bit shift count284* OutResult - Pointer to where the result is returned285*286* DESCRIPTION: Perform a short right shift.287*288******************************************************************************/289290ACPI_STATUS291AcpiUtShortShiftRight (292UINT64 Operand,293UINT32 Count,294UINT64 *OutResult)295{296UINT64_OVERLAY OperandOvl;297298299ACPI_FUNCTION_TRACE (UtShortShiftRight);300301302OperandOvl.Full = Operand;303304if ((Count & 63) >= 32)305{306OperandOvl.Part.Lo = OperandOvl.Part.Hi;307OperandOvl.Part.Hi = 0;308Count = (Count & 63) - 32;309}310ACPI_SHIFT_RIGHT_64_BY_32 (OperandOvl.Part.Hi,311OperandOvl.Part.Lo, Count);312313/* Return only what was requested */314315if (OutResult)316{317*OutResult = OperandOvl.Full;318}319320return_ACPI_STATUS (AE_OK);321}322#else323324/*******************************************************************************325*326* FUNCTION: AcpiUtShortMultiply327*328* PARAMETERS: See function headers above329*330* DESCRIPTION: Native version of the UtShortMultiply function.331*332******************************************************************************/333334ACPI_STATUS335AcpiUtShortMultiply (336UINT64 Multiplicand,337UINT32 Multiplier,338UINT64 *OutProduct)339{340341ACPI_FUNCTION_TRACE (UtShortMultiply);342343344/* Return only what was requested */345346if (OutProduct)347{348*OutProduct = Multiplicand * Multiplier;349}350351return_ACPI_STATUS (AE_OK);352}353354/*******************************************************************************355*356* FUNCTION: AcpiUtShortShiftLeft357*358* PARAMETERS: See function headers above359*360* DESCRIPTION: Native version of the UtShortShiftLeft function.361*362******************************************************************************/363364ACPI_STATUS365AcpiUtShortShiftLeft (366UINT64 Operand,367UINT32 Count,368UINT64 *OutResult)369{370371ACPI_FUNCTION_TRACE (UtShortShiftLeft);372373374/* Return only what was requested */375376if (OutResult)377{378*OutResult = Operand << Count;379}380381return_ACPI_STATUS (AE_OK);382}383384/*******************************************************************************385*386* FUNCTION: AcpiUtShortShiftRight387*388* PARAMETERS: See function headers above389*390* DESCRIPTION: Native version of the UtShortShiftRight function.391*392******************************************************************************/393394ACPI_STATUS395AcpiUtShortShiftRight (396UINT64 Operand,397UINT32 Count,398UINT64 *OutResult)399{400401ACPI_FUNCTION_TRACE (UtShortShiftRight);402403404/* Return only what was requested */405406if (OutResult)407{408*OutResult = Operand >> Count;409}410411return_ACPI_STATUS (AE_OK);412}413#endif414415/*416* Optional support for 64-bit double-precision integer divide. This code417* is configurable and is implemented in order to support 32-bit kernel418* environments where a 64-bit double-precision math library is not available.419*420* Support for a more normal 64-bit divide/modulo (with check for a divide-421* by-zero) appears after this optional section of code.422*/423#ifndef ACPI_USE_NATIVE_DIVIDE424425426/*******************************************************************************427*428* FUNCTION: AcpiUtShortDivide429*430* PARAMETERS: Dividend - 64-bit dividend431* Divisor - 32-bit divisor432* OutQuotient - Pointer to where the quotient is returned433* OutRemainder - Pointer to where the remainder is returned434*435* RETURN: Status (Checks for divide-by-zero)436*437* DESCRIPTION: Perform a short (maximum 64 bits divided by 32 bits)438* divide and modulo. The result is a 64-bit quotient and a439* 32-bit remainder.440*441******************************************************************************/442443ACPI_STATUS444AcpiUtShortDivide (445UINT64 Dividend,446UINT32 Divisor,447UINT64 *OutQuotient,448UINT32 *OutRemainder)449{450UINT64_OVERLAY DividendOvl;451UINT64_OVERLAY Quotient;452UINT32 Remainder32;453454455ACPI_FUNCTION_TRACE (UtShortDivide);456457458/* Always check for a zero divisor */459460if (Divisor == 0)461{462ACPI_ERROR ((AE_INFO, "Divide by zero"));463return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO);464}465466DividendOvl.Full = Dividend;467468/*469* The quotient is 64 bits, the remainder is always 32 bits,470* and is generated by the second divide.471*/472ACPI_DIV_64_BY_32 (0, DividendOvl.Part.Hi, Divisor,473Quotient.Part.Hi, Remainder32);474475ACPI_DIV_64_BY_32 (Remainder32, DividendOvl.Part.Lo, Divisor,476Quotient.Part.Lo, Remainder32);477478/* Return only what was requested */479480if (OutQuotient)481{482*OutQuotient = Quotient.Full;483}484if (OutRemainder)485{486*OutRemainder = Remainder32;487}488489return_ACPI_STATUS (AE_OK);490}491492493/*******************************************************************************494*495* FUNCTION: AcpiUtDivide496*497* PARAMETERS: InDividend - Dividend498* InDivisor - Divisor499* OutQuotient - Pointer to where the quotient is returned500* OutRemainder - Pointer to where the remainder is returned501*502* RETURN: Status (Checks for divide-by-zero)503*504* DESCRIPTION: Perform a divide and modulo.505*506******************************************************************************/507508ACPI_STATUS509AcpiUtDivide (510UINT64 InDividend,511UINT64 InDivisor,512UINT64 *OutQuotient,513UINT64 *OutRemainder)514{515UINT64_OVERLAY Dividend;516UINT64_OVERLAY Divisor;517UINT64_OVERLAY Quotient;518UINT64_OVERLAY Remainder;519UINT64_OVERLAY NormalizedDividend;520UINT64_OVERLAY NormalizedDivisor;521UINT32 Partial1;522UINT64_OVERLAY Partial2;523UINT64_OVERLAY Partial3;524525526ACPI_FUNCTION_TRACE (UtDivide);527528529/* Always check for a zero divisor */530531if (InDivisor == 0)532{533ACPI_ERROR ((AE_INFO, "Divide by zero"));534return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO);535}536537Divisor.Full = InDivisor;538Dividend.Full = InDividend;539if (Divisor.Part.Hi == 0)540{541/*542* 1) Simplest case is where the divisor is 32 bits, we can543* just do two divides544*/545Remainder.Part.Hi = 0;546547/*548* The quotient is 64 bits, the remainder is always 32 bits,549* and is generated by the second divide.550*/551ACPI_DIV_64_BY_32 (0, Dividend.Part.Hi, Divisor.Part.Lo,552Quotient.Part.Hi, Partial1);553554ACPI_DIV_64_BY_32 (Partial1, Dividend.Part.Lo, Divisor.Part.Lo,555Quotient.Part.Lo, Remainder.Part.Lo);556}557558else559{560/*561* 2) The general case where the divisor is a full 64 bits562* is more difficult563*/564Quotient.Part.Hi = 0;565NormalizedDividend = Dividend;566NormalizedDivisor = Divisor;567568/* Normalize the operands (shift until the divisor is < 32 bits) */569570do571{572ACPI_SHIFT_RIGHT_64 (573NormalizedDivisor.Part.Hi, NormalizedDivisor.Part.Lo);574ACPI_SHIFT_RIGHT_64 (575NormalizedDividend.Part.Hi, NormalizedDividend.Part.Lo);576577} while (NormalizedDivisor.Part.Hi != 0);578579/* Partial divide */580581ACPI_DIV_64_BY_32 (582NormalizedDividend.Part.Hi, NormalizedDividend.Part.Lo,583NormalizedDivisor.Part.Lo, Quotient.Part.Lo, Partial1);584585/*586* The quotient is always 32 bits, and simply requires587* adjustment. The 64-bit remainder must be generated.588*/589Partial1 = Quotient.Part.Lo * Divisor.Part.Hi;590Partial2.Full = (UINT64) Quotient.Part.Lo * Divisor.Part.Lo;591Partial3.Full = (UINT64) Partial2.Part.Hi + Partial1;592593Remainder.Part.Hi = Partial3.Part.Lo;594Remainder.Part.Lo = Partial2.Part.Lo;595596if (Partial3.Part.Hi == 0)597{598if (Partial3.Part.Lo >= Dividend.Part.Hi)599{600if (Partial3.Part.Lo == Dividend.Part.Hi)601{602if (Partial2.Part.Lo > Dividend.Part.Lo)603{604Quotient.Part.Lo--;605Remainder.Full -= Divisor.Full;606}607}608else609{610Quotient.Part.Lo--;611Remainder.Full -= Divisor.Full;612}613}614615Remainder.Full = Remainder.Full - Dividend.Full;616Remainder.Part.Hi = (UINT32) -((INT32) Remainder.Part.Hi);617Remainder.Part.Lo = (UINT32) -((INT32) Remainder.Part.Lo);618619if (Remainder.Part.Lo)620{621Remainder.Part.Hi--;622}623}624}625626/* Return only what was requested */627628if (OutQuotient)629{630*OutQuotient = Quotient.Full;631}632if (OutRemainder)633{634*OutRemainder = Remainder.Full;635}636637return_ACPI_STATUS (AE_OK);638}639640#else641642/*******************************************************************************643*644* FUNCTION: AcpiUtShortDivide, AcpiUtDivide645*646* PARAMETERS: See function headers above647*648* DESCRIPTION: Native versions of the UtDivide functions. Use these if either649* 1) The target is a 64-bit platform and therefore 64-bit650* integer math is supported directly by the machine.651* 2) The target is a 32-bit or 16-bit platform, and the652* double-precision integer math library is available to653* perform the divide.654*655******************************************************************************/656657ACPI_STATUS658AcpiUtShortDivide (659UINT64 InDividend,660UINT32 Divisor,661UINT64 *OutQuotient,662UINT32 *OutRemainder)663{664665ACPI_FUNCTION_TRACE (UtShortDivide);666667668/* Always check for a zero divisor */669670if (Divisor == 0)671{672ACPI_ERROR ((AE_INFO, "Divide by zero"));673return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO);674}675676/* Return only what was requested */677678if (OutQuotient)679{680*OutQuotient = InDividend / Divisor;681}682if (OutRemainder)683{684*OutRemainder = (UINT32) (InDividend % Divisor);685}686687return_ACPI_STATUS (AE_OK);688}689690ACPI_STATUS691AcpiUtDivide (692UINT64 InDividend,693UINT64 InDivisor,694UINT64 *OutQuotient,695UINT64 *OutRemainder)696{697ACPI_FUNCTION_TRACE (UtDivide);698699700/* Always check for a zero divisor */701702if (InDivisor == 0)703{704ACPI_ERROR ((AE_INFO, "Divide by zero"));705return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO);706}707708709/* Return only what was requested */710711if (OutQuotient)712{713*OutQuotient = InDividend / InDivisor;714}715if (OutRemainder)716{717*OutRemainder = InDividend % InDivisor;718}719720return_ACPI_STATUS (AE_OK);721}722723#endif724725726