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 ffs #875

Merged
merged 7 commits into from
Apr 4, 2024
Merged

⚡️ Optimize ffs #875

merged 7 commits into from
Apr 4, 2024

Conversation

adhusson
Copy link
Contributor

@adhusson adhusson commented Apr 3, 2024

Description

Cheaper FFS using a de Bruijn-like sequence for the first 3 bits of the lsb position.

Works using a sequence where substrings don't appear only once (like in a de Bruijn sequence) but only appear in one given 32-bit block. So (sequence * isolated_bit)[0:5] identifies the first 3 bits of the isolated bit position. Details here.

Also replaced b with x.

Checklist

Ensure you completed all of the steps below before submitting your pull request:

  • Ran forge fmt?
  • Ran forge snapshot?
  • Ran forge test?

Pull requests with an incomplete checklist will be thrown out.

@Vectorized
Copy link
Owner

This is hands down the most impressive PR to Solady I have ever seen.

@atarpara
Copy link
Collaborator

atarpara commented Apr 3, 2024

Very impressive PR. Great work.

@Vectorized Vectorized merged commit 5087f5d into Vectorized:main Apr 4, 2024
@Vectorized
Copy link
Owner

z3 to hunt for the two constants if anyone is interested:

from z3 import *
import math

def lookup(j, l, q):
    j = j & 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
    t = (l * j) & 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
    t = (t >> 250) 
    t = (t << 2) 
    t = (q << t) & 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
    t = (t >> 252) 
    return t

l = BitVec('l', 512) 
q = BitVec('q', 512)

solve(*[
    lookup(1 << i, l, q) == ((int(math.log2(1 << i)) >> 5) & 0b111)
    for i in range(256)
], lookup(1 << 256, l, q) == 0b1000, l >> 256 == 0, q >> 256 == 0)

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