(root)/
gmp-6.3.0/
mpz/
prodlimbs.c
       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-impl.h"
      39  
      40  /*********************************************************/
      41  /* Section list-prod: product of a list -> mpz_t         */
      42  /*********************************************************/
      43  
      44  /* FIXME: should be tuned */
      45  #ifndef RECURSIVE_PROD_THRESHOLD
      46  #define RECURSIVE_PROD_THRESHOLD (MUL_TOOM22_THRESHOLD)
      47  #endif
      48  
      49  /* Computes the product of the j>1 limbs pointed by factors, puts the
      50   * result in x. It assumes that all limbs are non-zero. Above
      51   * Karatsuba's threshold it uses a binary splitting strategy, to gain
      52   * speed by the asymptotically fast multiplication algorithms.
      53   *
      54   * The list in  {factors, j} is overwritten.
      55   * Returns the size of the result
      56   */
      57  
      58  mp_size_t
      59  mpz_prodlimbs (mpz_ptr x, mp_ptr factors, mp_size_t j)
      60  {
      61    mp_limb_t cy;
      62    mp_size_t size, i;
      63    mp_ptr    prod;
      64  
      65    ASSERT (j > 1);
      66    ASSERT (RECURSIVE_PROD_THRESHOLD > 3);
      67  
      68    if (BELOW_THRESHOLD (j, RECURSIVE_PROD_THRESHOLD)) {
      69      j--;
      70      size = 1;
      71  
      72      for (i = 1; i < j; i++)
      73        {
      74  	cy = mpn_mul_1 (factors, factors, size, factors[i]);
      75  	factors[size] = cy;
      76  	size += cy != 0;
      77        };
      78  
      79      prod = MPZ_NEWALLOC (x, size + 1);
      80  
      81      cy = mpn_mul_1 (prod, factors, size, factors[i]);
      82      prod[size] = cy;
      83      return SIZ (x) = size + (cy != 0);
      84    } else {
      85      mpz_t x1, x2;
      86      TMP_DECL;
      87  
      88      i = j >> 1;
      89      j -= i;
      90      TMP_MARK;
      91  
      92      MPZ_TMP_INIT (x2, j);
      93  
      94      PTR (x1) = factors + i;
      95      ALLOC (x1) = j;
      96      j = mpz_prodlimbs (x2, factors + i, j);
      97      i = mpz_prodlimbs (x1, factors, i);
      98      size = i + j;
      99      prod = MPZ_NEWALLOC (x, size);
     100      if (i >= j)
     101        cy = mpn_mul (prod, PTR(x1), i, PTR(x2), j);
     102      else
     103        cy = mpn_mul (prod, PTR(x2), j, PTR(x1), i);
     104      TMP_FREE;
     105  
     106      return SIZ (x) = size - (cy == 0);
     107    }
     108  }