It struck me there must be a clever way to do this. This isn’t for homework, or work or anything. I was just noodling around with a file format that has data interleaved.
So, in generic C/C++, (or whatever) given some array
int x[] = ...
is there a clever way of splitting it into two short arrays
short sa1[], sa2[]
such that the int array is split down the middle
x[i] = 1111111111111111 1111111111111111
sa1[i] sa2[i]
Edit: Sorry if this is not phrased well. For each i-th element of the int array, the left-most 16 bits become the i-th element of one array, and the right-most 16bits become the i-th element of a 2nd array.
so given
x[i] = 0001111111111111 1111111100011111
then
sa1[i] = 0001111111111111
sa2[i] = 1111111100011111
I’m looking for non-obvious answers that do not loop over each element and shift and mask each element. That’s easy 🙂
There’s a lot of ways to do this:
Assumptions:
shortis 16 bits.intis 32 bits.Method 1: (A simple loop)
Method 2: SSE2
This last example is very fast if the loop is further unrolled. I won’t be surprised if this can beat all byte-manipulation libraries.
However, it has the following restrictions:
The first two of these can be solved by cleanup code. It’s messy, but if you really desire performance, it may be worth it.
EDIT:
Yes this violates strict-aliasing, but it’s nearly impossible to use SSE intrinsics without doing so.