• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2024 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 <gtest/gtest.h>
17 
18 #include "runtime/interpreter/math_helpers.h"
19 
20 namespace ark::interpreter::math_helpers::test {
21 
22 // NOLINTBEGIN(hicpp-signed-bitwise)
23 
24 template <class T>
TestBitShl()25 void TestBitShl()
26 {
27     std::ostringstream ss;
28     ss << "Test bit_shl with sizeof(T) = ";
29     ss << sizeof(T);
30 
31     using UnsignedType = std::make_unsigned_t<T>;
32 
33     {
34         T v = 1;
35         size_t shift = 5;
36         T res = bit_shl<T>()(v, shift);
37         EXPECT_EQ(res, v << shift) << ss.str();
38     }
39 
40     {
41         T v = 1;
42         size_t shift = std::numeric_limits<UnsignedType>::digits - 1;
43         T res = bit_shl<T>()(v, shift);
44         EXPECT_EQ(res, static_cast<T>(v << shift)) << ss.str();
45     }
46 
47     {
48         T v = 1;
49         size_t shift = std::numeric_limits<UnsignedType>::digits;
50         T res = bit_shl<T>()(v, shift);
51         EXPECT_EQ(res, v) << ss.str();
52     }
53 
54     {
55         T v = 1;
56         size_t shift = std::numeric_limits<UnsignedType>::digits + 2;
57         T res = bit_shl<T>()(v, shift);
58         EXPECT_EQ(res, v << 2U) << ss.str();
59     }
60 }
61 
62 template <class T>
TestBitShr()63 void TestBitShr()
64 {
65     std::ostringstream ss;
66     ss << "Test bit_shr with sizeof(T) = ";
67     ss << sizeof(T);
68 
69     using UnsignedType = std::make_unsigned_t<T>;
70 
71     {
72         // NOLINTNEXTLINE(readability-magic-numbers)
73         T v = 64;
74         T shift = 5;
75         T res = bit_shr<T>()(v, shift);
76         EXPECT_EQ(res, v >> shift) << ss.str();
77     }
78 
79     {
80         T v = std::numeric_limits<T>::min();
81         T shift = std::numeric_limits<UnsignedType>::digits - 1;
82         T res = bit_shr<T>()(v, shift);
83         EXPECT_EQ(res, 1) << ss.str();
84     }
85 
86     {
87         T v = 1;
88         T shift = std::numeric_limits<UnsignedType>::digits;
89         T res = bit_shr<T>()(v, shift);
90         EXPECT_EQ(res, v) << ss.str();
91     }
92 
93     {
94         // NOLINTNEXTLINE(readability-magic-numbers)
95         T v = 20;
96         T shift = std::numeric_limits<UnsignedType>::digits + 2;
97         T res = bit_shr<T>()(v, shift);
98         EXPECT_EQ(res, v >> 2U) << ss.str();
99     }
100 }
101 
102 template <class T>
TestBitAshr()103 void TestBitAshr()
104 {
105     std::ostringstream ss;
106     ss << "Test bit_ashr with sizeof(T) = ";
107     ss << sizeof(T);
108 
109     using UnsignedType = std::make_unsigned_t<T>;
110 
111     {
112         // NOLINTNEXTLINE(readability-magic-numbers)
113         T v = 64;
114         T shift = 5;
115         T res = bit_ashr<T>()(v, shift);
116         EXPECT_EQ(res, v >> shift) << ss.str();
117     }
118 
119     {
120         T v = std::numeric_limits<T>::min();
121         T shift = std::numeric_limits<UnsignedType>::digits - 1;
122         T res = bit_ashr<T>()(v, shift);
123         EXPECT_EQ(res, -1) << ss.str();
124     }
125 
126     {
127         T v = 1;
128         T shift = std::numeric_limits<UnsignedType>::digits;
129         T res = bit_ashr<T>()(v, shift);
130         EXPECT_EQ(res, v) << ss.str();
131     }
132 
133     {
134         // NOLINTNEXTLINE(readability-magic-numbers)
135         T v = 20;
136         T shift = std::numeric_limits<UnsignedType>::digits + 2;
137         T res = bit_ashr<T>()(v, shift);
138         EXPECT_EQ(res, v >> 2U) << ss.str();
139     }
140 }
141 
142 // NOLINTEND(hicpp-signed-bitwise)
143 
144 template <class T>
145 T GetNaN();
146 
147 template <>
GetNaN()148 float GetNaN()
149 {
150     return nanf("");
151 }
152 
153 template <>
GetNaN()154 double GetNaN()
155 {
156     return nan("");
157 }
158 
159 template <class T>
TestFcmpl()160 void TestFcmpl()
161 {
162     std::ostringstream ss;
163     ss << "Test fcmpl with sizeof(T) = ";
164     ss << sizeof(T);
165 
166     {
167         T v1 = 1.0;
168         T v2 = GetNaN<T>();
169         EXPECT_EQ(fcmpl<T>()(v1, v2), -1);
170     }
171 
172     {
173         T v1 = GetNaN<T>();
174         T v2 = 1.0;
175         EXPECT_EQ(fcmpl<T>()(v1, v2), -1);
176     }
177 
178     {
179         T v1 = GetNaN<T>();
180         T v2 = GetNaN<T>();
181         EXPECT_EQ(fcmpl<T>()(v1, v2), -1);
182     }
183 
184     {
185         T v1 = 1.0;
186         // NOLINTNEXTLINE(readability-magic-numbers)
187         T v2 = 2.0;
188         EXPECT_EQ(fcmpl<T>()(v1, v2), -1);
189     }
190 
191     {
192         T v1 = 1.0;
193         T v2 = v1;
194         EXPECT_EQ(fcmpl<T>()(v1, v2), 0);
195     }
196 
197     {
198         // NOLINTNEXTLINE(readability-magic-numbers)
199         T v1 = 2.0;
200         T v2 = 1.0;
201         EXPECT_EQ(fcmpl<T>()(v1, v2), 1);
202     }
203 }
204 
205 template <class T>
TestFcmpg()206 void TestFcmpg()
207 {
208     std::ostringstream ss;
209     ss << "Test fcmpg with sizeof(T) = ";
210     ss << sizeof(T);
211 
212     {
213         T v1 = 1.0;
214         T v2 = GetNaN<T>();
215         EXPECT_EQ(fcmpg<T>()(v1, v2), 1);
216     }
217 
218     {
219         T v1 = GetNaN<T>();
220         T v2 = 1.0;
221         EXPECT_EQ(fcmpg<T>()(v1, v2), 1);
222     }
223 
224     {
225         T v1 = GetNaN<T>();
226         T v2 = GetNaN<T>();
227         EXPECT_EQ(fcmpg<T>()(v1, v2), 1);
228     }
229 
230     {
231         T v1 = 1.0;
232         // NOLINTNEXTLINE(readability-magic-numbers)
233         T v2 = 2.0;
234         EXPECT_EQ(fcmpg<T>()(v1, v2), -1);
235     }
236 
237     {
238         T v1 = 1.0;
239         T v2 = v1;
240         EXPECT_EQ(fcmpg<T>()(v1, v2), 0);
241     }
242 
243     {
244         // NOLINTNEXTLINE(readability-magic-numbers)
245         T v1 = 2.0;
246         T v2 = 1.0;
247         EXPECT_EQ(fcmpg<T>()(v1, v2), 1);
248     }
249 }
250 
TEST(MathHelpers,BitShl)251 TEST(MathHelpers, BitShl)
252 {
253     TestBitShl<int8_t>();
254     TestBitShl<int16_t>();
255     TestBitShl<int32_t>();
256     TestBitShl<int64_t>();
257 }
258 
TEST(MathHelpers,BitShr)259 TEST(MathHelpers, BitShr)
260 {
261     TestBitShr<int8_t>();
262     TestBitShr<int16_t>();
263     TestBitShr<int32_t>();
264     TestBitShr<int64_t>();
265 }
266 
TEST(MathHelpers,BitAshr)267 TEST(MathHelpers, BitAshr)
268 {
269     TestBitAshr<int8_t>();
270     TestBitAshr<int16_t>();
271     TestBitAshr<int32_t>();
272     TestBitAshr<int64_t>();
273 }
274 
TEST(MathHelpers,Fcmpl)275 TEST(MathHelpers, Fcmpl)
276 {
277     TestFcmpl<float>();
278     TestFcmpl<double>();
279 }
280 
TEST(MathHelpers,Fcmpg)281 TEST(MathHelpers, Fcmpg)
282 {
283     TestFcmpg<float>();
284     TestFcmpg<double>();
285 }
286 
287 }  // namespace ark::interpreter::math_helpers::test
288