emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js
Implementation of "<<" for Long works, added tests for "+".
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 ];
42 for (var i = 0; i < a.length; i++) {
44 var low_digit = low % 10;
45 digit = (res % 10) + low_digit;
47 low = Math.floor(low / 10);
48 res = Math.floor(res / 10);
54 s = String(digit).concat(s);
56 return String(res).concat(s);
61 Number.prototype.add64 = function(x) {
68 var hi = (this.high32() + x.high32() + carry) | 0;
69 return hi.next32(low);
72 Number.prototype.div64 = function(x) {
73 var low = Math.floor(this.toFP() / x.toFP()); // TODO: not exact enough
75 var hi = Math.floor(low / (__m32+1)) | 0;
76 return hi.next32(low % (__m32+1));
81 Number.prototype.and64 = function(x) {
83 if (this.hi && x.hi) {
84 var hi = this.hi & x.hi;
85 return hi.next32(low);
90 Number.prototype.shl64 = function(x) {
92 var hi = (this << (x - 32)) | 0;
95 var hi = (this.high32() << x) | 0;
96 var low_reminder = this >> (32 - x);
99 low += (low < 0) ? (__m32+1) : 0;
100 return hi.next32(low);
104 Number.prototype.shr64 = function(x) {
106 var low = (this.high32() >> (x - 32)) & 0xFFFFFFFF;
109 var low = (this >> x) & 0xFFFFFFFF;
110 var hi_reminder = (this.high32() << (32 - x)) >> (32 - x);
112 var hi = this.high32() >> x;
113 return hi.next32(low);
117 Number.prototype.compare64 = function(x) {
118 if (this.hi == x.hi) {
119 return (this == x) ? 0 : ((this < x) ? -1 : 1);
121 return (this.hi < x.hi) ? -1 : 1;