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


##########
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:
   Fixed in 5a15ad2a (formatted in 68bda06e). Replaced `DecodeFixed16/32/64` in 
`Arm64ExtendCrc32` and `Arm64ExtendCrc32Pmull` with explicit little-endian 
loaders (`ArmLoadLe16/32/64`) gated on compiler endianness (`__BYTE_ORDER__ == 
__ORDER_BIG_ENDIAN__` / `__AARCH64EB__` / `__ARM_BIG_ENDIAN`) rather than 
`ARCH_CPU_LITTLE_ENDIAN`, and added `CRC.ChunkIndependence`.



##########
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:
   Fixed in d5f9577d. Separated `<arm_acle.h>` (`target("crc")` / `+crc`) from 
`<arm_neon.h>` (`target("crc,aes")` / `+crc+crypto`), and guarded 
`<arm_neon.h>`, `isArmPmull()`, `ArmVmullP64()`, `ComputeCrc32cShiftConst()`, 
`Arm64ExtendCrc32Pmull()`, and the PMULL dispatch branch behind `__ARM_NEON` 
(`BUTIL_ARM64_PMULL_ENABLED`) so `-march=armv8-a+nosimd` builds keep scalar 
CRC32 without requiring NEON.



##########
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:
   Fixed in 085b6250 (formatted in 68bda06e). Added 
`IsArm64PmullSelectedForTest()` checking that `Choose_Extend()` selects 
`Arm64ExtendCrc32Pmull`, asserted it against OS capability probes in 
`CRC.fast_is_on`, gated all `Arm64ExtendCrc32PmullForTest` calls on it, and 
added a dedicated `CRC.Arm64PmullStripeBoundaries` test that explicitly calls 
`GTEST_SKIP()` when PMULL is unsupported or SIMD is disabled.



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