53443
|
1 |
/*
|
|
2 |
* Copyright (c) 2019, 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 |
#include "precompiled.hpp"
|
|
26 |
#include "libadt/vectset.hpp"
|
|
27 |
#include "runtime/os.hpp"
|
|
28 |
#include "utilities/population_count.hpp"
|
|
29 |
#include "utilities/globalDefinitions.hpp"
|
|
30 |
#include "unittest.hpp"
|
|
31 |
|
|
32 |
|
|
33 |
TEST(population_count, sparse) {
|
|
34 |
extern uint8_t bitsInByte[BITS_IN_BYTE_ARRAY_SIZE];
|
|
35 |
// Step through the entire input range from a random starting point,
|
|
36 |
// verify population_count return values against the lookup table
|
|
37 |
// approach used historically
|
|
38 |
uint32_t step = 4711;
|
|
39 |
for (uint32_t value = os::random() % step; value < UINT_MAX - step; value += step) {
|
|
40 |
uint32_t lookup = bitsInByte[(value >> 24) & 0xff] +
|
|
41 |
bitsInByte[(value >> 16) & 0xff] +
|
|
42 |
bitsInByte[(value >> 8) & 0xff] +
|
|
43 |
bitsInByte[ value & 0xff];
|
|
44 |
|
|
45 |
EXPECT_EQ(lookup, population_count(value))
|
|
46 |
<< "value = " << value;
|
|
47 |
}
|
|
48 |
|
|
49 |
// Test a few edge cases
|
|
50 |
EXPECT_EQ(0u, population_count(0u))
|
|
51 |
<< "value = " << 0;
|
|
52 |
EXPECT_EQ(1u, population_count(1u))
|
|
53 |
<< "value = " << 1;
|
|
54 |
EXPECT_EQ(1u, population_count(2u))
|
|
55 |
<< "value = " << 2;
|
|
56 |
EXPECT_EQ(32u, population_count(UINT_MAX))
|
|
57 |
<< "value = " << UINT_MAX;
|
|
58 |
EXPECT_EQ(31u, population_count(UINT_MAX - 1))
|
|
59 |
<< "value = " << (UINT_MAX - 1);
|
|
60 |
}
|