/** * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include "runtime/interpreter/math_helpers.h" namespace panda::interpreter::math_helpers::test { template void TestBitShl() { std::ostringstream ss; ss << "Test bit_shl with sizeof(T) = "; ss << sizeof(T); using unsigned_type = std::make_unsigned_t; { T v = 1; size_t shift = 5; T res = bit_shl()(v, shift); EXPECT_EQ(res, v << shift) << ss.str(); } { T v = 1; size_t shift = std::numeric_limits::digits - 1; T res = bit_shl()(v, shift); EXPECT_EQ(res, static_cast(v << shift)) << ss.str(); } { T v = 1; size_t shift = std::numeric_limits::digits; T res = bit_shl()(v, shift); EXPECT_EQ(res, v) << ss.str(); } { T v = 1; size_t shift = std::numeric_limits::digits + 2; T res = bit_shl()(v, shift); EXPECT_EQ(res, v << 2) << ss.str(); } } template void TestBitShr() { std::ostringstream ss; ss << "Test bit_shr with sizeof(T) = "; ss << sizeof(T); using unsigned_type = std::make_unsigned_t; { T v = 64; T shift = 5; T res = bit_shr()(v, shift); EXPECT_EQ(res, v >> shift) << ss.str(); } { T v = std::numeric_limits::min(); T shift = std::numeric_limits::digits - 1; T res = bit_shr()(v, shift); EXPECT_EQ(res, 1) << ss.str(); } { T v = 1; T shift = std::numeric_limits::digits; T res = bit_shr()(v, shift); EXPECT_EQ(res, v) << ss.str(); } { T v = 20; T shift = std::numeric_limits::digits + 2; T res = bit_shr()(v, shift); EXPECT_EQ(res, v >> 2) << ss.str(); } } template void TestBitAshr() { std::ostringstream ss; ss << "Test bit_ashr with sizeof(T) = "; ss << sizeof(T); using unsigned_type = std::make_unsigned_t; { T v = 64; T shift = 5; T res = bit_ashr()(v, shift); EXPECT_EQ(res, v >> shift) << ss.str(); } { T v = std::numeric_limits::min(); T shift = std::numeric_limits::digits - 1; T res = bit_ashr()(v, shift); EXPECT_EQ(res, -1) << ss.str(); } { T v = 1; T shift = std::numeric_limits::digits; T res = bit_ashr()(v, shift); EXPECT_EQ(res, v) << ss.str(); } { T v = 20; T shift = std::numeric_limits::digits + 2; T res = bit_ashr()(v, shift); EXPECT_EQ(res, v >> 2) << ss.str(); } } template T GetNaN(); template <> float GetNaN() { return nanf(""); } template <> double GetNaN() { return nan(""); } template void TestFcmpl() { std::ostringstream ss; ss << "Test fcmpl with sizeof(T) = "; ss << sizeof(T); { T v1 = 1.0; T v2 = GetNaN(); EXPECT_EQ(fcmpl()(v1, v2), -1); } { T v1 = GetNaN(); T v2 = 1.0; EXPECT_EQ(fcmpl()(v1, v2), -1); } { T v1 = GetNaN(); T v2 = GetNaN(); EXPECT_EQ(fcmpl()(v1, v2), -1); } { T v1 = 1.0; T v2 = 2.0; EXPECT_EQ(fcmpl()(v1, v2), -1); } { T v1 = 1.0; T v2 = v1; EXPECT_EQ(fcmpl()(v1, v2), 0); } { T v1 = 2.0; T v2 = 1.0; EXPECT_EQ(fcmpl()(v1, v2), 1); } } template void TestFcmpg() { std::ostringstream ss; ss << "Test fcmpg with sizeof(T) = "; ss << sizeof(T); { T v1 = 1.0; T v2 = GetNaN(); EXPECT_EQ(fcmpg()(v1, v2), 1); } { T v1 = GetNaN(); T v2 = 1.0; EXPECT_EQ(fcmpg()(v1, v2), 1); } { T v1 = GetNaN(); T v2 = GetNaN(); EXPECT_EQ(fcmpg()(v1, v2), 1); } { T v1 = 1.0; T v2 = 2.0; EXPECT_EQ(fcmpg()(v1, v2), -1); } { T v1 = 1.0; T v2 = v1; EXPECT_EQ(fcmpg()(v1, v2), 0); } { T v1 = 2.0; T v2 = 1.0; EXPECT_EQ(fcmpg()(v1, v2), 1); } } TEST(MathHelpers, BitShl) { TestBitShl(); TestBitShl(); TestBitShl(); TestBitShl(); } TEST(MathHelpers, BitShr) { TestBitShr(); TestBitShr(); TestBitShr(); TestBitShr(); } TEST(MathHelpers, BitAshr) { TestBitAshr(); TestBitAshr(); TestBitAshr(); TestBitAshr(); } TEST(MathHelpers, Fcmpl) { TestFcmpl(); TestFcmpl(); } TEST(MathHelpers, Fcmpg) { TestFcmpg(); TestFcmpg(); } } // namespace panda::interpreter::math_helpers::test