https://gcc.gnu.org/g:c5424185b0c3652086efc914fa1e0c83365f6072

commit r15-6323-gc5424185b0c3652086efc914fa1e0c83365f6072
Author: Xi Ruoyao <xry...@xry111.site>
Date:   Fri Dec 13 15:46:00 2024 +0800

    LoongArch: Add crc tests
    
    gcc/testsuite/ChangeLog:
    
            * g++.target/loongarch/crc.C: New test.
            * g++.target/loongarch/crc-scan.C: New test.

Diff:
---
 gcc/testsuite/g++.target/loongarch/crc-scan.C |  13 +++
 gcc/testsuite/g++.target/loongarch/crc.C      | 120 ++++++++++++++++++++++++++
 2 files changed, 133 insertions(+)

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 000000000000..971580f0d03a
--- /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 000000000000..16df4a1ae631
--- /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 <class T, uint32_t poly>
+__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 <class T, uint32_t poly>
+__attribute__ ((noipa, optimize (0))) uint32_t
+crc32_ref (const T *data, size_t len)
+{
+  return crc32_impl<T, poly> (data, len);
+}
+
+template <class T, uint32_t poly>
+__attribute__ ((noipa)) uint32_t
+crc32_opt (const T *data, size_t len)
+{
+  return crc32_impl<T, poly> (data, len);
+}
+
+template <class T, uint32_t poly>
+__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 <class T, uint32_t poly>
+void
+test_crc32 ()
+{
+  constexpr size_t len = sizeof (test_data) / sizeof (T);
+  uint32_t ref = crc32_ref<T, poly> (test_data, len);
+  assert_eq (ref, crc32_opt<T, poly> (test_data, len));
+  assert_eq (ref, crc32_alt<T, poly> (test_data, len));
+}
+
+int
+main (void)
+{
+  test_crc32<uint32_t, 0xEDB88320u> ();
+  test_crc32<uint16_t, 0xEDB88320u> ();
+  test_crc32<uint8_t, 0xEDB88320u> ();
+  test_crc32<uint32_t, 0x82F63B78u> ();
+  test_crc32<uint16_t, 0x82F63B78u> ();
+  test_crc32<uint8_t, 0x82F63B78u> ();
+}

Reply via email to