forked from TunSafe/TunSafe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bit_ops.h
49 lines (40 loc) · 1.24 KB
/
bit_ops.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// SPDX-License-Identifier: AGPL-1.0-only
// Copyright (C) 2018 Ludvig Strigeus <info@tunsafe.com>. All Rights Reserved.
#pragma once
#include "tunsafe_types.h"
#include "tunsafe_endian.h"
#if !defined(ARCH_CPU_X86_64) && defined(COMPILER_MSVC)
static inline int _BitScanReverse64(unsigned long *index, uint64 x) {
if (_BitScanReverse(index, x >> 32)) {
(*index) += 32;
return true;
}
return _BitScanReverse(index, (uint32)x);
}
#endif
#if !defined(COMPILER_MSVC)
static inline int _BitScanReverse64(unsigned long *index, uint64 x) {
*index = 63 - __builtin_clzll(x);
return (x != 0);
}
static inline int _BitScanReverse(unsigned long *index, uint32 x) {
*index = 31 - __builtin_clz(x);
return (x != 0);
}
#endif
static inline int FindHighestSetBit32(uint32 x) {
unsigned long index;
return _BitScanReverse(&index, x) ? (int)(index + 1) : 0;
}
static inline int FindLastSetBit32(uint32 x) {
unsigned long index;
_BitScanReverse(&index, x);
return index;
}
static inline int FindHighestSetBit64(uint64 x) {
unsigned long index;
return _BitScanReverse64(&index, x) ? (int)(index + 1) : 0;
}
static inline int FindHighestSetBit128(uint64 hi, uint64 lo) {
return hi ? 64 + FindHighestSetBit64(hi) : FindHighestSetBit64(lo);
}