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

     1  /* mpz_prodlimbs(RESULT, V, LEN) -- Set RESULT to V[0]*V[1]*...*V[LEN-1].
     2  
     3  Contributed to the GNU project by Marco Bodrato.
     4  
     5  THE FUNCTION IN THIS FILE IS INTERNAL WITH A MUTABLE INTERFACE.
     6  IT IS ONLY SAFE TO REACH IT THROUGH DOCUMENTED INTERFACES.
     7  IN FACT, IT IS ALMOST GUARANTEED THAT IT WILL CHANGE OR
     8  DISAPPEAR IN A FUTURE GNU MP RELEASE.
     9  
    10  Copyright 2010-2012 Free Software Foundation, Inc.
    11  
    12  This file is part of the GNU MP Library.
    13  
    14  The GNU MP Library is free software; you can redistribute it and/or modify
    15  it under the terms of either:
    16  
    17    * the GNU Lesser General Public License as published by the Free
    18      Software Foundation; either version 3 of the License, or (at your
    19      option) any later version.
    20  
    21  or
    22  
    23    * the GNU General Public License as published by the Free Software
    24      Foundation; either version 2 of the License, or (at your option) any
    25      later version.
    26  
    27  or both in parallel, as here.
    28  
    29  The GNU MP Library is distributed in the hope that it will be useful, but
    30  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    31  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    32  for more details.
    33  
    34  You should have received copies of the GNU General Public License and the
    35  GNU Lesser General Public License along with the GNU MP Library.  If not,
    36  see https://www.gnu.org/licenses/.  */
    37  
    38  #include "gmp.h"
    39  #include "gmp-impl.h"
    40  
    41  /*********************************************************/
    42  /* Section list-prod: product of a list -> mpz_t         */
    43  /*********************************************************/
    44  
    45  /* FIXME: should be tuned */
    46  #ifndef RECURSIVE_PROD_THRESHOLD
    47  #define RECURSIVE_PROD_THRESHOLD (MUL_TOOM22_THRESHOLD)
    48  #endif
    49  
    50  /* Computes the product of the j>1 limbs pointed by factors, puts the
    51   * result in x. It assumes that all limbs are non-zero. Above
    52   * Karatsuba's threshold it uses a binary splitting strategy, to gain
    53   * speed by the asymptotically fast multiplication algorithms.
    54   *
    55   * The list in  {factors, j} is overwritten.
    56   * Returns the size of the result
    57   */
    58  
    59  mp_size_t
    60  mpz_prodlimbs (mpz_ptr x, mp_ptr factors, mp_size_t j)
    61  {
    62    mp_limb_t cy;
    63    mp_size_t size, i;
    64    mp_ptr    prod;
    65  
    66    ASSERT (j > 1);
    67    ASSERT (RECURSIVE_PROD_THRESHOLD > 3);
    68  
    69    if (BELOW_THRESHOLD (j, RECURSIVE_PROD_THRESHOLD)) {
    70      j--;
    71      size = 1;
    72  
    73      for (i = 1; i < j; i++)
    74        {
    75  	cy = mpn_mul_1 (factors, factors, size, factors[i]);
    76  	factors[size] = cy;
    77  	size += cy != 0;
    78        };
    79  
    80      prod = MPZ_NEWALLOC (x, size + 1);
    81  
    82      cy = mpn_mul_1 (prod, factors, size, factors[i]);
    83      prod[size] = cy;
    84      return SIZ (x) = size + (cy != 0);
    85    } else {
    86      mpz_t x1, x2;
    87      TMP_DECL;
    88  
    89      i = j >> 1;
    90      j -= i;
    91      TMP_MARK;
    92  
    93      MPZ_TMP_INIT (x2, j);
    94  
    95      PTR (x1) = factors + i;
    96      ALLOC (x1) = j;
    97      j = mpz_prodlimbs (x2, factors + i, j);
    98      i = mpz_prodlimbs (x1, factors, i);
    99      size = i + j;
   100      prod = MPZ_NEWALLOC (x, size);
   101      if (i >= j)
   102        cy = mpn_mul (prod, PTR(x1), i, PTR(x2), j);
   103      else
   104        cy = mpn_mul (prod, PTR(x2), j, PTR(x1), i);
   105      TMP_FREE;
   106  
   107      return SIZ (x) = size - (cy == 0);
   108    }
   109  }