Skip to content

Commit 0d972a5

Browse files
committed
runtime: use exponentiation operator
Signed-off-by: Sora Morimoto <[email protected]>
1 parent 07d2e90 commit 0d972a5

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

runtime/ieee_754.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ function caml_int64_bits_of_float(x) {
5757
let exp = jsoo_floor_log2(x) + 1023;
5858
if (exp <= 0) {
5959
exp = 0;
60-
x /= Math.pow(2, -1026);
60+
x /= 2 ** -1026;
6161
} else {
62-
x /= Math.pow(2, exp - 1027);
62+
x /= 2 ** (exp - 1027);
6363
if (x < 16) {
6464
x *= 2;
6565
exp -= 1;
@@ -68,7 +68,7 @@ function caml_int64_bits_of_float(x) {
6868
x /= 2;
6969
}
7070
}
71-
const k = Math.pow(2, 24);
71+
const k = 2 ** 24;
7272
let r3 = x | 0;
7373
x = (x - r3) * k;
7474
const r2 = x | 0;
@@ -130,7 +130,7 @@ function caml_hexstring_of_float(x, prec, style) {
130130
}
131131
if (prec >= 0 && prec < 13) {
132132
/* If a precision is given, and is small, round mantissa accordingly */
133-
const cst = Math.pow(2, prec * 4);
133+
const cst = 2 ** (prec * 4);
134134
x = Math.round(x * cst) / cst;
135135
}
136136
let x_str = x.toString(16);
@@ -161,12 +161,12 @@ function caml_int64_float_of_bits(x) {
161161
return hi & 0x8000 ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY;
162162
else return Number.NaN;
163163
}
164-
const k = Math.pow(2, -24);
164+
const k = 2 ** -24;
165165
let res = (lo * k + mi) * k + (hi & 0xf);
166166
if (exp > 0) {
167167
res += 16;
168-
res *= Math.pow(2, exp - 1027);
169-
} else res *= Math.pow(2, -1026);
168+
res *= 2 ** (exp - 1027);
169+
} else res *= 2 ** -1026;
170170
if (hi & 0x8000) res = -res;
171171
return res;
172172
}
@@ -177,8 +177,8 @@ function caml_nextafter_float(x, y) {
177177
if (Number.isNaN(x) || Number.isNaN(y)) return Number.NaN;
178178
if (x === y) return y;
179179
if (x === 0) {
180-
if (y < 0) return -Math.pow(2, -1074);
181-
else return Math.pow(2, -1074);
180+
if (y < 0) return -(2 ** -1074);
181+
else return 2 ** -1074;
182182
}
183183
let bits = caml_int64_bits_of_float(x);
184184
const one = caml_int64_of_int32(1);
@@ -230,18 +230,18 @@ function caml_ldexp_float(x, exp) {
230230
exp |= 0;
231231
if (exp > 1023) {
232232
exp -= 1023;
233-
x *= Math.pow(2, 1023);
233+
x *= 2 ** 1023;
234234
if (exp > 1023) {
235235
// in case x is subnormal
236236
exp -= 1023;
237-
x *= Math.pow(2, 1023);
237+
x *= 2 ** 1023;
238238
}
239239
}
240240
if (exp < -1023) {
241241
exp += 1023;
242-
x *= Math.pow(2, -1023);
242+
x *= 2 ** -1023;
243243
}
244-
x *= Math.pow(2, exp);
244+
x *= 2 ** exp;
245245
return x;
246246
}
247247
//Provides: caml_frexp_float const
@@ -251,7 +251,7 @@ function caml_frexp_float(x) {
251251
const neg = x < 0;
252252
if (neg) x = -x;
253253
let exp = Math.max(-1023, jsoo_floor_log2(x) + 1);
254-
x *= Math.pow(2, -exp);
254+
x *= 2 ** -exp;
255255
while (x < 0.5) {
256256
x *= 2;
257257
exp--;
@@ -293,7 +293,7 @@ function caml_expm1_float(x) {
293293
}
294294
//Provides: caml_exp2_float const
295295
function caml_exp2_float(x) {
296-
return Math.pow(2, x);
296+
return 2 ** x;
297297
}
298298
//Provides: caml_log1p_float const
299299
function caml_log1p_float(x) {
@@ -379,12 +379,12 @@ function caml_erfc_float(x) {
379379

380380
//Provides: caml_fma_float const
381381
function caml_fma_float(x, y, z) {
382-
const SPLIT = Math.pow(2, 27) + 1;
383-
const MIN_VALUE = Math.pow(2, -1022);
384-
const EPSILON = Math.pow(2, -52);
382+
const SPLIT = 2 ** 27 + 1;
383+
const MIN_VALUE = 2 ** -1022;
384+
const EPSILON = 2 ** -52;
385385
const C = 416;
386-
const A = Math.pow(2, +C);
387-
const B = Math.pow(2, -C);
386+
const A = 2 ** +C;
387+
const B = 2 ** -C;
388388

389389
function multiply(a, b) {
390390
const at = SPLIT * a;
@@ -500,7 +500,7 @@ function caml_format_float(fmt, x) {
500500
let e = Number.parseInt(x.toString().split("+")[1]);
501501
if (e > 20) {
502502
e -= 20;
503-
x /= Math.pow(10, e);
503+
x /= 10 ** e;
504504
x += new Array(e + 1).join("0");
505505
if (dp > 0) {
506506
x = x + "." + new Array(dp + 1).join("0");
@@ -586,7 +586,7 @@ function caml_float_of_string(s) {
586586
const m3 = m[3].replace(/0+$/, "");
587587
const mantissa = Number.parseInt(m[1] + m[2] + m3, 16);
588588
const exponent = (m[5] | 0) - 4 * m3.length;
589-
res = mantissa * Math.pow(2, exponent);
589+
res = mantissa * 2 ** exponent;
590590
return res;
591591
}
592592
if (/^\+?inf(inity)?$/i.test(s)) return Number.POSITIVE_INFINITY;

runtime/int64.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1919

2020
//Provides: caml_int64_offset
21-
const caml_int64_offset = Math.pow(2, -24);
21+
const caml_int64_offset = 2 ** -24;
2222

2323
//Provides: MlInt64
2424
//Requires: caml_int64_offset, caml_raise_zero_divide
@@ -203,9 +203,7 @@ MlInt64.prototype.toInt = function () {
203203
return this.lo | (this.mi << 24);
204204
};
205205
MlInt64.prototype.toFloat = function () {
206-
return (
207-
(this.hi << 16) * Math.pow(2, 32) + this.mi * Math.pow(2, 24) + this.lo
208-
);
206+
return (this.hi << 16) * 2 ** 32 + this.mi * 2 ** 24 + this.lo;
209207
};
210208
MlInt64.prototype.toArray = function () {
211209
return [

0 commit comments

Comments
 (0)