summaryrefslogtreecommitdiff
path: root/src/common/bit_util.h
diff options
context:
space:
mode:
authorGravatar Lioncash2018-12-19 15:25:12 -0500
committerGravatar Lioncash2018-12-21 07:04:18 -0500
commitfc8da2d5e36b665dae784ee8e9915dfffb379830 (patch)
tree9364f2a3d986820e7f01096c27710b23f4622b98 /src/common/bit_util.h
parentMerge pull request #1907 from lioncash/attribute (diff)
downloadyuzu-fc8da2d5e36b665dae784ee8e9915dfffb379830.tar.gz
yuzu-fc8da2d5e36b665dae784ee8e9915dfffb379830.tar.xz
yuzu-fc8da2d5e36b665dae784ee8e9915dfffb379830.zip
common: Add basic bit manipulation utility function to Common
Diffstat (limited to '')
-rw-r--r--src/common/bit_util.h61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/common/bit_util.h b/src/common/bit_util.h
new file mode 100644
index 000000000..1eea17ba1
--- /dev/null
+++ b/src/common/bit_util.h
@@ -0,0 +1,61 @@
1// Copyright 2018 yuzu emulator team
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <climits>
8#include <cstddef>
9
10#ifdef _MSC_VER
11#include <intrin.h>
12#endif
13
14#include "common/common_types.h"
15
16namespace Common {
17
18/// Gets the size of a specified type T in bits.
19template <typename T>
20constexpr std::size_t BitSize() {
21 return sizeof(T) * CHAR_BIT;
22}
23
24#ifdef _MSC_VER
25inline u32 CountLeadingZeroes32(u32 value) {
26 unsigned long leading_zero = 0;
27
28 if (_BitScanReverse(&leading_zero, value) != 0) {
29 return 31 - leading_zero;
30 }
31
32 return 32;
33}
34
35inline u64 CountLeadingZeroes64(u64 value) {
36 unsigned long leading_zero = 0;
37
38 if (_BitScanReverse64(&leading_zero, value) != 0) {
39 return 63 - leading_zero;
40 }
41
42 return 64;
43}
44#else
45inline u32 CountLeadingZeroes32(u32 value) {
46 if (value == 0) {
47 return 32;
48 }
49
50 return __builtin_clz(value);
51}
52
53inline u64 CountLeadingZeroes64(u64 value) {
54 if (value == 0) {
55 return 64;
56 }
57
58 return __builtin_clzll(value);
59}
60#endif
61} // namespace Common