//===-- Unittests for backends --------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #include "src/__support/CPP/Array.h" #include "src/__support/CPP/ArrayRef.h" #include "src/__support/CPP/Bit.h" #include "src/__support/architectures.h" #include "src/string/memory_utils/backends.h" #include "utils/UnitTest/Test.h" #include namespace __llvm_libc { template using Buffer = cpp::Array; static char GetRandomChar() { // Implementation of C++ minstd_rand seeded with 123456789. // https://en.cppreference.com/w/cpp/numeric/random // "Minimum standard", recommended by Park, Miller, and Stockmeyer in 1993 static constexpr const uint64_t a = 48271; static constexpr const uint64_t c = 0; static constexpr const uint64_t m = 2147483647; static uint64_t seed = 123456789; seed = (a * seed + c) % m; return seed; } static void Randomize(cpp::MutableArrayRef buffer) { for (auto ¤t : buffer) current = GetRandomChar(); } template static Buffer GetRandomBuffer() { Buffer buffer; Randomize(buffer); return buffer; } template struct Conf { static_assert(Backend::IS_BACKEND_TYPE); using BufferT = Buffer; using T = typename Backend::template getNextType; static_assert(sizeof(T) == Size); static constexpr size_t SIZE = Size; static BufferT splat(ubyte value) { return bit_cast(Backend::template splat(value)); } static uint64_t notEquals(const BufferT &v1, const BufferT &v2) { return Backend::template notEquals(bit_cast(v1), bit_cast(v2)); } static int32_t threeWayCmp(const BufferT &v1, const BufferT &v2) { return Backend::template threeWayCmp(bit_cast(v1), bit_cast(v2)); } }; using FunctionTypes = testing::TypeList< // #if defined(LLVM_LIBC_ARCH_X86) // Conf, // Conf, // Conf, // Conf, // #if HAS_M128 Conf, // #endif #if HAS_M256 Conf, // #endif #if HAS_M512 Conf, // #endif #endif // defined(LLVM_LIBC_ARCH_X86) Conf, // Conf, // Conf, // Conf // >; TYPED_TEST(LlvmLibcMemoryBackend, splat, FunctionTypes) { for (auto value : cpp::Array{0u, 1u, 255u}) { alignas(64) const auto stored = ParamType::splat(bit_cast(value)); for (size_t i = 0; i < ParamType::SIZE; ++i) EXPECT_EQ(bit_cast(stored[i]), value); } } TYPED_TEST(LlvmLibcMemoryBackend, notEquals, FunctionTypes) { alignas(64) const auto a = GetRandomBuffer(); EXPECT_EQ(ParamType::notEquals(a, a), uint64_t(0)); for (size_t i = 0; i < a.size(); ++i) { alignas(64) auto b = a; ++b[i]; EXPECT_NE(ParamType::notEquals(a, b), uint64_t(0)); EXPECT_NE(ParamType::notEquals(b, a), uint64_t(0)); } } TYPED_TEST(LlvmLibcMemoryBackend, threeWayCmp, FunctionTypes) { alignas(64) const auto a = GetRandomBuffer(); EXPECT_EQ(ParamType::threeWayCmp(a, a), 0); for (size_t i = 0; i < a.size(); ++i) { alignas(64) auto b = a; ++b[i]; const auto cmp = memcmp(&a, &b, sizeof(a)); ASSERT_NE(cmp, 0); if (cmp > 0) { EXPECT_GT(ParamType::threeWayCmp(a, b), 0); EXPECT_LT(ParamType::threeWayCmp(b, a), 0); } else { EXPECT_LT(ParamType::threeWayCmp(a, b), 0); EXPECT_GT(ParamType::threeWayCmp(b, a), 0); } } } template struct LoadStoreConf { static_assert(Backend::IS_BACKEND_TYPE); using BufferT = Buffer; using T = typename Backend::template getNextType; static_assert(sizeof(T) == Size); static constexpr size_t SIZE = Size; static BufferT load(const BufferT &ref) { const auto *ptr = bit_cast(ref.data()); const T value = Backend::template load(ptr); return bit_cast(value); } static void store(BufferT &ref, const BufferT value) { auto *ptr = bit_cast(ref.data()); Backend::template store(ptr, bit_cast(value)); } }; using LoadStoreTypes = testing::TypeList< // #if defined(LLVM_LIBC_ARCH_X86) // LoadStoreConf, // LoadStoreConf, // LoadStoreConf, // LoadStoreConf, // LoadStoreConf, // LoadStoreConf, // LoadStoreConf, // LoadStoreConf, // #if HAS_M128 LoadStoreConf, // LoadStoreConf, // LoadStoreConf, // #endif #if HAS_M256 LoadStoreConf, // LoadStoreConf, // LoadStoreConf, // #endif #if HAS_M512 LoadStoreConf, // LoadStoreConf, // LoadStoreConf, // #endif #endif // defined(LLVM_LIBC_ARCH_X86) LoadStoreConf, // LoadStoreConf, // LoadStoreConf, // LoadStoreConf, // LoadStoreConf, // LoadStoreConf, // LoadStoreConf, // LoadStoreConf // >; TYPED_TEST(LlvmLibcMemoryBackend, load, LoadStoreTypes) { alignas(64) const auto expected = GetRandomBuffer(); const auto loaded = ParamType::load(expected); for (size_t i = 0; i < ParamType::SIZE; ++i) EXPECT_EQ(loaded[i], expected[i]); } TYPED_TEST(LlvmLibcMemoryBackend, store, LoadStoreTypes) { alignas(64) const auto expected = GetRandomBuffer(); alignas(64) typename ParamType::BufferT stored; ParamType::store(stored, expected); for (size_t i = 0; i < ParamType::SIZE; ++i) EXPECT_EQ(stored[i], expected[i]); } } // namespace __llvm_libc