From 67584a367621e9712a8bdb21ce7237c79acf2418 Mon Sep 17 00:00:00 2001 From: zapashcanon Date: Fri, 23 Feb 2024 15:59:47 +0100 Subject: [PATCH] simplify rotl and rotr --- src/int32.ml | 11 ++++------- src/int64.ml | 11 ++++------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/src/int32.ml b/src/int32.ml index ead26c290..cb3b914b7 100644 --- a/src/int32.ml +++ b/src/int32.ml @@ -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 diff --git a/src/int64.ml b/src/int64.ml index 85cdbf5c2..570e71801 100644 --- a/src/int64.ml +++ b/src/int64.ml @@ -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