Home | History | Annotate | Line # | Download | only in utilities
utmath.c revision 1.1.1.9.2.1
      1          1.1    jruoho /*******************************************************************************
      2          1.1    jruoho  *
      3          1.1    jruoho  * Module Name: utmath - Integer math support routines
      4          1.1    jruoho  *
      5          1.1    jruoho  ******************************************************************************/
      6          1.1    jruoho 
      7      1.1.1.2    jruoho /*
      8  1.1.1.9.2.1  pgoyette  * Copyright (C) 2000 - 2018, Intel Corp.
      9          1.1    jruoho  * All rights reserved.
     10          1.1    jruoho  *
     11      1.1.1.2    jruoho  * Redistribution and use in source and binary forms, with or without
     12      1.1.1.2    jruoho  * modification, are permitted provided that the following conditions
     13      1.1.1.2    jruoho  * are met:
     14      1.1.1.2    jruoho  * 1. Redistributions of source code must retain the above copyright
     15      1.1.1.2    jruoho  *    notice, this list of conditions, and the following disclaimer,
     16      1.1.1.2    jruoho  *    without modification.
     17      1.1.1.2    jruoho  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     18      1.1.1.2    jruoho  *    substantially similar to the "NO WARRANTY" disclaimer below
     19      1.1.1.2    jruoho  *    ("Disclaimer") and any redistribution must be conditioned upon
     20      1.1.1.2    jruoho  *    including a substantially similar Disclaimer requirement for further
     21      1.1.1.2    jruoho  *    binary redistribution.
     22      1.1.1.2    jruoho  * 3. Neither the names of the above-listed copyright holders nor the names
     23      1.1.1.2    jruoho  *    of any contributors may be used to endorse or promote products derived
     24      1.1.1.2    jruoho  *    from this software without specific prior written permission.
     25      1.1.1.2    jruoho  *
     26      1.1.1.2    jruoho  * Alternatively, this software may be distributed under the terms of the
     27      1.1.1.2    jruoho  * GNU General Public License ("GPL") version 2 as published by the Free
     28      1.1.1.2    jruoho  * Software Foundation.
     29      1.1.1.2    jruoho  *
     30      1.1.1.2    jruoho  * NO WARRANTY
     31      1.1.1.2    jruoho  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     32      1.1.1.2    jruoho  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     33      1.1.1.2    jruoho  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
     34      1.1.1.2    jruoho  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     35      1.1.1.2    jruoho  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36      1.1.1.2    jruoho  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37      1.1.1.2    jruoho  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38      1.1.1.2    jruoho  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39      1.1.1.2    jruoho  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     40      1.1.1.2    jruoho  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41      1.1.1.2    jruoho  * POSSIBILITY OF SUCH DAMAGES.
     42      1.1.1.2    jruoho  */
     43          1.1    jruoho 
     44          1.1    jruoho #include "acpi.h"
     45          1.1    jruoho #include "accommon.h"
     46          1.1    jruoho 
     47          1.1    jruoho 
     48          1.1    jruoho #define _COMPONENT          ACPI_UTILITIES
     49          1.1    jruoho         ACPI_MODULE_NAME    ("utmath")
     50          1.1    jruoho 
     51      1.1.1.2    jruoho /* Structures used only for 64-bit divide */
     52      1.1.1.2    jruoho 
     53      1.1.1.2    jruoho typedef struct uint64_struct
     54      1.1.1.2    jruoho {
     55      1.1.1.2    jruoho     UINT32                          Lo;
     56      1.1.1.2    jruoho     UINT32                          Hi;
     57      1.1.1.2    jruoho 
     58      1.1.1.2    jruoho } UINT64_STRUCT;
     59      1.1.1.2    jruoho 
     60      1.1.1.2    jruoho typedef union uint64_overlay
     61      1.1.1.2    jruoho {
     62      1.1.1.2    jruoho     UINT64                          Full;
     63      1.1.1.2    jruoho     UINT64_STRUCT                   Part;
     64      1.1.1.2    jruoho 
     65      1.1.1.2    jruoho } UINT64_OVERLAY;
     66      1.1.1.2    jruoho 
     67      1.1.1.8  christos /*
     68      1.1.1.8  christos  * Optional support for 64-bit double-precision integer multiply and shift.
     69      1.1.1.8  christos  * This code is configurable and is implemented in order to support 32-bit
     70      1.1.1.8  christos  * kernel environments where a 64-bit double-precision math library is not
     71      1.1.1.8  christos  * available.
     72      1.1.1.8  christos  */
     73      1.1.1.8  christos #ifndef ACPI_USE_NATIVE_MATH64
     74      1.1.1.8  christos 
     75      1.1.1.8  christos /*******************************************************************************
     76      1.1.1.8  christos  *
     77      1.1.1.8  christos  * FUNCTION:    AcpiUtShortMultiply
     78      1.1.1.8  christos  *
     79      1.1.1.8  christos  * PARAMETERS:  Multiplicand        - 64-bit multiplicand
     80      1.1.1.8  christos  *              Multiplier          - 32-bit multiplier
     81      1.1.1.8  christos  *              OutProduct          - Pointer to where the product is returned
     82      1.1.1.8  christos  *
     83      1.1.1.8  christos  * DESCRIPTION: Perform a short multiply.
     84      1.1.1.8  christos  *
     85      1.1.1.8  christos  ******************************************************************************/
     86      1.1.1.8  christos 
     87      1.1.1.8  christos ACPI_STATUS
     88      1.1.1.8  christos AcpiUtShortMultiply (
     89      1.1.1.8  christos     UINT64                  Multiplicand,
     90      1.1.1.8  christos     UINT32                  Multiplier,
     91      1.1.1.8  christos     UINT64                  *OutProduct)
     92      1.1.1.8  christos {
     93      1.1.1.8  christos     UINT64_OVERLAY          MultiplicandOvl;
     94      1.1.1.8  christos     UINT64_OVERLAY          Product;
     95      1.1.1.8  christos     UINT32                  Carry32;
     96      1.1.1.8  christos 
     97      1.1.1.8  christos 
     98      1.1.1.8  christos     ACPI_FUNCTION_TRACE (UtShortMultiply);
     99      1.1.1.8  christos 
    100      1.1.1.8  christos 
    101      1.1.1.8  christos     MultiplicandOvl.Full = Multiplicand;
    102      1.1.1.8  christos 
    103      1.1.1.8  christos     /*
    104      1.1.1.8  christos      * The Product is 64 bits, the carry is always 32 bits,
    105      1.1.1.8  christos      * and is generated by the second multiply.
    106      1.1.1.8  christos      */
    107      1.1.1.8  christos     ACPI_MUL_64_BY_32 (0, MultiplicandOvl.Part.Hi, Multiplier,
    108      1.1.1.8  christos         Product.Part.Hi, Carry32);
    109      1.1.1.8  christos 
    110      1.1.1.8  christos     ACPI_MUL_64_BY_32 (0, MultiplicandOvl.Part.Lo, Multiplier,
    111      1.1.1.8  christos         Product.Part.Lo, Carry32);
    112      1.1.1.8  christos 
    113      1.1.1.8  christos     Product.Part.Hi += Carry32;
    114      1.1.1.8  christos 
    115      1.1.1.8  christos     /* Return only what was requested */
    116      1.1.1.8  christos 
    117      1.1.1.8  christos     if (OutProduct)
    118      1.1.1.8  christos     {
    119      1.1.1.8  christos         *OutProduct = Product.Full;
    120      1.1.1.8  christos     }
    121      1.1.1.8  christos 
    122      1.1.1.8  christos     return_ACPI_STATUS (AE_OK);
    123      1.1.1.8  christos }
    124      1.1.1.8  christos 
    125      1.1.1.8  christos 
    126      1.1.1.8  christos /*******************************************************************************
    127      1.1.1.8  christos  *
    128      1.1.1.8  christos  * FUNCTION:    AcpiUtShortShiftLeft
    129      1.1.1.8  christos  *
    130      1.1.1.8  christos  * PARAMETERS:  Operand             - 64-bit shift operand
    131      1.1.1.8  christos  *              Count               - 32-bit shift count
    132      1.1.1.8  christos  *              OutResult           - Pointer to where the result is returned
    133      1.1.1.8  christos  *
    134      1.1.1.8  christos  * DESCRIPTION: Perform a short left shift.
    135      1.1.1.8  christos  *
    136      1.1.1.8  christos  ******************************************************************************/
    137      1.1.1.8  christos 
    138      1.1.1.8  christos ACPI_STATUS
    139      1.1.1.8  christos AcpiUtShortShiftLeft (
    140      1.1.1.8  christos     UINT64                  Operand,
    141      1.1.1.8  christos     UINT32                  Count,
    142      1.1.1.8  christos     UINT64                  *OutResult)
    143      1.1.1.8  christos {
    144      1.1.1.8  christos     UINT64_OVERLAY          OperandOvl;
    145      1.1.1.8  christos 
    146      1.1.1.8  christos 
    147      1.1.1.8  christos     ACPI_FUNCTION_TRACE (UtShortShiftLeft);
    148      1.1.1.8  christos 
    149      1.1.1.8  christos 
    150      1.1.1.8  christos     OperandOvl.Full = Operand;
    151      1.1.1.8  christos 
    152      1.1.1.8  christos     if ((Count & 63) >= 32)
    153      1.1.1.8  christos     {
    154      1.1.1.8  christos         OperandOvl.Part.Hi = OperandOvl.Part.Lo;
    155      1.1.1.9  christos         OperandOvl.Part.Lo = 0;
    156      1.1.1.8  christos         Count = (Count & 63) - 32;
    157      1.1.1.8  christos     }
    158      1.1.1.8  christos     ACPI_SHIFT_LEFT_64_BY_32 (OperandOvl.Part.Hi,
    159      1.1.1.8  christos         OperandOvl.Part.Lo, Count);
    160      1.1.1.8  christos 
    161      1.1.1.8  christos     /* Return only what was requested */
    162      1.1.1.8  christos 
    163      1.1.1.8  christos     if (OutResult)
    164      1.1.1.8  christos     {
    165      1.1.1.8  christos         *OutResult = OperandOvl.Full;
    166      1.1.1.8  christos     }
    167      1.1.1.8  christos 
    168      1.1.1.8  christos     return_ACPI_STATUS (AE_OK);
    169      1.1.1.8  christos }
    170      1.1.1.8  christos 
    171      1.1.1.8  christos /*******************************************************************************
    172      1.1.1.8  christos  *
    173      1.1.1.8  christos  * FUNCTION:    AcpiUtShortShiftRight
    174      1.1.1.8  christos  *
    175      1.1.1.8  christos  * PARAMETERS:  Operand             - 64-bit shift operand
    176      1.1.1.8  christos  *              Count               - 32-bit shift count
    177      1.1.1.8  christos  *              OutResult           - Pointer to where the result is returned
    178      1.1.1.8  christos  *
    179      1.1.1.8  christos  * DESCRIPTION: Perform a short right shift.
    180      1.1.1.8  christos  *
    181      1.1.1.8  christos  ******************************************************************************/
    182      1.1.1.8  christos 
    183      1.1.1.8  christos ACPI_STATUS
    184      1.1.1.8  christos AcpiUtShortShiftRight (
    185      1.1.1.8  christos     UINT64                  Operand,
    186      1.1.1.8  christos     UINT32                  Count,
    187      1.1.1.8  christos     UINT64                  *OutResult)
    188      1.1.1.8  christos {
    189      1.1.1.8  christos     UINT64_OVERLAY          OperandOvl;
    190      1.1.1.8  christos 
    191      1.1.1.8  christos 
    192      1.1.1.8  christos     ACPI_FUNCTION_TRACE (UtShortShiftRight);
    193      1.1.1.8  christos 
    194      1.1.1.8  christos 
    195      1.1.1.8  christos     OperandOvl.Full = Operand;
    196      1.1.1.8  christos 
    197      1.1.1.8  christos     if ((Count & 63) >= 32)
    198      1.1.1.8  christos     {
    199      1.1.1.8  christos         OperandOvl.Part.Lo = OperandOvl.Part.Hi;
    200      1.1.1.9  christos         OperandOvl.Part.Hi = 0;
    201      1.1.1.8  christos         Count = (Count & 63) - 32;
    202      1.1.1.8  christos     }
    203      1.1.1.8  christos     ACPI_SHIFT_RIGHT_64_BY_32 (OperandOvl.Part.Hi,
    204      1.1.1.8  christos         OperandOvl.Part.Lo, Count);
    205      1.1.1.8  christos 
    206      1.1.1.8  christos     /* Return only what was requested */
    207      1.1.1.8  christos 
    208      1.1.1.8  christos     if (OutResult)
    209      1.1.1.8  christos     {
    210      1.1.1.8  christos         *OutResult = OperandOvl.Full;
    211      1.1.1.8  christos     }
    212      1.1.1.8  christos 
    213      1.1.1.8  christos     return_ACPI_STATUS (AE_OK);
    214      1.1.1.8  christos }
    215      1.1.1.8  christos #else
    216      1.1.1.8  christos 
    217      1.1.1.8  christos /*******************************************************************************
    218      1.1.1.8  christos  *
    219      1.1.1.8  christos  * FUNCTION:    AcpiUtShortMultiply
    220      1.1.1.8  christos  *
    221      1.1.1.8  christos  * PARAMETERS:  See function headers above
    222      1.1.1.8  christos  *
    223      1.1.1.8  christos  * DESCRIPTION: Native version of the UtShortMultiply function.
    224      1.1.1.8  christos  *
    225      1.1.1.8  christos  ******************************************************************************/
    226      1.1.1.8  christos 
    227      1.1.1.8  christos ACPI_STATUS
    228      1.1.1.8  christos AcpiUtShortMultiply (
    229      1.1.1.8  christos     UINT64                  Multiplicand,
    230      1.1.1.8  christos     UINT32                  Multiplier,
    231      1.1.1.8  christos     UINT64                  *OutProduct)
    232      1.1.1.8  christos {
    233      1.1.1.8  christos 
    234      1.1.1.8  christos     ACPI_FUNCTION_TRACE (UtShortMultiply);
    235      1.1.1.8  christos 
    236      1.1.1.8  christos 
    237      1.1.1.8  christos     /* Return only what was requested */
    238      1.1.1.8  christos 
    239      1.1.1.8  christos     if (OutProduct)
    240      1.1.1.8  christos     {
    241      1.1.1.8  christos         *OutProduct = Multiplicand * Multiplier;
    242      1.1.1.8  christos     }
    243      1.1.1.8  christos 
    244      1.1.1.8  christos     return_ACPI_STATUS (AE_OK);
    245      1.1.1.8  christos }
    246      1.1.1.8  christos 
    247      1.1.1.8  christos /*******************************************************************************
    248      1.1.1.8  christos  *
    249      1.1.1.8  christos  * FUNCTION:    AcpiUtShortShiftLeft
    250      1.1.1.8  christos  *
    251      1.1.1.8  christos  * PARAMETERS:  See function headers above
    252      1.1.1.8  christos  *
    253      1.1.1.8  christos  * DESCRIPTION: Native version of the UtShortShiftLeft function.
    254      1.1.1.8  christos  *
    255      1.1.1.8  christos  ******************************************************************************/
    256      1.1.1.8  christos 
    257      1.1.1.8  christos ACPI_STATUS
    258      1.1.1.8  christos AcpiUtShortShiftLeft (
    259      1.1.1.8  christos     UINT64                  Operand,
    260      1.1.1.8  christos     UINT32                  Count,
    261      1.1.1.8  christos     UINT64                  *OutResult)
    262      1.1.1.8  christos {
    263      1.1.1.8  christos 
    264      1.1.1.8  christos     ACPI_FUNCTION_TRACE (UtShortShiftLeft);
    265      1.1.1.8  christos 
    266      1.1.1.8  christos 
    267      1.1.1.8  christos     /* Return only what was requested */
    268      1.1.1.8  christos 
    269      1.1.1.8  christos     if (OutResult)
    270      1.1.1.8  christos     {
    271      1.1.1.8  christos         *OutResult = Operand << Count;
    272      1.1.1.8  christos     }
    273      1.1.1.8  christos 
    274      1.1.1.8  christos     return_ACPI_STATUS (AE_OK);
    275      1.1.1.8  christos }
    276      1.1.1.8  christos 
    277      1.1.1.8  christos /*******************************************************************************
    278      1.1.1.8  christos  *
    279      1.1.1.8  christos  * FUNCTION:    AcpiUtShortShiftRight
    280      1.1.1.8  christos  *
    281      1.1.1.8  christos  * PARAMETERS:  See function headers above
    282      1.1.1.8  christos  *
    283      1.1.1.8  christos  * DESCRIPTION: Native version of the UtShortShiftRight function.
    284      1.1.1.8  christos  *
    285      1.1.1.8  christos  ******************************************************************************/
    286      1.1.1.8  christos 
    287      1.1.1.8  christos ACPI_STATUS
    288      1.1.1.8  christos AcpiUtShortShiftRight (
    289      1.1.1.8  christos     UINT64                  Operand,
    290      1.1.1.8  christos     UINT32                  Count,
    291      1.1.1.8  christos     UINT64                  *OutResult)
    292      1.1.1.8  christos {
    293      1.1.1.8  christos 
    294      1.1.1.8  christos     ACPI_FUNCTION_TRACE (UtShortShiftRight);
    295      1.1.1.8  christos 
    296      1.1.1.8  christos 
    297      1.1.1.8  christos     /* Return only what was requested */
    298      1.1.1.8  christos 
    299      1.1.1.8  christos     if (OutResult)
    300      1.1.1.8  christos     {
    301      1.1.1.8  christos         *OutResult = Operand >> Count;
    302      1.1.1.8  christos     }
    303      1.1.1.8  christos 
    304      1.1.1.8  christos     return_ACPI_STATUS (AE_OK);
    305      1.1.1.8  christos }
    306      1.1.1.8  christos #endif
    307      1.1.1.8  christos 
    308      1.1.1.8  christos /*
    309      1.1.1.8  christos  * Optional support for 64-bit double-precision integer divide. This code
    310      1.1.1.8  christos  * is configurable and is implemented in order to support 32-bit kernel
    311      1.1.1.8  christos  * environments where a 64-bit double-precision math library is not available.
    312      1.1.1.8  christos  *
    313      1.1.1.8  christos  * Support for a more normal 64-bit divide/modulo (with check for a divide-
    314      1.1.1.8  christos  * by-zero) appears after this optional section of code.
    315      1.1.1.8  christos  */
    316      1.1.1.8  christos #ifndef ACPI_USE_NATIVE_DIVIDE
    317      1.1.1.8  christos 
    318      1.1.1.2    jruoho 
    319          1.1    jruoho /*******************************************************************************
    320          1.1    jruoho  *
    321          1.1    jruoho  * FUNCTION:    AcpiUtShortDivide
    322          1.1    jruoho  *
    323          1.1    jruoho  * PARAMETERS:  Dividend            - 64-bit dividend
    324          1.1    jruoho  *              Divisor             - 32-bit divisor
    325          1.1    jruoho  *              OutQuotient         - Pointer to where the quotient is returned
    326          1.1    jruoho  *              OutRemainder        - Pointer to where the remainder is returned
    327          1.1    jruoho  *
    328          1.1    jruoho  * RETURN:      Status (Checks for divide-by-zero)
    329          1.1    jruoho  *
    330          1.1    jruoho  * DESCRIPTION: Perform a short (maximum 64 bits divided by 32 bits)
    331      1.1.1.3  christos  *              divide and modulo. The result is a 64-bit quotient and a
    332          1.1    jruoho  *              32-bit remainder.
    333          1.1    jruoho  *
    334          1.1    jruoho  ******************************************************************************/
    335          1.1    jruoho 
    336          1.1    jruoho ACPI_STATUS
    337          1.1    jruoho AcpiUtShortDivide (
    338          1.1    jruoho     UINT64                  Dividend,
    339          1.1    jruoho     UINT32                  Divisor,
    340          1.1    jruoho     UINT64                  *OutQuotient,
    341          1.1    jruoho     UINT32                  *OutRemainder)
    342          1.1    jruoho {
    343          1.1    jruoho     UINT64_OVERLAY          DividendOvl;
    344          1.1    jruoho     UINT64_OVERLAY          Quotient;
    345          1.1    jruoho     UINT32                  Remainder32;
    346          1.1    jruoho 
    347          1.1    jruoho 
    348          1.1    jruoho     ACPI_FUNCTION_TRACE (UtShortDivide);
    349          1.1    jruoho 
    350          1.1    jruoho 
    351          1.1    jruoho     /* Always check for a zero divisor */
    352          1.1    jruoho 
    353          1.1    jruoho     if (Divisor == 0)
    354          1.1    jruoho     {
    355          1.1    jruoho         ACPI_ERROR ((AE_INFO, "Divide by zero"));
    356          1.1    jruoho         return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO);
    357          1.1    jruoho     }
    358          1.1    jruoho 
    359          1.1    jruoho     DividendOvl.Full = Dividend;
    360          1.1    jruoho 
    361          1.1    jruoho     /*
    362          1.1    jruoho      * The quotient is 64 bits, the remainder is always 32 bits,
    363          1.1    jruoho      * and is generated by the second divide.
    364          1.1    jruoho      */
    365          1.1    jruoho     ACPI_DIV_64_BY_32 (0, DividendOvl.Part.Hi, Divisor,
    366      1.1.1.6  christos         Quotient.Part.Hi, Remainder32);
    367      1.1.1.6  christos 
    368          1.1    jruoho     ACPI_DIV_64_BY_32 (Remainder32, DividendOvl.Part.Lo, Divisor,
    369      1.1.1.6  christos         Quotient.Part.Lo, Remainder32);
    370          1.1    jruoho 
    371          1.1    jruoho     /* Return only what was requested */
    372          1.1    jruoho 
    373          1.1    jruoho     if (OutQuotient)
    374          1.1    jruoho     {
    375          1.1    jruoho         *OutQuotient = Quotient.Full;
    376          1.1    jruoho     }
    377          1.1    jruoho     if (OutRemainder)
    378          1.1    jruoho     {
    379          1.1    jruoho         *OutRemainder = Remainder32;
    380          1.1    jruoho     }
    381          1.1    jruoho 
    382          1.1    jruoho     return_ACPI_STATUS (AE_OK);
    383          1.1    jruoho }
    384          1.1    jruoho 
    385          1.1    jruoho 
    386          1.1    jruoho /*******************************************************************************
    387          1.1    jruoho  *
    388          1.1    jruoho  * FUNCTION:    AcpiUtDivide
    389          1.1    jruoho  *
    390          1.1    jruoho  * PARAMETERS:  InDividend          - Dividend
    391          1.1    jruoho  *              InDivisor           - Divisor
    392          1.1    jruoho  *              OutQuotient         - Pointer to where the quotient is returned
    393          1.1    jruoho  *              OutRemainder        - Pointer to where the remainder is returned
    394          1.1    jruoho  *
    395          1.1    jruoho  * RETURN:      Status (Checks for divide-by-zero)
    396          1.1    jruoho  *
    397          1.1    jruoho  * DESCRIPTION: Perform a divide and modulo.
    398          1.1    jruoho  *
    399          1.1    jruoho  ******************************************************************************/
    400          1.1    jruoho 
    401          1.1    jruoho ACPI_STATUS
    402          1.1    jruoho AcpiUtDivide (
    403          1.1    jruoho     UINT64                  InDividend,
    404          1.1    jruoho     UINT64                  InDivisor,
    405          1.1    jruoho     UINT64                  *OutQuotient,
    406          1.1    jruoho     UINT64                  *OutRemainder)
    407          1.1    jruoho {
    408          1.1    jruoho     UINT64_OVERLAY          Dividend;
    409          1.1    jruoho     UINT64_OVERLAY          Divisor;
    410          1.1    jruoho     UINT64_OVERLAY          Quotient;
    411          1.1    jruoho     UINT64_OVERLAY          Remainder;
    412          1.1    jruoho     UINT64_OVERLAY          NormalizedDividend;
    413          1.1    jruoho     UINT64_OVERLAY          NormalizedDivisor;
    414          1.1    jruoho     UINT32                  Partial1;
    415          1.1    jruoho     UINT64_OVERLAY          Partial2;
    416          1.1    jruoho     UINT64_OVERLAY          Partial3;
    417          1.1    jruoho 
    418          1.1    jruoho 
    419          1.1    jruoho     ACPI_FUNCTION_TRACE (UtDivide);
    420          1.1    jruoho 
    421          1.1    jruoho 
    422          1.1    jruoho     /* Always check for a zero divisor */
    423          1.1    jruoho 
    424          1.1    jruoho     if (InDivisor == 0)
    425          1.1    jruoho     {
    426          1.1    jruoho         ACPI_ERROR ((AE_INFO, "Divide by zero"));
    427          1.1    jruoho         return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO);
    428          1.1    jruoho     }
    429          1.1    jruoho 
    430          1.1    jruoho     Divisor.Full  = InDivisor;
    431          1.1    jruoho     Dividend.Full = InDividend;
    432          1.1    jruoho     if (Divisor.Part.Hi == 0)
    433          1.1    jruoho     {
    434          1.1    jruoho         /*
    435          1.1    jruoho          * 1) Simplest case is where the divisor is 32 bits, we can
    436          1.1    jruoho          * just do two divides
    437          1.1    jruoho          */
    438          1.1    jruoho         Remainder.Part.Hi = 0;
    439          1.1    jruoho 
    440          1.1    jruoho         /*
    441          1.1    jruoho          * The quotient is 64 bits, the remainder is always 32 bits,
    442          1.1    jruoho          * and is generated by the second divide.
    443          1.1    jruoho          */
    444          1.1    jruoho         ACPI_DIV_64_BY_32 (0, Dividend.Part.Hi, Divisor.Part.Lo,
    445      1.1.1.6  christos             Quotient.Part.Hi, Partial1);
    446      1.1.1.6  christos 
    447          1.1    jruoho         ACPI_DIV_64_BY_32 (Partial1, Dividend.Part.Lo, Divisor.Part.Lo,
    448      1.1.1.6  christos             Quotient.Part.Lo, Remainder.Part.Lo);
    449          1.1    jruoho     }
    450          1.1    jruoho 
    451          1.1    jruoho     else
    452          1.1    jruoho     {
    453          1.1    jruoho         /*
    454          1.1    jruoho          * 2) The general case where the divisor is a full 64 bits
    455          1.1    jruoho          * is more difficult
    456          1.1    jruoho          */
    457          1.1    jruoho         Quotient.Part.Hi   = 0;
    458          1.1    jruoho         NormalizedDividend = Dividend;
    459          1.1    jruoho         NormalizedDivisor  = Divisor;
    460          1.1    jruoho 
    461          1.1    jruoho         /* Normalize the operands (shift until the divisor is < 32 bits) */
    462          1.1    jruoho 
    463          1.1    jruoho         do
    464          1.1    jruoho         {
    465      1.1.1.6  christos             ACPI_SHIFT_RIGHT_64 (
    466      1.1.1.6  christos                 NormalizedDivisor.Part.Hi, NormalizedDivisor.Part.Lo);
    467      1.1.1.6  christos             ACPI_SHIFT_RIGHT_64 (
    468      1.1.1.6  christos                 NormalizedDividend.Part.Hi, NormalizedDividend.Part.Lo);
    469          1.1    jruoho 
    470          1.1    jruoho         } while (NormalizedDivisor.Part.Hi != 0);
    471          1.1    jruoho 
    472          1.1    jruoho         /* Partial divide */
    473          1.1    jruoho 
    474      1.1.1.6  christos         ACPI_DIV_64_BY_32 (
    475      1.1.1.6  christos             NormalizedDividend.Part.Hi, NormalizedDividend.Part.Lo,
    476      1.1.1.6  christos             NormalizedDivisor.Part.Lo, Quotient.Part.Lo, Partial1);
    477          1.1    jruoho 
    478          1.1    jruoho         /*
    479      1.1.1.6  christos          * The quotient is always 32 bits, and simply requires
    480      1.1.1.6  christos          * adjustment. The 64-bit remainder must be generated.
    481          1.1    jruoho          */
    482      1.1.1.6  christos         Partial1 = Quotient.Part.Lo * Divisor.Part.Hi;
    483          1.1    jruoho         Partial2.Full = (UINT64) Quotient.Part.Lo * Divisor.Part.Lo;
    484          1.1    jruoho         Partial3.Full = (UINT64) Partial2.Part.Hi + Partial1;
    485          1.1    jruoho 
    486          1.1    jruoho         Remainder.Part.Hi = Partial3.Part.Lo;
    487          1.1    jruoho         Remainder.Part.Lo = Partial2.Part.Lo;
    488          1.1    jruoho 
    489          1.1    jruoho         if (Partial3.Part.Hi == 0)
    490          1.1    jruoho         {
    491          1.1    jruoho             if (Partial3.Part.Lo >= Dividend.Part.Hi)
    492          1.1    jruoho             {
    493          1.1    jruoho                 if (Partial3.Part.Lo == Dividend.Part.Hi)
    494          1.1    jruoho                 {
    495          1.1    jruoho                     if (Partial2.Part.Lo > Dividend.Part.Lo)
    496          1.1    jruoho                     {
    497          1.1    jruoho                         Quotient.Part.Lo--;
    498          1.1    jruoho                         Remainder.Full -= Divisor.Full;
    499          1.1    jruoho                     }
    500          1.1    jruoho                 }
    501          1.1    jruoho                 else
    502          1.1    jruoho                 {
    503          1.1    jruoho                     Quotient.Part.Lo--;
    504          1.1    jruoho                     Remainder.Full -= Divisor.Full;
    505          1.1    jruoho                 }
    506          1.1    jruoho             }
    507          1.1    jruoho 
    508      1.1.1.6  christos             Remainder.Full = Remainder.Full - Dividend.Full;
    509          1.1    jruoho             Remainder.Part.Hi = (UINT32) -((INT32) Remainder.Part.Hi);
    510          1.1    jruoho             Remainder.Part.Lo = (UINT32) -((INT32) Remainder.Part.Lo);
    511          1.1    jruoho 
    512          1.1    jruoho             if (Remainder.Part.Lo)
    513          1.1    jruoho             {
    514          1.1    jruoho                 Remainder.Part.Hi--;
    515          1.1    jruoho             }
    516          1.1    jruoho         }
    517          1.1    jruoho     }
    518          1.1    jruoho 
    519          1.1    jruoho     /* Return only what was requested */
    520          1.1    jruoho 
    521          1.1    jruoho     if (OutQuotient)
    522          1.1    jruoho     {
    523          1.1    jruoho         *OutQuotient = Quotient.Full;
    524          1.1    jruoho     }
    525          1.1    jruoho     if (OutRemainder)
    526          1.1    jruoho     {
    527          1.1    jruoho         *OutRemainder = Remainder.Full;
    528          1.1    jruoho     }
    529          1.1    jruoho 
    530          1.1    jruoho     return_ACPI_STATUS (AE_OK);
    531          1.1    jruoho }
    532          1.1    jruoho 
    533          1.1    jruoho #else
    534          1.1    jruoho 
    535          1.1    jruoho /*******************************************************************************
    536          1.1    jruoho  *
    537          1.1    jruoho  * FUNCTION:    AcpiUtShortDivide, AcpiUtDivide
    538          1.1    jruoho  *
    539          1.1    jruoho  * PARAMETERS:  See function headers above
    540          1.1    jruoho  *
    541          1.1    jruoho  * DESCRIPTION: Native versions of the UtDivide functions. Use these if either
    542          1.1    jruoho  *              1) The target is a 64-bit platform and therefore 64-bit
    543          1.1    jruoho  *                 integer math is supported directly by the machine.
    544          1.1    jruoho  *              2) The target is a 32-bit or 16-bit platform, and the
    545          1.1    jruoho  *                 double-precision integer math library is available to
    546          1.1    jruoho  *                 perform the divide.
    547          1.1    jruoho  *
    548          1.1    jruoho  ******************************************************************************/
    549          1.1    jruoho 
    550          1.1    jruoho ACPI_STATUS
    551          1.1    jruoho AcpiUtShortDivide (
    552          1.1    jruoho     UINT64                  InDividend,
    553          1.1    jruoho     UINT32                  Divisor,
    554          1.1    jruoho     UINT64                  *OutQuotient,
    555          1.1    jruoho     UINT32                  *OutRemainder)
    556          1.1    jruoho {
    557          1.1    jruoho 
    558          1.1    jruoho     ACPI_FUNCTION_TRACE (UtShortDivide);
    559          1.1    jruoho 
    560          1.1    jruoho 
    561          1.1    jruoho     /* Always check for a zero divisor */
    562          1.1    jruoho 
    563          1.1    jruoho     if (Divisor == 0)
    564          1.1    jruoho     {
    565          1.1    jruoho         ACPI_ERROR ((AE_INFO, "Divide by zero"));
    566          1.1    jruoho         return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO);
    567          1.1    jruoho     }
    568          1.1    jruoho 
    569          1.1    jruoho     /* Return only what was requested */
    570          1.1    jruoho 
    571          1.1    jruoho     if (OutQuotient)
    572          1.1    jruoho     {
    573          1.1    jruoho         *OutQuotient = InDividend / Divisor;
    574          1.1    jruoho     }
    575          1.1    jruoho     if (OutRemainder)
    576          1.1    jruoho     {
    577          1.1    jruoho         *OutRemainder = (UINT32) (InDividend % Divisor);
    578          1.1    jruoho     }
    579          1.1    jruoho 
    580          1.1    jruoho     return_ACPI_STATUS (AE_OK);
    581          1.1    jruoho }
    582          1.1    jruoho 
    583          1.1    jruoho ACPI_STATUS
    584          1.1    jruoho AcpiUtDivide (
    585          1.1    jruoho     UINT64                  InDividend,
    586          1.1    jruoho     UINT64                  InDivisor,
    587          1.1    jruoho     UINT64                  *OutQuotient,
    588          1.1    jruoho     UINT64                  *OutRemainder)
    589          1.1    jruoho {
    590          1.1    jruoho     ACPI_FUNCTION_TRACE (UtDivide);
    591          1.1    jruoho 
    592          1.1    jruoho 
    593          1.1    jruoho     /* Always check for a zero divisor */
    594          1.1    jruoho 
    595          1.1    jruoho     if (InDivisor == 0)
    596          1.1    jruoho     {
    597          1.1    jruoho         ACPI_ERROR ((AE_INFO, "Divide by zero"));
    598          1.1    jruoho         return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO);
    599          1.1    jruoho     }
    600          1.1    jruoho 
    601          1.1    jruoho 
    602          1.1    jruoho     /* Return only what was requested */
    603          1.1    jruoho 
    604          1.1    jruoho     if (OutQuotient)
    605          1.1    jruoho     {
    606          1.1    jruoho         *OutQuotient = InDividend / InDivisor;
    607          1.1    jruoho     }
    608          1.1    jruoho     if (OutRemainder)
    609          1.1    jruoho     {
    610          1.1    jruoho         *OutRemainder = InDividend % InDivisor;
    611          1.1    jruoho     }
    612          1.1    jruoho 
    613          1.1    jruoho     return_ACPI_STATUS (AE_OK);
    614          1.1    jruoho }
    615          1.1    jruoho 
    616          1.1    jruoho #endif
    617