| 1 | /**************************************************************** |
| 2 | |
| 3 | The author of this software is David M. Gay. |
| 4 | |
| 5 | Copyright (C) 1998 by Lucent Technologies |
| 6 | All Rights Reserved |
| 7 | |
| 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. |
| 17 | |
| 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. |
| 26 | |
| 27 | ****************************************************************/ |
| 28 | |
| 29 | /* Please send bug reports to David M. Gay (dmg at acm dot org, |
| 30 | * with " at " changed at "@" and " dot " changed to ".").	*/ |
| 31 | |
| 32 | #include "gdtoaimp.h" |
| 33 | |
| 34 | void rshift (Bigint *b, int k) |
| 35 | { |
| 36 | 	ULong *x, *x1, *xe, y; |
| 37 | 	int n; |
| 38 | |
| 39 | 	x = x1 = b->x; |
| 40 | 	n = k >> kshift; |
| 41 | 	if (n < b->wds) { |
| 42 | 		xe = x + b->wds; |
| 43 | 		x += n; |
| 44 | 		if (k &= kmask) { |
| 45 | 			n = ULbits - k; |
| 46 | 			y = *x++ >> k; |
| 47 | 			while(x < xe) { |
| 48 | 				*x1++ = (y | (*x << n)) & ALL_ON; |
| 49 | 				y = *x++ >> k; |
| 50 | 			} |
| 51 | 			if ((*x1 = y) !=0) |
| 52 | 				x1++; |
| 53 | 		} |
| 54 | 		else |
| 55 | 			while(x < xe) |
| 56 | 				*x1++ = *x++; |
| 57 | 	} |
| 58 | 	if ((b->wds = x1 - b->x) == 0) |
| 59 | 		b->x[0] = 0; |
| 60 | } |
| 61 | |
| 62 | int trailz (Bigint *b) |
| 63 | { |
| 64 | 	ULong L, *x, *xe; |
| 65 | 	int n = 0; |
| 66 | |
| 67 | 	x = b->x; |
| 68 | 	xe = x + b->wds; |
| 69 | 	for(n = 0; x < xe && !*x; x++) |
| 70 | 		n += ULbits; |
| 71 | 	if (x < xe) { |
| 72 | 		L = *x; |
| 73 | 		n += lo0bits(&L); |
| 74 | 	} |
| 75 | 	return n; |
| 76 | } |