Skip to content

Commit 7e23a82

Browse files
committed
acl_hash: fix integer conversion warning
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>
1 parent bf0ad80 commit 7e23a82

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

lib/acl_hash/src/acl_hash.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,11 @@ static void l_close(acl_hash_sha1_context_t *c, char *digest_buf) {
213213
digest_buf[40] = 0;
214214
}
215215

216-
static uint32_t l_leftrotate(uint32_t v, unsigned bits) {
217-
uint64_t both = ((uint64_t)v) << bits;
218-
uint32_t hi = both >> 32;
219-
uint32_t lo = both & 0xffffffff;
220-
return hi | lo;
216+
// See Safe, Efficient, and Portable Rotate in C/C++ by John Regehr.
217+
// https://blog.regehr.org/archives/1063
218+
static uint32_t l_leftrotate(uint32_t v, uint32_t bits) {
219+
assert(bits < 32);
220+
return (v << bits) | (v >> ((-bits) & 31));
221221
}
222222

223223
static uint32_t l_read_bigendian_i32(const unsigned char *buf) {

0 commit comments

Comments
 (0)