github.com/goplusjs/gopherjs@v1.2.6-0.20211206034512-f187917453b8/compiler/prelude/numeric.go (about) 1 package prelude 2 3 const numeric = ` 4 var $min = Math.min; 5 var $mod = function(x, y) { return x % y; }; 6 var $parseInt = parseInt; 7 var $parseFloat = function(f) { 8 if (f !== undefined && f !== null && f.constructor === Number) { 9 return f; 10 } 11 return parseFloat(f); 12 }; 13 14 var $froundBuf = new Float32Array(1); 15 var $fround = Math.fround || function(f) { 16 $froundBuf[0] = f; 17 return $froundBuf[0]; 18 }; 19 20 var $imul = Math.imul || function(a, b) { 21 var ah = (a >>> 16) & 0xffff; 22 var al = a & 0xffff; 23 var bh = (b >>> 16) & 0xffff; 24 var bl = b & 0xffff; 25 return ((al * bl) + (((ah * bl + al * bh) << 16) >>> 0) >> 0); 26 }; 27 28 var $floatKey = function(f) { 29 if (f !== f) { 30 $idCounter++; 31 return "NaN$" + $idCounter; 32 } 33 return String(f); 34 }; 35 36 var $flatten64 = function(x) { 37 return x.$high * 4294967296 + x.$low; 38 }; 39 40 var $shiftLeft64 = function(x, y) { 41 if (y === 0) { 42 return x; 43 } 44 if (y < 32) { 45 return new x.constructor(x.$high << y | x.$low >>> (32 - y), (x.$low << y) >>> 0); 46 } 47 if (y < 64) { 48 return new x.constructor(x.$low << (y - 32), 0); 49 } 50 return new x.constructor(0, 0); 51 }; 52 53 var $shiftRightInt64 = function(x, y) { 54 if (y === 0) { 55 return x; 56 } 57 if (y < 32) { 58 return new x.constructor(x.$high >> y, (x.$low >>> y | x.$high << (32 - y)) >>> 0); 59 } 60 if (y < 64) { 61 return new x.constructor(x.$high >> 31, (x.$high >> (y - 32)) >>> 0); 62 } 63 if (x.$high < 0) { 64 return new x.constructor(-1, 4294967295); 65 } 66 return new x.constructor(0, 0); 67 }; 68 69 var $shiftRightUint64 = function(x, y) { 70 if (y === 0) { 71 return x; 72 } 73 if (y < 32) { 74 return new x.constructor(x.$high >>> y, (x.$low >>> y | x.$high << (32 - y)) >>> 0); 75 } 76 if (y < 64) { 77 return new x.constructor(0, x.$high >>> (y - 32)); 78 } 79 return new x.constructor(0, 0); 80 }; 81 82 var $mul64 = function(x, y) { 83 var high = 0, low = 0; 84 if ((y.$low & 1) !== 0) { 85 high = x.$high; 86 low = x.$low; 87 } 88 for (var i = 1; i < 32; i++) { 89 if ((y.$low & 1<<i) !== 0) { 90 high += x.$high << i | x.$low >>> (32 - i); 91 low += (x.$low << i) >>> 0; 92 } 93 } 94 for (var i = 0; i < 32; i++) { 95 if ((y.$high & 1<<i) !== 0) { 96 high += x.$low << i; 97 } 98 } 99 return new x.constructor(high, low); 100 }; 101 102 var $div64 = function(x, y, returnRemainder) { 103 if (y.$high === 0 && y.$low === 0) { 104 $throwRuntimeError("integer divide by zero"); 105 } 106 107 var s = 1; 108 var rs = 1; 109 110 var xHigh = x.$high; 111 var xLow = x.$low; 112 if (xHigh < 0) { 113 s = -1; 114 rs = -1; 115 xHigh = -xHigh; 116 if (xLow !== 0) { 117 xHigh--; 118 xLow = 4294967296 - xLow; 119 } 120 } 121 122 var yHigh = y.$high; 123 var yLow = y.$low; 124 if (y.$high < 0) { 125 s *= -1; 126 yHigh = -yHigh; 127 if (yLow !== 0) { 128 yHigh--; 129 yLow = 4294967296 - yLow; 130 } 131 } 132 133 var high = 0, low = 0, n = 0; 134 while (yHigh < 2147483648 && ((xHigh > yHigh) || (xHigh === yHigh && xLow > yLow))) { 135 yHigh = (yHigh << 1 | yLow >>> 31) >>> 0; 136 yLow = (yLow << 1) >>> 0; 137 n++; 138 } 139 for (var i = 0; i <= n; i++) { 140 high = high << 1 | low >>> 31; 141 low = (low << 1) >>> 0; 142 if ((xHigh > yHigh) || (xHigh === yHigh && xLow >= yLow)) { 143 xHigh = xHigh - yHigh; 144 xLow = xLow - yLow; 145 if (xLow < 0) { 146 xHigh--; 147 xLow += 4294967296; 148 } 149 low++; 150 if (low === 4294967296) { 151 high++; 152 low = 0; 153 } 154 } 155 yLow = (yLow >>> 1 | yHigh << (32 - 1)) >>> 0; 156 yHigh = yHigh >>> 1; 157 } 158 159 if (returnRemainder) { 160 return new x.constructor(xHigh * rs, xLow * rs); 161 } 162 return new x.constructor(high * s, low * s); 163 }; 164 165 var $divComplex = function(n, d) { 166 var ninf = n.$real === Infinity || n.$real === -Infinity || n.$imag === Infinity || n.$imag === -Infinity; 167 var dinf = d.$real === Infinity || d.$real === -Infinity || d.$imag === Infinity || d.$imag === -Infinity; 168 var nnan = !ninf && (n.$real !== n.$real || n.$imag !== n.$imag); 169 var dnan = !dinf && (d.$real !== d.$real || d.$imag !== d.$imag); 170 if(nnan || dnan) { 171 return new n.constructor(NaN, NaN); 172 } 173 if (ninf && !dinf) { 174 return new n.constructor(Infinity, Infinity); 175 } 176 if (!ninf && dinf) { 177 return new n.constructor(0, 0); 178 } 179 if (d.$real === 0 && d.$imag === 0) { 180 if (n.$real === 0 && n.$imag === 0) { 181 return new n.constructor(NaN, NaN); 182 } 183 return new n.constructor(Infinity, Infinity); 184 } 185 var a = Math.abs(d.$real); 186 var b = Math.abs(d.$imag); 187 if (a <= b) { 188 var ratio = d.$real / d.$imag; 189 var denom = d.$real * ratio + d.$imag; 190 return new n.constructor((n.$real * ratio + n.$imag) / denom, (n.$imag * ratio - n.$real) / denom); 191 } 192 var ratio = d.$imag / d.$real; 193 var denom = d.$imag * ratio + d.$real; 194 return new n.constructor((n.$imag * ratio + n.$real) / denom, (n.$imag - n.$real * ratio) / denom); 195 }; 196 `