Division and Remainder Instruction Optimization on AArch64#506
Merged
mohanson merged 4 commits intonervosnetwork:developfrom Apr 1, 2026
Merged
Division and Remainder Instruction Optimization on AArch64#506mohanson merged 4 commits intonervosnetwork:developfrom
mohanson merged 4 commits intonervosnetwork:developfrom
Conversation
Utilize `csel` to eliminate branches
Use 32-bit registers for division directly instead of extending and dividing
There was a problem hiding this comment.
Pull request overview
This PR updates the AArch64 assembly implementation of RISC-V M-extension division and remainder instructions to reduce control-flow and instruction count in the handlers.
Changes:
- Replaced divide-by-zero conditional branches with
cmp+cselin DIV/DIVU/DIVW/DIVUW and REM/REMU/REMW/REMUW handlers. - Switched *W handlers to native 32-bit
sdiv/udivforms (and corresponding 32-bitmsub) to avoid explicit operand extends.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Use `csinv` in the division instruction handlers to eliminate the redundant `mov TEMP2 UINT64_MAX`
mohanson
approved these changes
Apr 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two independent optimizations were applied to the AArch64 assembly handlers for the RISC-V division and remainder instructions.
Optimization 1: Branchless
cselSummary
Replaced conditional branches with ARM64
csel(Conditional SELect) in all 8 division/remainder instruction handlers (DIV, DIVU, DIVW, DIVUW, REM, REMU, REMW, REMUW). This eliminates branch misprediction penalties and improves instruction-level parallelism.Before / After
Before (DIV, branched):
After (DIV, branchless):
Remainder uses the same pattern: compute via
sdiv+msub, thencselbetween the computed remainder and the original dividend (RISC-V spec: rem by zero returns the dividend).Why This Is Safe
sdiv/udivwith divisor=0 returns 0 without trapping, so speculatively executing the division is harmless.sdivwith INT64_MIN / -1 returns INT64_MIN, matching RISC-V spec. No fixup needed for the overflow case.Trade-offs
Gains:
Costs:
Optimization 2: 32-bit Division and Remainder
Summary
Replaced the sign/zero-extend-then-divide-64-bit pattern in DIVW, DIVUW, REMW and REMUW, with direct 32-bit division instructions (e.g.
sdiv Wd, Wn, Wm). This removes 2 instructions per handler by letting the 32-bit instruction forms handle operand masking natively.Before / After
Before (DIVW):
After (DIVW):
Why This Is Safe
sdiv Wd, Wn, Wmreads only the low 32 bits of its source registers and writes a zero-extended 32-bit result to the destination, ignoring upper bits exactly as RISC-V requires.sdivon INT32_MIN / -1 produces0x80000000without trapping. The subsequentsxtwsign-extends this to0xFFFFFFFF80000000= INT32_MIN, which is the correct RISC-V DIVW overflow result.cmp RS2w, 0/cselpattern for divide-by-zero is unchanged.Benchmark
1M chained
divanddivwinstructions (125K iterations × 8 unrolled). Chained dependency serializes divisions to measure handler latency rather than throughput.Environment: Aliyun
ecs.g8y.small, YiTian 710 (1 core), 4 GB RAMdiv_microbench(measures Optimization 1:csel):divw_microbench(measures Optimization 2: 32-bit division; before = post-csel, after = post-32-bit-div):Interpretation
Optimization 1: The 5–6% regression is expected:
cseladdsmov TEMP2, UINT64_MAXto the common (non-zero divisor) path, which the branched version reaches without that instruction. The standard deviation nearly halves (−48.9%) becausecselremoves the rare-but-expensive branch-misprediction tail. The benefit ofcselmaterializes in production code where the divisor can be zero unpredictably.Optimization 2: Median improves by 6.7% and mean by 6.3%. Replacing the two
sxtwinstructions with native 32-bitsdiv/udivshortens the unconditional fast path, producing a clear speed improvement across all runs.