GithubHelp home page GithubHelp logo

alexaltea / ntypes Goto Github PK

View Code? Open in Web Editor NEW
20.0 20.0 2.0 70 KB

Emulate native integer and floating-point types in Python

Home Page: https://pypi.python.org/pypi/nativetypes

License: MIT License

Python 99.51% Batchfile 0.28% Shell 0.21%

ntypes's People

Contributors

alexaltea avatar chaoses-ib avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

chaoses-ib

ntypes's Issues

inplace operators are slooooow

Hi !

I am trying to use your module to play with a Feistel cipher and noticed that at each iteration, the code gets slower.
I figured that using inplace operators was the problem.

I came up with a minimal example showing the difference between inplace and "normal" operators:

This code is getting slower after a few iterations:

from nativetypes import *
x = uint32(1)
while True:
    print(x)
    x *= x

Replacing the inplace operator gets a significant speed improvement:

from nativetypes import *
x = uint32(1)
while True:
    print(x)
    x = x * x

From what I understand, this happens because the following code assumes that the op result is an integer, where it is in fact another nint object. This leads to nested nint objects that slow down the operations.

def op_binary_inplace(self, value, op):
result_int = op(self.v, value)
self.set(result_int)
return self

Unable to get slices including the MSB

It is not possible to get a slice from a nint including the most significant bit:

>>> from nativetypes import uint8
>>> u8=uint8(1<<7)
>>> bin(u8)
'0b10000000'
>>> u = u8[7:8]
Traceback (most recent call last):
  File "native_int.py", line 121, in __getitem__
    assert 0 <= key.stop < self.b
AssertionError

IMO the assertion in __getitem__ is wrong and should be instead

assert 0 <= key.stop <= self.b

since the last element index is actually key.stop-1 in Python

This seems to work:

>>> from nativetypes import uint8
>>> u8=uint8(1<<7)
>>> bin(u8)
'0b10000000'
>>> u = u8[7:8]
>>> u
nint(1)

Implementing the C-style modulo

Would be great to implement the C-style modulo (the one that applies the sign of the dividend to the result) along with the Python-style modulo, because the vast majority of low-level languages use that implementation.

I mean to define a modulo operation like this:

def mod(a, n):
    return a - int(a / n) * n

Or maybe this one, less readable you know the operator priorities in Python:

def mod(a, n):
    return a - a // n * n

The thing is that I don't know how to implement that in your code so it becomes trivial to use. The __mod__ operator is already used for the python operator.mod. I don't know if is sane to add a new method to the class or is better to replace the current one.


Off-topic: Thank you! I started a week ago to write my int32 class for a ported code, and today I noticed that this code already existed. I'll migrate all now. Surprisingly our coding style is very similar. ๐Ÿ˜„

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.