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

Shortcuts for division modulo 85 #4

Open
DonaldTsang opened this issue Mar 22, 2019 · 2 comments
Open

Shortcuts for division modulo 85 #4

DonaldTsang opened this issue Mar 22, 2019 · 2 comments

Comments

@DonaldTsang
Copy link

DonaldTsang commented Mar 22, 2019

Since 85*3 = 255, why not divmod 255 first? My idea would be

def div255(x):
  q, r = 0, x
  while r > 255:
    q, r = q + (r << 8), (r & 255) + (r << 8)
  return q, r

def fast255(x):
  # first round, r will be smaller than or equal to 4 * 255
  q = (((x >> 24) & 255) << 16) + (((x >> 24) & 255) << 8) + \
  ((x >> 24) & 255) + (((x >> 16) & 255) << 8) + ((x >> 16) & 255) + ((x >> 8) & 255)
  r = ((x >> 24) & 255) + ((x >> 16) & 255) + ((x >> 8) & 255) + (x & 255)
  # second round, r will be smaller than or equal to 255
  q += (((r >> 16) & 255) << 8) + ((r >> 16) & 255) + ((r >> 8) & 255)
  r = (r >> 8) + (r & 255)
  return q, r

def div85(x):
  qx, rx = div255(x)
  q, r = qx * 3 , rx
  while r > 85: # r will never be larger than 255
    q, r = q + (r << 7), (r & 127) + 43 * (r << 7)
  return q, r

def fast85(x):
  # to be written

Would this make the calculation faster (if implemented in C)?

@kstenerud
Copy link
Owner

Hmm those look interesting!

TBH I don't know which would be faster. In traditional architectures, this would be faster, but with the instruction scheduling and specialized math units these days, there'd be no way to tell without actually profiling...

@DonaldTsang DonaldTsang changed the title Idea: Shortcuts for division modulo 85 Shortcuts for division modulo 85 Mar 25, 2019
@DonaldTsang
Copy link
Author

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

No branches or pull requests

2 participants