author | darcy |
Tue, 12 Nov 2019 10:45:23 -0800 | |
changeset 59037 | 3d2575331a41 |
parent 53244 | 9807daeb47c4 |
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:
47216
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:
47216
diff
changeset
|
25 |
#ifndef OS_CPU_SOLARIS_SPARC_COUNT_TRAILING_ZEROS_SOLARIS_SPARC_HPP |
9807daeb47c4
8216167: Update include guards to reflect correct directories
coleenp
parents:
47216
diff
changeset
|
26 |
#define OS_CPU_SOLARIS_SPARC_COUNT_TRAILING_ZEROS_SOLARIS_SPARC_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/globalDefinitions.hpp" |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
29 |
|
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
30 |
inline unsigned count_trailing_zeros(uintx x) { |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
31 |
assert(x != 0, "precondition"); |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
32 |
// Reduce to mask with ones in all positions below the least |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
33 |
// significant set bit of x, and zeros otherwise. |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
34 |
uintx rx = (x - 1) & ~x; // sub x, 1, rx; andn rx, x, rx; |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
35 |
// Then count the set bits in the reduction. |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
36 |
uintx result; |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
37 |
__asm__(" popc %1, %0\n\t" : "=r" (result) : "r" (rx)); |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
38 |
return result; |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
39 |
} |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
40 |
|
53244
9807daeb47c4
8216167: Update include guards to reflect correct directories
coleenp
parents:
47216
diff
changeset
|
41 |
#endif // OS_CPU_SOLARIS_SPARC_COUNT_TRAILING_ZEROS_SOLARIS_SPARC_HPP |