From c5424185b0c3652086efc914fa1e0c83365f6072 Mon Sep 17 00:00:00 2001 From: Xi Ruoyao Date: Fri, 13 Dec 2024 15:46:00 +0800 Subject: [PATCH] LoongArch: Add crc tests gcc/testsuite/ChangeLog: * g++.target/loongarch/crc.C: New test. * g++.target/loongarch/crc-scan.C: New test. --- gcc/testsuite/g++.target/loongarch/crc-scan.C | 13 ++ gcc/testsuite/g++.target/loongarch/crc.C | 120 ++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 gcc/testsuite/g++.target/loongarch/crc-scan.C create mode 100644 gcc/testsuite/g++.target/loongarch/crc.C diff --git a/gcc/testsuite/g++.target/loongarch/crc-scan.C b/gcc/testsuite/g++.target/loongarch/crc-scan.C new file mode 100644 index 00000000000..971580f0d03 --- /dev/null +++ b/gcc/testsuite/g++.target/loongarch/crc-scan.C @@ -0,0 +1,13 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -march=loongarch64" } */ + +#include "crc.C" + +/* { dg-final { scan-assembler-times "crc\\.w\\.b\\.w" 2 } } */ +/* { dg-final { scan-assembler-times "crc\\.w\\.h\\.w" 2 } } */ +/* { dg-final { scan-assembler-times "crc\\.w\\.w\\.w" 2 } } */ +/* { dg-final { scan-assembler-times "crcc\\.w\\.b\\.w" 2 } } */ +/* { dg-final { scan-assembler-times "crcc\\.w\\.h\\.w" 2 } } */ +/* { dg-final { scan-assembler-times "crcc\\.w\\.w\\.w" 2 } } */ +/* { dg-final { scan-assembler-not "crc\\.w\\.\[bhw\]\\.w\t\\\$r\[0-9\]+,\\\$r0" } } */ +/* { dg-final { scan-assembler-not "crcc\\.w\\.\[bhw\]\\.w\t\\\$r\[0-9\]+,\\\$r0" } } */ diff --git a/gcc/testsuite/g++.target/loongarch/crc.C b/gcc/testsuite/g++.target/loongarch/crc.C new file mode 100644 index 00000000000..16df4a1ae63 --- /dev/null +++ b/gcc/testsuite/g++.target/loongarch/crc.C @@ -0,0 +1,120 @@ +/* { dg-do run } */ +/* { dg-options "-O2" } */ + +typedef __UINT8_TYPE__ uint8_t; +typedef __UINT16_TYPE__ uint16_t; +typedef __UINT32_TYPE__ uint32_t; +typedef __UINT64_TYPE__ uint64_t; +typedef __SIZE_TYPE__ size_t; + +template +__attribute__ ((always_inline)) inline uint32_t +crc32_impl (const T *data, size_t len) +{ + uint32_t ret = 0xffffffffu; + for (size_t k = 0; k < len; k++) + { + ret ^= data[k]; + for (int i = 0; i < 8 * sizeof (T); i++) + if (ret & 1) + ret = (ret >> 1) ^ poly; + else + ret >>= 1; + } + return ret; +} + +template +__attribute__ ((noipa, optimize (0))) uint32_t +crc32_ref (const T *data, size_t len) +{ + return crc32_impl (data, len); +} + +template +__attribute__ ((noipa)) uint32_t +crc32_opt (const T *data, size_t len) +{ + return crc32_impl (data, len); +} + +template +__attribute__ ((noipa)) uint32_t +crc32_alt (const T *data, size_t len) +{ + uint32_t ret = 0xffffffffu; + for (size_t k = 0; k < len; k++) + { + T x = data[k]; + for (int i = 0; i < 8 * sizeof (T); i++) + { + if ((ret & 1) ^ (x & 1)) + ret = (ret >> 1) ^ poly; + else + ret >>= 1; + x >>= 1; + } + } + return ret; +} + +union test_data_t +{ + uint8_t u8[1024]; + uint16_t u16[512]; + uint32_t u32[256]; + + operator const uint8_t * () const { return u8; } + operator const uint16_t * () const { return u16; } + operator const uint32_t * () const { return u32; } + + constexpr + test_data_t () + : u8{} + { + } +}; + +/* Generate test data at compile time with minstd_rand0 algorithm. */ +constexpr test_data_t +gen (uint64_t seed) +{ + uint64_t state = seed; + test_data_t ret; + for (int i = 0; i < sizeof (ret); i++) + { + state = state * 16807 % 2147483647; + ret.u8[i] = (uint8_t)state; + } + return ret; +} + +constexpr union test_data_t test_data = gen (0xdeadbeef); + +void +assert_eq (uint32_t x, uint32_t y) +{ + if (x != y) + __builtin_trap (); +} + +template +void +test_crc32 () +{ + constexpr size_t len = sizeof (test_data) / sizeof (T); + uint32_t ref = crc32_ref (test_data, len); + assert_eq (ref, crc32_opt (test_data, len)); + assert_eq (ref, crc32_alt (test_data, len)); +} + +int +main (void) +{ + test_crc32 (); + test_crc32 (); + test_crc32 (); + test_crc32 (); + test_crc32 (); + test_crc32 (); +}