-
Notifications
You must be signed in to change notification settings - Fork 171
In emulation mode, u8-u64 should be represented by just int #2173
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
Comments
The "-" is two's complement, so
The "~" is one's complement, so:
And in Python it also does:
The relation between "-" and "~" is:
Using these definitions, we have We have to provide other functions to implement the wraparound if that is needed. |
As a consequence, since
|
Example: this
will not compile. Must be rewritten to:
This is not super easy to type, so we could provide casting from signed to unsigned with wrap around, such as:
Note that in full emulation mode, u16 and i16 is a no-op, but u16_wrap correctly wraps it using 2**16 (a simple implementation). So effectively it becomes:
So @Shaikh-Ubaid, do you want to implement it? |
The error message for:
Could be:
It should offer |
It seems using |
What should be the behaviour for casting a negative integer to unsigned integer in LPython as well as CPython. For example, what should be the output of the following in CPython and LPython. print(u16(-5)) |
Also, do we need to support runtime checks for binary operations on integers as well as unsigned integers? Is it a part of this issue? |
With respect to this #2173 (comment), similar to LPython, should CPython catch and report overflow errors? |
Here #2173 (comment) we discussed about |
I think with merging of #2189 we would complete this issue. |
Perfect! Thank you @Shaikh-Ubaid. |
Currently we created our own class UnsignedInteger and we provide a CPython incompatible behavior of some operators such as ~. Rather, if we need a CPython incompatible behavior, we should introduce some special functions, such as
from lpython import uneg
. The intrinsic operators should work just like in CPython.Here is an example:
This currently prints:
So the
~
is behaving differently for u16 and Python ints. It should not. The design of u16 and i16 is that they are strict value restricted subset of Python ints and should behave exactly identical on this subset, and LPython should give compile time or runtime error messages if any operation is performed outside of this subset.Since in CPython ~255 becomes -256, then the same behavior must happen on ~u16(255). In order to provide the other operation, we can make
uneg(255)
be 65280. Then the operation can be correctly implemented in emulation mode, and all unsigned integers can be just integers, which will speedup operation and remove many bugs that we currently have, when we are passing UnsignedInteger somewhere where a regular integer is expected.I think the current behavior was introduced by trying to emulate C, but Python is not C. We need to be compatible with Python, not C. If C behavior is desired, it must be done using functions like
uneg
, not using intrinsic integer operators (which should rather behave like CPython does).The text was updated successfully, but these errors were encountered: