How to view-cast / reinterpret-cast in pythran / numpy?

228 views Asked by At

I'm trying to do numpy view-casting (which I believe in C/C++ land would be called reinterpret-casting) in pythran:

The following silly made-up example takes an array of unsigned 8-byte integers, reinterprets them as twice as many unsigned 4-byte integers, slices off the first and last (this also does not touch the actual data; it only changes the "base pointer") and reinterprets as unsigned 8-byte again, the total effect being a frame shift. (We'll worry about endianness some other time.)

import numpy as np

A = np.arange(5,dtype="u8")
a = A.view("u4")
B = a[1:9].view("u8")
A
# array([0, 1, 2, 3, 4], dtype=uint64)
B
# array([ 4294967296,  8589934592, 12884901888, 17179869184], dtype=uint64)
np.shares_memory(A,B)
# True

I cannot have pythran translate this directly because it doesn't know the .view attribute.

Is there a way to reinterpret cast arrays in pythran?

1

There are 1 answers

2
Luketl98 On

As far as I can see there is no direct way to perform reinterpret casting of arrays in Pythran. Pythran doesn't support the numpy.view function, and there isn't a direct equivalent that can be used instead. Its just a limitation of Pythran as it only supports a subset of numpy's functionality.

Your best bet is probably to perform the casting in Python using numpy, then pass the result to the Pythran function. that could be feasible if the casting operation isn't a major bottleneck in your code.

Or you could use a different compiler such as Cython if you're familiar with one.