Import gdtoa-20081205.
[dragonfly.git] / contrib / gdtoa / gmisc.c
blob8270ef944797b8a707a33160a6207b2f3baa15be
1 /****************************************************************
3 The author of this software is David M. Gay.
5 Copyright (C) 1998 by Lucent Technologies
6 All Rights Reserved
8 Permission to use, copy, modify, and distribute this software and
9 its documentation for any purpose and without fee is hereby
10 granted, provided that the above copyright notice appear in all
11 copies and that both that the copyright notice and this
12 permission notice and warranty disclaimer appear in supporting
13 documentation, and that the name of Lucent or any of its entities
14 not be used in advertising or publicity pertaining to
15 distribution of the software without specific, written prior
16 permission.
18 LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
20 IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
21 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
23 IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
24 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
25 THIS SOFTWARE.
27 ****************************************************************/
29 /* Please send bug reports to David M. Gay (dmg at acm dot org,
30 * with " at " changed at "@" and " dot " changed to "."). */
32 #include "gdtoaimp.h"
34 void
35 #ifdef KR_headers
36 rshift(b, k) Bigint *b; int k;
37 #else
38 rshift(Bigint *b, int k)
39 #endif
41 ULong *x, *x1, *xe, y;
42 int n;
44 x = x1 = b->x;
45 n = k >> kshift;
46 if (n < b->wds) {
47 xe = x + b->wds;
48 x += n;
49 if (k &= kmask) {
50 n = ULbits - k;
51 y = *x++ >> k;
52 while(x < xe) {
53 *x1++ = (y | (*x << n)) & ALL_ON;
54 y = *x++ >> k;
56 if ((*x1 = y) !=0)
57 x1++;
59 else
60 while(x < xe)
61 *x1++ = *x++;
63 if ((b->wds = x1 - b->x) == 0)
64 b->x[0] = 0;
67 int
68 #ifdef KR_headers
69 trailz(b) Bigint *b;
70 #else
71 trailz(Bigint *b)
72 #endif
74 ULong L, *x, *xe;
75 int n = 0;
77 x = b->x;
78 xe = x + b->wds;
79 for(n = 0; x < xe && !*x; x++)
80 n += ULbits;
81 if (x < xe) {
82 L = *x;
83 n += lo0bits(&L);
85 return n;