mul.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* mpn_mul -- Multiply two natural numbers.
  2. Copyright (C) 1991, 1993, 1994, 1996 Free Software Foundation, Inc.
  3. This file is part of the GNU MP Library.
  4. The GNU MP Library is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or (at your
  7. option) any later version.
  8. The GNU MP Library is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with the GNU MP Library; see the file COPYING.LIB. If not, write to
  14. the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  15. MA 02111-1307, USA. */
  16. #include <config.h>
  17. #include "gmp-impl.h"
  18. /* Multiply the natural numbers u (pointed to by UP, with USIZE limbs)
  19. and v (pointed to by VP, with VSIZE limbs), and store the result at
  20. PRODP. USIZE + VSIZE limbs are always stored, but if the input
  21. operands are normalized. Return the most significant limb of the
  22. result.
  23. NOTE: The space pointed to by PRODP is overwritten before finished
  24. with U and V, so overlap is an error.
  25. Argument constraints:
  26. 1. USIZE >= VSIZE.
  27. 2. PRODP != UP and PRODP != VP, i.e. the destination
  28. must be distinct from the multiplier and the multiplicand. */
  29. /* If KARATSUBA_THRESHOLD is not already defined, define it to a
  30. value which is good on most machines. */
  31. #ifndef KARATSUBA_THRESHOLD
  32. #define KARATSUBA_THRESHOLD 32
  33. #endif
  34. mp_limb_t
  35. #if __STDC__
  36. mpn_mul (mp_ptr prodp,
  37. mp_srcptr up, mp_size_t usize,
  38. mp_srcptr vp, mp_size_t vsize)
  39. #else
  40. mpn_mul (prodp, up, usize, vp, vsize)
  41. mp_ptr prodp;
  42. mp_srcptr up;
  43. mp_size_t usize;
  44. mp_srcptr vp;
  45. mp_size_t vsize;
  46. #endif
  47. {
  48. mp_ptr prod_endp = prodp + usize + vsize - 1;
  49. mp_limb_t cy;
  50. mp_ptr tspace;
  51. if (vsize < KARATSUBA_THRESHOLD)
  52. {
  53. /* Handle simple cases with traditional multiplication.
  54. This is the most critical code of the entire function. All
  55. multiplies rely on this, both small and huge. Small ones arrive
  56. here immediately. Huge ones arrive here as this is the base case
  57. for Karatsuba's recursive algorithm below. */
  58. mp_size_t i;
  59. mp_limb_t cy_limb;
  60. mp_limb_t v_limb;
  61. if (vsize == 0)
  62. return 0;
  63. /* Multiply by the first limb in V separately, as the result can be
  64. stored (not added) to PROD. We also avoid a loop for zeroing. */
  65. v_limb = vp[0];
  66. if (v_limb <= 1)
  67. {
  68. if (v_limb == 1)
  69. MPN_COPY (prodp, up, usize);
  70. else
  71. MPN_ZERO (prodp, usize);
  72. cy_limb = 0;
  73. }
  74. else
  75. cy_limb = mpn_mul_1 (prodp, up, usize, v_limb);
  76. prodp[usize] = cy_limb;
  77. prodp++;
  78. /* For each iteration in the outer loop, multiply one limb from
  79. U with one limb from V, and add it to PROD. */
  80. for (i = 1; i < vsize; i++)
  81. {
  82. v_limb = vp[i];
  83. if (v_limb <= 1)
  84. {
  85. cy_limb = 0;
  86. if (v_limb == 1)
  87. cy_limb = mpn_add_n (prodp, prodp, up, usize);
  88. }
  89. else
  90. cy_limb = mpn_addmul_1 (prodp, up, usize, v_limb);
  91. prodp[usize] = cy_limb;
  92. prodp++;
  93. }
  94. return cy_limb;
  95. }
  96. tspace = (mp_ptr) alloca (2 * vsize * BYTES_PER_MP_LIMB);
  97. MPN_MUL_N_RECURSE (prodp, up, vp, vsize, tspace);
  98. prodp += vsize;
  99. up += vsize;
  100. usize -= vsize;
  101. if (usize >= vsize)
  102. {
  103. mp_ptr tp = (mp_ptr) alloca (2 * vsize * BYTES_PER_MP_LIMB);
  104. do
  105. {
  106. MPN_MUL_N_RECURSE (tp, up, vp, vsize, tspace);
  107. cy = mpn_add_n (prodp, prodp, tp, vsize);
  108. mpn_add_1 (prodp + vsize, tp + vsize, vsize, cy);
  109. prodp += vsize;
  110. up += vsize;
  111. usize -= vsize;
  112. }
  113. while (usize >= vsize);
  114. }
  115. /* True: usize < vsize. */
  116. /* Make life simple: Recurse. */
  117. if (usize != 0)
  118. {
  119. mpn_mul (tspace, vp, vsize, up, usize);
  120. cy = mpn_add_n (prodp, prodp, tspace, vsize);
  121. mpn_add_1 (prodp + vsize, tspace + vsize, usize, cy);
  122. }
  123. return *prod_endp;
  124. }