ia64/xen-unstable

view extras/mini-os/lib/math.c @ 4146:f2d61710e4d9

bitkeeper revision 1.1236.25.24 (42366e9aQ71LQ8uCB-Y1IwVNqx5eqA)

Merge djm@kirby.fc.hp.com://home/djm/src/xen/xeno-unstable-ia64.bk
into sportsman.spdomain:/home/djm/xeno-unstable-ia64.bk
author djm@sportsman.spdomain
date Tue Mar 15 05:11:54 2005 +0000 (2005-03-15)
parents 2d4c4c4574f2
children e9e327c3e81b
line source
1 /* -*- Mode:C; c-basic-offset:4; tab-width:4 -*-
2 ****************************************************************************
3 * (C) 2003 - Rolf Neugebauer - Intel Research Cambridge
4 ****************************************************************************
5 *
6 * File: math.c
7 * Author: Rolf Neugebauer (neugebar@dcs.gla.ac.uk)
8 * Changes:
9 *
10 * Date: Aug 2003
11 *
12 * Environment: Xen Minimal OS
13 * Description: Library functions for 64bit arith and other
14 * from freebsd, files in sys/libkern/ (qdivrem.c, etc)
15 *
16 ****************************************************************************
17 * $Id: c-insert.c,v 1.7 2002/11/08 16:04:34 rn Exp $
18 ****************************************************************************
19 *-
20 * Copyright (c) 1992, 1993
21 * The Regents of the University of California. All rights reserved.
22 *
23 * This software was developed by the Computer Systems Engineering group
24 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
25 * contributed to Berkeley.
26 *
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
29 * are met:
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * SUCH DAMAGE.
54 *
55 * $FreeBSD: src/sys/libkern/divdi3.c,v 1.6 1999/08/28 00:46:31 peter Exp $
56 */
58 #include <types.h>
60 /*
61 * Depending on the desired operation, we view a `long long' (aka quad_t) in
62 * one or more of the following formats.
63 */
64 union uu {
65 s64 q; /* as a (signed) quad */
66 s64 uq; /* as an unsigned quad */
67 long sl[2]; /* as two signed longs */
68 unsigned long ul[2]; /* as two unsigned longs */
69 };
70 /* XXX RN: Yuck hardcoded endianess :) */
71 #define _QUAD_HIGHWORD 1
72 #define _QUAD_LOWWORD 0
73 /*
74 * Define high and low longwords.
75 */
76 #define H _QUAD_HIGHWORD
77 #define L _QUAD_LOWWORD
79 /*
80 * Total number of bits in a quad_t and in the pieces that make it up.
81 * These are used for shifting, and also below for halfword extraction
82 * and assembly.
83 */
84 #define CHAR_BIT 8 /* number of bits in a char */
85 #define QUAD_BITS (sizeof(s64) * CHAR_BIT)
86 #define LONG_BITS (sizeof(long) * CHAR_BIT)
87 #define HALF_BITS (sizeof(long) * CHAR_BIT / 2)
89 /*
90 * Extract high and low shortwords from longword, and move low shortword of
91 * longword to upper half of long, i.e., produce the upper longword of
92 * ((quad_t)(x) << (number_of_bits_in_long/2)). (`x' must actually be u_long.)
93 *
94 * These are used in the multiply code, to split a longword into upper
95 * and lower halves, and to reassemble a product as a quad_t, shifted left
96 * (sizeof(long)*CHAR_BIT/2).
97 */
98 #define HHALF(x) ((x) >> HALF_BITS)
99 #define LHALF(x) ((x) & ((1UL << HALF_BITS) - 1))
100 #define LHUP(x) ((x) << HALF_BITS)
102 /*
103 * Multiprecision divide. This algorithm is from Knuth vol. 2 (2nd ed),
104 * section 4.3.1, pp. 257--259.
105 */
106 #define B (1UL << HALF_BITS) /* digit base */
108 /* Combine two `digits' to make a single two-digit number. */
109 #define COMBINE(a, b) (((u_long)(a) << HALF_BITS) | (b))
111 /* select a type for digits in base B: use unsigned short if they fit */
112 #if ULONG_MAX == 0xffffffff && USHRT_MAX >= 0xffff
113 typedef unsigned short digit;
114 #else
115 typedef u_long digit;
116 #endif
119 /*
120 * Shift p[0]..p[len] left `sh' bits, ignoring any bits that
121 * `fall out' the left (there never will be any such anyway).
122 * We may assume len >= 0. NOTE THAT THIS WRITES len+1 DIGITS.
123 */
124 static void
125 shl(register digit *p, register int len, register int sh)
126 {
127 register int i;
129 for (i = 0; i < len; i++)
130 p[i] = LHALF(p[i] << sh) | (p[i + 1] >> (HALF_BITS - sh));
131 p[i] = LHALF(p[i] << sh);
132 }
134 /*
135 * __qdivrem(u, v, rem) returns u/v and, optionally, sets *rem to u%v.
136 *
137 * We do this in base 2-sup-HALF_BITS, so that all intermediate products
138 * fit within u_long. As a consequence, the maximum length dividend and
139 * divisor are 4 `digits' in this base (they are shorter if they have
140 * leading zeros).
141 */
142 u64
143 __qdivrem(u64 uq, u64 vq, u64 *arq)
144 {
145 union uu tmp;
146 digit *u, *v, *q;
147 register digit v1, v2;
148 u_long qhat, rhat, t;
149 int m, n, d, j, i;
150 digit uspace[5], vspace[5], qspace[5];
152 /*
153 * Take care of special cases: divide by zero, and u < v.
154 */
155 if (vq == 0) {
156 /* divide by zero. */
157 static volatile const unsigned int zero = 0;
159 tmp.ul[H] = tmp.ul[L] = 1 / zero;
160 if (arq)
161 *arq = uq;
162 return (tmp.q);
163 }
164 if (uq < vq) {
165 if (arq)
166 *arq = uq;
167 return (0);
168 }
169 u = &uspace[0];
170 v = &vspace[0];
171 q = &qspace[0];
173 /*
174 * Break dividend and divisor into digits in base B, then
175 * count leading zeros to determine m and n. When done, we
176 * will have:
177 * u = (u[1]u[2]...u[m+n]) sub B
178 * v = (v[1]v[2]...v[n]) sub B
179 * v[1] != 0
180 * 1 < n <= 4 (if n = 1, we use a different division algorithm)
181 * m >= 0 (otherwise u < v, which we already checked)
182 * m + n = 4
183 * and thus
184 * m = 4 - n <= 2
185 */
186 tmp.uq = uq;
187 u[0] = 0;
188 u[1] = HHALF(tmp.ul[H]);
189 u[2] = LHALF(tmp.ul[H]);
190 u[3] = HHALF(tmp.ul[L]);
191 u[4] = LHALF(tmp.ul[L]);
192 tmp.uq = vq;
193 v[1] = HHALF(tmp.ul[H]);
194 v[2] = LHALF(tmp.ul[H]);
195 v[3] = HHALF(tmp.ul[L]);
196 v[4] = LHALF(tmp.ul[L]);
197 for (n = 4; v[1] == 0; v++) {
198 if (--n == 1) {
199 u_long rbj; /* r*B+u[j] (not root boy jim) */
200 digit q1, q2, q3, q4;
202 /*
203 * Change of plan, per exercise 16.
204 * r = 0;
205 * for j = 1..4:
206 * q[j] = floor((r*B + u[j]) / v),
207 * r = (r*B + u[j]) % v;
208 * We unroll this completely here.
209 */
210 t = v[2]; /* nonzero, by definition */
211 q1 = u[1] / t;
212 rbj = COMBINE(u[1] % t, u[2]);
213 q2 = rbj / t;
214 rbj = COMBINE(rbj % t, u[3]);
215 q3 = rbj / t;
216 rbj = COMBINE(rbj % t, u[4]);
217 q4 = rbj / t;
218 if (arq)
219 *arq = rbj % t;
220 tmp.ul[H] = COMBINE(q1, q2);
221 tmp.ul[L] = COMBINE(q3, q4);
222 return (tmp.q);
223 }
224 }
226 /*
227 * By adjusting q once we determine m, we can guarantee that
228 * there is a complete four-digit quotient at &qspace[1] when
229 * we finally stop.
230 */
231 for (m = 4 - n; u[1] == 0; u++)
232 m--;
233 for (i = 4 - m; --i >= 0;)
234 q[i] = 0;
235 q += 4 - m;
237 /*
238 * Here we run Program D, translated from MIX to C and acquiring
239 * a few minor changes.
240 *
241 * D1: choose multiplier 1 << d to ensure v[1] >= B/2.
242 */
243 d = 0;
244 for (t = v[1]; t < B / 2; t <<= 1)
245 d++;
246 if (d > 0) {
247 shl(&u[0], m + n, d); /* u <<= d */
248 shl(&v[1], n - 1, d); /* v <<= d */
249 }
250 /*
251 * D2: j = 0.
252 */
253 j = 0;
254 v1 = v[1]; /* for D3 -- note that v[1..n] are constant */
255 v2 = v[2]; /* for D3 */
256 do {
257 register digit uj0, uj1, uj2;
259 /*
260 * D3: Calculate qhat (\^q, in TeX notation).
261 * Let qhat = min((u[j]*B + u[j+1])/v[1], B-1), and
262 * let rhat = (u[j]*B + u[j+1]) mod v[1].
263 * While rhat < B and v[2]*qhat > rhat*B+u[j+2],
264 * decrement qhat and increase rhat correspondingly.
265 * Note that if rhat >= B, v[2]*qhat < rhat*B.
266 */
267 uj0 = u[j + 0]; /* for D3 only -- note that u[j+...] change */
268 uj1 = u[j + 1]; /* for D3 only */
269 uj2 = u[j + 2]; /* for D3 only */
270 if (uj0 == v1) {
271 qhat = B;
272 rhat = uj1;
273 goto qhat_too_big;
274 } else {
275 u_long nn = COMBINE(uj0, uj1);
276 qhat = nn / v1;
277 rhat = nn % v1;
278 }
279 while (v2 * qhat > COMBINE(rhat, uj2)) {
280 qhat_too_big:
281 qhat--;
282 if ((rhat += v1) >= B)
283 break;
284 }
285 /*
286 * D4: Multiply and subtract.
287 * The variable `t' holds any borrows across the loop.
288 * We split this up so that we do not require v[0] = 0,
289 * and to eliminate a final special case.
290 */
291 for (t = 0, i = n; i > 0; i--) {
292 t = u[i + j] - v[i] * qhat - t;
293 u[i + j] = LHALF(t);
294 t = (B - HHALF(t)) & (B - 1);
295 }
296 t = u[j] - t;
297 u[j] = LHALF(t);
298 /*
299 * D5: test remainder.
300 * There is a borrow if and only if HHALF(t) is nonzero;
301 * in that (rare) case, qhat was too large (by exactly 1).
302 * Fix it by adding v[1..n] to u[j..j+n].
303 */
304 if (HHALF(t)) {
305 qhat--;
306 for (t = 0, i = n; i > 0; i--) { /* D6: add back. */
307 t += u[i + j] + v[i];
308 u[i + j] = LHALF(t);
309 t = HHALF(t);
310 }
311 u[j] = LHALF(u[j] + t);
312 }
313 q[j] = qhat;
314 } while (++j <= m); /* D7: loop on j. */
316 /*
317 * If caller wants the remainder, we have to calculate it as
318 * u[m..m+n] >> d (this is at most n digits and thus fits in
319 * u[m+1..m+n], but we may need more source digits).
320 */
321 if (arq) {
322 if (d) {
323 for (i = m + n; i > m; --i)
324 u[i] = (u[i] >> d) |
325 LHALF(u[i - 1] << (HALF_BITS - d));
326 u[i] = 0;
327 }
328 tmp.ul[H] = COMBINE(uspace[1], uspace[2]);
329 tmp.ul[L] = COMBINE(uspace[3], uspace[4]);
330 *arq = tmp.q;
331 }
333 tmp.ul[H] = COMBINE(qspace[1], qspace[2]);
334 tmp.ul[L] = COMBINE(qspace[3], qspace[4]);
335 return (tmp.q);
336 }
339 /*
340 * Divide two signed quads.
341 * ??? if -1/2 should produce -1 on this machine, this code is wrong
342 */
343 s64
344 __divdi3(s64 a, s64 b)
345 {
346 u64 ua, ub, uq;
347 int neg;
349 if (a < 0)
350 ua = -(u64)a, neg = 1;
351 else
352 ua = a, neg = 0;
353 if (b < 0)
354 ub = -(u64)b, neg ^= 1;
355 else
356 ub = b;
357 uq = __qdivrem(ua, ub, (u64 *)0);
358 return (neg ? -uq : uq);
359 }
361 /*
362 * Divide two unsigned quads.
363 */
364 u64
365 __udivdi3(u64 a, u64 b)
366 {
367 return (__qdivrem(a, b, (u64 *)0));
368 }
371 /*
372 * Return remainder after dividing two unsigned quads.
373 */
374 u_quad_t
375 __umoddi3(u_quad_t a, u_quad_t b)
376 {
377 u_quad_t r;
379 (void)__qdivrem(a, b, &r);
380 return (r);
381 }