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

Use intrinsic for GetHighestSetBitIndex #7224

Merged
merged 2 commits into from
Jun 27, 2024
Merged

Use intrinsic for GetHighestSetBitIndex #7224

merged 2 commits into from
Jun 27, 2024

Conversation

benaadams
Copy link
Member

Changes

  • Use BitOperations.LeadingZeroCount rather than a bunch of ifs

Types of changes

What types of changes does your code introduce?

  • Optimization

Testing

Requires testing

  • No

@smartprogrammer93
Copy link
Contributor

Benchmarks?

@benaadams
Copy link
Member Author

Benchmarks?

Is single instruction lzcnt vs 6 conditional branches

Before

G_M000_IG03:                ;; offset=0x0010
       movzx    rcx, bl
       test     cl, 128
       je       SHORT G_M000_IG06

G_M000_IG04:                ;; offset=0x0018
       mov      eax, 8

G_M000_IG05:                ;; offset=0x001D
       add      rsp, 32
       pop      rbx
       ret      

G_M000_IG06:                ;; offset=0x0023
       test     cl, 64
       je       SHORT G_M000_IG08
       mov      eax, 7

G_M000_IG07:                ;; offset=0x002D
       add      rsp, 32
       pop      rbx
       ret      

G_M000_IG08:                ;; offset=0x0033
       test     cl, 32
       je       SHORT G_M000_IG10
       mov      eax, 6

G_M000_IG09:                ;; offset=0x003D
       add      rsp, 32
       pop      rbx
       ret      

G_M000_IG10:                ;; offset=0x0043
       test     cl, 16
       je       SHORT G_M000_IG11
       mov      eax, 5
       jmp      SHORT G_M000_IG14

G_M000_IG11:                ;; offset=0x004F
       test     cl, 8
       je       SHORT G_M000_IG12
       mov      eax, 4
       jmp      SHORT G_M000_IG14

G_M000_IG12:                ;; offset=0x005B
       test     cl, 4
       je       SHORT G_M000_IG13
       mov      eax, 3
       jmp      SHORT G_M000_IG14

G_M000_IG13:                ;; offset=0x0067
       mov      eax, 2
       test     cl, 2
       cmove    eax, ecx

G_M000_IG14:                ;; offset=0x0072
       add      rsp, 32
       pop      rbx
       ret

After

G_M000_IG03:                ;; offset=0x0010
       movzx    rax, bl
       lzcnt    eax, eax
       neg      eax
       add      eax, 32

G_M000_IG04:                ;; offset=0x001C
       add      rsp, 32
       pop      rbx
       ret      

@benaadams benaadams merged commit 24f4142 into master Jun 27, 2024
68 checks passed
@benaadams benaadams deleted the bitops branch June 27, 2024 11:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants