-
Notifications
You must be signed in to change notification settings - Fork 70
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
Conversation
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>
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 |
fpga-runtime-for-opencl/lib/acl_hash/src/acl_hash.c Lines 216 to 221 in 7e23a82
|
There was a problem hiding this 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!
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().