Skip to content

Commit

Permalink
simplify rotl and rotr
Browse files Browse the repository at this point in the history
  • Loading branch information
zapashcanon committed Feb 23, 2024
1 parent f6e44a1 commit f4027ea
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
11 changes: 4 additions & 7 deletions src/int32.ml
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,13 @@ let shr_s x y = shift shift_right x y

let shr_u x y = sx (shift shift_right_logical x y)

(* We must mask the count to implement rotates via shifts. *)
let clamp_rotate_count n = to_int (logand n 31l)

let rotl x y =
let n = clamp_rotate_count y in
logor (shl x (of_int n)) (shr_u x (of_int (32 - n)))
let n = logand y 31l in
logor (shl x n) (shr_u x (sub 32l n))

let rotr x y =
let n = clamp_rotate_count y in
logor (shr_u x (of_int n)) (shl x (of_int (32 - n)))
let n = logand y 31l in
logor (shr_u x n) (shl x (sub 32l n))

let extend_s n x =
let shift = 32 - n in
Expand Down
11 changes: 4 additions & 7 deletions src/int64.ml
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,13 @@ let shr_s x y = shift shift_right x y

let shr_u x y = shift shift_right_logical x y

(* We must mask the count to implement rotates via shifts. *)
let clamp_rotate_count n = to_int (logand n (of_int 63))

let rotl x y =
let n = clamp_rotate_count y in
logor (shl x (of_int n)) (shr_u x (of_int (64 - n)))
let n = logand y 63L in
logor (shl x n) (shr_u x (sub 64L n))

let rotr x y =
let n = clamp_rotate_count y in
logor (shr_u x (of_int n)) (shl x (of_int (64 - n)))
let n = logand y 63L in
logor (shr_u x n) (shl x (sub 64L n))

let extend_s n x =
let shift = 64 - n in
Expand Down

0 comments on commit f4027ea

Please sign in to comment.