Replace FSF snail mail address with URLs.
[jlayton/glibc.git] / stdlib / mod_1.c
1 /* mpn_mod_1(dividend_ptr, dividend_size, divisor_limb) --
2    Divide (DIVIDEND_PTR,,DIVIDEND_SIZE) by DIVISOR_LIMB.
3    Return the single-limb remainder.
4    There are no constraints on the value of the divisor.
5
6 Copyright (C) 1991, 1993, 1994, Free Software Foundation, Inc.
7
8 This file is part of the GNU MP Library.
9
10 The GNU MP Library is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or (at your
13 option) any later version.
14
15 The GNU MP Library is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
18 License for more details.
19
20 You should have received a copy of the GNU Lesser General Public License
21 along with the GNU MP Library; see the file COPYING.LIB.  If not, see
22 <http://www.gnu.org/licenses/>.  */
23
24 #include <gmp.h>
25 #include "gmp-impl.h"
26 #include "longlong.h"
27
28 #ifndef UMUL_TIME
29 #define UMUL_TIME 1
30 #endif
31
32 #ifndef UDIV_TIME
33 #define UDIV_TIME UMUL_TIME
34 #endif
35
36 /* FIXME: We should be using invert_limb (or invert_normalized_limb)
37    here (not udiv_qrnnd).  */
38
39 mp_limb_t
40 #if __STDC__
41 mpn_mod_1 (mp_srcptr dividend_ptr, mp_size_t dividend_size,
42            mp_limb_t divisor_limb)
43 #else
44 mpn_mod_1 (dividend_ptr, dividend_size, divisor_limb)
45      mp_srcptr dividend_ptr;
46      mp_size_t dividend_size;
47      mp_limb_t divisor_limb;
48 #endif
49 {
50   mp_size_t i;
51   mp_limb_t n1, n0, r;
52   mp_limb_t dummy;
53
54   /* Botch: Should this be handled at all?  Rely on callers?  */
55   if (dividend_size == 0)
56     return 0;
57
58   /* If multiplication is much faster than division, and the
59      dividend is large, pre-invert the divisor, and use
60      only multiplications in the inner loop.  */
61
62   /* This test should be read:
63        Does it ever help to use udiv_qrnnd_preinv?
64          && Does what we save compensate for the inversion overhead?  */
65   if (UDIV_TIME > (2 * UMUL_TIME + 6)
66       && (UDIV_TIME - (2 * UMUL_TIME + 6)) * dividend_size > UDIV_TIME)
67     {
68       int normalization_steps;
69
70       count_leading_zeros (normalization_steps, divisor_limb);
71       if (normalization_steps != 0)
72         {
73           mp_limb_t divisor_limb_inverted;
74
75           divisor_limb <<= normalization_steps;
76
77           /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB.  The
78              result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
79              most significant bit (with weight 2**N) implicit.  */
80
81           /* Special case for DIVISOR_LIMB == 100...000.  */
82           if (divisor_limb << 1 == 0)
83             divisor_limb_inverted = ~(mp_limb_t) 0;
84           else
85             udiv_qrnnd (divisor_limb_inverted, dummy,
86                         -divisor_limb, 0, divisor_limb);
87
88           n1 = dividend_ptr[dividend_size - 1];
89           r = n1 >> (BITS_PER_MP_LIMB - normalization_steps);
90
91           /* Possible optimization:
92              if (r == 0
93              && divisor_limb > ((n1 << normalization_steps)
94                              | (dividend_ptr[dividend_size - 2] >> ...)))
95              ...one division less... */
96
97           for (i = dividend_size - 2; i >= 0; i--)
98             {
99               n0 = dividend_ptr[i];
100               udiv_qrnnd_preinv (dummy, r, r,
101                                  ((n1 << normalization_steps)
102                                   | (n0 >> (BITS_PER_MP_LIMB - normalization_steps))),
103                                  divisor_limb, divisor_limb_inverted);
104               n1 = n0;
105             }
106           udiv_qrnnd_preinv (dummy, r, r,
107                              n1 << normalization_steps,
108                              divisor_limb, divisor_limb_inverted);
109           return r >> normalization_steps;
110         }
111       else
112         {
113           mp_limb_t divisor_limb_inverted;
114
115           /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB.  The
116              result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
117              most significant bit (with weight 2**N) implicit.  */
118
119           /* Special case for DIVISOR_LIMB == 100...000.  */
120           if (divisor_limb << 1 == 0)
121             divisor_limb_inverted = ~(mp_limb_t) 0;
122           else
123             udiv_qrnnd (divisor_limb_inverted, dummy,
124                         -divisor_limb, 0, divisor_limb);
125
126           i = dividend_size - 1;
127           r = dividend_ptr[i];
128
129           if (r >= divisor_limb)
130             r = 0;
131           else
132             i--;
133
134           for (; i >= 0; i--)
135             {
136               n0 = dividend_ptr[i];
137               udiv_qrnnd_preinv (dummy, r, r,
138                                  n0, divisor_limb, divisor_limb_inverted);
139             }
140           return r;
141         }
142     }
143   else
144     {
145       if (UDIV_NEEDS_NORMALIZATION)
146         {
147           int normalization_steps;
148
149           count_leading_zeros (normalization_steps, divisor_limb);
150           if (normalization_steps != 0)
151             {
152               divisor_limb <<= normalization_steps;
153
154               n1 = dividend_ptr[dividend_size - 1];
155               r = n1 >> (BITS_PER_MP_LIMB - normalization_steps);
156
157               /* Possible optimization:
158                  if (r == 0
159                  && divisor_limb > ((n1 << normalization_steps)
160                                  | (dividend_ptr[dividend_size - 2] >> ...)))
161                  ...one division less... */
162
163               for (i = dividend_size - 2; i >= 0; i--)
164                 {
165                   n0 = dividend_ptr[i];
166                   udiv_qrnnd (dummy, r, r,
167                               ((n1 << normalization_steps)
168                                | (n0 >> (BITS_PER_MP_LIMB - normalization_steps))),
169                               divisor_limb);
170                   n1 = n0;
171                 }
172               udiv_qrnnd (dummy, r, r,
173                           n1 << normalization_steps,
174                           divisor_limb);
175               return r >> normalization_steps;
176             }
177         }
178       /* No normalization needed, either because udiv_qrnnd doesn't require
179          it, or because DIVISOR_LIMB is already normalized.  */
180
181       i = dividend_size - 1;
182       r = dividend_ptr[i];
183
184       if (r >= divisor_limb)
185         r = 0;
186       else
187         i--;
188
189       for (; i >= 0; i--)
190         {
191           n0 = dividend_ptr[i];
192           udiv_qrnnd (dummy, r, r, n0, divisor_limb);
193         }
194       return r;
195     }
196 }