Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uint256: optimize div-related functions by removing unnecessary computation #169

Merged
merged 2 commits into from
May 21, 2024

Conversation

AaronChen0
Copy link
Contributor

@AaronChen0 AaronChen0 commented May 17, 2024

The functions Div, SDiv and MulDivOverflow don't need the remainder, so we change the api of udivrem to pass nil to skip the last part of the computatoin of rem. This PR doesn't affect the computation of quotient quot.

Test

go test ./...
ok  	github.com/holiman/uint256	1.366s

Benchmark

goos: linux
goarch: amd64
pkg: github.com/holiman/uint256
cpu: AMD Ryzen 7 7735H with Radeon Graphics         
                                 │     old      │                 new                 │
                                 │    sec/op    │   sec/op     vs base                │
Div/small/uint256-16                3.105n ± 0%   3.105n ± 1%        ~ (p=0.988 n=10)
Div/mod64/uint256-16                21.57n ± 2%   21.20n ± 2%   -1.72% (p=0.017 n=10)
Div/mod128/uint256-16               38.65n ± 2%   37.28n ± 1%   -3.53% (p=0.000 n=10)
Div/mod192/uint256-16               34.94n ± 1%   32.80n ± 2%   -6.14% (p=0.000 n=10)
Div/mod256/uint256-16               27.29n ± 1%   23.99n ± 1%  -12.08% (p=0.000 n=10)
Mod/small/uint256-16                4.065n ± 2%   4.082n ± 1%        ~ (p=0.617 n=10)
Mod/mod64/uint256-16                27.16n ± 2%   22.75n ± 1%  -16.24% (p=0.000 n=10)
Mod/mod128/uint256-16               40.44n ± 2%   39.97n ± 2%        ~ (p=0.060 n=10)
Mod/mod192/uint256-16               36.69n ± 1%   36.53n ± 0%        ~ (p=0.101 n=10)
Mod/mod256/uint256-16               28.71n ± 1%   28.48n ± 2%   -0.78% (p=0.001 n=10)
AddMod/small/uint256-16             6.372n ± 2%   6.205n ± 2%   -2.63% (p=0.000 n=10)
AddMod/mod64/uint256-16            10.080n ± 1%   9.048n ± 0%  -10.23% (p=0.000 n=10)
AddMod/mod128/uint256-16            18.88n ± 2%   18.15n ± 1%   -3.84% (p=0.000 n=10)
AddMod/mod192/uint256-16            20.69n ± 1%   19.75n ± 0%   -4.54% (p=0.000 n=10)
AddMod/mod256/uint256-16            6.434n ± 1%   6.457n ± 1%   +0.36% (p=0.020 n=10)
MulMod/small/uint256-16             17.61n ± 1%   15.95n ± 1%   -9.40% (p=0.000 n=10)
MulMod/mod64/uint256-16             36.81n ± 2%   29.49n ± 1%  -19.88% (p=0.000 n=10)
MulMod/mod128/uint256-16            56.83n ± 2%   52.33n ± 1%   -7.91% (p=0.000 n=10)
MulMod/mod192/uint256-16            71.45n ± 2%   66.25n ± 1%   -7.28% (p=0.000 n=10)
MulMod/mod256/uint256-16            87.00n ± 1%   86.02n ± 1%        ~ (p=0.190 n=10)
MulMod/mod256/uint256r-16           37.83n ± 2%   37.84n ± 2%        ~ (p=0.839 n=10)
SDiv/large/uint256-16               35.52n ± 1%   33.66n ± 1%   -5.25% (p=0.000 n=10)
MulDivOverflow/small/uint256-16     22.30n ± 2%   21.40n ± 1%   -4.06% (p=0.000 n=10)
MulDivOverflow/div64/uint256-16     27.43n ± 1%   24.33n ± 1%  -11.29% (p=0.000 n=10)
MulDivOverflow/div128/uint256-16    41.56n ± 0%   39.32n ± 2%   -5.40% (p=0.000 n=10)
MulDivOverflow/div192/uint256-16    53.98n ± 1%   50.98n ± 1%   -5.57% (p=0.000 n=10)
MulDivOverflow/div256/uint256-16    69.49n ± 1%   65.30n ± 1%   -6.04% (p=0.000 n=10)
geomean                             24.99n        23.61n        -5.54%

Copy link

codecov bot commented May 17, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (11a325c) to head (21940a9).

Additional details and impacted files
@@            Coverage Diff            @@
##            master      #169   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            5         5           
  Lines         1628      1632    +4     
=========================================
+ Hits          1628      1632    +4     

Copy link
Owner

@holiman holiman left a comment

Choose a reason for hiding this comment

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

LGTM

@holiman holiman merged commit 70cbe2b into holiman:master May 21, 2024
6 checks passed
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