Skip to content

Commit ff57030

Browse files
authored
Change %mulint primitive behavior (#7358)
* Change `%mulint` primitive behavior * add changelog
1 parent 2fb6cc8 commit ff57030

32 files changed

+54
-58
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
- Replace ~date with ~day in Date.make\*. https://github.com/rescript-lang/rescript/pull/7324
2929
- Remove `-bs-jsx-mode`. https://github.com/rescript-lang/rescript/pull/7327
3030
- Drop Node.js version <20 support, as it is reaching End-of-Life. https://github.com/rescript-lang/rescript-compiler/pull/7354
31+
- Treat `int` multiplication as a normal int32 operation instead of using `Math.imul`. https://github.com/rescript-lang/rescript/pull/7358
3132

3233
#### :house: Internal
3334

compiler/core/js_exp_make.ml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,14 +1607,8 @@ let int32_mul ?comment (e1 : J.expression) (e2 : J.expression) : J.expression =
16071607
| {expression_desc = Number (Int {i = i0}); _}, e ->
16081608
let i = is_pos_pow i0 in
16091609
if i >= 0 then int32_lsl e (small_int i)
1610-
else
1611-
call ?comment ~info:Js_call_info.builtin_runtime_call
1612-
(dot (js_global "Math") Literals.imul)
1613-
[e1; e2]
1614-
| _ ->
1615-
call ?comment ~info:Js_call_info.builtin_runtime_call
1616-
(dot (js_global "Math") Literals.imul)
1617-
[e1; e2]
1610+
else to_int32 (float_mul ?comment e1 e2)
1611+
| _ -> to_int32 (float_mul ?comment e1 e2)
16181612

16191613
let unchecked_int32_mul ?comment e1 e2 : J.expression =
16201614
{comment; expression_desc = Bin (Mul, e1, e2)}

compiler/ext/literals.ml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ let runtime = "runtime" (* runtime directory *)
5050

5151
let stdlib = "stdlib"
5252

53-
let imul = "imul" (* signed int32 mul *)
54-
5553
let setter_suffix = "#="
5654

5755
let setter_suffix_len = String.length setter_suffix

lib/es6/Primitive_hash.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function unsafe_pop(q) {
3636
RE_EXN_ID: "Assert_failure",
3737
_1: [
3838
"Primitive_hash.res",
39-
70,
39+
73,
4040
12
4141
],
4242
Error: new Error()

lib/es6/Stdlib_Int.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function range(start, end, optionsOpt) {
4646
let range$2 = options.inclusive === true ? range$1 + 1 | 0 : range$1;
4747
length = Math.ceil(range$2 / abs(step)) | 0;
4848
}
49-
return Stdlib_Array.fromInitializer(length, i => start + Math.imul(i, step) | 0);
49+
return Stdlib_Array.fromInitializer(length, i => start + (i * step | 0) | 0);
5050
}
5151

5252
function rangeWithOptions(start, end, options) {

lib/js/Primitive_hash.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function unsafe_pop(q) {
3636
RE_EXN_ID: "Assert_failure",
3737
_1: [
3838
"Primitive_hash.res",
39-
70,
39+
73,
4040
12
4141
],
4242
Error: new Error()

lib/js/Stdlib_Int.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function range(start, end, optionsOpt) {
4646
let range$2 = options.inclusive === true ? range$1 + 1 | 0 : range$1;
4747
length = Math.ceil(range$2 / abs(step)) | 0;
4848
}
49-
return Stdlib_Array.fromInitializer(length, i => start + Math.imul(i, step) | 0);
49+
return Stdlib_Array.fromInitializer(length, i => start + (i * step | 0) | 0);
5050
}
5151

5252
function rangeWithOptions(start, end, options) {

runtime/Primitive_hash.res

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ module String = Primitive_string_extern
2929

3030
@send external charCodeAt: (string, int) => int = "charCodeAt"
3131

32+
// Multiply int32 with C-style overflow behavior
33+
external imul: (int, int) => int = "Math.imul"
34+
3235
type rec cell<'a> = {
3336
content: 'a,
3437
mutable next: option<cell<'a>>,
@@ -85,19 +88,19 @@ let rotl32 = (x: int, n) => lor(lsl(x, n), lsr(x, 32 - n))
8588

8689
let hash_mix_int = (h, d) => {
8790
let d = ref(d)
88-
d.contents = d.contents * 0xcc9e2d51
91+
d.contents = imul(d.contents, 0xcc9e2d51)
8992
d.contents = rotl32(d.contents, 15)
90-
d.contents = d.contents * 0x1b873593
93+
d.contents = imul(d.contents, 0x1b873593)
9194
let h = ref(lxor(h, d.contents))
9295
h.contents = rotl32(h.contents, 13)
9396
h.contents + lsl(h.contents, 2) + 0xe6546b64
9497
}
9598

9699
let hash_final_mix = h => {
97100
let h = ref(lxor(h, lsr(h, 16)))
98-
h.contents = h.contents * 0x85ebca6b
101+
h.contents = imul(h.contents, 0x85ebca6b)
99102
h.contents = lxor(h.contents, lsr(h.contents, 13))
100-
h.contents = h.contents * 0xc2b2ae35
103+
h.contents = imul(h.contents, 0xc2b2ae35)
101104
lxor(h.contents, lsr(h.contents, 16))
102105
}
103106

tests/gentype_tests/typescript-react-example/src/Records.res.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/gentype_tests/typescript-react-example/src/nested/Tuples.res.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)