author | coleenp |
Thu, 10 Jan 2019 15:13:51 -0500 | |
changeset 53244 | 9807daeb47c4 |
parent 51605 | 2ca553d34949 |
permissions | -rw-r--r-- |
46437
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
1 |
/* |
53244
9807daeb47c4
8216167: Update include guards to reflect correct directories
coleenp
parents:
51605
diff
changeset
|
2 |
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved. |
46437
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
4 |
* |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
8 |
* |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
13 |
* accompanied this code). |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
14 |
* |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
18 |
* |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
21 |
* questions. |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
22 |
* |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
23 |
*/ |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
24 |
|
53244
9807daeb47c4
8216167: Update include guards to reflect correct directories
coleenp
parents:
51605
diff
changeset
|
25 |
#ifndef SHARE_UTILITIES_COUNT_TRAILING_ZEROS_HPP |
9807daeb47c4
8216167: Update include guards to reflect correct directories
coleenp
parents:
51605
diff
changeset
|
26 |
#define SHARE_UTILITIES_COUNT_TRAILING_ZEROS_HPP |
46437
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
27 |
|
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
28 |
#include "utilities/debug.hpp" |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
29 |
#include "utilities/globalDefinitions.hpp" |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
30 |
|
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
31 |
// unsigned count_trailing_zeros(uintx x) |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
32 |
// Return the number of trailing zeros in x, e.g. the zero-based index |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
33 |
// of the least significant set bit in x. |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
34 |
// Precondition: x != 0. |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
35 |
|
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
36 |
// Dispatch on toolchain to select implementation. |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
37 |
|
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
38 |
/***************************************************************************** |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
39 |
* GCC and compatible (including Clang) |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
40 |
*****************************************************************************/ |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
41 |
#if defined(TARGET_COMPILER_gcc) |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
42 |
|
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
43 |
inline unsigned count_trailing_zeros(uintx x) { |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
44 |
STATIC_ASSERT(sizeof(unsigned long) == sizeof(uintx)); |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
45 |
assert(x != 0, "precondition"); |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
46 |
return __builtin_ctzl(x); |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
47 |
} |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
48 |
|
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
49 |
/***************************************************************************** |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
50 |
* Microsoft Visual Studio |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
51 |
*****************************************************************************/ |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
52 |
#elif defined(TARGET_COMPILER_visCPP) |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
53 |
|
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
54 |
#include <intrin.h> |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
55 |
|
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
56 |
#ifdef _LP64 |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
57 |
#pragma intrinsic(_BitScanForward64) |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
58 |
#else |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
59 |
#pragma intrinsic(_BitScanForward) |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
60 |
#endif |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
61 |
|
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
62 |
inline unsigned count_trailing_zeros(uintx x) { |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
63 |
assert(x != 0, "precondition"); |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
64 |
unsigned long index; |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
65 |
#ifdef _LP64 |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
66 |
_BitScanForward64(&index, x); |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
67 |
#else |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
68 |
_BitScanForward(&index, x); |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
69 |
#endif |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
70 |
return index; |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
71 |
} |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
72 |
|
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
73 |
/***************************************************************************** |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
74 |
* IBM XL C/C++ |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
75 |
*****************************************************************************/ |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
76 |
#elif defined(TARGET_COMPILER_xlc) |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
77 |
|
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
78 |
#include <builtins.h> |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
79 |
|
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
80 |
inline unsigned count_trailing_zeros(uintx x) { |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
81 |
assert(x != 0, "precondition"); |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
82 |
#ifdef _LP64 |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
83 |
return __cnttz8(x); |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
84 |
#else |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
85 |
return __cnttz4(x); |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
86 |
#endif |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
87 |
} |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
88 |
|
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
89 |
/***************************************************************************** |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
90 |
* Oracle Studio |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
91 |
*****************************************************************************/ |
51605 | 92 |
#elif defined(TARGET_COMPILER_solstudio) |
46437
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
93 |
|
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
94 |
// No compiler built-in / intrinsic, so use inline assembler. |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
95 |
|
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
96 |
#include "utilities/macros.hpp" |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
97 |
|
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
98 |
#include OS_CPU_HEADER(count_trailing_zeros) |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
99 |
|
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
100 |
/***************************************************************************** |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
101 |
* Unknown toolchain |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
102 |
*****************************************************************************/ |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
103 |
#else |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
104 |
#error Unknown TARGET_COMPILER |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
105 |
|
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
106 |
#endif // Toolchain dispatch |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
107 |
|
53244
9807daeb47c4
8216167: Update include guards to reflect correct directories
coleenp
parents:
51605
diff
changeset
|
108 |
#endif // SHARE_UTILITIES_COUNT_TRAILING_ZEROS_HPP |