author | manc |
Mon, 14 Oct 2019 18:48:10 -0700 | |
changeset 58652 | 9b67dd88a931 |
parent 53532 | bc20d0376402 |
permissions | -rw-r--r-- |
46437
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
1 |
/* |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
2 |
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
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 |
|
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
25 |
#include "precompiled.hpp" |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
26 |
#include "utilities/count_trailing_zeros.hpp" |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
27 |
#include "utilities/globalDefinitions.hpp" |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
28 |
#include "unittest.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 |
TEST(count_trailing_zeros, one_or_two_set_bits) { |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
31 |
unsigned i = 0; // Position of a set bit. |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
32 |
for (uintx ix = 1; ix != 0; ix <<= 1, ++i) { |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
33 |
unsigned j = 0; // Position of a set bit. |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
34 |
for (uintx jx = 1; jx != 0; jx <<= 1, ++j) { |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
35 |
uintx value = ix | jx; |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
36 |
EXPECT_EQ(MIN2(i, j), count_trailing_zeros(value)) |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
37 |
<< "value = " << value; |
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 |
} |
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 |
|
53532 | 42 |
TEST(count_trailing_zeros, high_zeros_low_ones) { |
46437
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
43 |
uintx value = ~(uintx)0; |
53532 | 44 |
for ( ; value != 0; value >>= 1) { |
46437
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
45 |
EXPECT_EQ(0u, count_trailing_zeros(value)) |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
46 |
<< "value = " << value; |
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 |
|
53532 | 50 |
TEST(count_trailing_zeros, high_ones_low_zeros) { |
46437
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
51 |
unsigned i = 0; // Index of least significant set bit. |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
52 |
uintx value = ~(uintx)0; |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
53 |
for ( ; value != 0; value <<= 1, ++i) { |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
54 |
EXPECT_EQ(i, count_trailing_zeros(value)) |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
55 |
<< "value = " << value; |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
56 |
} |
cda23e690843
8179004: Add an efficient implementation of the "count trailing zeros" operation
kbarrett
parents:
diff
changeset
|
57 |
} |