Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/butil/third_party/snappy/snappy-stubs-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,16 @@ inline void UNALIGNED_STORE64(void *p, uint64_t v) {
// This can be more efficient than UNALIGNED_LOAD64 + UNALIGNED_STORE64
// on some platforms, in particular ARM.
inline void UnalignedCopy64(const void *src, void *dst) {
#if defined(__riscv) && __riscv_xlen == 64
// RISC-V optimized: single ld/sd pair for 8-byte copy
uint64_t tmp;
__asm__ volatile(
"ld %0, %1\n\t"
"sd %0, %2\n\t"
: "=&r"(tmp)
: "m"(*(const uint64_t*)src), "m"(*(uint64_t*)dst)
: "memory");
#else
if (sizeof(void *) == 8) {
UNALIGNED_STORE64(dst, UNALIGNED_LOAD64(src));
} else {
Expand All @@ -173,6 +183,7 @@ inline void UnalignedCopy64(const void *src, void *dst) {
UNALIGNED_STORE32(dst_char, UNALIGNED_LOAD32(src_char));
UNALIGNED_STORE32(dst_char + 4, UNALIGNED_LOAD32(src_char + 4));
}
#endif
}

// Convert to little-endian storage, opposite of network format.
Expand Down
24 changes: 24 additions & 0 deletions src/butil/third_party/snappy/snappy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,33 @@ static const uint32_t kMaximumTagLength = 5; // COPY_4_BYTE_OFFSET plus the act
// or memmove().
static inline void IncrementalCopy(const char* src, char* op, ssize_t len) {
assert(len > 0);
#if defined(__riscv) && __riscv_xlen == 64
// RISC-V optimized: use 8-byte copies when possible
if (len >= 8 && (op - src >= 8 || src - op >= 8)) {
// Non-overlapping or safe overlap: copy 8 bytes at a time
do {
uint64_t tmp;
__asm__ volatile(
"ld %0, %1\n\t"
"sd %0, %2\n\t"
: "=&r"(tmp)
: "m"(*(const uint64_t*)src), "m"(*(uint64_t*)op)
: "memory");
src += 8;
op += 8;
len -= 8;
} while (len >= 8);
}
// Copy remaining bytes
while (len > 0) {
*op++ = *src++;
--len;
}
#else
do {
*op++ = *src++;
} while (--len > 0);
#endif
}

// Equivalent to IncrementalCopy except that it can write up to ten extra
Expand Down
Loading