1 /* mpn_redc_2. Set rp[] <- up[]/R^n mod mp[]. Clobber up[]. 2 mp[] is n limbs; up[] is 2n limbs. 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 (C) 2000, 2001, 2002, 2004, 2008, 2012 Free Software Foundation, 8 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 #include "longlong.h" 28 29 30 #if GMP_NAIL_BITS != 0 31 you lose 32 #endif 33 34 /* For testing purposes, define our own mpn_addmul_2 if there is none already 35 available. */ 36 #ifndef HAVE_NATIVE_mpn_addmul_2 37 #undef mpn_addmul_2 38 static mp_limb_t 39 mpn_addmul_2 (mp_ptr rp, mp_srcptr up, mp_size_t n, mp_srcptr vp) 40 { 41 rp[n] = mpn_addmul_1 (rp, up, n, vp[0]); 42 return mpn_addmul_1 (rp + 1, up, n, vp[1]); 43 } 44 #endif 45 46 #if defined (__GNUC__) && defined (__ia64) && W_TYPE_SIZE == 64 47 #define umul2low(ph, pl, uh, ul, vh, vl) \ 48 do { \ 49 mp_limb_t _ph, _pl; \ 50 __asm__ ("xma.hu %0 = %3, %5, f0\n\t" \ 51 "xma.l %1 = %3, %5, f0\n\t" \ 52 ";;\n\t" \ 53 "xma.l %0 = %3, %4, %0\n\t" \ 54 ";;\n\t" \ 55 "xma.l %0 = %2, %5, %0" \ 56 : "=&f" (ph), "=&f" (pl) \ 57 : "f" (uh), "f" (ul), "f" (vh), "f" (vl)); \ 58 } while (0) 59 #endif 60 61 #ifndef umul2low 62 #define umul2low(ph, pl, uh, ul, vh, vl) \ 63 do { \ 64 mp_limb_t _ph, _pl; \ 65 umul_ppmm (_ph, _pl, ul, vl); \ 66 (ph) = _ph + (ul) * (vh) + (uh) * (vl); \ 67 (pl) = _pl; \ 68 } while (0) 69 #endif 70 71 mp_limb_t 72 mpn_redc_2 (mp_ptr rp, mp_ptr up, mp_srcptr mp, mp_size_t n, mp_srcptr mip) 73 { 74 mp_limb_t q[2]; 75 mp_size_t j; 76 mp_limb_t upn; 77 mp_limb_t cy; 78 79 ASSERT (n > 0); 80 ASSERT_MPN (up, 2*n); 81 82 if ((n & 1) != 0) 83 { 84 up[0] = mpn_addmul_1 (up, mp, n, (up[0] * mip[0]) & GMP_NUMB_MASK); 85 up++; 86 } 87 88 for (j = n - 2; j >= 0; j -= 2) 89 { 90 umul2low (q[1], q[0], mip[1], mip[0], up[1], up[0]); 91 upn = up[n]; /* mpn_addmul_2 overwrites this */ 92 up[1] = mpn_addmul_2 (up, mp, n, q); 93 up[0] = up[n]; 94 up[n] = upn; 95 up += 2; 96 } 97 98 cy = mpn_add_n (rp, up, up - n, n); 99 return cy; 100 } 101