Skip to main content

Implement faster divmod() for moduli with trailing 0 bits

Project description

Info:

This is the README file for ShiftDivMod.

Author:

Shlomi Fish <shlomif@cpan.org>

Date:
2020-09-13
Version:
0.0.2
https://travis-ci.org/shlomif/shift_divmod.svg?branch=master

PURPOSE

This distribution implements faster divmod() (and .mod()) operations for moduli with a large number of trailing 0 bits (where the div/mod base is divisible by 2 ** n for an integer n).

It should yield the same result as the built-n divmod() function for positive numerators (its behaviour for negative ones is currently untested and undefined).

INSTALLATION

pip3 install shift_divmod

USAGE

from shift_divmod import ShiftDivMod

base = 997
shift = 1200
modder = ShiftDivMod(base, shift)
# Alternative constructor which may require more
# work and eventualy calls the default constructor
modder = ShiftDivMod.from_int(base << shift)

x = 10 ** 500
# Same as divmod(x, (base << shift))
print( modder.divmod(x) )

NOTES

The code from which this distribution has been derived, was proposed as a proof-of-concept for a potential improvement for the built in cpython3 operations here: https://bugs.python.org/issue41487 . However, changing cpython3 in this manner was rejected.

libdivide ( https://github.com/ridiculousfish/libdivide ) provides a different, but also interesting, approach for optimizing division.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

shift_divmod-0.0.2.tar.gz (11.5 kB view details)

Uploaded Source

File details

Details for the file shift_divmod-0.0.2.tar.gz.

File metadata

  • Download URL: shift_divmod-0.0.2.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.5

File hashes

Hashes for shift_divmod-0.0.2.tar.gz
Algorithm Hash digest
SHA256 1619425d1a0951f0644bb7f1073e108e6722ced3d2423977753f0d9ecf0c3646
MD5 575b0255ca0166856f605a3bbc146af5
BLAKE2b-256 dbb3bc229ee7a3f50f6c254b95d57b2ef9a97af5b1e42724570e7f7a82a08aa4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page