Skip to content

Commit 7753427

Browse files
committed
refactor: Align RISC-V implementation with AArch64 branchless mask extraction
1 parent 6281a07 commit 7753427

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

snappy.cc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1375,7 +1375,13 @@ inline uint32_t ExtractOffset(uint32_t val, size_t tag_type) {
13751375
reinterpret_cast<const char*>(&kExtractMasksCombined) + 2 * tag_type,
13761376
sizeof(result));
13771377
return val & result;
1378-
#elif defined(__aarch64__)
1378+
// For AArch64 and RISC-V, use a bit-twiddling trick to extract the mask from a
1379+
// single combined constant instead of a lookup table. The constant packs multiple
1380+
// 16-bit masks based on tag_type (see implementation below). The code calculates
1381+
// the shift amount from tag_type, right-shifts the constant to move the desired
1382+
// mask to the LSB position, then extracts it with & 0xFFFF. This branchless
1383+
// approach is often more performant on modern CPUs.
1384+
#elif defined(__aarch64__) || (defined(__riscv) && (__riscv_xlen == 64))
13791385
constexpr uint64_t kExtractMasksCombined = 0x0000FFFF00FF0000ull;
13801386
return val & static_cast<uint32_t>(
13811387
(kExtractMasksCombined >> (tag_type * 16)) & 0xFFFF);

0 commit comments

Comments
 (0)