xref: /plan9/sys/src/cmd/unix/drawterm/libmp/mpleft.c (revision 8ccd4a6360d974db7bd7bbd4f37e7018419ea908)
1 #include "os.h"
2 #include <mp.h>
3 #include "dat.h"
4 
5 // res = b << shift
6 void
mpleft(mpint * b,int shift,mpint * res)7 mpleft(mpint *b, int shift, mpint *res)
8 {
9 	int d, l, r, i, otop;
10 	mpdigit this, last;
11 
12 	// a negative left shift is a right shift
13 	if(shift < 0){
14 		mpright(b, -shift, res);
15 		return;
16 	}
17 
18 	// b and res may be the same so remember the old top
19 	otop = b->top;
20 
21 	// shift
22 	mpbits(res, otop*Dbits + shift);	// overkill
23 	res->top = DIGITS(otop*Dbits + shift);
24 	d = shift/Dbits;
25 	l = shift - d*Dbits;
26 	r = Dbits - l;
27 
28 	if(l == 0){
29 		for(i = otop-1; i >= 0; i--)
30 			res->p[i+d] = b->p[i];
31 	} else {
32 		last = 0;
33 		for(i = otop-1; i >= 0; i--) {
34 			this = b->p[i];
35 			res->p[i+d+1] = (last<<l) | (this>>r);
36 			last = this;
37 		}
38 		res->p[d] = last<<l;
39 	}
40 	for(i = 0; i < d; i++)
41 		res->p[i] = 0;
42 
43 	// normalize
44 	while(res->top > 0 && res->p[res->top-1] == 0)
45 		res->top--;
46 }
47