Skip to content

Commit c2b1355

Browse files
authored
flambda-backend: Fix wrong shift generation in Cmm_helpers (oxcaml#347)
1 parent 739243b commit c2b1355

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

asmcomp/cmm_helpers.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,10 @@ let untag_int i dbg =
239239
match i with
240240
Cconst_int (n, _) -> Cconst_int(n asr 1, dbg)
241241
| Cop(Cor, [Cop(Casr, [c; Cconst_int (n, _)], _); Cconst_int (1, _)], _)
242-
when n > 0 && n < size_int * 8 ->
242+
when n > 0 && n < size_int * 8 - 1 ->
243243
Cop(Casr, [c; Cconst_int (n+1, dbg)], dbg)
244244
| Cop(Cor, [Cop(Clsr, [c; Cconst_int (n, _)], _); Cconst_int (1, _)], _)
245-
when n > 0 && n < size_int * 8 ->
245+
when n > 0 && n < size_int * 8 - 1 ->
246246
Cop(Clsr, [c; Cconst_int (n+1, dbg)], dbg)
247247
| c -> asr_int c (Cconst_int (1, dbg)) dbg
248248

@@ -433,6 +433,7 @@ let rec div_int c1 c2 is_safe dbg =
433433
res = shift-right-signed(c1 + t, l)
434434
*)
435435
Cop(Casr, [bind "dividend" c1 (fun c1 ->
436+
assert (l >= 1);
436437
let t = asr_int c1 (Cconst_int (l - 1, dbg)) dbg in
437438
let t =
438439
lsr_int t (Cconst_int (Nativeint.size - l, dbg)) dbg
@@ -490,6 +491,7 @@ let mod_int c1 c2 is_safe dbg =
490491
res = c1 - t
491492
*)
492493
bind "dividend" c1 (fun c1 ->
494+
assert (l >= 1);
493495
let t = asr_int c1 (Cconst_int (l - 1, dbg)) dbg in
494496
let t = lsr_int t (Cconst_int (Nativeint.size - l, dbg)) dbg in
495497
let t = add_int c1 t dbg in

testsuite/tests/basic/lsr_mod.ml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
(* TEST *)
2+
3+
let [@inline never] lsr_63 (x : int) =
4+
x lsr 63
5+
6+
let [@inline never] mod_2 (x : int) =
7+
x mod 2
8+
9+
let [@inline never] lsr_63_pipe_mod_2 (x : int) =
10+
(lsr_63 x) |> mod_2
11+
12+
let [@inline never] lsr_63_mod_2 (x : int) =
13+
(x lsr 63) mod 2
14+
15+
let () =
16+
assert (Int.equal (lsr_63_pipe_mod_2 0) (lsr_63_mod_2 0))

testsuite/tests/basic/lsr_mod.reference

Whitespace-only changes.

0 commit comments

Comments
 (0)