[pypy-dev] Integer arithmetic
guenter.jantzen at netcologne.de
Fri Apr 8 22:16:55 CEST 2005
again I point to C#. Please google for "C# checked" or "C# unchecked".
Maybe it is a good idea to implement a default behaviour, but to give
the user possibilities to control this behaviour.
Hmm, this seems to be exactly what you are proposing to do with
regards from Cologne
Armin Rigo schrieb:
> Hi all,
> (IRC summary with Samuele and myself)
> We propose the following rules for how integer operations should behave in
> R-Python code:
> * operations on plain integers produce longs in CPython when they overflow,
> and wrap-around silently on platforms like C. In other words, they behave
> differently when run on top of Python or after translation. We cannot easily
> change that, but instead the following rule seems to be the sanest idea: in
> both cases, the only guarantee is that the last 32 (or 64) bits of the result
> are correct. If needed we must call a function, intmask(x), to ensure that we
> mask off the high bits on Python. intmask() is a no-op after translation to
> * unsigned arithmetic: the r_uint class is fine for that. It provides
> wrap-around unsigned arithmetic on top of Python, and for the translator it is
> a marker that unsigned ints should be generated. Warning, mixed operations
> r_uint+int produce r_uint results; be careful about unlimited propagation.
> Conversion: r_uint(x) for int-to-uint; intmask(x) for uint-to-int.
> * a new space.uint_w() turns a non-negative wrapped int or long into an
> * overflow detection: only for signed arithmetic, and only explicitely using a
> macro-ish function ovfchecked(). It's only allowed to call it as
> ovfchecked(<variable> <operation> <variable>); no subexpressions. It means
> that this single operation is the one that is done with overflow checking.
> When running on top of Python, ovfchecked(x) just returns x unless it is too
> large, in which case you get an OverflowError. After translation, the two
> basic block operations "z = op(x,y)" and "simple_call(ovfchecked, z)" can be
> detected easily and turned into whatever the back-end does for overflow
> checking. (Additionally, hpk proposed a hack that checks the syntax of the
> ovfchecked() call when running on top of Python, to avoid problems with
> expressions like ovfchecked(x+y+z) where after translation only the last
> operation is checked.)
> * Christian's very nice r_int is on the way out... its main usage, in
> intobject.py, will be replaced by ovfchecked(). It's just more explicit and
> not really longer. It's also more in line with Python's evoluation which says
> that arithmetic operations don't raise OverflowErrors any more.
> Comments welcome!
> pypy-dev at codespeak.net
More information about the Pypy-dev