debuggers.hg

view xen/common/lib.c @ 3726:88957a238191

bitkeeper revision 1.1159.1.544 (4207248crq3YxiyLWjUehtHv_Yd3tg)

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