2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This software was developed by the Computer Systems Engineering group
6 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7 * contributed to Berkeley.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * $FreeBSD: src/lib/libstand/qdivrem.c,v 1.2 1999/08/28 00:05:33 peter Exp $
34 * From: Id: qdivrem.c,v 1.7 1997/11/07 09:20:40 phk Exp
38 * Multiprecision divide. This algorithm is from Knuth vol. 2 (2nd ed),
39 * section 4.3.1, pp. 257--259.
43 #include <sys/endian.h> /* _QUAD_HIGHWORD */
45 #define B (1 << HALF_BITS) /* digit base */
48 * Define high and low longwords.
50 #define H _QUAD_HIGHWORD
51 #define L _QUAD_LOWWORD
53 /* Combine two `digits' to make a single two-digit number. */
54 #define COMBINE(a, b) (((u_int)(a) << HALF_BITS) | (b))
56 _Static_assert(sizeof(int) / 2 == sizeof(short),
57 "Bitwise functions in libstand are broken on this architecture");
59 /* select a type for digits in base B: use unsigned short if they fit */
60 typedef unsigned short digit
;
63 * Shift p[0]..p[len] left `sh' bits, ignoring any bits that
64 * `fall out' the left (there never will be any such anyway).
65 * We may assume len >= 0. NOTE THAT THIS WRITES len+1 DIGITS.
68 shl(digit
*p
, int len
, int sh
)
72 for (i
= 0; i
< len
; i
++)
73 p
[i
] = LHALF(p
[i
] << sh
) | (p
[i
+ 1] >> (HALF_BITS
- sh
));
74 p
[i
] = LHALF(p
[i
] << sh
);
78 * __udivmoddi4(u, v, rem) returns u/v and, optionally, sets *rem to u%v.
80 * We do this in base 2-sup-HALF_BITS, so that all intermediate products
81 * fit within u_int. As a consequence, the maximum length dividend and
82 * divisor are 4 `digits' in this base (they are shorter if they have
86 __udivmoddi4(u_quad_t uq
, u_quad_t vq
, u_quad_t
*arq
)
93 digit uspace
[5], vspace
[5], qspace
[5];
96 * Take care of special cases: divide by zero, and u < v.
100 static volatile const unsigned int zero
= 0;
102 tmp
.ul
[H
] = tmp
.ul
[L
] = 1 / zero
;
117 * Break dividend and divisor into digits in base B, then
118 * count leading zeros to determine m and n. When done, we
120 * u = (u[1]u[2]...u[m+n]) sub B
121 * v = (v[1]v[2]...v[n]) sub B
123 * 1 < n <= 4 (if n = 1, we use a different division algorithm)
124 * m >= 0 (otherwise u < v, which we already checked)
131 u
[1] = HHALF(tmp
.ul
[H
]);
132 u
[2] = LHALF(tmp
.ul
[H
]);
133 u
[3] = HHALF(tmp
.ul
[L
]);
134 u
[4] = LHALF(tmp
.ul
[L
]);
136 v
[1] = HHALF(tmp
.ul
[H
]);
137 v
[2] = LHALF(tmp
.ul
[H
]);
138 v
[3] = HHALF(tmp
.ul
[L
]);
139 v
[4] = LHALF(tmp
.ul
[L
]);
140 for (n
= 4; v
[1] == 0; v
++) {
142 u_int rbj
; /* r*B+u[j] (not root boy jim) */
143 digit q1
, q2
, q3
, q4
;
146 * Change of plan, per exercise 16.
149 * q[j] = floor((r*B + u[j]) / v),
150 * r = (r*B + u[j]) % v;
151 * We unroll this completely here.
153 t
= v
[2]; /* nonzero, by definition */
155 rbj
= COMBINE(u
[1] % t
, u
[2]);
157 rbj
= COMBINE(rbj
% t
, u
[3]);
159 rbj
= COMBINE(rbj
% t
, u
[4]);
163 tmp
.ul
[H
] = COMBINE(q1
, q2
);
164 tmp
.ul
[L
] = COMBINE(q3
, q4
);
170 * By adjusting q once we determine m, we can guarantee that
171 * there is a complete four-digit quotient at &qspace[1] when
174 for (m
= 4 - n
; u
[1] == 0; u
++)
176 for (i
= 4 - m
; --i
>= 0;)
181 * Here we run Program D, translated from MIX to C and acquiring
182 * a few minor changes.
184 * D1: choose multiplier 1 << d to ensure v[1] >= B/2.
187 for (t
= v
[1]; t
< B
/ 2; t
<<= 1)
190 shl(&u
[0], m
+ n
, d
); /* u <<= d */
191 shl(&v
[1], n
- 1, d
); /* v <<= d */
197 v1
= v
[1]; /* for D3 -- note that v[1..n] are constant */
198 v2
= v
[2]; /* for D3 */
203 * D3: Calculate qhat (\^q, in TeX notation).
204 * Let qhat = min((u[j]*B + u[j+1])/v[1], B-1), and
205 * let rhat = (u[j]*B + u[j+1]) mod v[1].
206 * While rhat < B and v[2]*qhat > rhat*B+u[j+2],
207 * decrement qhat and increase rhat correspondingly.
208 * Note that if rhat >= B, v[2]*qhat < rhat*B.
210 uj0
= u
[j
+ 0]; /* for D3 only -- note that u[j+...] change */
211 uj1
= u
[j
+ 1]; /* for D3 only */
212 uj2
= u
[j
+ 2]; /* for D3 only */
218 u_int nn
= COMBINE(uj0
, uj1
);
222 while (v2
* qhat
> COMBINE(rhat
, uj2
)) {
225 if ((rhat
+= v1
) >= B
)
229 * D4: Multiply and subtract.
230 * The variable `t' holds any borrows across the loop.
231 * We split this up so that we do not require v[0] = 0,
232 * and to eliminate a final special case.
234 for (t
= 0, i
= n
; i
> 0; i
--) {
235 t
= u
[i
+ j
] - v
[i
] * qhat
- t
;
237 t
= (B
- HHALF(t
)) & (B
- 1);
242 * D5: test remainder.
243 * There is a borrow if and only if HHALF(t) is nonzero;
244 * in that (rare) case, qhat was too large (by exactly 1).
245 * Fix it by adding v[1..n] to u[j..j+n].
249 for (t
= 0, i
= n
; i
> 0; i
--) { /* D6: add back. */
250 t
+= u
[i
+ j
] + v
[i
];
254 u
[j
] = LHALF(u
[j
] + t
);
257 } while (++j
<= m
); /* D7: loop on j. */
260 * If caller wants the remainder, we have to calculate it as
261 * u[m..m+n] >> d (this is at most n digits and thus fits in
262 * u[m+1..m+n], but we may need more source digits).
266 for (i
= m
+ n
; i
> m
; --i
)
268 LHALF(u
[i
- 1] << (HALF_BITS
- d
));
271 tmp
.ul
[H
] = COMBINE(uspace
[1], uspace
[2]);
272 tmp
.ul
[L
] = COMBINE(uspace
[3], uspace
[4]);
276 tmp
.ul
[H
] = COMBINE(qspace
[1], qspace
[2]);
277 tmp
.ul
[L
] = COMBINE(qspace
[3], qspace
[4]);
282 * Divide two unsigned quads.
286 __udivdi3(u_quad_t a
, u_quad_t b
)
289 return (__udivmoddi4(a
, b
, (u_quad_t
*)0));
293 * Return remainder after dividing two unsigned quads.
296 __umoddi3(u_quad_t a
, u_quad_t b
)
300 (void)__udivmoddi4(a
, b
, &r
);