Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/gtest/utilities/test_count_trailing_zeros.cpp
41144 views
1
/*
2
* Copyright (c) 2017, 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 "utilities/count_trailing_zeros.hpp"
27
#include "utilities/globalDefinitions.hpp"
28
#include "unittest.hpp"
29
30
31
template <typename T> static void test_one_or_two_set_bits() {
32
unsigned i = 0; // Position of a set bit.
33
unsigned max = sizeof(T) * BitsPerByte;
34
for (T ix = T(1); i < max; ix <<= 1, ++i) {
35
unsigned j = 0; // Position of a set bit.
36
for (T jx = T(1); j < max; jx <<= 1, ++j) {
37
T value = ix | jx;
38
EXPECT_EQ(MIN2(i, j), count_trailing_zeros(value))
39
<< "value = " << value;
40
}
41
}
42
}
43
44
TEST(count_trailing_zeros, one_or_two_set_bits) {
45
test_one_or_two_set_bits<int8_t>();
46
test_one_or_two_set_bits<int16_t>();
47
test_one_or_two_set_bits<int32_t>();
48
test_one_or_two_set_bits<int64_t>();
49
test_one_or_two_set_bits<uint8_t>();
50
test_one_or_two_set_bits<uint16_t>();
51
test_one_or_two_set_bits<uint32_t>();
52
test_one_or_two_set_bits<uint64_t>();
53
}
54
55
template <typename T> static void test_high_zeros_low_ones() {
56
T value = std::numeric_limits<T>::max();
57
for ( ; value != 0; value >>= 1) {
58
EXPECT_EQ(0u, count_trailing_zeros(value))
59
<< "value = " << value;
60
}
61
}
62
63
TEST(count_trailing_zeros, high_zeros_low_ones) {
64
test_high_zeros_low_ones<int8_t>();
65
test_high_zeros_low_ones<int16_t>();
66
test_high_zeros_low_ones<int32_t>();
67
test_high_zeros_low_ones<int64_t>();
68
test_high_zeros_low_ones<uint8_t>();
69
test_high_zeros_low_ones<uint16_t>();
70
test_high_zeros_low_ones<uint32_t>();
71
test_high_zeros_low_ones<uint64_t>();
72
}
73
74
template <typename T> static void test_high_ones_low_zeros() {
75
unsigned i = 0; // Index of least significant set bit.
76
T value = ~T(0);
77
unsigned max = sizeof(T) * BitsPerByte;
78
for ( ; i < max; value <<= 1, ++i) {
79
EXPECT_EQ(i, count_trailing_zeros(value))
80
<< "value = " << value;
81
}
82
}
83
84
TEST(count_trailing_zeros, high_ones_low_zeros) {
85
test_high_ones_low_zeros<int8_t>();
86
test_high_ones_low_zeros<int16_t>();
87
test_high_ones_low_zeros<int32_t>();
88
test_high_ones_low_zeros<int64_t>();
89
test_high_ones_low_zeros<uint8_t>();
90
test_high_ones_low_zeros<uint16_t>();
91
test_high_ones_low_zeros<uint32_t>();
92
test_high_ones_low_zeros<uint64_t>();
93
}
94
95