emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js
Long - conversion to exact string representation works.
1 // empty line needed here
2 Number.prototype.add32 = function(x) { return (this + x) | 0; };
3 Number.prototype.sub32 = function(x) { return (this - x) | 0; };
4 Number.prototype.mul32 = function(x) {
5 return (((this * (x >> 16)) << 16) + this * (x & 0xFFFF)) | 0;
8 Number.prototype.toInt8 = function() { return (this << 24) >> 24; };
9 Number.prototype.toInt16 = function() { return (this << 16) >> 16; };
11 var __m32 = 0xFFFFFFFF;
13 Number.prototype.next32 = function(low) {
17 var l = new Number(low);
22 Number.prototype.high32 = function() {
23 return this.hi ? this.hi : (Math.floor(this / (__m32+1))) | 0;
25 Number.prototype.toInt32 = function() { return this | 0; };
26 Number.prototype.toFP = function() {
27 return this.hi ? this.hi * (__m32+1) + this : this;
29 Number.prototype.toLong = function() {
30 var hi = (this > __m32) ? (Math.floor(this / (__m32+1))) | 0 : 0;
31 return hi.next32(Math.floor(this % (__m32+1)));
34 Number.prototype.toExactString = function() {
37 var a = [ 6,9,2,7,6,9,4,9,2,4 ];
40 var neg = this.hi < 0;
49 for (var i = 0; i < a.length; i++) {
51 var low_digit = low % 10;
52 digit = (res % 10) + low_digit;
54 low = Math.floor(low / 10);
55 res = Math.floor(res / 10);
61 s = String(digit).concat(s);
63 return (neg ? '-' : '').concat(res).concat(s);
68 Number.prototype.add64 = function(x) {
75 var hi = (this.high32() + x.high32() + carry) | 0;
76 return hi.next32(low);
79 Number.prototype.sub64 = function(x) {
86 var hi = (this.high32() - x.high32() - carry) | 0;
87 return hi.next32(low);
90 Number.prototype.mul64 = function(x) {
91 var low = this.mul32(x);
92 low += (low < 0) ? (__m32+1) : 0;
93 // first count upper 32 bits of (this.low * x.low)
97 for (var i = 0; i < 32; i++) {
100 hi_low += this & 0xFFFF
103 hi_low += (hi_hi & 1) ? 0x8000 : 0;
107 var hi = (hi_hi << 16) + hi_low;
109 var m1 = this.high32().mul32(x);
110 var m2 = this.mul32(x.high32());
111 hi = hi.add32(m1).add32(m2);
113 return hi.next32(low);
116 Number.prototype.div64 = function(x) {
117 var low = Math.floor(this.toFP() / x.toFP()); // TODO: not exact enough
119 var hi = Math.floor(low / (__m32+1)) | 0;
120 return hi.next32(low % (__m32+1));
125 Number.prototype.and64 = function(x) {
127 low += (low < 0) ? (__m32+1) : 0;
128 if (this.hi && x.hi) {
129 var hi = this.hi & x.hi;
130 return hi.next32(low);
135 Number.prototype.or64 = function(x) {
137 low += (low < 0) ? (__m32+1) : 0;
138 if (this.hi || x.hi) {
139 var hi = this.hi | x.hi;
140 return hi.next32(low);
145 Number.prototype.xor64 = function(x) {
147 low += (low < 0) ? (__m32+1) : 0;
148 if (this.hi || x.hi) {
149 var hi = this.hi ^ x.hi;
150 return hi.next32(low);
155 Number.prototype.shl64 = function(x) {
157 var hi = this << (x - 32);
160 var hi = this.high32() << x;
161 var low_reminder = this >> (32 - x);
164 low += (low < 0) ? (__m32+1) : 0;
165 return hi.next32(low);
169 Number.prototype.shr64 = function(x) {
171 var low = this.high32() >> (x - 32);
172 low += (low < 0) ? (__m32+1) : 0;
176 var hi_reminder = this.high32() << (32 - x);
178 low += (low < 0) ? (__m32+1) : 0;
179 var hi = this.high32() >> x;
180 return hi.next32(low);
184 Number.prototype.ushr64 = function(x) {
186 var low = this.high32() >>> (x - 32);
187 low += (low < 0) ? (__m32+1) : 0;
190 var low = this >>> x;
191 var hi_reminder = this.high32() << (32 - x);
193 low += (low < 0) ? (__m32+1) : 0;
194 var hi = this.high32() >>> x;
195 return hi.next32(low);
199 Number.prototype.compare64 = function(x) {
200 if (this.hi == x.hi) {
201 return (this == x) ? 0 : ((this < x) ? -1 : 1);
203 return (this.hi < x.hi) ? -1 : 1;
206 Number.prototype.neg64 = function() {
207 var hi = this.high32();
209 if ((hi === 0) && (low < 0)) { return -low; }
212 low += (low < 0) ? (__m32+1) : 0;
213 var ret = hi.next32(low);