jimmystewpot commented on code in PR #3754:
URL: https://github.com/apache/brpc/pull/3754#discussion_r4716207003


##########
src/butil/crc32c.cc:
##########
@@ -829,6 +1226,16 @@ static inline Function Choose_Extend() {
     return (Function)ExtendImpl<FastCRC32Functor>;
   }
 #endif
+#if defined(__aarch64__)
+  if (isArmCrc32()) {
+#if defined(BUTIL_ARM64_PMULL_ENABLED)
+    if (isArmPmull()) {
+      return (Function)Arm64ExtendCrc32Pmull;
+    }
+#endif
+    return (Function)Arm64ExtendCrc32;

Review Comment:
   Fixed in `b1672b1b`.
   
   Standardised endianness detection in `src/butil/crc32c.cc` around a single 
`BUTIL_CRC32C_LITTLE_ENDIAN` macro (checking compiler big-endian macros before 
falling back to `ARCH_CPU_LITTLE_ENDIAN`) and updated `DecodeFixed32` and 
`DecodeFixed64` to use it. ARM64 hardware acceleration 
(`BUTIL_ARM64_CRC32_ENABLED`, including PMULL) is now gated on little-endian 
ARM64, and the separate `ArmLoadLe16/32/64` helpers have been removed in favour 
of the shared `LE_LOAD*` / `DecodeFixed*` path.
   
   On big-endian ARM64, both CRC32-capable and non-CRC32 CPUs now execute the 
same byte-assembled software path, producing consistent RFC 3720 CRC32C 
checksums independent of hardware feature flags and avoiding untested 
big-endian PMULL codegen.



##########
test/crc32c_unittest.cc:
##########
@@ -71,6 +160,287 @@ TEST_F(CRC, Mask) {
 
 TEST_F(CRC, fast_is_on) {
   std::cout << "IsFastCrc32Supported=" << IsFastCrc32Supported() << std::endl;
+#if defined(__aarch64__) && defined(__APPLE__)
+  int val = 0;
+  size_t len = sizeof(val);
+  const bool sysctl_crc32 =
+      (sysctlbyname("hw.optional.armv8_crc32", &val, &len, nullptr, 0) == 0 &&
+       val != 0);
+  EXPECT_EQ(sysctl_crc32, IsFastCrc32Supported());
+  val = 0;
+  len = sizeof(val);
+  const bool sysctl_pmull = (sysctlbyname("hw.optional.arm.FEAT_PMULL", &val,
+                                          &len, nullptr, 0) == 0 &&
+                             val != 0);
+#if defined(__ARM_NEON)
+  EXPECT_EQ(sysctl_crc32 && sysctl_pmull, IsArm64PmullSelectedForTest());
+#else
+  (void)sysctl_pmull;
+  EXPECT_FALSE(IsArm64PmullSelectedForTest());
+#endif
+#elif defined(__aarch64__) && defined(__linux__)
+  const unsigned long hwcap = getauxval(AT_HWCAP);
+  const bool hw_crc32 = (hwcap & HWCAP_CRC32) != 0;
+  const bool hw_pmull = (hwcap & HWCAP_PMULL) != 0;
+  EXPECT_EQ(hw_crc32, IsFastCrc32Supported());
+#if defined(__ARM_NEON)
+  EXPECT_EQ(hw_crc32 && hw_pmull, IsArm64PmullSelectedForTest());
+#else
+  (void)hw_pmull;
+  EXPECT_FALSE(IsArm64PmullSelectedForTest());

Review Comment:
   Fixed in `b1672b1b` and `67cb7032`.
   
   Exposed `IsArm64Crc32CompiledForTest()` and `IsArm64PmullCompiledForTest()` 
from `src/butil/crc32c.cc` so the test queries the capabilities actually 
compiled into the library rather than checking `__ARM_NEON` in 
`test/crc32c_unittest.cc`. `CRC.fast_is_on` now checks runtime selection 
against `IsArm64Crc32CompiledForTest()` and `IsArm64PmullCompiledForTest()` 
combined with the OS hardware probes.
   
   Verified with mixed translation-unit builds (`crc32c.cc` compiled with 
`-march=armv8-a+nosimd` and `crc32c_unittest.cc` compiled with default 
`-march=armv8-a`, as well as the reverse): `CRC.fast_is_on` passes and 
`CRC.Arm64PmullStripeBoundaries` skips cleanly when PMULL is compiled out.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to