1 /* mpn_mul_basecase -- Internal routine to multiply two natural numbers 2 of length m and n. 3 4 THIS IS AN INTERNAL FUNCTION WITH A MUTABLE INTERFACE. IT IS ONLY 5 SAFE TO REACH THIS FUNCTION THROUGH DOCUMENTED INTERFACES. 6 7 Copyright 1991, 1992, 1993, 1994, 1996, 1997, 2000, 2001, 2002 Free Software 8 Foundation, Inc. 9 10 This file is part of the GNU MP Library. 11 12 The GNU MP Library is free software; you can redistribute it and/or modify 13 it under the terms of the GNU Lesser General Public License as published by 14 the Free Software Foundation; either version 3 of the License, or (at your 15 option) any later version. 16 17 The GNU MP Library is distributed in the hope that it will be useful, but 18 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 19 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 20 License for more details. 21 22 You should have received a copy of the GNU Lesser General Public License 23 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */ 24 25 #include "gmp.h" 26 #include "gmp-impl.h" 27 28 29 /* Multiply {up,usize} by {vp,vsize} and write the result to 30 {prodp,usize+vsize}. Must have usize>=vsize. 31 32 Note that prodp gets usize+vsize limbs stored, even if the actual result 33 only needs usize+vsize-1. 34 35 There's no good reason to call here with vsize>=MUL_TOOM22_THRESHOLD. 36 Currently this is allowed, but it might not be in the future. 37 38 This is the most critical code for multiplication. All multiplies rely 39 on this, both small and huge. Small ones arrive here immediately, huge 40 ones arrive here as this is the base case for Karatsuba's recursive 41 algorithm. */ 42 43 void 44 mpn_mul_basecase (mp_ptr rp, 45 mp_srcptr up, mp_size_t un, 46 mp_srcptr vp, mp_size_t vn) 47 { 48 ASSERT (un >= vn); 49 ASSERT (vn >= 1); 50 ASSERT (! MPN_OVERLAP_P (rp, un+vn, up, un)); 51 ASSERT (! MPN_OVERLAP_P (rp, un+vn, vp, vn)); 52 53 /* We first multiply by the low order limb (or depending on optional function 54 availability, limbs). This result can be stored, not added, to rp. We 55 also avoid a loop for zeroing this way. */ 56 57 #if HAVE_NATIVE_mpn_mul_2 58 if (vn >= 2) 59 { 60 rp[un + 1] = mpn_mul_2 (rp, up, un, vp); 61 rp += 2, vp += 2, vn -= 2; 62 } 63 else 64 { 65 rp[un] = mpn_mul_1 (rp, up, un, vp[0]); 66 return; 67 } 68 #else 69 rp[un] = mpn_mul_1 (rp, up, un, vp[0]); 70 rp += 1, vp += 1, vn -= 1; 71 #endif 72 73 /* Now accumulate the product of up[] and the next higher limb (or depending 74 on optional function availability, limbs) from vp[]. */ 75 76 #define MAX_LEFT MP_SIZE_T_MAX /* Used to simplify loops into if statements */ 77 78 79 #if HAVE_NATIVE_mpn_addmul_6 80 while (vn >= 6) 81 { 82 rp[un + 6 - 1] = mpn_addmul_6 (rp, up, un, vp); 83 if (MAX_LEFT == 6) 84 return; 85 rp += 6, vp += 6, vn -= 6; 86 if (MAX_LEFT < 2 * 6) 87 break; 88 } 89 #undef MAX_LEFT 90 #define MAX_LEFT (6 - 1) 91 #endif 92 93 #if HAVE_NATIVE_mpn_addmul_5 94 while (vn >= 5) 95 { 96 rp[un + 5 - 1] = mpn_addmul_5 (rp, up, un, vp); 97 if (MAX_LEFT == 5) 98 return; 99 rp += 5, vp += 5, vn -= 5; 100 if (MAX_LEFT < 2 * 5) 101 break; 102 } 103 #undef MAX_LEFT 104 #define MAX_LEFT (5 - 1) 105 #endif 106 107 #if HAVE_NATIVE_mpn_addmul_4 108 while (vn >= 4) 109 { 110 rp[un + 4 - 1] = mpn_addmul_4 (rp, up, un, vp); 111 if (MAX_LEFT == 4) 112 return; 113 rp += 4, vp += 4, vn -= 4; 114 if (MAX_LEFT < 2 * 4) 115 break; 116 } 117 #undef MAX_LEFT 118 #define MAX_LEFT (4 - 1) 119 #endif 120 121 #if HAVE_NATIVE_mpn_addmul_3 122 while (vn >= 3) 123 { 124 rp[un + 3 - 1] = mpn_addmul_3 (rp, up, un, vp); 125 if (MAX_LEFT == 3) 126 return; 127 rp += 3, vp += 3, vn -= 3; 128 if (MAX_LEFT < 2 * 3) 129 break; 130 } 131 #undef MAX_LEFT 132 #define MAX_LEFT (3 - 1) 133 #endif 134 135 #if HAVE_NATIVE_mpn_addmul_2 136 while (vn >= 2) 137 { 138 rp[un + 2 - 1] = mpn_addmul_2 (rp, up, un, vp); 139 if (MAX_LEFT == 2) 140 return; 141 rp += 2, vp += 2, vn -= 2; 142 if (MAX_LEFT < 2 * 2) 143 break; 144 } 145 #undef MAX_LEFT 146 #define MAX_LEFT (2 - 1) 147 #endif 148 149 while (vn >= 1) 150 { 151 rp[un] = mpn_addmul_1 (rp, up, un, vp[0]); 152 if (MAX_LEFT == 1) 153 return; 154 rp += 1, vp += 1, vn -= 1; 155 } 156 } 157