Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/gtest/utilities/test_count_leading_zeros.cpp
41144 views
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 "metaprogramming/isSigned.hpp"
27
#include "utilities/count_leading_zeros.hpp"
28
#include "utilities/globalDefinitions.hpp"
29
#include "unittest.hpp"
30
31
#include <limits>
32
33
template <typename T> void one_or_two_set_bits() {
34
uint32_t bit1_pos = 0;
35
uint32_t bits = sizeof(T) * BitsPerByte;
36
uint32_t limit = bits - (IsSigned<T>::value ? 1 : 0);
37
for (uint64_t ix = 1; bit1_pos < limit; ix = ix * 2, ++bit1_pos) {
38
uint32_t bit2_pos = 0;
39
for (uint64_t jx = 1; bit2_pos < limit; jx = jx * 2, ++bit2_pos) {
40
T value = T(ix | jx);
41
EXPECT_EQ((uint32_t)(bits - 1u - MAX2(bit1_pos, bit2_pos)), count_leading_zeros(value))
42
<< "value = " << value;
43
}
44
}
45
}
46
47
TEST(count_leading_zeros, one_or_two_set_bits) {
48
one_or_two_set_bits<int8_t>();
49
one_or_two_set_bits<int16_t>();
50
one_or_two_set_bits<int32_t>();
51
one_or_two_set_bits<int64_t>();
52
one_or_two_set_bits<uint8_t>();
53
one_or_two_set_bits<uint16_t>();
54
one_or_two_set_bits<uint32_t>();
55
one_or_two_set_bits<uint64_t>();
56
}
57
58
template <typename T> void high_zeros_low_ones() {
59
uint32_t number_of_leading_zeros = (IsSigned<T>::value ? 1 : 0);
60
T value = std::numeric_limits<T>::max();
61
for ( ; value != 0; value >>= 1, ++number_of_leading_zeros) {
62
EXPECT_EQ(number_of_leading_zeros, count_leading_zeros(value))
63
<< "value = " << value;
64
}
65
}
66
67
TEST(count_leading_zeros, high_zeros_low_ones) {
68
high_zeros_low_ones<int8_t>();
69
high_zeros_low_ones<int16_t>();
70
high_zeros_low_ones<int32_t>();
71
high_zeros_low_ones<int64_t>();
72
high_zeros_low_ones<uint8_t>();
73
high_zeros_low_ones<uint16_t>();
74
high_zeros_low_ones<uint32_t>();
75
high_zeros_low_ones<uint64_t>();
76
}
77
78
template <typename T> void high_ones_low_zeros() {
79
T value = std::numeric_limits<T>::max();
80
81
uint32_t number_of_leading_zeros = (IsSigned<T>::value ? 1 : 0);
82
for (uint64_t i = 1; value != 0; value -= i, i <<= 1) {
83
EXPECT_EQ(number_of_leading_zeros, count_leading_zeros(value))
84
<< "value = " << value;
85
}
86
value = (T)(~((uint64_t)0)); // all ones
87
EXPECT_EQ(0u, count_leading_zeros(value))
88
<< "value = " << value;
89
}
90
91
TEST(count_leading_zeros, high_ones_low_zeros) {
92
high_ones_low_zeros<int8_t>();
93
high_ones_low_zeros<int16_t>();
94
high_ones_low_zeros<int32_t>();
95
high_ones_low_zeros<int64_t>();
96
high_ones_low_zeros<uint8_t>();
97
high_ones_low_zeros<uint16_t>();
98
high_ones_low_zeros<uint32_t>();
99
high_ones_low_zeros<uint64_t>();
100
}
101