github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/mpz/mul_i.h (about)

     1  /* mpz_mul_ui/si (product, multiplier, small_multiplicand) -- Set PRODUCT to
     2     MULTIPLICATOR times SMALL_MULTIPLICAND.
     3  
     4  Copyright 1991, 1993, 1994, 1996, 2000-2002, 2005, 2008, 2012 Free Software
     5  Foundation, Inc.
     6  
     7  This file is part of the GNU MP Library.
     8  
     9  The GNU MP Library is free software; you can redistribute it and/or modify
    10  it under the terms of either:
    11  
    12    * the GNU Lesser General Public License as published by the Free
    13      Software Foundation; either version 3 of the License, or (at your
    14      option) any later version.
    15  
    16  or
    17  
    18    * the GNU General Public License as published by the Free Software
    19      Foundation; either version 2 of the License, or (at your option) any
    20      later version.
    21  
    22  or both in parallel, as here.
    23  
    24  The GNU MP Library is distributed in the hope that it will be useful, but
    25  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    26  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    27  for more details.
    28  
    29  You should have received copies of the GNU General Public License and the
    30  GNU Lesser General Public License along with the GNU MP Library.  If not,
    31  see https://www.gnu.org/licenses/.  */
    32  
    33  #include "gmp.h"
    34  #include "gmp-impl.h"
    35  
    36  
    37  #ifdef OPERATION_mul_si
    38  #define FUNCTION               mpz_mul_si
    39  #define MULTIPLICAND_UNSIGNED
    40  #define MULTIPLICAND_ABS(x)    ABS_CAST(unsigned long, (x))
    41  #endif
    42  
    43  #ifdef OPERATION_mul_ui
    44  #define FUNCTION               mpz_mul_ui
    45  #define MULTIPLICAND_UNSIGNED  unsigned
    46  #define MULTIPLICAND_ABS(x)    x
    47  #endif
    48  
    49  #ifndef FUNCTION
    50  Error, error, unrecognised OPERATION
    51  #endif
    52  
    53  
    54  void
    55  FUNCTION (mpz_ptr prod, mpz_srcptr mult,
    56            MULTIPLICAND_UNSIGNED long int small_mult)
    57  {
    58    mp_size_t size;
    59    mp_size_t sign_product;
    60    mp_limb_t sml;
    61    mp_limb_t cy;
    62    mp_ptr pp;
    63  
    64    sign_product = SIZ(mult);
    65    if (sign_product == 0 || small_mult == 0)
    66      {
    67        SIZ(prod) = 0;
    68        return;
    69      }
    70  
    71    size = ABS (sign_product);
    72  
    73    sml = MULTIPLICAND_ABS (small_mult);
    74  
    75    if (sml <= GMP_NUMB_MAX)
    76      {
    77        pp = MPZ_REALLOC (prod, size + 1);
    78        cy = mpn_mul_1 (pp, PTR(mult), size, sml);
    79        pp[size] = cy;
    80        size += cy != 0;
    81      }
    82  #if GMP_NAIL_BITS != 0
    83    else
    84      {
    85        /* Operand too large for the current nails size.  Use temporary for
    86  	 intermediate products, to allow prod and mult being identical.  */
    87        mp_ptr tp;
    88        TMP_DECL;
    89        TMP_MARK;
    90  
    91        tp = TMP_ALLOC_LIMBS (size + 2);
    92  
    93        /* Use, maybe, mpn_mul_2? */
    94        cy = mpn_mul_1 (tp, PTR(mult), size, sml & GMP_NUMB_MASK);
    95        tp[size] = cy;
    96        cy = mpn_addmul_1 (tp + 1, PTR(mult), size, sml >> GMP_NUMB_BITS);
    97        tp[size + 1] = cy;
    98        size += 2;
    99        MPN_NORMALIZE_NOT_ZERO (tp, size); /* too general, need to trim one or two limb */
   100        pp = MPZ_REALLOC (prod, size);
   101        MPN_COPY (pp, tp, size);
   102        TMP_FREE;
   103      }
   104  #endif
   105  
   106    SIZ(prod) = ((sign_product < 0) ^ (small_mult < 0)) ? -size : size;
   107  }