1 /* Cray PVP/IEEE mpn_mul_1 -- multiply a limb vector with a limb and store the 2 result in a second limb vector. 3 4 Copyright 2000, 2001 Free Software Foundation, Inc. 5 6 This file is part of the GNU MP Library. 7 8 The GNU MP Library is free software; you can redistribute it and/or modify 9 it under the terms of the GNU Lesser General Public License as published by 10 the Free Software Foundation; either version 3 of the License, or (at your 11 option) any later version. 12 13 The GNU MP Library is distributed in the hope that it will be useful, but 14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 16 License for more details. 17 18 You should have received a copy of the GNU Lesser General Public License 19 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */ 20 21 /* This code runs at 5 cycles/limb on a T90. That would probably 22 be hard to improve upon, even with assembly code. */ 23 24 #include <intrinsics.h> 25 #include "gmp.h" 26 #include "gmp-impl.h" 27 28 mp_limb_t 29 mpn_mul_1 (mp_ptr rp, mp_srcptr up, mp_size_t n, mp_limb_t vl) 30 { 31 mp_limb_t cy[n]; 32 mp_limb_t a, b, r, s0, s1, c0, c1; 33 mp_size_t i; 34 int more_carries; 35 36 if (up == rp) 37 { 38 /* The algorithm used below cannot handle overlap. Handle it here by 39 making a temporary copy of the source vector, then call ourselves. */ 40 mp_limb_t xp[n]; 41 MPN_COPY (xp, up, n); 42 return mpn_mul_1 (rp, xp, n, vl); 43 } 44 45 a = up[0] * vl; 46 rp[0] = a; 47 cy[0] = 0; 48 49 /* Main multiply loop. Generate a raw accumulated output product in rp[] 50 and a carry vector in cy[]. */ 51 #pragma _CRI ivdep 52 for (i = 1; i < n; i++) 53 { 54 a = up[i] * vl; 55 b = _int_mult_upper (up[i - 1], vl); 56 s0 = a + b; 57 c0 = ((a & b) | ((a | b) & ~s0)) >> 63; 58 rp[i] = s0; 59 cy[i] = c0; 60 } 61 /* Carry add loop. Add the carry vector cy[] to the raw sum rp[] and 62 store the new sum back to rp[0]. */ 63 more_carries = 0; 64 #pragma _CRI ivdep 65 for (i = 2; i < n; i++) 66 { 67 r = rp[i]; 68 c0 = cy[i - 1]; 69 s0 = r + c0; 70 rp[i] = s0; 71 c0 = (r & ~s0) >> 63; 72 more_carries += c0; 73 } 74 /* If that second loop generated carry, handle that in scalar loop. */ 75 if (more_carries) 76 { 77 mp_limb_t cyrec = 0; 78 /* Look for places where rp[k] is zero and cy[k-1] is non-zero. 79 These are where we got a recurrency carry. */ 80 for (i = 2; i < n; i++) 81 { 82 r = rp[i]; 83 c0 = (r == 0 && cy[i - 1] != 0); 84 s0 = r + cyrec; 85 rp[i] = s0; 86 c1 = (r & ~s0) >> 63; 87 cyrec = c0 | c1; 88 } 89 return _int_mult_upper (up[n - 1], vl) + cyrec + cy[n - 1]; 90 } 91 92 return _int_mult_upper (up[n - 1], vl) + cy[n - 1]; 93 } 94