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