1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ecmascript/base/bit_helper.h"
17 #include "ecmascript/tests/test_helper.h"
18
19 using namespace panda::ecmascript;
20 using namespace panda::ecmascript::base;
21
22 namespace panda::test {
23 class BitHelperTest : public testing::Test {
24 public:
SetUpTestCase()25 static void SetUpTestCase()
26 {
27 GTEST_LOG_(INFO) << "SetUpTestCase";
28 }
29
TearDownTestCase()30 static void TearDownTestCase()
31 {
32 GTEST_LOG_(INFO) << "TearDownCase";
33 }
34
SetUp()35 void SetUp() override
36 {
37 TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
38 }
39
TearDown()40 void TearDown() override
41 {
42 TestHelper::DestroyEcmaVMWithScope(instance, scope);
43 }
44
45 EcmaVM *instance {nullptr};
46 EcmaHandleScope *scope {nullptr};
47 JSThread *thread {nullptr};
48 protected:
49 template <class To, class From>
MemoryCast(const From & src)50 inline To MemoryCast(const From &src) noexcept
51 {
52 static_assert(sizeof(To) == sizeof(From), "size of the types must be equal");
53 To dst;
54 if (memcpy_s(&dst, sizeof(To), &src, sizeof(From)) != EOK) {
55 LOG_FULL(FATAL) << "memcpy_s failed";
56 UNREACHABLE();
57 }
58 return dst;
59 }
60 };
61
HWTEST_F_L0(BitHelperTest,CountLeadingZeros_CountTrailingZeros)62 HWTEST_F_L0(BitHelperTest, CountLeadingZeros_CountTrailingZeros)
63 {
64 uint8_t uint8MaxValue = std::numeric_limits<uint8_t>::max();
65 uint8_t uint8MinValue = std::numeric_limits<uint8_t>::min();
66 EXPECT_EQ(CountLeadingZeros<uint8_t>(uint8MaxValue), 0U);
67 EXPECT_EQ(CountLeadingZeros<uint8_t>(uint8MinValue), 8U);
68 EXPECT_EQ(CountTrailingZeros<uint8_t>(uint8MaxValue), 0U);
69 EXPECT_EQ(CountTrailingZeros<uint8_t>(uint8MinValue), 8U);
70
71 uint16_t uint16MaxValue = std::numeric_limits<uint16_t>::max();
72 uint16_t uint16MinValue = std::numeric_limits<uint16_t>::min();
73 EXPECT_EQ(CountLeadingZeros<uint16_t>(uint16MaxValue), 0U);
74 EXPECT_EQ(CountLeadingZeros<uint16_t>(uint16MinValue), 16U);
75 EXPECT_EQ(CountTrailingZeros<uint16_t>(uint16MaxValue), 0U);
76 EXPECT_EQ(CountTrailingZeros<uint16_t>(uint16MinValue), 16U);
77
78 uint32_t uint32MaxValue = std::numeric_limits<uint32_t>::max();
79 uint32_t uint32MinValue = std::numeric_limits<uint32_t>::min();
80 EXPECT_EQ(CountLeadingZeros<uint32_t>(uint32MaxValue), 0U);
81 EXPECT_EQ(CountLeadingZeros<uint32_t>(uint32MinValue), 32U);
82 EXPECT_EQ(CountTrailingZeros<uint32_t>(uint32MaxValue), 0U);
83 EXPECT_EQ(CountTrailingZeros<uint32_t>(uint32MinValue), 32U);
84
85 uint64_t uint64MaxValue = std::numeric_limits<uint64_t>::max();
86 uint64_t uint64MinValue = std::numeric_limits<uint64_t>::min();
87 EXPECT_EQ(CountLeadingZeros<uint64_t>(uint64MaxValue), 0U);
88 EXPECT_EQ(CountLeadingZeros<uint64_t>(uint64MinValue), 64U);
89 EXPECT_EQ(CountTrailingZeros<uint64_t>(uint64MaxValue), 0U);
90 EXPECT_EQ(CountTrailingZeros<uint64_t>(uint64MinValue), 64U);
91 }
92
HWTEST_F_L0(BitHelperTest,CountLeadingZeros32_CountLeadingOnes32)93 HWTEST_F_L0(BitHelperTest, CountLeadingZeros32_CountLeadingOnes32)
94 {
95 uint32_t uint32MaxValue = std::numeric_limits<uint32_t>::max();
96 uint32_t uint32CommonValue1 = std::numeric_limits<uint32_t>::max() >> 1;
97 uint32_t uint32CommonValue2 = std::numeric_limits<uint32_t>::max() >> 31; // 31 : right shift digit
98 uint32_t uint32MinValue = std::numeric_limits<uint32_t>::min();
99 EXPECT_EQ(CountLeadingZeros32(uint32MaxValue), 0U);
100 EXPECT_EQ(CountLeadingZeros32(uint32CommonValue1), 1U);
101 EXPECT_EQ(CountLeadingZeros32(uint32CommonValue2), 31U);
102 EXPECT_EQ(CountLeadingZeros32(uint32MinValue), 32U);
103 EXPECT_EQ(CountLeadingOnes32(uint32MaxValue), 32U);
104 EXPECT_EQ(CountLeadingOnes32(uint32CommonValue1), 0U);
105 EXPECT_EQ(CountLeadingOnes32(uint32CommonValue2), 0U);
106 EXPECT_EQ(CountLeadingOnes32(uint32MinValue), 0U);
107 }
108
HWTEST_F_L0(BitHelperTest,CountLeadingZeros64_CountLeadingOnes64)109 HWTEST_F_L0(BitHelperTest, CountLeadingZeros64_CountLeadingOnes64)
110 {
111 uint64_t uint64MaxValue = std::numeric_limits<uint64_t>::max();
112 uint64_t uint64CommonValue1 = std::numeric_limits<uint64_t>::max() >> 1;
113 uint64_t uint64CommonValue2 = std::numeric_limits<uint64_t>::max() >> 63; // 63 : right shift digit
114 uint64_t uint64MinValue = std::numeric_limits<uint64_t>::min();
115 EXPECT_EQ(CountLeadingZeros64(uint64MaxValue), 0U);
116 EXPECT_EQ(CountLeadingZeros64(uint64CommonValue1), 1U);
117 EXPECT_EQ(CountLeadingZeros64(uint64CommonValue2), 63U);
118 EXPECT_EQ(CountLeadingZeros64(uint64MinValue), 64U);
119 EXPECT_EQ(CountLeadingOnes64(uint64MaxValue), 64U);
120 EXPECT_EQ(CountLeadingOnes64(uint64CommonValue1), 0U);
121 EXPECT_EQ(CountLeadingOnes64(uint64CommonValue2), 0U);
122 EXPECT_EQ(CountLeadingOnes64(uint64MinValue), 0U);
123 }
124
HWTEST_F_L0(BitHelperTest,bit_cast)125 HWTEST_F_L0(BitHelperTest, bit_cast)
126 {
127 int8_t int8Value = -128;
128 uint8_t uint8Value = 255;
129 char char8Value = 127;
130 EXPECT_EQ(bit_cast<uint8_t>(int8Value), MemoryCast<uint8_t>(int8Value));
131 EXPECT_EQ(bit_cast<char>(int8Value), MemoryCast<char>(int8Value));
132 EXPECT_EQ(bit_cast<int8_t>(uint8Value), MemoryCast<int8_t>(uint8Value));
133 EXPECT_EQ(bit_cast<char>(uint8Value), MemoryCast<char>(uint8Value));
134 EXPECT_EQ(bit_cast<int8_t>(char8Value), MemoryCast<int8_t>(char8Value));
135 EXPECT_EQ(bit_cast<uint8_t>(char8Value), MemoryCast<uint8_t>(char8Value));
136
137 int16_t int16Value = -32768;
138 uint16_t uint16Value = 65535;
139 char16_t char16Value = 32767;
140 EXPECT_EQ(bit_cast<uint16_t>(int16Value), MemoryCast<uint16_t>(int16Value));
141 EXPECT_EQ(bit_cast<char16_t>(int16Value), MemoryCast<char16_t>(int16Value));
142 EXPECT_EQ(bit_cast<int16_t>(uint16Value), MemoryCast<int16_t>(uint16Value));
143 EXPECT_EQ(bit_cast<char16_t>(uint16Value), MemoryCast<char16_t>(uint16Value));
144 EXPECT_EQ(bit_cast<int16_t>(char16Value), MemoryCast<int16_t>(char16Value));
145 EXPECT_EQ(bit_cast<uint16_t>(char16Value), MemoryCast<uint16_t>(char16Value));
146
147 int32_t int32Value = -2147483648;
148 uint32_t uint32Value = 2147483648;
149 float floatValue = -13.569243f;
150 char32_t char32Value = 2147483648;
151 EXPECT_EQ(bit_cast<uint32_t>(int32Value), MemoryCast<uint32_t>(int32Value));
152 EXPECT_EQ(bit_cast<float>(int32Value), MemoryCast<float>(int32Value));
153 EXPECT_EQ(bit_cast<char32_t>(int32Value), MemoryCast<char32_t>(int32Value));
154 EXPECT_EQ(bit_cast<int32_t>(uint32Value), MemoryCast<int32_t>(uint32Value));
155 EXPECT_EQ(bit_cast<float>(uint32Value), MemoryCast<float>(uint32Value));
156 EXPECT_EQ(bit_cast<char32_t>(uint32Value), MemoryCast<char32_t>(uint32Value));
157 EXPECT_EQ(bit_cast<int32_t>(floatValue), MemoryCast<int32_t>(floatValue));
158 EXPECT_EQ(bit_cast<uint32_t>(floatValue), MemoryCast<uint32_t>(floatValue));
159 EXPECT_EQ(bit_cast<char32_t>(floatValue), MemoryCast<char32_t>(floatValue));
160 EXPECT_EQ(bit_cast<int32_t>(char32Value), MemoryCast<int32_t>(char32Value));
161 EXPECT_EQ(bit_cast<uint32_t>(char32Value), MemoryCast<uint32_t>(char32Value));
162 EXPECT_EQ(bit_cast<float>(char32Value), MemoryCast<float>(char32Value));
163
164 int64_t int64Value = -9223372036854775807LL;
165 uint64_t uint64Value = 9223372036854775808ULL;
166 double doubleValue = -257.93458301390463;
167 EXPECT_EQ(bit_cast<uint64_t>(int64Value), MemoryCast<uint64_t>(int64Value));
168 EXPECT_EQ(bit_cast<double>(int64Value), MemoryCast<double>(int64Value));
169 EXPECT_EQ(bit_cast<int64_t>(uint64Value), MemoryCast<int64_t>(uint64Value));
170 EXPECT_EQ(bit_cast<double>(uint64Value), MemoryCast<double>(uint64Value));
171 EXPECT_EQ(bit_cast<int64_t>(doubleValue), MemoryCast<int64_t>(doubleValue));
172 EXPECT_EQ(bit_cast<uint64_t>(doubleValue), MemoryCast<uint64_t>(doubleValue));
173 }
174 } // namespace panda::test
175