Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/utilities/count_trailing_zeros.hpp
40950 views
1
/*
2
* Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*
23
*/
24
25
#ifndef SHARE_UTILITIES_COUNT_TRAILING_ZEROS_HPP
26
#define SHARE_UTILITIES_COUNT_TRAILING_ZEROS_HPP
27
28
#include "metaprogramming/enableIf.hpp"
29
#include "utilities/debug.hpp"
30
#include "utilities/globalDefinitions.hpp"
31
32
// unsigned count_trailing_zeros(T x)
33
34
// Return the number of trailing zeros in x, e.g. the zero-based index
35
// of the least significant set bit in x.
36
// Precondition: x != 0.
37
38
// We implement and support variants for 8, 16, 32 and 64 bit integral types.
39
40
// Dispatch on toolchain to select implementation.
41
42
/*****************************************************************************
43
* GCC and compatible (including Clang)
44
*****************************************************************************/
45
#if defined(TARGET_COMPILER_gcc)
46
47
inline unsigned count_trailing_zeros_32(uint32_t x) {
48
return __builtin_ctz(x);
49
}
50
51
inline unsigned count_trailing_zeros_64(uint64_t x) {
52
return __builtin_ctzll(x);
53
}
54
55
/*****************************************************************************
56
* Microsoft Visual Studio
57
*****************************************************************************/
58
#elif defined(TARGET_COMPILER_visCPP)
59
60
#include <intrin.h>
61
62
#pragma intrinsic(_BitScanForward)
63
#ifdef _LP64
64
#pragma intrinsic(_BitScanForward64)
65
#endif
66
67
inline unsigned count_trailing_zeros_32(uint32_t x) {
68
unsigned long index;
69
_BitScanForward(&index, x);
70
return index;
71
}
72
73
inline unsigned count_trailing_zeros_64(uint64_t x) {
74
unsigned long index;
75
#ifdef _LP64
76
_BitScanForward64(&index, x);
77
#else
78
if (_BitScanForward(&index, static_cast<uint32_t>(x)) == 0) {
79
// no bit found? If so, try the upper dword. Otherwise index already contains the result
80
_BitScanForward(&index, static_cast<uint32_t>(x >> 32));
81
index += 32;
82
}
83
#endif
84
return index;
85
}
86
87
/*****************************************************************************
88
* IBM XL C/C++
89
*****************************************************************************/
90
#elif defined(TARGET_COMPILER_xlc)
91
92
#include <builtins.h>
93
94
inline unsigned count_trailing_zeros_32(uint32_t x) {
95
return __cnttz4(x);
96
}
97
98
inline unsigned count_trailing_zeros_64(uint64_t x) {
99
return __cnttz8(x);
100
}
101
102
/*****************************************************************************
103
* Unknown toolchain
104
*****************************************************************************/
105
#else
106
#error Unknown TARGET_COMPILER
107
108
#endif // Toolchain dispatch
109
110
template<typename T,
111
ENABLE_IF(std::is_integral<T>::value),
112
ENABLE_IF(sizeof(T) <= sizeof(uint64_t))>
113
inline unsigned count_trailing_zeros(T x) {
114
assert(x != 0, "precondition");
115
return (sizeof(x) <= sizeof(uint32_t)) ?
116
count_trailing_zeros_32(static_cast<uint32_t>(x)) :
117
count_trailing_zeros_64(x);
118
}
119
120
121
#endif // SHARE_UTILITIES_COUNT_TRAILING_ZEROS_HPP
122
123