Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/gtest/utilities/test_population_count.cpp
41144 views
1
/*
2
* Copyright (c) 2019, 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
#include "precompiled.hpp"
26
#include "runtime/os.hpp"
27
#include "utilities/population_count.hpp"
28
#include "utilities/powerOfTwo.hpp"
29
#include "utilities/globalDefinitions.hpp"
30
#include <limits>
31
#include "unittest.hpp"
32
33
#define BITS_IN_BYTE_ARRAY_SIZE 256
34
35
const uint8_t test_popcnt_bitsInByte[BITS_IN_BYTE_ARRAY_SIZE] = {
36
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
37
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
38
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
39
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
40
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
41
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
42
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
43
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
44
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
45
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
46
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
47
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
48
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
49
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
50
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
51
4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
52
};
53
54
template <typename T>
55
static void sparse() {
56
const T max_val = std::numeric_limits<T>::max();
57
58
// Step through the entire input range from a random starting point,
59
// verify population_count return values against the lookup table
60
// approach used historically
61
T step = T(1) << ((sizeof(T) * 8) - 7);
62
63
for (T value = os::random() % step; value < max_val - step; value += step) {
64
uint64_t v = (uint64_t)value;
65
unsigned lookup = 0u;
66
for (unsigned i = 0u; i < sizeof(T); i++) {
67
lookup += test_popcnt_bitsInByte[v & 0xff];
68
v >>= 8u;
69
}
70
EXPECT_EQ(lookup, population_count(value))
71
<< "value = " << value;
72
}
73
74
// Test a few edge cases
75
EXPECT_EQ(0u, population_count(T(0u)))
76
<< "value = " << 0;
77
EXPECT_EQ(1u, population_count(T(1u)))
78
<< "value = " << 1;
79
EXPECT_EQ(1u, population_count(T(2u)))
80
<< "value = " << 2;
81
EXPECT_EQ(T(sizeof(T) * BitsPerByte), population_count(max_val))
82
<< "value = " << max_val;
83
EXPECT_EQ(T(sizeof(T) * BitsPerByte - 1u), population_count(T(max_val - 1u)))
84
<< "value = " << (max_val - 1u);
85
}
86
87
88
TEST(population_count, sparse8) {
89
sparse<uint8_t>();
90
}
91
TEST(population_count, sparse16) {
92
sparse<uint16_t>();
93
}
94
TEST(population_count, sparse32) {
95
sparse<uint32_t>();
96
}
97
TEST(population_count, sparse64) {
98
sparse<uint64_t>();
99
}
100
101