Skip to content

Optimize AddMod for small reduced moduli - #222

Open
perfloop-agent wants to merge 2 commits into
holiman:masterfrom
perfloop:perfloop-pr-open-m9z3z5h7be
Open

Optimize AddMod for small reduced moduli#222
perfloop-agent wants to merge 2 commits into
holiman:masterfrom
perfloop:perfloop-pr-open-m9z3z5h7be

Conversation

@perfloop-agent

@perfloop-agent perfloop-agent commented Aug 17, 2026

Copy link
Copy Markdown

Summary

  • Use fixed-width conditional reduction for reduced or near-reduced two- and three-word AddMod operands, plus direct scalar reduction for eligible one-word sums.
  • Keep the wide and division-backed paths for inputs outside those bounds, preserve result aliases, and return zero before the small-modulus dispatch when the modulus is zero.
  • Cover zero and one moduli, limb boundaries, carries, fallbacks, and aliases in the AddMod tests and fuzz seeds.

Benchmark

Workload: AddMod reduced operands / 64-bit modulus

Metric Before After Change
ns/op 11.2 5.43 51.5% lower

Workload: AddMod reduced operands / 128-bit modulus

Metric Before After Change
ns/op 23.11 5.875 74.6% lower

Workload: AddMod reduced operands / 192-bit modulus

Metric Before After Change
ns/op 26.18 5.736 78.1% lower

Workload: AddMod unreduced operand / 64-bit modulus

Metric Before After Change
ns/op 7.825 5.16 34.1% lower

Workload: AddMod operands at the modulus / 64-bit modulus

Metric Before After Change
ns/op 25.09 4.911 80.4% lower

Workload: AddMod operands at the modulus / 128-bit modulus

Metric Before After Change
ns/op 43.17 9.011 79.1% lower

Workload: AddMod operands at the modulus / 192-bit modulus

Metric Before After Change
ns/op 66.23 11.67 82.4% lower

Test plan

  • go test ./... -count=1
  • golangci-lint run
  • go test . -run - -fuzz FuzzAddModSmallModulus -fuzztime 20s
  • go test . -run - -fuzz FuzzTernaryOperations -fuzztime 20s
  • env GOARCH=386 go test -c -o /tmp/uint256.test-386 . && env GOARCH=ppc64 go test -c -o /tmp/uint256.test-ppc64 .

Checks: 5 passed.


Generated by Perfloop. Human sponsor: Tomás Senart. Measurements and checks.

@perfloop-agent
perfloop-agent force-pushed the perfloop-pr-open-m9z3z5h7be branch from 52f381a to a701252 Compare August 17, 2026 22:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant