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

[mono][interp] Intrinsify BitOperations.Log2 #40069

Closed
wants to merge 3 commits into from

Conversation

EgorBo
Copy link
Member

@EgorBo EgorBo commented Jul 29, 2020

It's used in NumberFormatter, Array.Sort and SpanHelpers.

e.g. the following code uses it under the hood:
235235235325.ToString("X")

^ For Mono-Wasm:
before: 18.4 ms (in a loop)
after: 15.2 ms

@ghost
Copy link

ghost commented Jul 29, 2020

Tagging subscribers to this area: @BrzVlad
See info in area-owners.md if you want to be subscribed.

@EgorBo EgorBo requested a review from vargaz July 29, 2020 08:44
@@ -5358,6 +5358,16 @@ interp_exec_method (InterpFrame *frame, ThreadContext *context, FrameClauseArgs
ip++;
MINT_IN_BREAK;
}
MINT_IN_CASE(MINT_INTRINS_BITOPS_LOG2_32) {
sp [-1].data.i = 31 ^ (gint32)__builtin_clz ((guint32)sp [-1].data.i | 1);
Copy link
Member Author

Choose a reason for hiding this comment

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

__builtin_clz uses bsr since we don't set -march=native or -mattr+ https://godbolt.org/z/bMqcM5
lzcnt makes it even faster.

Copy link
Contributor

Choose a reason for hiding this comment

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

This might need an ifdef gcc or something.

Copy link
Member Author

Choose a reason for hiding this comment

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

@vargaz works fine on clang and should be correctly expanded for Arm

Copy link
Member Author

Choose a reason for hiding this comment

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

ah, forgot about MSVC

Copy link
Member

Choose a reason for hiding this comment

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

With MSVC, we can use _BitScan{Forward,Reverse} with this conversion. (can be wrapped in a macro?)

Copy link
Member

Choose a reason for hiding this comment

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

@EgorBo you could extract sgen_clz out of sgen into utils and reuse it here. We have the logic there to use _BitScan on msvc

@EgorBo EgorBo closed this Sep 8, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants