| 1 | // Copyright 2021 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 "hwy/tests/test_util.h" |
| 17 | |
| 18 | #include <stddef.h> |
| 19 | #include <stdio.h> |
| 20 | |
| 21 | #include <cmath> |
| 22 | |
| 23 | #include "hwy/base.h" |
| 24 | #include "hwy/print.h" |
| 25 | |
| 26 | namespace hwy { |
| 27 | |
| 28 | HWY_TEST_DLLEXPORT bool BytesEqual(const void* p1, const void* p2, |
| 29 | const size_t size, size_t* pos) { |
| 30 | const uint8_t* bytes1 = reinterpret_cast<const uint8_t*>(p1); |
| 31 | const uint8_t* bytes2 = reinterpret_cast<const uint8_t*>(p2); |
| 32 | for (size_t i = 0; i < size; ++i) { |
| 33 | if (bytes1[i] != bytes2[i]) { |
| 34 | if (pos != nullptr) { |
| 35 | *pos = i; |
| 36 | } |
| 37 | return false; |
| 38 | } |
| 39 | } |
| 40 | return true; |
| 41 | } |
| 42 | |
| 43 | void AssertStringEqual(const char* expected, const char* actual, |
| 44 | const char* target_name, const char* filename, |
| 45 | int line) { |
| 46 | while (*expected == *actual++) { |
| 47 | if (*expected++ == '\0') return; |
| 48 | } |
| 49 | |
| 50 | Abort(filename, line, "%s string mismatch: expected '%s', got '%s'.\n", |
| 51 | target_name, expected, actual); |
| 52 | } |
| 53 | |
| 54 | namespace detail { |
| 55 | |
| 56 | HWY_TEST_DLLEXPORT bool IsEqual(const TypeInfo& info, const void* expected_ptr, |
| 57 | const void* actual_ptr) { |
| 58 | if (!info.is_float) { |
| 59 | return BytesEqual(expected_ptr, actual_ptr, info.sizeof_t); |
| 60 | } |
| 61 | |
| 62 | if (info.sizeof_t == 4) { |
| 63 | float expected, actual; |
| 64 | CopyBytes<4>(expected_ptr, &expected); |
| 65 | CopyBytes<4>(actual_ptr, &actual); |
| 66 | return ComputeUlpDelta(expected, actual) <= 1; |
| 67 | } else if (info.sizeof_t == 8) { |
| 68 | double expected, actual; |
| 69 | CopyBytes<8>(expected_ptr, &expected); |
| 70 | CopyBytes<8>(actual_ptr, &actual); |
| 71 | return ComputeUlpDelta(expected, actual) <= 1; |
| 72 | } else { |
| 73 | HWY_ABORT("Unexpected float size %d\n", static_cast<int>(info.sizeof_t)); |
| 74 | return false; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | HWY_TEST_DLLEXPORT HWY_NORETURN void PrintMismatchAndAbort( |
| 79 | const TypeInfo& info, const void* expected_ptr, const void* actual_ptr, |
| 80 | const char* target_name, const char* filename, int line, size_t lane, |
| 81 | size_t num_lanes) { |
| 82 | char type_name[100]; |
| 83 | TypeName(info, 1, type_name); |
| 84 | char expected_str[100]; |
| 85 | ToString(info, expected_ptr, expected_str); |
| 86 | char actual_str[100]; |
| 87 | ToString(info, actual_ptr, actual_str); |
| 88 | Abort(filename, line, |
| 89 | "%s, %sx%d lane %d mismatch: expected '%s', got '%s'.\n", target_name, |
| 90 | type_name, static_cast<int>(num_lanes), static_cast<int>(lane), |
| 91 | expected_str, actual_str); |
| 92 | } |
| 93 | |
| 94 | HWY_TEST_DLLEXPORT void AssertArrayEqual(const TypeInfo& info, |
| 95 | const void* expected_void, |
| 96 | const void* actual_void, size_t N, |
| 97 | const char* target_name, |
| 98 | const char* filename, int line) { |
| 99 | const uint8_t* expected_array = |
| 100 | reinterpret_cast<const uint8_t*>(expected_void); |
| 101 | const uint8_t* actual_array = reinterpret_cast<const uint8_t*>(actual_void); |
| 102 | for (size_t i = 0; i < N; ++i) { |
| 103 | const void* expected_ptr = expected_array + i * info.sizeof_t; |
| 104 | const void* actual_ptr = actual_array + i * info.sizeof_t; |
| 105 | if (!IsEqual(info, expected_ptr, actual_ptr)) { |
| 106 | fprintf(stderr, "\n\n"); |
| 107 | PrintArray(info, "expect", expected_array, N, i); |
| 108 | PrintArray(info, "actual", actual_array, N, i); |
| 109 | |
| 110 | PrintMismatchAndAbort(info, expected_ptr, actual_ptr, target_name, |
| 111 | filename, line, i, N); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | } // namespace detail |
| 117 | } // namespace hwy |