Blame stdlib/mul.c

Packit 6c4009
/* mpn_mul -- Multiply two natural numbers.
Packit 6c4009
Packit 6c4009
Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit 6c4009
Packit 6c4009
This file is part of the GNU MP Library.
Packit 6c4009
Packit 6c4009
The GNU MP Library is free software; you can redistribute it and/or modify
Packit 6c4009
it under the terms of the GNU Lesser General Public License as published by
Packit 6c4009
the Free Software Foundation; either version 2.1 of the License, or (at your
Packit 6c4009
option) any later version.
Packit 6c4009
Packit 6c4009
The GNU MP Library is distributed in the hope that it will be useful, but
Packit 6c4009
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
Packit 6c4009
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
Packit 6c4009
License for more details.
Packit 6c4009
Packit 6c4009
You should have received a copy of the GNU Lesser General Public License
Packit 6c4009
along with the GNU MP Library; see the file COPYING.LIB.  If not, see
Packit 6c4009
<http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <gmp.h>
Packit 6c4009
#include "gmp-impl.h"
Packit 6c4009
Packit 6c4009
/* Multiply the natural numbers u (pointed to by UP, with USIZE limbs)
Packit 6c4009
   and v (pointed to by VP, with VSIZE limbs), and store the result at
Packit 6c4009
   PRODP.  USIZE + VSIZE limbs are always stored, but if the input
Packit 6c4009
   operands are normalized.  Return the most significant limb of the
Packit 6c4009
   result.
Packit 6c4009
Packit 6c4009
   NOTE: The space pointed to by PRODP is overwritten before finished
Packit 6c4009
   with U and V, so overlap is an error.
Packit 6c4009
Packit 6c4009
   Argument constraints:
Packit 6c4009
   1. USIZE >= VSIZE.
Packit 6c4009
   2. PRODP != UP and PRODP != VP, i.e. the destination
Packit 6c4009
      must be distinct from the multiplier and the multiplicand.  */
Packit 6c4009
Packit 6c4009
/* If KARATSUBA_THRESHOLD is not already defined, define it to a
Packit 6c4009
   value which is good on most machines.  */
Packit 6c4009
#ifndef KARATSUBA_THRESHOLD
Packit 6c4009
#define KARATSUBA_THRESHOLD 32
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
mp_limb_t
Packit 6c4009
mpn_mul (mp_ptr prodp,
Packit 6c4009
	 mp_srcptr up, mp_size_t usize,
Packit 6c4009
	 mp_srcptr vp, mp_size_t vsize)
Packit 6c4009
{
Packit 6c4009
  mp_ptr prod_endp = prodp + usize + vsize - 1;
Packit 6c4009
  mp_limb_t cy;
Packit 6c4009
  mp_ptr tspace;
Packit 6c4009
  TMP_DECL (marker);
Packit 6c4009
Packit 6c4009
  if (vsize < KARATSUBA_THRESHOLD)
Packit 6c4009
    {
Packit 6c4009
      /* Handle simple cases with traditional multiplication.
Packit 6c4009
Packit 6c4009
	 This is the most critical code of the entire function.  All
Packit 6c4009
	 multiplies rely on this, both small and huge.  Small ones arrive
Packit 6c4009
	 here immediately.  Huge ones arrive here as this is the base case
Packit 6c4009
	 for Karatsuba's recursive algorithm below.  */
Packit 6c4009
      mp_size_t i;
Packit 6c4009
      mp_limb_t cy_limb;
Packit 6c4009
      mp_limb_t v_limb;
Packit 6c4009
Packit 6c4009
      if (vsize == 0)
Packit 6c4009
	return 0;
Packit 6c4009
Packit 6c4009
      /* Multiply by the first limb in V separately, as the result can be
Packit 6c4009
	 stored (not added) to PROD.  We also avoid a loop for zeroing.  */
Packit 6c4009
      v_limb = vp[0];
Packit 6c4009
      if (v_limb <= 1)
Packit 6c4009
	{
Packit 6c4009
	  if (v_limb == 1)
Packit 6c4009
	    MPN_COPY (prodp, up, usize);
Packit 6c4009
	  else
Packit 6c4009
	    MPN_ZERO (prodp, usize);
Packit 6c4009
	  cy_limb = 0;
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	cy_limb = mpn_mul_1 (prodp, up, usize, v_limb);
Packit 6c4009
Packit 6c4009
      prodp[usize] = cy_limb;
Packit 6c4009
      prodp++;
Packit 6c4009
Packit 6c4009
      /* For each iteration in the outer loop, multiply one limb from
Packit 6c4009
	 U with one limb from V, and add it to PROD.  */
Packit 6c4009
      for (i = 1; i < vsize; i++)
Packit 6c4009
	{
Packit 6c4009
	  v_limb = vp[i];
Packit 6c4009
	  if (v_limb <= 1)
Packit 6c4009
	    {
Packit 6c4009
	      cy_limb = 0;
Packit 6c4009
	      if (v_limb == 1)
Packit 6c4009
		cy_limb = mpn_add_n (prodp, prodp, up, usize);
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    cy_limb = mpn_addmul_1 (prodp, up, usize, v_limb);
Packit 6c4009
Packit 6c4009
	  prodp[usize] = cy_limb;
Packit 6c4009
	  prodp++;
Packit 6c4009
	}
Packit 6c4009
      return cy_limb;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  TMP_MARK (marker);
Packit 6c4009
Packit 6c4009
  tspace = (mp_ptr) TMP_ALLOC (2 * vsize * BYTES_PER_MP_LIMB);
Packit 6c4009
  MPN_MUL_N_RECURSE (prodp, up, vp, vsize, tspace);
Packit 6c4009
Packit 6c4009
  prodp += vsize;
Packit 6c4009
  up += vsize;
Packit 6c4009
  usize -= vsize;
Packit 6c4009
  if (usize >= vsize)
Packit 6c4009
    {
Packit 6c4009
      mp_ptr tp = (mp_ptr) TMP_ALLOC (2 * vsize * BYTES_PER_MP_LIMB);
Packit 6c4009
      do
Packit 6c4009
	{
Packit 6c4009
	  MPN_MUL_N_RECURSE (tp, up, vp, vsize, tspace);
Packit 6c4009
	  cy = mpn_add_n (prodp, prodp, tp, vsize);
Packit 6c4009
	  mpn_add_1 (prodp + vsize, tp + vsize, vsize, cy);
Packit 6c4009
	  prodp += vsize;
Packit 6c4009
	  up += vsize;
Packit 6c4009
	  usize -= vsize;
Packit 6c4009
	}
Packit 6c4009
      while (usize >= vsize);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* True: usize < vsize.  */
Packit 6c4009
Packit 6c4009
  /* Make life simple: Recurse.  */
Packit 6c4009
Packit 6c4009
  if (usize != 0)
Packit 6c4009
    {
Packit 6c4009
      mpn_mul (tspace, vp, vsize, up, usize);
Packit 6c4009
      cy = mpn_add_n (prodp, prodp, tspace, vsize);
Packit 6c4009
      mpn_add_1 (prodp + vsize, tspace + vsize, usize, cy);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  TMP_FREE (marker);
Packit 6c4009
  return *prod_endp;
Packit 6c4009
}