Skip to content

Commit 9058c3f

Browse files
committed
fix logical right shift bug
1 parent 96b889d commit 9058c3f

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

otherlibs/stdlib_beta/int16.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ let shift_left x y = of_int (Int.shift_left (to_int x) y)
6868

6969
let shift_right x y = of_int (Int.shift_right (to_int x) y)
7070

71-
let shift_right_logical x y = of_int (Int.shift_right_logical (to_int x) y)
71+
let shift_right_logical x y =
72+
of_int (Int.shift_right_logical (to_int x land mask) y)
7273

7374
let equal x y = Int.equal (to_int x) (to_int y)
7475

otherlibs/stdlib_beta/int8.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ let shift_left x y = of_int (Int.shift_left (to_int x) y)
6868

6969
let shift_right x y = of_int (Int.shift_right (to_int x) y)
7070

71-
let shift_right_logical x y = of_int (Int.shift_right_logical (to_int x) y)
71+
let shift_right_logical x y =
72+
of_int (Int.shift_right_logical (to_int x land mask) y)
7273

7374
let equal x y = Int.equal (to_int x) (to_int y)
7475

testsuite/tests/lib-smallint/test_int16.ml

+7-2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ let test_logical1 = test_conv1 ~equal:equal_logical
8282

8383
let test_logical2 = test_conv2 ~equal:equal_logical
8484

85+
let reference_shift_right_logical x i =
86+
(* we need to ensure that we shift in zero bytes, which is incompatible with
87+
sign-extension *)
88+
Int.shift_right_logical (if i > 0 then x land mask else x) i
89+
8590
let () =
8691
test_round_trip ();
8792
assert (to_int Int16.zero == Int.zero);
@@ -104,8 +109,8 @@ let () =
104109
test_logical1 (apply_shift Int16.shift_right) (apply_shift Int.shift_right);
105110
test_conv1
106111
(apply_shift Int16.shift_right_logical)
107-
(apply_shift Int.shift_right_logical)
108-
~equal:(if shift = 0 then equal_logical else equal_arith);
112+
(apply_shift reference_shift_right_logical)
113+
~equal:equal_logical;
109114
test_conv1
110115
(apply_shift Int16.shift_left)
111116
(apply_shift Int.shift_left)

testsuite/tests/lib-smallint/test_int8.ml

+7-2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ let test_logical1 = test_conv1 ~equal:equal_logical
8282

8383
let test_logical2 = test_conv2 ~equal:equal_logical
8484

85+
let reference_shift_right_logical x i =
86+
(* we need to ensure that we shift in zero bytes, which is incompatible with
87+
sign-extension *)
88+
Int.shift_right_logical (if i > 0 then x land mask else x) i
89+
8590
let () =
8691
test_round_trip ();
8792
assert (to_int Int8.zero == Int.zero);
@@ -104,8 +109,8 @@ let () =
104109
test_logical1 (apply_shift Int8.shift_right) (apply_shift Int.shift_right);
105110
test_conv1
106111
(apply_shift Int8.shift_right_logical)
107-
(apply_shift Int.shift_right_logical)
108-
~equal:(if shift = 0 then equal_logical else equal_arith);
112+
(apply_shift reference_shift_right_logical)
113+
~equal:equal_logical;
109114
test_conv1
110115
(apply_shift Int8.shift_left)
111116
(apply_shift Int.shift_left)

0 commit comments

Comments
 (0)