From 6ea6cceb634b1faac443d8dfdffa8f5d0a0a6165 Mon Sep 17 00:00:00 2001 From: tevador <37503146+tevador@users.noreply.github.com> Date: Tue, 25 Jun 2019 23:41:50 +0200 Subject: [PATCH] Fix undefined behavior in rotr/rotl (#86) --- src/instructions_portable.cpp | 12 ++++++------ src/intrin_portable.h | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/instructions_portable.cpp b/src/instructions_portable.cpp index 8e9abaa..797c84c 100644 --- a/src/instructions_portable.cpp +++ b/src/instructions_portable.cpp @@ -51,10 +51,10 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include - uint64_t rotl(uint64_t x, int c) { + uint64_t rotl(uint64_t x, unsigned int c) { return _rotl64(x, c); } - uint64_t rotr(uint64_t x , int c) { + uint64_t rotr(uint64_t x, unsigned int c) { return _rotr64(x, c); } #define HAVE_ROTL @@ -89,15 +89,15 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #endif #ifndef HAVE_ROTR - uint64_t rotr(uint64_t a, int b) { - return (a >> b) | (a << (64 - b)); + uint64_t rotr(uint64_t a, unsigned int b) { + return (a >> b) | (a << (-b & 63)); } #define HAVE_ROTR #endif #ifndef HAVE_ROTL - uint64_t rotl(uint64_t a, int b) { - return (a << b) | (a >> (64 - b)); + uint64_t rotl(uint64_t a, unsigned int b) { + return (a << b) | (a >> (-b & 63)); } #define HAVE_ROTL #endif diff --git a/src/intrin_portable.h b/src/intrin_portable.h index 2f8afe6..14176d9 100644 --- a/src/intrin_portable.h +++ b/src/intrin_portable.h @@ -594,5 +594,5 @@ void rx_set_rounding_mode(uint32_t mode); double loadDoublePortable(const void* addr); uint64_t mulh(uint64_t, uint64_t); int64_t smulh(int64_t, int64_t); -uint64_t rotl(uint64_t, int); -uint64_t rotr(uint64_t, int); +uint64_t rotl(uint64_t, unsigned int); +uint64_t rotr(uint64_t, unsigned int);