Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

acl_hash: fix integer conversion warning #199

Merged
merged 3 commits into from
Nov 16, 2022
Merged

Conversation

pcolberg
Copy link
Contributor

Avoid integer conversion altogether by using a more efficient
implementation which compiles to branchless code with a rotate
instruction, assuming the number of bits to rotate by is known
at compile time so the compiler may eliminate the assert().

This compiles the library code twice, for the library and the unit tests.

Signed-off-by: Peter Colberg <peter.colberg@intel.com>
Signed-off-by: Peter Colberg <peter.colberg@intel.com>
Avoid integer conversion altogether by using a more efficient
implementation which compiles to branchless code with a rotate
instruction, assuming the number of bits to rotate by is known
at compile time so the compiler may eliminate the assert().

Signed-off-by: Peter Colberg <peter.colberg@intel.com>
@pcolberg pcolberg added the bug Something isn't working label Nov 15, 2022
@pcolberg pcolberg added this to the 2023.1 milestone Nov 15, 2022
@pcolberg pcolberg self-assigned this Nov 15, 2022
@pcolberg
Copy link
Contributor Author

Current version of bitwise left-rotate:

// rotate_old.c 
#include <stdint.h>
uint32_t l_leftrotate(uint32_t v, unsigned bits) {
  uint64_t both = ((uint64_t)v) << bits;
  uint32_t hi = both >> 32;
  uint32_t lo = both & 0xffffffff;
  return hi | lo;
}

Improved version of bitwise left-rotate:

// rotate.c
#include <assert.h>
#include <stdint.h>
// See Safe, Efficient, and Portable Rotate in C/C++ by John Regehr.
// https://blog.regehr.org/archives/1063
uint32_t l_leftrotate(uint32_t v, uint32_t bits) {
  assert(bits < 32);
  return (v << bits) | (v >> ((-bits) & 31));
}

Comparison with GCC 10.2.1, improved version emits rotate instruction:

--- rotate_old.s
+++ rotate.s
@@ -1,20 +1,17 @@
-	.file	"rotate_old.c"
+	.file	"rotate.c"
 	.text
 	.p2align 4
 	.globl	l_leftrotate
 	.type	l_leftrotate, @function
 l_leftrotate:
 .LFB0:
 	.cfi_startproc
 	movl	%edi, %eax
 	movl	%esi, %ecx
-	salq	%cl, %rax
-	movq	%rax, %rdi
-	shrq	$32, %rdi
-	orl	%edi, %eax
+	roll	%cl, %eax
 	ret
 	.cfi_endproc
 .LFE0:
 	.size	l_leftrotate, .-l_leftrotate
 	.ident	"GCC: (Debian 10.2.1-6) 10.2.1 20210110"
 	.section	.note.GNU-stack,"",@progbits

Comparison with Clang 11.0.1, improved version emits rotate instruction:

--- rotate_old.s
+++ rotate.s
@@ -1,24 +1,20 @@
 	.text
-	.file	"rotate_old.c"
+	.file	"rotate.c"
 	.globl	l_leftrotate                    # -- Begin function l_leftrotate
 	.p2align	4, 0x90
 	.type	l_leftrotate,@function
 l_leftrotate:                           # @l_leftrotate
 	.cfi_startproc
 # %bb.0:
 	movl	%esi, %ecx
-	movl	%edi, %edx
+	movl	%edi, %eax
                                         # kill: def $cl killed $cl killed $ecx
-	shlq	%cl, %rdx
-	movq	%rdx, %rax
-	shrq	$32, %rax
-	orl	%edx, %eax
-                                        # kill: def $eax killed $eax killed $rax
+	roll	%cl, %eax
 	retq
 .Lfunc_end0:
 	.size	l_leftrotate, .Lfunc_end0-l_leftrotate
 	.cfi_endproc
                                         # -- End function
 	.ident	"Debian clang version 11.0.1-2"
 	.section	".note.GNU-stack","",@progbits
 	.addrsig

@pcolberg pcolberg linked an issue Nov 15, 2022 that may be closed by this pull request
1 task
@pcolberg
Copy link
Contributor Author

pcolberg commented Nov 15, 2022

@pcolberg
Copy link
Contributor Author

pcolberg commented Nov 15, 2022

MSVC on Windows fails with

acl_hash.c(220): error C4146: unary minus operator applied to unsigned type, result still unsigned

// See Safe, Efficient, and Portable Rotate in C/C++ by John Regehr.
// https://blog.regehr.org/archives/1063
static uint32_t l_leftrotate(uint32_t v, uint32_t bits) {
assert(bits < 32);
return (v << bits) | (v >> ((-bits) & 31));
}

@pcolberg pcolberg marked this pull request as ready for review November 16, 2022 02:12
@pcolberg pcolberg requested a review from zibaiwan November 16, 2022 02:12
Copy link
Contributor

@zibaiwan zibaiwan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @pcolberg for fixing this!

@pcolberg pcolberg merged commit 79228be into intel:main Nov 16, 2022
@pcolberg pcolberg deleted the roll branch November 16, 2022 16:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix warnings in acl_hash
2 participants