Home | History | Annotate | Line # | Download | only in utilities
utmath.c revision 1.1.1.6
      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.6  christos  * Copyright (C) 2000 - 2016, 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    jruoho /*
     52  1.1.1.2    jruoho  * Optional support for 64-bit double-precision integer divide. This code
     53  1.1.1.2    jruoho  * is configurable and is implemented in order to support 32-bit kernel
     54  1.1.1.2    jruoho  * environments where a 64-bit double-precision math library is not available.
     55  1.1.1.2    jruoho  *
     56  1.1.1.2    jruoho  * Support for a more normal 64-bit divide/modulo (with check for a divide-
     57  1.1.1.2    jruoho  * by-zero) appears after this optional section of code.
     58      1.1    jruoho  */
     59      1.1    jruoho #ifndef ACPI_USE_NATIVE_DIVIDE
     60  1.1.1.2    jruoho 
     61  1.1.1.2    jruoho /* Structures used only for 64-bit divide */
     62  1.1.1.2    jruoho 
     63  1.1.1.2    jruoho typedef struct uint64_struct
     64  1.1.1.2    jruoho {
     65  1.1.1.2    jruoho     UINT32                          Lo;
     66  1.1.1.2    jruoho     UINT32                          Hi;
     67  1.1.1.2    jruoho 
     68  1.1.1.2    jruoho } UINT64_STRUCT;
     69  1.1.1.2    jruoho 
     70  1.1.1.2    jruoho typedef union uint64_overlay
     71  1.1.1.2    jruoho {
     72  1.1.1.2    jruoho     UINT64                          Full;
     73  1.1.1.2    jruoho     UINT64_STRUCT                   Part;
     74  1.1.1.2    jruoho 
     75  1.1.1.2    jruoho } UINT64_OVERLAY;
     76  1.1.1.2    jruoho 
     77  1.1.1.2    jruoho 
     78      1.1    jruoho /*******************************************************************************
     79      1.1    jruoho  *
     80      1.1    jruoho  * FUNCTION:    AcpiUtShortDivide
     81      1.1    jruoho  *
     82      1.1    jruoho  * PARAMETERS:  Dividend            - 64-bit dividend
     83      1.1    jruoho  *              Divisor             - 32-bit divisor
     84      1.1    jruoho  *              OutQuotient         - Pointer to where the quotient is returned
     85      1.1    jruoho  *              OutRemainder        - Pointer to where the remainder is returned
     86      1.1    jruoho  *
     87      1.1    jruoho  * RETURN:      Status (Checks for divide-by-zero)
     88      1.1    jruoho  *
     89      1.1    jruoho  * DESCRIPTION: Perform a short (maximum 64 bits divided by 32 bits)
     90  1.1.1.3  christos  *              divide and modulo. The result is a 64-bit quotient and a
     91      1.1    jruoho  *              32-bit remainder.
     92      1.1    jruoho  *
     93      1.1    jruoho  ******************************************************************************/
     94      1.1    jruoho 
     95      1.1    jruoho ACPI_STATUS
     96      1.1    jruoho AcpiUtShortDivide (
     97      1.1    jruoho     UINT64                  Dividend,
     98      1.1    jruoho     UINT32                  Divisor,
     99      1.1    jruoho     UINT64                  *OutQuotient,
    100      1.1    jruoho     UINT32                  *OutRemainder)
    101      1.1    jruoho {
    102      1.1    jruoho     UINT64_OVERLAY          DividendOvl;
    103      1.1    jruoho     UINT64_OVERLAY          Quotient;
    104      1.1    jruoho     UINT32                  Remainder32;
    105      1.1    jruoho 
    106      1.1    jruoho 
    107      1.1    jruoho     ACPI_FUNCTION_TRACE (UtShortDivide);
    108      1.1    jruoho 
    109      1.1    jruoho 
    110      1.1    jruoho     /* Always check for a zero divisor */
    111      1.1    jruoho 
    112      1.1    jruoho     if (Divisor == 0)
    113      1.1    jruoho     {
    114      1.1    jruoho         ACPI_ERROR ((AE_INFO, "Divide by zero"));
    115      1.1    jruoho         return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO);
    116      1.1    jruoho     }
    117      1.1    jruoho 
    118      1.1    jruoho     DividendOvl.Full = Dividend;
    119      1.1    jruoho 
    120      1.1    jruoho     /*
    121      1.1    jruoho      * The quotient is 64 bits, the remainder is always 32 bits,
    122      1.1    jruoho      * and is generated by the second divide.
    123      1.1    jruoho      */
    124      1.1    jruoho     ACPI_DIV_64_BY_32 (0, DividendOvl.Part.Hi, Divisor,
    125  1.1.1.6  christos         Quotient.Part.Hi, Remainder32);
    126  1.1.1.6  christos 
    127      1.1    jruoho     ACPI_DIV_64_BY_32 (Remainder32, DividendOvl.Part.Lo, Divisor,
    128  1.1.1.6  christos         Quotient.Part.Lo, Remainder32);
    129      1.1    jruoho 
    130      1.1    jruoho     /* Return only what was requested */
    131      1.1    jruoho 
    132      1.1    jruoho     if (OutQuotient)
    133      1.1    jruoho     {
    134      1.1    jruoho         *OutQuotient = Quotient.Full;
    135      1.1    jruoho     }
    136      1.1    jruoho     if (OutRemainder)
    137      1.1    jruoho     {
    138      1.1    jruoho         *OutRemainder = Remainder32;
    139      1.1    jruoho     }
    140      1.1    jruoho 
    141      1.1    jruoho     return_ACPI_STATUS (AE_OK);
    142      1.1    jruoho }
    143      1.1    jruoho 
    144      1.1    jruoho 
    145      1.1    jruoho /*******************************************************************************
    146      1.1    jruoho  *
    147      1.1    jruoho  * FUNCTION:    AcpiUtDivide
    148      1.1    jruoho  *
    149      1.1    jruoho  * PARAMETERS:  InDividend          - Dividend
    150      1.1    jruoho  *              InDivisor           - Divisor
    151      1.1    jruoho  *              OutQuotient         - Pointer to where the quotient is returned
    152      1.1    jruoho  *              OutRemainder        - Pointer to where the remainder is returned
    153      1.1    jruoho  *
    154      1.1    jruoho  * RETURN:      Status (Checks for divide-by-zero)
    155      1.1    jruoho  *
    156      1.1    jruoho  * DESCRIPTION: Perform a divide and modulo.
    157      1.1    jruoho  *
    158      1.1    jruoho  ******************************************************************************/
    159      1.1    jruoho 
    160      1.1    jruoho ACPI_STATUS
    161      1.1    jruoho AcpiUtDivide (
    162      1.1    jruoho     UINT64                  InDividend,
    163      1.1    jruoho     UINT64                  InDivisor,
    164      1.1    jruoho     UINT64                  *OutQuotient,
    165      1.1    jruoho     UINT64                  *OutRemainder)
    166      1.1    jruoho {
    167      1.1    jruoho     UINT64_OVERLAY          Dividend;
    168      1.1    jruoho     UINT64_OVERLAY          Divisor;
    169      1.1    jruoho     UINT64_OVERLAY          Quotient;
    170      1.1    jruoho     UINT64_OVERLAY          Remainder;
    171      1.1    jruoho     UINT64_OVERLAY          NormalizedDividend;
    172      1.1    jruoho     UINT64_OVERLAY          NormalizedDivisor;
    173      1.1    jruoho     UINT32                  Partial1;
    174      1.1    jruoho     UINT64_OVERLAY          Partial2;
    175      1.1    jruoho     UINT64_OVERLAY          Partial3;
    176      1.1    jruoho 
    177      1.1    jruoho 
    178      1.1    jruoho     ACPI_FUNCTION_TRACE (UtDivide);
    179      1.1    jruoho 
    180      1.1    jruoho 
    181      1.1    jruoho     /* Always check for a zero divisor */
    182      1.1    jruoho 
    183      1.1    jruoho     if (InDivisor == 0)
    184      1.1    jruoho     {
    185      1.1    jruoho         ACPI_ERROR ((AE_INFO, "Divide by zero"));
    186      1.1    jruoho         return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO);
    187      1.1    jruoho     }
    188      1.1    jruoho 
    189      1.1    jruoho     Divisor.Full  = InDivisor;
    190      1.1    jruoho     Dividend.Full = InDividend;
    191      1.1    jruoho     if (Divisor.Part.Hi == 0)
    192      1.1    jruoho     {
    193      1.1    jruoho         /*
    194      1.1    jruoho          * 1) Simplest case is where the divisor is 32 bits, we can
    195      1.1    jruoho          * just do two divides
    196      1.1    jruoho          */
    197      1.1    jruoho         Remainder.Part.Hi = 0;
    198      1.1    jruoho 
    199      1.1    jruoho         /*
    200      1.1    jruoho          * The quotient is 64 bits, the remainder is always 32 bits,
    201      1.1    jruoho          * and is generated by the second divide.
    202      1.1    jruoho          */
    203      1.1    jruoho         ACPI_DIV_64_BY_32 (0, Dividend.Part.Hi, Divisor.Part.Lo,
    204  1.1.1.6  christos             Quotient.Part.Hi, Partial1);
    205  1.1.1.6  christos 
    206      1.1    jruoho         ACPI_DIV_64_BY_32 (Partial1, Dividend.Part.Lo, Divisor.Part.Lo,
    207  1.1.1.6  christos             Quotient.Part.Lo, Remainder.Part.Lo);
    208      1.1    jruoho     }
    209      1.1    jruoho 
    210      1.1    jruoho     else
    211      1.1    jruoho     {
    212      1.1    jruoho         /*
    213      1.1    jruoho          * 2) The general case where the divisor is a full 64 bits
    214      1.1    jruoho          * is more difficult
    215      1.1    jruoho          */
    216      1.1    jruoho         Quotient.Part.Hi   = 0;
    217      1.1    jruoho         NormalizedDividend = Dividend;
    218      1.1    jruoho         NormalizedDivisor  = Divisor;
    219      1.1    jruoho 
    220      1.1    jruoho         /* Normalize the operands (shift until the divisor is < 32 bits) */
    221      1.1    jruoho 
    222      1.1    jruoho         do
    223      1.1    jruoho         {
    224  1.1.1.6  christos             ACPI_SHIFT_RIGHT_64 (
    225  1.1.1.6  christos                 NormalizedDivisor.Part.Hi, NormalizedDivisor.Part.Lo);
    226  1.1.1.6  christos             ACPI_SHIFT_RIGHT_64 (
    227  1.1.1.6  christos                 NormalizedDividend.Part.Hi, NormalizedDividend.Part.Lo);
    228      1.1    jruoho 
    229      1.1    jruoho         } while (NormalizedDivisor.Part.Hi != 0);
    230      1.1    jruoho 
    231      1.1    jruoho         /* Partial divide */
    232      1.1    jruoho 
    233  1.1.1.6  christos         ACPI_DIV_64_BY_32 (
    234  1.1.1.6  christos             NormalizedDividend.Part.Hi, NormalizedDividend.Part.Lo,
    235  1.1.1.6  christos             NormalizedDivisor.Part.Lo, Quotient.Part.Lo, Partial1);
    236      1.1    jruoho 
    237      1.1    jruoho         /*
    238  1.1.1.6  christos          * The quotient is always 32 bits, and simply requires
    239  1.1.1.6  christos          * adjustment. The 64-bit remainder must be generated.
    240      1.1    jruoho          */
    241  1.1.1.6  christos         Partial1 = Quotient.Part.Lo * Divisor.Part.Hi;
    242      1.1    jruoho         Partial2.Full = (UINT64) Quotient.Part.Lo * Divisor.Part.Lo;
    243      1.1    jruoho         Partial3.Full = (UINT64) Partial2.Part.Hi + Partial1;
    244      1.1    jruoho 
    245      1.1    jruoho         Remainder.Part.Hi = Partial3.Part.Lo;
    246      1.1    jruoho         Remainder.Part.Lo = Partial2.Part.Lo;
    247      1.1    jruoho 
    248      1.1    jruoho         if (Partial3.Part.Hi == 0)
    249      1.1    jruoho         {
    250      1.1    jruoho             if (Partial3.Part.Lo >= Dividend.Part.Hi)
    251      1.1    jruoho             {
    252      1.1    jruoho                 if (Partial3.Part.Lo == Dividend.Part.Hi)
    253      1.1    jruoho                 {
    254      1.1    jruoho                     if (Partial2.Part.Lo > Dividend.Part.Lo)
    255      1.1    jruoho                     {
    256      1.1    jruoho                         Quotient.Part.Lo--;
    257      1.1    jruoho                         Remainder.Full -= Divisor.Full;
    258      1.1    jruoho                     }
    259      1.1    jruoho                 }
    260      1.1    jruoho                 else
    261      1.1    jruoho                 {
    262      1.1    jruoho                     Quotient.Part.Lo--;
    263      1.1    jruoho                     Remainder.Full -= Divisor.Full;
    264      1.1    jruoho                 }
    265      1.1    jruoho             }
    266      1.1    jruoho 
    267  1.1.1.6  christos             Remainder.Full = Remainder.Full - Dividend.Full;
    268      1.1    jruoho             Remainder.Part.Hi = (UINT32) -((INT32) Remainder.Part.Hi);
    269      1.1    jruoho             Remainder.Part.Lo = (UINT32) -((INT32) Remainder.Part.Lo);
    270      1.1    jruoho 
    271      1.1    jruoho             if (Remainder.Part.Lo)
    272      1.1    jruoho             {
    273      1.1    jruoho                 Remainder.Part.Hi--;
    274      1.1    jruoho             }
    275      1.1    jruoho         }
    276      1.1    jruoho     }
    277      1.1    jruoho 
    278      1.1    jruoho     /* Return only what was requested */
    279      1.1    jruoho 
    280      1.1    jruoho     if (OutQuotient)
    281      1.1    jruoho     {
    282      1.1    jruoho         *OutQuotient = Quotient.Full;
    283      1.1    jruoho     }
    284      1.1    jruoho     if (OutRemainder)
    285      1.1    jruoho     {
    286      1.1    jruoho         *OutRemainder = Remainder.Full;
    287      1.1    jruoho     }
    288      1.1    jruoho 
    289      1.1    jruoho     return_ACPI_STATUS (AE_OK);
    290      1.1    jruoho }
    291      1.1    jruoho 
    292      1.1    jruoho #else
    293      1.1    jruoho 
    294      1.1    jruoho /*******************************************************************************
    295      1.1    jruoho  *
    296      1.1    jruoho  * FUNCTION:    AcpiUtShortDivide, AcpiUtDivide
    297      1.1    jruoho  *
    298      1.1    jruoho  * PARAMETERS:  See function headers above
    299      1.1    jruoho  *
    300      1.1    jruoho  * DESCRIPTION: Native versions of the UtDivide functions. Use these if either
    301      1.1    jruoho  *              1) The target is a 64-bit platform and therefore 64-bit
    302      1.1    jruoho  *                 integer math is supported directly by the machine.
    303      1.1    jruoho  *              2) The target is a 32-bit or 16-bit platform, and the
    304      1.1    jruoho  *                 double-precision integer math library is available to
    305      1.1    jruoho  *                 perform the divide.
    306      1.1    jruoho  *
    307      1.1    jruoho  ******************************************************************************/
    308      1.1    jruoho 
    309      1.1    jruoho ACPI_STATUS
    310      1.1    jruoho AcpiUtShortDivide (
    311      1.1    jruoho     UINT64                  InDividend,
    312      1.1    jruoho     UINT32                  Divisor,
    313      1.1    jruoho     UINT64                  *OutQuotient,
    314      1.1    jruoho     UINT32                  *OutRemainder)
    315      1.1    jruoho {
    316      1.1    jruoho 
    317      1.1    jruoho     ACPI_FUNCTION_TRACE (UtShortDivide);
    318      1.1    jruoho 
    319      1.1    jruoho 
    320      1.1    jruoho     /* Always check for a zero divisor */
    321      1.1    jruoho 
    322      1.1    jruoho     if (Divisor == 0)
    323      1.1    jruoho     {
    324      1.1    jruoho         ACPI_ERROR ((AE_INFO, "Divide by zero"));
    325      1.1    jruoho         return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO);
    326      1.1    jruoho     }
    327      1.1    jruoho 
    328      1.1    jruoho     /* Return only what was requested */
    329      1.1    jruoho 
    330      1.1    jruoho     if (OutQuotient)
    331      1.1    jruoho     {
    332      1.1    jruoho         *OutQuotient = InDividend / Divisor;
    333      1.1    jruoho     }
    334      1.1    jruoho     if (OutRemainder)
    335      1.1    jruoho     {
    336      1.1    jruoho         *OutRemainder = (UINT32) (InDividend % Divisor);
    337      1.1    jruoho     }
    338      1.1    jruoho 
    339      1.1    jruoho     return_ACPI_STATUS (AE_OK);
    340      1.1    jruoho }
    341      1.1    jruoho 
    342      1.1    jruoho ACPI_STATUS
    343      1.1    jruoho AcpiUtDivide (
    344      1.1    jruoho     UINT64                  InDividend,
    345      1.1    jruoho     UINT64                  InDivisor,
    346      1.1    jruoho     UINT64                  *OutQuotient,
    347      1.1    jruoho     UINT64                  *OutRemainder)
    348      1.1    jruoho {
    349      1.1    jruoho     ACPI_FUNCTION_TRACE (UtDivide);
    350      1.1    jruoho 
    351      1.1    jruoho 
    352      1.1    jruoho     /* Always check for a zero divisor */
    353      1.1    jruoho 
    354      1.1    jruoho     if (InDivisor == 0)
    355      1.1    jruoho     {
    356      1.1    jruoho         ACPI_ERROR ((AE_INFO, "Divide by zero"));
    357      1.1    jruoho         return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO);
    358      1.1    jruoho     }
    359      1.1    jruoho 
    360      1.1    jruoho 
    361      1.1    jruoho     /* Return only what was requested */
    362      1.1    jruoho 
    363      1.1    jruoho     if (OutQuotient)
    364      1.1    jruoho     {
    365      1.1    jruoho         *OutQuotient = InDividend / InDivisor;
    366      1.1    jruoho     }
    367      1.1    jruoho     if (OutRemainder)
    368      1.1    jruoho     {
    369      1.1    jruoho         *OutRemainder = InDividend % InDivisor;
    370      1.1    jruoho     }
    371      1.1    jruoho 
    372      1.1    jruoho     return_ACPI_STATUS (AE_OK);
    373      1.1    jruoho }
    374      1.1    jruoho 
    375      1.1    jruoho #endif
    376