aboutsummaryrefslogtreecommitdiff
path: root/src/libmp/port/mpleft.c
blob: 6da07d2ef03786b199a1a1e2a60aedd38f968790 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "os.h"
#include <mp.h>
#include "dat.h"

/* res = b << shift */
void
mpleft(mpint *b, int shift, mpint *res)
{
	int d, l, r, i, otop;
	mpdigit this, last;

	res->sign = b->sign;
	if(b->top==0){
		res->top = 0;
		return;
	}

	/* a negative left shift is a right shift */
	if(shift < 0){
		mpright(b, -shift, res);
		return;
	}

	/* b and res may be the same so remember the old top */
	otop = b->top;

	/* shift */
	mpbits(res, otop*Dbits + shift);	/* overkill */
	res->top = DIGITS(otop*Dbits + shift);
	d = shift/Dbits;
	l = shift - d*Dbits;
	r = Dbits - l;

	if(l == 0){
		for(i = otop-1; i >= 0; i--)
			res->p[i+d] = b->p[i];
	} else {
		last = 0;
		for(i = otop-1; i >= 0; i--) {
			this = b->p[i];
			res->p[i+d+1] = (last<<l) | (this>>r);
			last = this;
		}
		res->p[d] = last<<l;
	}
	for(i = 0; i < d; i++)
		res->p[i] = 0;

	/* normalize */
	while(res->top > 0 && res->p[res->top-1] == 0)
		res->top--;
}