[pypy-dev] Shift and typed array

Tuom Larsen tuom.larsen at gmail.com
Mon Apr 4 09:10:10 EDT 2016


Hello!

Suppose I'm on 64-bit machine and there is an `a = arrar.array('L',
[0])` (item size is 8 bytes). In Python, when an integer does not fit
machine width it gets promoted to "long" integer of arbitrary size. So
this will fail:

    a[0] = 2**63 << 1

To fix this, one could instead write:

    a[0] = (2**63 << 1) & (2**64 - 1)

My question is, when I know that the result will be stored in
`array.array` anyway, how to prevent the promotion to long integers?
What is the most performat way to perform such calculations? Is PyPy
able to optimize away that `& (2**64 - 1)` when I use `'L'` typecode?

I mean, in C I wouldn't have to worry about it as everything above the
63rd bit will be simply cut off. I would like to help PyPy to generate
the best possible code, does anyone have some suggestions please?

Thanks!


More information about the pypy-dev mailing list