Skip to content

Use arithmetic primitives from intops#170

Merged
moigagoo merged 21 commits intostatus-im:masterfrom
moigagoo:feature/intops
Feb 9, 2026
Merged

Use arithmetic primitives from intops#170
moigagoo merged 21 commits intostatus-im:masterfrom
moigagoo:feature/intops

Conversation

@moigagoo
Copy link
Copy Markdown
Contributor

No description provided.

@moigagoo moigagoo marked this pull request as ready for review January 17, 2026 12:59
@moigagoo
Copy link
Copy Markdown
Contributor Author

I've removed the entire private/primitives directory and updated all places where functions from its modules had been used so that they used intops instead.

Currently, I'm seeing a ×2 drop in latency, roughly 13k ms to 26k ms (running nim r -d:danger --passC:"-march=native -O3" .\benchmarks\bench.nim).

I'll keep the PR as a draft until I'm able to improve the performance.

@moigagoo moigagoo marked this pull request as draft January 17, 2026 20:07
@moigagoo
Copy link
Copy Markdown
Contributor Author

I've found the source of the performance regression. The reason is that in intops, a pure Nim implementation of an operation is guaranteed to rely only on other pure Nim implementations. For composite ops like mulAcc this effectively omits all optimized implementations.

@moigagoo moigagoo marked this pull request as ready for review January 19, 2026 11:49
@moigagoo
Copy link
Copy Markdown
Contributor Author

@arnetheduck the performance of the original and intops versions is now the same. At least, I can't see any difference in my local benchmarks.

That makes this PR ready for review.

@moigagoo moigagoo mentioned this pull request Jan 22, 2026
5 tasks
# Undo normalization
result = result shr clz

for i in countdown(a.high, 0):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be staticFor to unroll the loop at compile time

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, it looks like we only unroll loops sometimes - let's leave that for a separate PR so that it can be measured appropriately.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arnetheduck made a separate PR from this branch to test this variant: #174

Locally, I'm not seeing any difference in performance (or a slight drop):

$ git co feature/intops
$ nim r -d:danger --passC:"-march=native -O3" .\benchmarks\bench.nim
Modmul (stint): 15112 ms
$ git co feature/intops_staticfor
$ nim r -d:danger --passC:"-march=native -O3" .\benchmarks\bench.nim
Modmul (stint): 15734 ms

Copy link
Copy Markdown
Member

@arnetheduck arnetheduck Jan 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Locally, I'm not seeing any difference in performance

yes, this is somewhat expected in a "simple" / small test since the C compiler will also do unrolling so it will largely depend on the target CPU, C compiler and its options whether it can "understand" an unrolled loop better or not .. the opportunity in "pre-unrolling" here instead of waiting for the C compiler to do it is that it's a targeted unroll for an operation that almost always benefits from it, until general C compiler optimizations that are applied to all loops and therefore tend to be more conservative in their unrolling.

Copy link
Copy Markdown
Contributor Author

@moigagoo moigagoo Mar 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arnetheduck I've added a template the injects staticFor for StInt[4096] and below (can be tuned but seems reasonable) and regular loops for larger StInts.

Locally, I'm still not seeing any improvements but I've decided to push a PR to get it reviewed nonetheless: #175

Comment thread stint/uintops.nim
for i in 0 ..< a.limbs.len:
subB(borrow, diff, a[i], b[i], borrow)
return bool(borrow)
(diff, borrow) = borrowingSub(a[i], b[i], borrow)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we discard the diff here, we should have variations of borrowingSub in intops that only return the carry (since this is the minimal information needed to correctly implement this algo)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've created an issue for that: vacp2p/nim-intops#22

Copy link
Copy Markdown
Member

@arnetheduck arnetheduck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - next step before merging is a PR in nimbus-eth1 that validates this change with the EVM code and the integration tests that run there (they are heavy on stint ops)

moigagoo added a commit to moigagoo/nim-stint that referenced this pull request Jan 29, 2026
@moigagoo
Copy link
Copy Markdown
Contributor Author

moigagoo commented Feb 2, 2026

nimbus-eth1 PR: status-im/nimbus-eth1#3956

@moigagoo
Copy link
Copy Markdown
Contributor Author

moigagoo commented Feb 9, 2026

nimbus-eth2 PR: status-im/nimbus-eth2#7938

@moigagoo moigagoo merged commit b8a63a8 into status-im:master Feb 9, 2026
12 checks passed
moigagoo added a commit to status-im/nimbus-eth1 that referenced this pull request Feb 9, 2026
…on, not forks.

The forks have been merged to the upstream repos, so this is no longer
needed.

- status-im/nim-stint#170
- status-im/nim-bncurve#16
tersec pushed a commit to status-im/nimbus-eth1 that referenced this pull request Feb 10, 2026
…cy (#3956)

* Deps: Add intops as a new dependency, use stint and bncurve forks that use intops.

* Bump copyright year.

* Update stint and bncurve dependencies to point to the status-im version, not forks.

The forks have been merged to the upstream repos, so this is no longer
needed.

- status-im/nim-stint#170
- status-im/nim-bncurve#16

* Update stint and bncurve submodules.

* Replace tabs with spaces.
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.

2 participants