beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpn / generic / dive_1.c
blob1c0a4e894db8a9816a917226e7d1aed359784660
1 /* mpn_divexact_1 -- mpn by limb exact division.
3 THE FUNCTIONS IN THIS FILE ARE FOR INTERNAL USE ONLY. THEY'RE ALMOST
4 CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR COMPLETELY IN
5 FUTURE GNU MP RELEASES.
7 Copyright 2000-2003, 2005, 2013 Free Software Foundation, Inc.
9 This file is part of the GNU MP Library.
11 The GNU MP Library is free software; you can redistribute it and/or modify
12 it under the terms of either:
14 * the GNU Lesser General Public License as published by the Free
15 Software Foundation; either version 3 of the License, or (at your
16 option) any later version.
20 * the GNU General Public License as published by the Free Software
21 Foundation; either version 2 of the License, or (at your option) any
22 later version.
24 or both in parallel, as here.
26 The GNU MP Library is distributed in the hope that it will be useful, but
27 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
28 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
29 for more details.
31 You should have received copies of the GNU General Public License and the
32 GNU Lesser General Public License along with the GNU MP Library. If not,
33 see https://www.gnu.org/licenses/. */
35 #include "gmp.h"
36 #include "gmp-impl.h"
37 #include "longlong.h"
41 /* Divide a={src,size} by d=divisor and store the quotient in q={dst,size}.
42 q will only be correct if d divides a exactly.
44 A separate loop is used for shift==0 because n<<GMP_LIMB_BITS doesn't
45 give zero on all CPUs (for instance it doesn't on the x86s). This
46 separate loop might run faster too, helping odd divisors.
48 Possibilities:
50 mpn_divexact_1c could be created, accepting and returning c. This would
51 let a long calculation be done piece by piece. Currently there's no
52 particular need for that, and not returning c means that a final umul can
53 be skipped.
55 Another use for returning c would be letting the caller know whether the
56 division was in fact exact. It would work just to return the carry bit
57 "c=(l>s)" and let the caller do a final umul if interested.
59 When the divisor is even, the factors of two could be handled with a
60 separate mpn_rshift, instead of shifting on the fly. That might be
61 faster on some CPUs and would mean just the shift==0 style loop would be
62 needed.
64 If n<<GMP_LIMB_BITS gives zero on a particular CPU then the separate
65 shift==0 loop is unnecessary, and could be eliminated if there's no great
66 speed difference.
68 It's not clear whether "/" is the best way to handle size==1. Alpha gcc
69 2.95 for instance has a poor "/" and might prefer the modular method.
70 Perhaps a tuned parameter should control this.
72 If src[size-1] < divisor then dst[size-1] will be zero, and one divide
73 step could be skipped. A test at last step for s<divisor (or ls in the
74 even case) might be a good way to do that. But if this code is often
75 used with small divisors then it might not be worth bothering */
77 void
78 mpn_divexact_1 (mp_ptr dst, mp_srcptr src, mp_size_t size, mp_limb_t divisor)
80 mp_size_t i;
81 mp_limb_t c, h, l, ls, s, s_next, inverse, dummy;
82 unsigned shift;
84 ASSERT (size >= 1);
85 ASSERT (divisor != 0);
86 ASSERT (MPN_SAME_OR_SEPARATE_P (dst, src, size));
87 ASSERT_MPN (src, size);
88 ASSERT_LIMB (divisor);
90 if ((divisor & 1) == 0)
92 count_trailing_zeros (shift, divisor);
93 divisor >>= shift;
95 else
96 shift = 0;
98 binvert_limb (inverse, divisor);
99 divisor <<= GMP_NAIL_BITS;
101 if (shift != 0)
103 c = 0;
105 s = src[0];
107 for (i = 1; i < size; i++)
109 s_next = src[i];
110 ls = ((s >> shift) | (s_next << (GMP_NUMB_BITS-shift))) & GMP_NUMB_MASK;
111 s = s_next;
113 SUBC_LIMB (c, l, ls, c);
115 l = (l * inverse) & GMP_NUMB_MASK;
116 dst[i - 1] = l;
118 umul_ppmm (h, dummy, l, divisor);
119 c += h;
121 while (i < size);
123 ls = s >> shift;
124 l = ls - c;
125 l = (l * inverse) & GMP_NUMB_MASK;
126 dst[size - 1] = l;
128 else
130 s = src[0];
132 l = (s * inverse) & GMP_NUMB_MASK;
133 dst[0] = l;
134 c = 0;
136 for (i = 1; i < size; i++)
138 umul_ppmm (h, dummy, l, divisor);
139 c += h;
141 s = src[i];
142 SUBC_LIMB (c, l, s, c);
144 l = (l * inverse) & GMP_NUMB_MASK;
145 dst[i] = l;