Cyan4973 / Cyan4973/xxHash

Proposal: Add RISC-V Optimization for XXH_mult32to64_add64 with Inline Assembly

Closed
#1,018 22 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

portability
Dominant language
C
Stars
11.3k
Forks
912
Avg merge
2h 58m
Merged PRs (30d)
24

Description

Hi @Cyan4973,

I’ve been working on compiling xxHash as part of CouchDB on a RISC-V machine (64-bit, riscv64-unknown-linux-gnu target). While reviewing `xxhash.h`, I noticed there’s no specific configuration or optimization for RISC-V, unlike the AArch64 case which uses `umaddl` for `XXH_mult32to64_add64`. Given RISC-V’s increasing relevance, I thought it might benefit from a tailored optimization.

To address this, I added a RISC-V-specific version of `XXH_mult32to64_add64` using inline assembly with `slli` and `srli` to mask inputs to 32 bits, followed by `mul` and `add`. Here’s the implementation I’ve tested:

```c
#elif defined(__riscv) && (__riscv_xlen == 64)
XXH_FORCE_INLINE xxh_u64
XXH_mult32to64_add64(xxh_u64 lhs, xxh_u64 rhs, xxh_u64 acc)
{
xxh_u64 ret;
__asm__ (
"slli t0, %1, 32 \n\t"
"srli t0, t0, 32 \n\t"
"slli t1, %2, 32 \n\t"
"srli t1, t1, 32 \n\t"
"mul %0, t0, t1 \n\t"
"add %0, %0, %3"
: "=r" (ret)
: "r" (lhs), "r" (rhs), "r" (acc)
: "t0", "t1"
);
return ret;
}
```

I built this within CouchDB on a RISC-V QEMU environment, disassembled the resulting `exxhash.so`, and confirmed the assembly matches my intent (e.g., `slli t0, a0, 32; srli t0, t0, 32; ...` appearing in `XXH3_hashLong_64b_default`).

Before I commit this change and create a pull request, I wanted to get your thoughts:

Do you think this RISC-V optimization aligns with xxHash’s goals?
Are there specific tests or benchmarks you’d recommend I run to validate it further?
Any suggestions for refining the implementation (e.g., alternative instructions or constraints)?
I’d be happy to provide more details or adjust based on your feedback. Thanks for your time and for maintaining this great library!

Best regards,
Jay

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The proposed change belongs in xxhash.h alongside the AArch64 implementation of XXH_mult32to64_add64. Start by reviewing that existing case and the generated XXH3_hashLong_64b_default code, then build the riscv64-unknown-linux-gnu target in the CouchDB/QEMU setup. Done means the implementation is validated for correctness and its generated exxhash.so assembly and performance are compared.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, linux
Domain
performance
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.