Subversion Repositories planix.SVN

Rev

Rev 33 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 1
#include "os.h"
2
#include <mp.h>
3
#include "dat.h"
4
 
5
// res = b >> shift
6
void
7
mpright(mpint *b, int shift, mpint *res)
8
{
9
	int d, l, r, i;
10
	mpdigit this, last;
11
 
12
	res->sign = b->sign;
13
	if(b->top==0){
14
		res->top = 0;
15
		return;
16
	}
17
 
18
	// a negative right shift is a left shift
19
	if(shift < 0){
20
		mpleft(b, -shift, res);
21
		return;
22
	}
23
 
24
	if(res != b)
25
		mpbits(res, b->top*Dbits - shift);
33 7u83 26
	else if(shift == 0)
27
		return;
28
 
2 - 29
	d = shift/Dbits;
30
	r = shift - d*Dbits;
31
	l = Dbits - r;
32
 
33
	//  shift all the bits out == zero
34
	if(d>=b->top){
33 7u83 35
		res->sign = 1;
2 - 36
		res->top = 0;
37
		return;
38
	}
39
 
40
	// special case digit shifts
41
	if(r == 0){
42
		for(i = 0; i < b->top-d; i++)
43
			res->p[i] = b->p[i+d];
44
	} else {
45
		last = b->p[d];
46
		for(i = 0; i < b->top-d-1; i++){
47
			this = b->p[i+d+1];
48
			res->p[i] = (this<<l) | (last>>r);
49
			last = this;
50
		}
51
		res->p[i++] = last>>r;
52
	}
33 7u83 53
 
2 - 54
	res->top = i;
33 7u83 55
	res->flags |= b->flags & MPtimesafe;
56
	mpnorm(res);
2 - 57
}