Skip to content

Commit d77879b

Browse files
committed
cleanup bit scan utils
1 parent feeb424 commit d77879b

File tree

8 files changed

+55
-72
lines changed

8 files changed

+55
-72
lines changed

src/base_alloc/base_alloc_global.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ 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 = utils_msb64(smallestSize);
7575

7676
LOG_DEBUG("UMF base allocator created");
7777
}
@@ -83,8 +83,8 @@ static int size_to_idx(size_t size) {
8383
}
8484

8585
int isPowerOf2 = (0 == (size & (size - 1)));
86-
int index =
87-
(int)(log2Utils(size) + !isPowerOf2 - BASE_ALLOC.smallest_ac_size_log2);
86+
int index = (int)(utils_msb64(size) + !isPowerOf2 -
87+
BASE_ALLOC.smallest_ac_size_log2);
8888

8989
assert(index >= 0);
9090
return index;

src/critnib/critnib.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
@@ -367,7 +368,7 @@ int critnib_insert(struct critnib *c, word key, void *value, int update) {
367368
}
368369

369370
/* and convert that to an index. */
370-
sh_t sh = utils_mssb_index(at) & (sh_t) ~(SLICE - 1);
371+
sh_t sh = utils_msb64(at) & (sh_t) ~(SLICE - 1);
371372

372373
struct critnib_node *m = alloc_node(c);
373374
if (!m) {

src/pool/pool_disjoint.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ static size_t size_to_idx(disjoint_pool_t *pool, size_t size) {
466466
}
467467

468468
// get the position of the leftmost set bit
469-
size_t position = getLeftmostSetBitPos(size);
469+
size_t position = utils_msb64(size);
470470

471471
bool is_power_of_2 = 0 == (size & (size - 1));
472472
bool larger_than_halfway_between_powers_of_2 =
@@ -622,7 +622,7 @@ umf_result_t disjoint_pool_initialize(umf_memory_provider_handle_t provider,
622622
Size1 = utils_max(Size1, UMF_DISJOINT_POOL_MIN_BUCKET_DEFAULT_SIZE);
623623

624624
// Calculate the exponent for min_bucket_size used for finding buckets.
625-
disjoint_pool->min_bucket_size_exp = (size_t)log2Utils(Size1);
625+
disjoint_pool->min_bucket_size_exp = (size_t)utils_msb64(Size1);
626626
disjoint_pool->default_shared_limits =
627627
umfDisjointPoolSharedLimitsCreate(SIZE_MAX);
628628

src/utils/CMakeLists.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ include(FindThreads)
77

88
set(UMF_UTILS_SOURCES_COMMON utils_common.c utils_log.c utils_load_library.c)
99

10-
set(UMF_UTILS_SOURCES_POSIX utils_posix_common.c utils_posix_concurrency.c
11-
utils_posix_math.c)
10+
set(UMF_UTILS_SOURCES_POSIX utils_posix_common.c utils_posix_concurrency.c)
1211

1312
set(UMF_UTILS_SOURCES_LINUX utils_linux_common.c)
1413

1514
set(UMF_UTILS_SOURCES_MACOSX utils_macosx_common.c)
1615

1716
set(UMF_UTILS_SOURCES_WINDOWS utils_windows_common.c
18-
utils_windows_concurrency.c utils_windows_math.c)
17+
utils_windows_concurrency.c)
1918

2019
if(UMF_USE_VALGRIND)
2120
if(UMF_USE_ASAN

src/utils/utils_concurrency.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,6 @@ void utils_init_once(UTIL_ONCE_FLAG *flag, void (*onceCb)(void));
8989

9090
#if defined(_WIN32)
9191

92-
static inline unsigned char utils_lssb_index(long long value) {
93-
unsigned long ret;
94-
_BitScanForward64(&ret, value);
95-
return (unsigned char)ret;
96-
}
97-
98-
static inline unsigned char utils_mssb_index(long long value) {
99-
unsigned long ret;
100-
_BitScanReverse64(&ret, value);
101-
return (unsigned char)ret;
102-
}
103-
10492
// There is no good way to do atomic_load on windows...
10593
static inline void utils_atomic_load_acquire_u64(uint64_t *ptr, uint64_t *out) {
10694
// NOTE: Windows cl complains about direct accessing 'ptr' which is next
@@ -166,9 +154,6 @@ static inline bool utils_compare_exchange_u64(uint64_t *ptr, uint64_t *expected,
166154

167155
#else // !defined(_WIN32)
168156

169-
#define utils_lssb_index(x) ((unsigned char)__builtin_ctzll(x))
170-
#define utils_mssb_index(x) ((unsigned char)(63 - __builtin_clzll(x)))
171-
172157
static inline void utils_atomic_load_acquire_u64(uint64_t *ptr, uint64_t *out) {
173158
ASSERT_IS_ALIGNED((uintptr_t)ptr, 8);
174159
ASSERT_IS_ALIGNED((uintptr_t)out, 8);

src/utils/utils_math.h

Lines changed: 46 additions & 4 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
@@ -11,16 +11,58 @@
1111
#define UMF_MATH_H 1
1212

1313
#include <assert.h>
14+
#include <limits.h>
1415
#include <stddef.h>
16+
#include <stdint.h>
1517

1618
#ifdef __cplusplus
1719
extern "C" {
1820
#endif
1921

20-
size_t getLeftmostSetBitPos(size_t num);
22+
#if defined(_WIN32)
2123

22-
// Logarithm is an index of the most significant non-zero bit.
23-
static inline size_t log2Utils(size_t num) { return getLeftmostSetBitPos(num); }
24+
#include "utils_windows_intrin.h"
25+
26+
#pragma intrinsic(_BitScanReverse64)
27+
#pragma intrinsic(_BitScanForward64)
28+
29+
// Retrieves the position of the leftmost set bit.
30+
// The position of the bit is counted from 0
31+
// e.g. for 01000011110 the position equals 9.
32+
static inline size_t utils_msb64(uint64_t num) {
33+
assert(num != 0 &&
34+
"Finding leftmost set bit when number equals zero is undefined");
35+
unsigned long index = 0;
36+
_BitScanReverse64(&index, num);
37+
return (size_t)index;
38+
}
39+
40+
static inline size_t utils_lsb64(uint64_t num) {
41+
assert(num != 0 &&
42+
"Finding rightmost set bit when number equals zero is undefined");
43+
unsigned long index = 0;
44+
_BitScanForward64(&index, num);
45+
return (size_t)index;
46+
}
47+
48+
#else // !defined(_WIN32)
49+
50+
// Retrieves the position of the leftmost set bit.
51+
// The position of the bit is counted from 0
52+
// e.g. for 01000011110 the position equals 9.
53+
static inline size_t utils_msb64(uint64_t num) {
54+
assert(num != 0 &&
55+
"Finding leftmost set bit when number equals zero is undefined");
56+
return 63 - __builtin_clzll(num);
57+
}
58+
59+
static inline size_t utils_lsb64(uint64_t num) {
60+
assert(num != 0 &&
61+
"Finding rightmost set bit when number equals zero is undefined");
62+
return __builtin_ctzll(num);
63+
}
64+
65+
#endif // !defined(_WIN32)
2466

2567
#ifdef __cplusplus
2668
}

src/utils/utils_posix_math.c

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/utils/utils_windows_math.c

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)