Skip to content

Commit cf66845

Browse files
committed
cleanup bit scan utils
1 parent eae8d63 commit cf66845

File tree

6 files changed

+33
-35
lines changed

6 files changed

+33
-35
lines changed

src/base_alloc/base_alloc_global.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ static void umf_ba_create_global(void) {
7171
}
7272

7373
size_t smallestSize = BASE_ALLOC.ac_sizes[0];
74-
BASE_ALLOC.smallest_ac_size_log2 = log2Utils(smallestSize);
74+
BASE_ALLOC.smallest_ac_size_log2 =
75+
utils_get_leftmost_set_bit_pos(smallestSize);
7576

7677
LOG_DEBUG("UMF base allocator created");
7778
}
@@ -83,8 +84,8 @@ static int size_to_idx(size_t size) {
8384
}
8485

8586
int isPowerOf2 = (0 == (size & (size - 1)));
86-
int index =
87-
(int)(log2Utils(size) + !isPowerOf2 - BASE_ALLOC.smallest_ac_size_log2);
87+
int index = (int)(utils_get_leftmost_set_bit_pos(size) + !isPowerOf2 -
88+
BASE_ALLOC.smallest_ac_size_log2);
8889

8990
assert(index >= 0);
9091
return index;

src/critnib/critnib.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
*
3-
* Copyright (C) 2023-2024 Intel Corporation
3+
* Copyright (C) 2023-2025 Intel Corporation
44
*
55
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
66
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
@@ -64,6 +64,7 @@
6464
#include "utils_assert.h"
6565
#include "utils_common.h"
6666
#include "utils_concurrency.h"
67+
#include "utils_math.h"
6768

6869
/*
6970
* A node that has been deleted is left untouched for this many delete
@@ -386,7 +387,7 @@ int critnib_insert(struct critnib *c, word key, void *value, int update) {
386387
}
387388

388389
/* and convert that to an index. */
389-
sh_t sh = utils_mssb_index(at) & (sh_t) ~(SLICE - 1);
390+
sh_t sh = utils_get_leftmost_set_bit_pos(at) & (sh_t) ~(SLICE - 1);
390391

391392
struct critnib_node *m = alloc_node(c);
392393
if (!m) {

src/utils/utils_concurrency.h

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515

1616
#ifdef _WIN32
1717
#include <windows.h>
18-
19-
#include "utils_windows_intrin.h"
20-
21-
#pragma intrinsic(_BitScanForward64)
2218
#else
2319
#include <pthread.h>
2420

@@ -79,18 +75,6 @@ void utils_init_once(UTIL_ONCE_FLAG *flag, void (*onceCb)(void));
7975

8076
#if defined(_WIN32)
8177

82-
static __inline unsigned char utils_lssb_index(long long value) {
83-
unsigned long ret;
84-
_BitScanForward64(&ret, value);
85-
return (unsigned char)ret;
86-
}
87-
88-
static __inline unsigned char utils_mssb_index(long long value) {
89-
unsigned long ret;
90-
_BitScanReverse64(&ret, value);
91-
return (unsigned char)ret;
92-
}
93-
9478
// There is no good way to do atomic_load on windows...
9579
#define utils_atomic_load_acquire(object, dest) \
9680
do { \
@@ -116,9 +100,6 @@ static __inline unsigned char utils_mssb_index(long long value) {
116100

117101
#else // !defined(_WIN32)
118102

119-
#define utils_lssb_index(x) ((unsigned char)__builtin_ctzll(x))
120-
#define utils_mssb_index(x) ((unsigned char)(63 - __builtin_clzll(x)))
121-
122103
#define utils_atomic_load_acquire(object, dest) \
123104
do { \
124105
utils_annotate_acquire((void *)object); \

src/utils/utils_math.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
*
3-
* Copyright (C) 2023-2024 Intel Corporation
3+
* Copyright (C) 2023-2025 Intel Corporation
44
*
55
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
66
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
@@ -12,15 +12,14 @@
1212

1313
#include <assert.h>
1414
#include <stddef.h>
15+
#include <stdint.h>
1516

1617
#ifdef __cplusplus
1718
extern "C" {
1819
#endif
1920

20-
size_t getLeftmostSetBitPos(size_t num);
21-
22-
// Logarithm is an index of the most significant non-zero bit.
23-
static inline size_t log2Utils(size_t num) { return getLeftmostSetBitPos(num); }
21+
size_t utils_get_leftmost_set_bit_pos(uint64_t num);
22+
size_t utils_get_rightmost_set_bit_pos(uint64_t num);
2423

2524
#ifdef __cplusplus
2625
}

src/utils/utils_posix_math.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
/*
22
*
3-
* Copyright (C) 2023 Intel Corporation
3+
* Copyright (C) 2023-2025 Intel Corporation
44
*
55
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
66
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
77
*
88
*/
99

10-
#include "utils_math.h"
1110
#include <limits.h>
1211

12+
#include "utils_math.h"
13+
1314
// Retrieves the position of the leftmost set bit.
1415
// The position of the bit is counted from 0
1516
// e.g. for 01000011110 the position equals 9.
16-
size_t getLeftmostSetBitPos(size_t num) {
17+
size_t utils_get_leftmost_set_bit_pos(uint64_t num) {
1718
assert(num != 0 &&
1819
"Finding leftmost set bit when number equals zero is undefined");
1920
return (sizeof(num) * CHAR_BIT - 1) - __builtin_clzll(num);
2021
}
22+
23+
size_t utils_get_rightmost_set_bit_pos(uint64_t num) {
24+
assert(num != 0 &&
25+
"Finding rightmost set bit when number equals zero is undefined");
26+
return __builtin_ctzll(num);
27+
}

src/utils/utils_windows_math.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,24 @@
1010
#include "utils_math.h"
1111
#include "utils_windows_intrin.h"
1212

13-
#pragma intrinsic(_BitScanReverse)
13+
#pragma intrinsic(_BitScanReverse64)
14+
#pragma intrinsic(_BitScanForward64)
1415

1516
// Retrieves the position of the leftmost set bit.
1617
// The position of the bit is counted from 0
1718
// e.g. for 01000011110 the position equals 9.
18-
size_t getLeftmostSetBitPos(size_t num) {
19+
size_t utils_get_leftmost_set_bit_pos(uint64_t num) {
1920
assert(num != 0 &&
2021
"Finding leftmost set bit when number equals zero is undefined");
2122
unsigned long index = 0;
22-
_BitScanReverse(&index, (unsigned long)num);
23+
_BitScanReverse64(&index, num);
24+
return (size_t)index;
25+
}
26+
27+
size_t utils_get_rightmost_set_bit_pos(uint64_t num) {
28+
assert(num != 0 &&
29+
"Finding rightmost set bit when number equals zero is undefined");
30+
unsigned long index = 0;
31+
_BitScanForward64(&index, num);
2332
return (size_t)index;
2433
}

0 commit comments

Comments
 (0)