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
//
6
//	divide two digits by one and return quotient
7
//
8
void
9
mpdigdiv(mpdigit *dividend, mpdigit divisor, mpdigit *quotient)
10
{
11
	mpdigit hi, lo, q, x, y;
12
	int i;
13
 
14
	hi = dividend[1];
15
	lo = dividend[0];
16
 
17
	// return highest digit value if the result >= 2**32
18
	if(hi >= divisor || divisor == 0){
19
		divisor = 0;
20
		*quotient = ~divisor;
21
		return;
22
	}
23
 
33 7u83 24
	// very common case
25
	if(~divisor == 0){
26
		lo += hi;
27
		if(lo < hi){
28
			hi++;
29
			lo++;
30
		}
31
		if(lo+1 == 0)
32
			hi++;
33
		*quotient = hi;
34
		return;
35
	}
36
 
2 - 37
	// at this point we know that hi < divisor
38
	// just shift and subtract till we're done
39
	q = 0;
40
	x = divisor;
41
	for(i = Dbits-1; hi > 0 && i >= 0; i--){
42
		x >>= 1;
43
		if(x > hi)
44
			continue;
45
		y = divisor<<i;
46
		if(x == hi && y > lo)
47
			continue;
48
		if(y > lo)
49
			hi--;
50
		lo -= y;
51
		hi -= x;
52
		q |= 1<<i;
53
	}
54
	q += lo/divisor;
55
	*quotient = q;
56
}