xref: /netbsd-src/external/lgpl3/gmp/dist/mpn/generic/mod_1_2.c (revision f14316bcbc544b96a93e884bc5c2b15fd60e22ae)
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, 2009, 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 the GNU Lesser General Public License as published by
18 the Free Software Foundation; either version 3 of the License, or (at your
19 option) any later version.
20 
21 The GNU MP Library is distributed in the hope that it will be useful, but
22 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
24 License for more details.
25 
26 You should have received a copy of the GNU Lesser General Public License
27 along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
28 
29 #include "gmp.h"
30 #include "gmp-impl.h"
31 #include "longlong.h"
32 
33 void
34 mpn_mod_1s_2p_cps (mp_limb_t cps[5], mp_limb_t b)
35 {
36   mp_limb_t bi;
37   mp_limb_t B1modb, B2modb, B3modb;
38   int cnt;
39 
40   ASSERT (b <= (~(mp_limb_t) 0) / 2);
41 
42   count_leading_zeros (cnt, b);
43 
44   b <<= cnt;
45   invert_limb (bi, b);
46 
47   cps[0] = bi;
48   cps[1] = cnt;
49 
50   B1modb = -b * ((bi >> (GMP_LIMB_BITS-cnt)) | (CNST_LIMB(1) << cnt));
51   ASSERT (B1modb <= b);		/* NB: not fully reduced mod b */
52   cps[2] = B1modb >> cnt;
53 
54   udiv_rnnd_preinv (B2modb, B1modb, CNST_LIMB(0), b, bi);
55   cps[3] = B2modb >> cnt;
56 
57   udiv_rnnd_preinv (B3modb, B2modb, CNST_LIMB(0), b, bi);
58   cps[4] = B3modb >> cnt;
59 
60 #if WANT_ASSERT
61   {
62     int i;
63     b = cps[2];
64     for (i = 3; i <= 4; i++)
65       {
66 	b += cps[i];
67 	ASSERT (b >= cps[i]);
68       }
69   }
70 #endif
71 }
72 
73 mp_limb_t
74 mpn_mod_1s_2p (mp_srcptr ap, mp_size_t n, mp_limb_t b, mp_limb_t cps[5])
75 {
76   mp_limb_t rh, rl, bi, ph, pl, ch, cl, r;
77   mp_limb_t B1modb, B2modb, B3modb;
78   mp_size_t i;
79   int cnt;
80 
81   ASSERT (n >= 1);
82 
83   B1modb = cps[2];
84   B2modb = cps[3];
85   B3modb = cps[4];
86 
87   if ((n & 1) != 0)
88     {
89       if (n == 1)
90 	{
91 	  rl = ap[n - 1];
92 	  bi = cps[0];
93 	  cnt = cps[1];
94 	  udiv_rnnd_preinv (r, rl >> (GMP_LIMB_BITS - cnt),
95 			     rl << cnt, b, bi);
96 	  return r >> cnt;
97 	}
98 
99       umul_ppmm (ph, pl, ap[n - 2], B1modb);
100       add_ssaaaa (ph, pl, ph, pl, 0, ap[n - 3]);
101       umul_ppmm (rh, rl, ap[n - 1], B2modb);
102       add_ssaaaa (rh, rl, rh, rl, ph, pl);
103       n--;
104     }
105   else
106     {
107       rh = ap[n - 1];
108       rl = ap[n - 2];
109     }
110 
111   for (i = n - 4; i >= 0; i -= 2)
112     {
113       /* rr = ap[i]				< B
114 	    + ap[i+1] * (B mod b)		<= (B-1)(b-1)
115 	    + LO(rr)  * (B^2 mod b)		<= (B-1)(b-1)
116 	    + HI(rr)  * (B^3 mod b)		<= (B-1)(b-1)
117       */
118       umul_ppmm (ph, pl, ap[i + 1], B1modb);
119       add_ssaaaa (ph, pl, ph, pl, 0, ap[i + 0]);
120 
121       umul_ppmm (ch, cl, rl, B2modb);
122       add_ssaaaa (ph, pl, ph, pl, ch, cl);
123 
124       umul_ppmm (rh, rl, rh, B3modb);
125       add_ssaaaa (rh, rl, rh, rl, ph, pl);
126     }
127 
128   umul_ppmm (rh, cl, rh, B1modb);
129   add_ssaaaa (rh, rl, rh, rl, 0, cl);
130 
131   cnt = cps[1];
132   bi = cps[0];
133 
134   r = (rh << cnt) | (rl >> (GMP_LIMB_BITS - cnt));
135   udiv_rnnd_preinv (r, r, rl << cnt, b, bi);
136 
137   return r >> cnt;
138 }
139