(root)/
gmp-6.3.0/
mpn/
generic/
mod_1_2.c
       1  /* mpn_mod_1s_2p (ap, n, b, cps)
       2     Divide (ap,,n) by b.  Return the single-limb remainder.
       3     Requires that b < B / 2.
       4  
       5     Contributed to the GNU project by Torbjorn Granlund.
       6     Based on a suggestion by Peter L. Montgomery.
       7  
       8     THE FUNCTIONS IN THIS FILE ARE INTERNAL WITH MUTABLE INTERFACES.  IT IS ONLY
       9     SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES.  IN FACT, IT IS ALMOST
      10     GUARANTEED THAT THEY WILL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE.
      11  
      12  Copyright 2008-2010 Free Software Foundation, Inc.
      13  
      14  This file is part of the GNU MP Library.
      15  
      16  The GNU MP Library is free software; you can redistribute it and/or modify
      17  it under the terms of either:
      18  
      19    * the GNU Lesser General Public License as published by the Free
      20      Software Foundation; either version 3 of the License, or (at your
      21      option) any later version.
      22  
      23  or
      24  
      25    * the GNU General Public License as published by the Free Software
      26      Foundation; either version 2 of the License, or (at your option) any
      27      later version.
      28  
      29  or both in parallel, as here.
      30  
      31  The GNU MP Library is distributed in the hope that it will be useful, but
      32  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
      33  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      34  for more details.
      35  
      36  You should have received copies of the GNU General Public License and the
      37  GNU Lesser General Public License along with the GNU MP Library.  If not,
      38  see https://www.gnu.org/licenses/.  */
      39  
      40  #include "gmp-impl.h"
      41  #include "longlong.h"
      42  
      43  void
      44  mpn_mod_1s_2p_cps (mp_limb_t cps[5], mp_limb_t b)
      45  {
      46    mp_limb_t bi;
      47    mp_limb_t B1modb, B2modb, B3modb;
      48    int cnt;
      49  
      50    ASSERT (b <= (~(mp_limb_t) 0) / 2);
      51  
      52    count_leading_zeros (cnt, b);
      53  
      54    b <<= cnt;
      55    invert_limb (bi, b);
      56  
      57    cps[0] = bi;
      58    cps[1] = cnt;
      59  
      60    B1modb = -b * ((bi >> (GMP_LIMB_BITS-cnt)) | (CNST_LIMB(1) << cnt));
      61    ASSERT (B1modb <= b);		/* NB: not fully reduced mod b */
      62    cps[2] = B1modb >> cnt;
      63  
      64    udiv_rnnd_preinv (B2modb, B1modb, CNST_LIMB(0), b, bi);
      65    cps[3] = B2modb >> cnt;
      66  
      67    udiv_rnnd_preinv (B3modb, B2modb, CNST_LIMB(0), b, bi);
      68    cps[4] = B3modb >> cnt;
      69  
      70  #if WANT_ASSERT
      71    {
      72      int i;
      73      b = cps[2];
      74      for (i = 3; i <= 4; i++)
      75        {
      76  	b += cps[i];
      77  	ASSERT (b >= cps[i]);
      78        }
      79    }
      80  #endif
      81  }
      82  
      83  mp_limb_t
      84  mpn_mod_1s_2p (mp_srcptr ap, mp_size_t n, mp_limb_t b, const mp_limb_t cps[5])
      85  {
      86    mp_limb_t rh, rl, bi, ph, pl, ch, cl, r;
      87    mp_limb_t B1modb, B2modb, B3modb;
      88    mp_size_t i;
      89    int cnt;
      90  
      91    ASSERT (n >= 1);
      92  
      93    B1modb = cps[2];
      94    B2modb = cps[3];
      95    B3modb = cps[4];
      96  
      97    if ((n & 1) != 0)
      98      {
      99        if (n == 1)
     100  	{
     101  	  rl = ap[n - 1];
     102  	  bi = cps[0];
     103  	  cnt = cps[1];
     104  	  udiv_rnnd_preinv (r, rl >> (GMP_LIMB_BITS - cnt),
     105  			     rl << cnt, b, bi);
     106  	  return r >> cnt;
     107  	}
     108  
     109        umul_ppmm (ph, pl, ap[n - 2], B1modb);
     110        add_ssaaaa (ph, pl, ph, pl, CNST_LIMB(0), ap[n - 3]);
     111        umul_ppmm (rh, rl, ap[n - 1], B2modb);
     112        add_ssaaaa (rh, rl, rh, rl, ph, pl);
     113        n--;
     114      }
     115    else
     116      {
     117        rh = ap[n - 1];
     118        rl = ap[n - 2];
     119      }
     120  
     121    for (i = n - 4; i >= 0; i -= 2)
     122      {
     123        /* rr = ap[i]				< B
     124  	    + ap[i+1] * (B mod b)		<= (B-1)(b-1)
     125  	    + LO(rr)  * (B^2 mod b)		<= (B-1)(b-1)
     126  	    + HI(rr)  * (B^3 mod b)		<= (B-1)(b-1)
     127        */
     128        umul_ppmm (ph, pl, ap[i + 1], B1modb);
     129        add_ssaaaa (ph, pl, ph, pl, CNST_LIMB(0), ap[i + 0]);
     130  
     131        umul_ppmm (ch, cl, rl, B2modb);
     132        add_ssaaaa (ph, pl, ph, pl, ch, cl);
     133  
     134        umul_ppmm (rh, rl, rh, B3modb);
     135        add_ssaaaa (rh, rl, rh, rl, ph, pl);
     136      }
     137  
     138    umul_ppmm (rh, cl, rh, B1modb);
     139    add_ssaaaa (rh, rl, rh, rl, CNST_LIMB(0), cl);
     140  
     141    cnt = cps[1];
     142    bi = cps[0];
     143  
     144    r = (rh << cnt) | (rl >> (GMP_LIMB_BITS - cnt));
     145    udiv_rnnd_preinv (r, r, rl << cnt, b, bi);
     146  
     147    return r >> cnt;
     148  }