| 1 | // Copyright 2019 Google LLC |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | #include <stddef.h> |
| 17 | #include <stdint.h> |
| 18 | |
| 19 | #include <string> |
| 20 | |
| 21 | #undef HWY_TARGET_INCLUDE |
| 22 | #define HWY_TARGET_INCLUDE "tests/test_util_test.cc" |
| 23 | #include "hwy/foreach_target.h" // IWYU pragma: keep |
| 24 | #include "hwy/highway.h" |
| 25 | #include "hwy/tests/test_util-inl.h" |
| 26 | |
| 27 | HWY_BEFORE_NAMESPACE(); |
| 28 | namespace hwy { |
| 29 | namespace HWY_NAMESPACE { |
| 30 | |
| 31 | struct TestName { |
| 32 | template <class T, class D> |
| 33 | HWY_NOINLINE void operator()(T t, D d) { |
| 34 | char num[10]; |
| 35 | std::string expected = IsFloat<T>() ? "f" : (IsSigned<T>() ? "i" : "u"); |
| 36 | snprintf(num, sizeof(num), "%u" , static_cast<unsigned>(sizeof(T) * 8)); |
| 37 | expected += num; |
| 38 | |
| 39 | const size_t N = Lanes(d); |
| 40 | if (N != 1) { |
| 41 | expected += 'x'; |
| 42 | snprintf(num, sizeof(num), "%u", static_cast<unsigned>(N)); |
| 43 | expected += num; |
| 44 | } |
| 45 | const std::string actual = TypeName(t, N); |
| 46 | if (expected != actual) { |
| 47 | HWY_ABORT("%s mismatch: expected '%s', got '%s'.\n", |
| 48 | hwy::TargetName(HWY_TARGET), expected.c_str(), actual.c_str()); |
| 49 | } |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | HWY_NOINLINE void TestAllName() { ForAllTypes(ForPartialVectors<TestName>()); } |
| 54 | |
| 55 | struct TestEqualInteger { |
| 56 | template <class T> |
| 57 | HWY_NOINLINE void operator()(T /*t*/) const { |
| 58 | HWY_ASSERT_EQ(T(0), T(0)); |
| 59 | HWY_ASSERT_EQ(T(1), T(1)); |
| 60 | HWY_ASSERT_EQ(T(-1), T(-1)); |
| 61 | HWY_ASSERT_EQ(LimitsMin<T>(), LimitsMin<T>()); |
| 62 | |
| 63 | HWY_ASSERT(!IsEqual(T(0), T(1))); |
| 64 | HWY_ASSERT(!IsEqual(T(1), T(0))); |
| 65 | HWY_ASSERT(!IsEqual(T(1), T(-1))); |
| 66 | HWY_ASSERT(!IsEqual(T(-1), T(1))); |
| 67 | HWY_ASSERT(!IsEqual(LimitsMin<T>(), LimitsMax<T>())); |
| 68 | HWY_ASSERT(!IsEqual(LimitsMax<T>(), LimitsMin<T>())); |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | struct TestEqualFloat { |
| 73 | template <class T> |
| 74 | HWY_NOINLINE void operator()(T /*t*/) const { |
| 75 | HWY_ASSERT(IsEqual(T(0), T(0))); |
| 76 | HWY_ASSERT(IsEqual(T(1), T(1))); |
| 77 | HWY_ASSERT(IsEqual(T(-1), T(-1))); |
| 78 | HWY_ASSERT(IsEqual(MantissaEnd<T>(), MantissaEnd<T>())); |
| 79 | |
| 80 | HWY_ASSERT(!IsEqual(T(0), T(1))); |
| 81 | HWY_ASSERT(!IsEqual(T(1), T(0))); |
| 82 | HWY_ASSERT(!IsEqual(T(1), T(-1))); |
| 83 | HWY_ASSERT(!IsEqual(T(-1), T(1))); |
| 84 | HWY_ASSERT(!IsEqual(LowestValue<T>(), HighestValue<T>())); |
| 85 | HWY_ASSERT(!IsEqual(HighestValue<T>(), LowestValue<T>())); |
| 86 | } |
| 87 | }; |
| 88 | |
| 89 | HWY_NOINLINE void TestAllEqual() { |
| 90 | ForIntegerTypes(TestEqualInteger()); |
| 91 | ForFloatTypes(TestEqualFloat()); |
| 92 | } |
| 93 | |
| 94 | // NOLINTNEXTLINE(google-readability-namespace-comments) |
| 95 | } // namespace HWY_NAMESPACE |
| 96 | } // namespace hwy |
| 97 | HWY_AFTER_NAMESPACE(); |
| 98 | |
| 99 | #if HWY_ONCE |
| 100 | |
| 101 | namespace hwy { |
| 102 | HWY_BEFORE_TEST(TestUtilTest); |
| 103 | HWY_EXPORT_AND_TEST_P(TestUtilTest, TestAllName); |
| 104 | HWY_EXPORT_AND_TEST_P(TestUtilTest, TestAllEqual); |
| 105 | } // namespace hwy |
| 106 | |
| 107 | #endif |