This repository has been archived by the owner on Mar 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
longvalue.js
151 lines (146 loc) · 3.79 KB
/
longvalue.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
function LongValue(bytes) {
this.bytes = bytes;
}
LongValue.prototype = {
sign: function() {
if (this.bytes[0] >= 0x80) {
return -1;
}
var i = 0;
while(i < 8 && this.bytes[i] === 0) {
++i;
}
return i < 8 ? 1 : 0;
},
add: function(other) {
var c = 0, result = [];
for (var i = 7; i >= 0; i--) {
var n = c + this.bytes[i] + other.bytes[i];
c = 0;
while (n >= 0x100) {
n -= 0x100;
c++;
}
result[i] = n;
}
return new LongValue(result);
},
not: function() {
var result = [];
for (var i = 0; i < 8; i++) {
result.push(0x100 - this.bytes[i]);
}
return new LongValue(result);
},
neg: function() {
return this.not().add(LongValue.One);
},
sub: function(other) {
return this.add(other.neg());
},
cmp: function(other) {
return this.sub(other).sign();
},
mul: function(other) {
var result = [], c = 0;
for (var i = 7; i >= 0; --i) {
for (var j = 0; j <= i; --j) {
c += this.bytes[j] * other.bytes[i - j];
}
var n = Math.floor(c / 256);
result.push(c - n * 256);
c = n;
}
return new LongValue(result);
},
div: function(other) {
var signOfThis = this.sign(), signOfOther = other.sign();
if (signOfOther === 0) { return; }
if (signOfThis === 0) { return LongValue.Zero; }
var dividend = signOfThis < 0 ? this.neg() : this;
var divisor = signOfOther < 0 ? other.neg() : other;
if (dividend.cmp(divisor) < 0) { return LongValue.Zero; }
var multiplier = LongValue.One, iterations = [[divisor,multiplier]];
while(divisor.bytes[0] < 0x40 && dividend.cmd(divisor.add(divisor)) >= 0) {
multiplier = multiplier.add(multiplier); // x2
divisor = divisor.add(divisor);
iterations.push([divisor, multiplier]);
}
var result = LongValue.Zero;
while (iterations.length > 0) {
var i = iterations.pop();
if(dividend.cmd(i[0]) >= 0) {
result = result.add(i[1]);
dividend = dividend.sub(i[0]);
}
}
return signOfThis === signOfOther ? result : result.neg();
},
rem: function(other) {
return this.sub(this.div(other).mul(other));
},
shl: function(sh) {
throw "not implemented";
},
shr: function(sh) {
throw "not implemented";
},
ushr: function(sh) {
throw "not implemented";
},
and: function(other) {
var result = [];
for (var i = 0; i < 8; ++i) {
result[i] = this.bytes[i] & other.bytes[i];
}
return new LongValue(result);
},
or: function(other) {
var result = [];
for (var i = 0; i < 8; ++i) {
result[i] = this.bytes[i] | other.bytes[i];
}
return new LongValue(result);
},
xor: function(other) {
var result = [];
for (var i = 0; i < 8; ++i) {
result[i] = this.bytes[i] ^ other.bytes[i];
}
return new LongValue(result);
},
toNumeric: function() {
var sign = 1, bytes = this.bytes;
if (this.sign() < 0) {
sign = -1;
bytes = this.neg().bytes;
}
var resultValue = 0;
for (var i = 0; i < bytes.length; ++i) {
resultValue = resultValue * 256 + bytes[i];
}
return resultValue * sign;
}
};
LongValue.fromNumeric = function(value) {
var sign = 1, bytes = [];
if (value < 0) {
sign = -1;
value = -value;
}
var n = Math.floor(value);
for (var i = 7; i >= 0; i--) {
var m = Math.floor(n / 256);
bytes[i] = 0 | (n - m * 256);
n = m;
}
var resultValue = new LongValue(bytes);
if (sign < 0) {
resultValue = resultValue.neg();
}
return resultValue;
};
LongValue.Zero = new LongValue([0,0,0,0,0,0,0,0]);
LongValue.One = new LongValue([0,0,0,0,0,0,0,1]);
LongValue.Two = new LongValue([0,0,0,0,0,0,0,2]);
LongValue.MinusOne = new LongValue([255,255,255,255,255,255,255,255]);