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


##########
src/butil/crc32c.cc:
##########
@@ -17,6 +17,29 @@
 #ifdef __SSE4_2__
 #include <nmmintrin.h>
 #endif
+#if defined(__aarch64__)
+#if defined(__clang__)
+#pragma clang attribute push(__attribute__((target("crc,aes"))), \
+                             apply_to = function)
+#elif defined(__GNUC__)
+#pragma GCC push_options
+#pragma GCC target("+crc+crypto")
+#endif
+#include <arm_acle.h>
+#include <arm_neon.h>

Review Comment:
   In a Clang 14 build with `-march=armv8-a+nosimd`, `__aarch64__` is defined, 
but `__ARM_NEON` is not. A function attribute cannot change the header's 
preprocessor conditions, so including `arm_neon.h` triggers `NEON support not 
enabled`. Please guard the PMULL header, implementation, and dispatch 
separately so builds with SIMD disabled can still use scalar or software CRC. 
[Clang 14 header generation 
logic](https://github.com/llvm/llvm-project/blob/llvmorg-14.0.6/clang/utils/TableGen/NeonEmitter.cpp#L2325-L2334)



##########
test/crc32c_unittest.cc:
##########
@@ -7,12 +7,72 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file. See the AUTHORS file for names of contributors.
 
-#include <gtest/gtest.h>
 #include "butil/crc32c.h"
 
+#include <gtest/gtest.h>
+
+#include <cstdlib>
+#include <memory>
+#include <vector>
+
+#include "butil/time.h"
+#if defined(__unix__) || defined(__APPLE__)
+#include <sys/mman.h>
+#include <unistd.h>
+#if !defined(MAP_ANON) && defined(MAP_ANONYMOUS)
+#define MAP_ANON MAP_ANONYMOUS
+#endif
+#endif
+#if defined(__aarch64__) && defined(__linux__)
+#include <asm/hwcap.h>
+#include <sys/auxv.h>
+#ifndef HWCAP_CRC32
+#define HWCAP_CRC32 (1UL << 7)
+#endif
+#elif defined(__aarch64__) && defined(__APPLE__)
+#include <sys/sysctl.h>
+#endif
+
 namespace butil {
 namespace crc32c {
 
+#if defined(__aarch64__)
+uint32_t Arm64ExtendCrc32ForTest(uint32_t crc, const char* buf, size_t size);
+uint32_t Arm64ExtendCrc32PmullForTest(uint32_t crc, const char* buf,
+                                      size_t size);
+#endif
+
+namespace {
+
+uint32_t ReferenceExtend(uint32_t init_crc, const char* data, size_t n) {
+  uint32_t c = init_crc ^ 0xffffffffU;
+  const uint8_t* p = reinterpret_cast<const uint8_t*>(data);
+  for (size_t i = 0; i < n; ++i) {
+    c ^= p[i];
+    for (int bit = 0; bit < 8; ++bit) {
+      c = (c >> 1) ^ ((c & 1U) ? 0x82f63b78U : 0U);
+    }
+  }
+  return c ^ 0xffffffffU;
+}
+
+void VerifyAllExtendPaths(const char* mode, uint32_t seed, const char* buf,
+                          size_t len, size_t offset) {
+  const uint32_t expected = ReferenceExtend(seed, buf, len);
+  ASSERT_EQ(expected, Extend(seed, buf, len))
+      << mode << " len=" << len << " offset=" << offset << " seed=" << seed;
+#if defined(__aarch64__)
+  if (IsFastCrc32Supported()) {
+    ASSERT_EQ(expected, Arm64ExtendCrc32ForTest(seed, buf, len))
+        << mode << " len=" << len << " offset=" << offset << " seed=" << seed;
+    ASSERT_EQ(expected, Arm64ExtendCrc32PmullForTest(seed, buf, len))
+        << mode << " len=" << len << " offset=" << offset << " seed=" << seed;

Review Comment:
   `Arm64ExtendCrc32PmullForTest` falls back to the scalar implementation when 
PMULL detection returns false. Therefore, even on a machine that supports 
PMULL, both assertions here would still pass if `isArmPmull()` were changed to 
always return `false`. Please make the dedicated test confirm that PMULL was 
actually selected and explicitly skip it when unsupported, so the stripe 
boundary cases verify the intended backend.



##########
src/butil/crc32c.cc:
##########
@@ -290,6 +313,21 @@ static const uint32_t table3_[256] = {
 // Lower-level versions of Get... that read directly from a character buffer
 // without any bounds checking.
 
+#if defined(__aarch64__)
+static inline uint16_t DecodeFixed16(const char* ptr) {
+#if defined(ARCH_CPU_LITTLE_ENDIAN) && ARCH_CPU_LITTLE_ENDIAN
+  // Load the raw bytes
+  uint16_t result;
+  memcpy(&result, ptr, sizeof(result));  // gcc optimizes this to a plain load
+  return result;

Review Comment:
   `ARCH_CPU_LITTLE_ENDIAN` is true for all `__aarch64__` targets in 
`build_config.h`. On big-endian ARM64, `01 02` is read here as `0x0102`, and 
`ArmCrc32ch` processes `02` first. Splitting the same input into two 
single-byte `Extend` calls processes `01` first, and the baseline also 
processes these two bytes one at a time. Please make multi-byte loads in the 
ARM hardware path explicitly produce little-endian values so that the CRC is 
independent of how the input is chunked.



-- 
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