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

Optimize integer arithmetic #7553

Merged
merged 7 commits into from
Aug 21, 2023
Merged

Conversation

bjorng
Copy link
Contributor

@bjorng bjorng commented Aug 11, 2023

This pull request optimizes some of the arithmetic operations for integers.

The most noticeable improvement is in multiplication of large integers. Consider this benchmark:

bench() ->
    Size = 1_262_000,
    String = binary:copy(<<"9">>, Size),
    {Time, _Val} = timer:tc(erlang, binary_to_integer, [String]),
    io:format("Size: ~p, seconds: ~p\n", [Size, Time / 1_000_000]).

Its running time is dominated by multiplication of large integers. This PR reduces the running time from about 0.45 seconds down to about 0.32 seconds on my computer (an M1 MacBook Pro).

@bjorng bjorng added team:VM Assigned to OTP team VM enhancement testing currently being tested, tag is used by OTP internal CI labels Aug 11, 2023
@bjorng bjorng self-assigned this Aug 11, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Aug 11, 2023

CT Test Results

       3 files     133 suites   48m 53s ⏱️
1 559 tests 1 503 ✔️ 54 💤 2
1 980 runs  1 905 ✔️ 73 💤 2

For more details on these failures, see this check.

Results for commit 4ca352e.

♻️ This comment has been updated with latest results.

To speed up review, make sure that you have read Contributing to Erlang/OTP and that all checks pass.

See the TESTING and DEVELOPMENT HowTo guides for details about how to run test locally.

Artifacts

// Erlang/OTP Github Action Bot

Copy link
Contributor

@jhogberg jhogberg left a comment

Choose a reason for hiding this comment

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

LGTM

erts/emulator/beam/jit/arm/beam_asm.hpp Outdated Show resolved Hide resolved
erts/emulator/beam/jit/arm/instr_arith.cpp Outdated Show resolved Hide resolved
erts/emulator/beam/jit/arm/instr_arith.cpp Outdated Show resolved Hide resolved
erts/emulator/beam/jit/arm/instr_arith.cpp Outdated Show resolved Hide resolved
erts/emulator/beam/jit/x86/instr_arith.cpp Show resolved Hide resolved
erts/emulator/beam/jit/x86/instr_arith.cpp Outdated Show resolved Hide resolved
@bjorng bjorng force-pushed the bjorn/jit/integer-arithmetic branch 2 times, most recently from 36c0ff3 to be5196d Compare August 14, 2023 09:31
@bjorng bjorng force-pushed the bjorn/jit/integer-arithmetic branch 3 times, most recently from e1e28c3 to 0da399f Compare August 15, 2023 13:07
@bjorng bjorng removed the testing currently being tested, tag is used by OTP internal CI label Aug 18, 2023
Fuse a multiplication operator followed by an addition operator.
That will generally reduce the number of instructions compared
to having separate operators.
We used to replace division by a power of two with a right shift
only when the dividend was known to be a positive integer. Extend
the implementation to do right shift when the range of the dividend
is unknown.
Inline the code for right shift a small operand any number steps.
We used to call a helper routine when the shift count exceeded the
number of bits in a small.
The routine for squaring a big integer did not have all optimizations
that the multiplication routine had.
This commit implements the Karatsuba algorithm in a way
that reduces the number of additions, resulting in a measureable
performance improvement for multiplication of large integers.
@bjorng bjorng force-pushed the bjorn/jit/integer-arithmetic branch from 0da399f to 4ca352e Compare August 18, 2023 11:25
@bjorng bjorng added the testing currently being tested, tag is used by OTP internal CI label Aug 18, 2023
@bjorng bjorng merged commit 272e678 into erlang:master Aug 21, 2023
14 of 16 checks passed
@bjorng bjorng deleted the bjorn/jit/integer-arithmetic branch August 21, 2023 08:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement team:VM Assigned to OTP team VM testing currently being tested, tag is used by OTP internal CI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants