Skip to content

Instantly share code, notes, and snippets.

@ayurchen
Created November 6, 2020 17:12
Show Gist options
  • Save ayurchen/77251795940805f21d4dd53fa04f087e to your computer and use it in GitHub Desktop.
Save ayurchen/77251795940805f21d4dd53fa04f087e to your computer and use it in GitHub Desktop.
diff --git a/galerautils/src/gu_crc32c_arm64.c b/galerautils/src/gu_crc32c_arm64.c
index 62c0a0fa..f8647abf 100644
--- a/galerautils/src/gu_crc32c_arm64.c
+++ b/galerautils/src/gu_crc32c_arm64.c
@@ -20,7 +20,6 @@
#include <stdbool.h>
#include <arm_acle.h>
-#include <arm_neon.h>
static inline gu_crc32c_t
crc32c_arm64_tail7(gu_crc32c_t state, const uint8_t* ptr, size_t len)
@@ -69,25 +68,36 @@ gu_crc32c_arm64(gu_crc32c_t state, const void* data, size_t len)
return crc32c_arm64_tail7(state, ptr, len);
}
+#include <asm/hwcap.h>
#include <sys/auxv.h>
-#ifndef HWCAP_CRC32
-#define HWCAP_CRC32 (1 << 7)
+#if defined(HWCAP_CRC32)
+# define GU_AT_HWCAP AT_HWCAP
+# define GU_HWCAP_CRC32 HWCAP_CRC32
+#elif defined(HWCAP2_CRC32)
+# define GU_AT_HWCAP AT_HWCAP2
+# define GU_HWCAP_CRC32 HWCAP2_CRC32
#endif /* HWCAP_CRC32 */
gu_crc32c_func_t
gu_crc32c_hardware()
{
- unsigned long int const hwcaps = getauxval(AT_HWCAP);
- if (hwcaps & HWCAP_CRC32)
+#if defined(GU_AT_HWCAP)
+ unsigned long int const hwcaps = getauxval(GU_AT_HWCAP);
+ if (hwcaps & GU_HWCAP_CRC32)
{
gu_info ("CRC-32C: using hardware acceleration.");
return gu_crc32c_arm64;
}
else
{
+ gu_info ("CRC-32C: hardware does not have CRC-32C capabilities.");
return NULL;
}
+#else
+ gu_info ("CRC-32C: compiled without hardware acceleration support.");
+ return NULL;
+#endif /* GU_AT_HWCAP */
}
#endif /* GU_CRC32C_ARM64 */
diff --git a/galerautils/tests/crc32c_bench.cpp b/galerautils/tests/crc32c_bench.cpp
index 74dd93fb..a713d338 100644
--- a/galerautils/tests/crc32c_bench.cpp
+++ b/galerautils/tests/crc32c_bench.cpp
@@ -93,6 +93,8 @@ run_bench_with_impl(gu_crc32c_func_t impl,
<< std::fixed << duration << '\t' << result << '\n';
}
+static gu_crc32c_func_t configured_impl;
+
static void
one_length(size_t const len, size_t const reps)
{
@@ -109,7 +111,8 @@ one_length(size_t const len, size_t const reps)
#endif /* GU_CRC32C_X86 */
#if defined(GU_CRC32C_ARM64)
- run_bench_with_impl(gu_crc32c_arm64, len, reps, "GU arm64 ");
+ if (gu_crc32c_arm64 == configured_impl)
+ run_bench_with_impl(gu_crc32c_arm64, len, reps, "GU arm64 ");
#endif /* GU_CRC32C_X86 */
}
@@ -117,9 +120,11 @@ int main()
{
gu_crc32c_configure(); // compute SW lookup tables
+ configured_impl = gu_crc32c_func;
+
one_length(11, 1<<22 /* 4M */);
one_length(31, 1<<21 /* 2M */);
one_length(64, 1<<20 /* 1M */);
one_length(512, 1<<17 /* 128K */);
- one_length(1<<20 /* 1M */, 64);
+ one_length(1<<20, 64 /* 1M */);
}
diff --git a/galerautils/tests/gu_crc32c_test.c b/galerautils/tests/gu_crc32c_test.c
index 96efb0bd..c3795107 100644
--- a/galerautils/tests/gu_crc32c_test.c
+++ b/galerautils/tests/gu_crc32c_test.c
@@ -144,8 +144,13 @@ END_TEST
#if defined(GU_CRC32C_ARM64)
START_TEST(test_gu_crc32c_arm64)
{
- gu_crc32c_func = gu_crc32c_arm64;
- test_function();
+ gu_crc32c_func = gu_crc32c_hardware();
+
+ if (NULL != gu_crc32c_func)
+ {
+ ck_assert(gu_crc32c_arm64 == gu_crc32c_func);
+ test_function();
+ }
}
END_TEST
#endif /* GU_CRC32C_ARM64 */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment