Species-agnostic shuffle

Martin Traverso mtraverso at gmail.com
Tue Jul 9 22:58:28 UTC 2024


Hi Vladimir,

Not sure I understand how that helps. Can you elaborate?

What I'm looking for is something like this:

private static <E> VectorShuffle<E> makeShuffle(VectorSpecies<E> species,
int[] template)
{
    if (species.length() % template.length != 0) {
        throw new IllegalArgumentException();
    }

    int[] indexes = new int[species.length()];

    for (int i = 0; i < indexes.length; i++) {
        int index = template[i % template.length] + i - i % template.length;
        if (index < 0 || index >= species.length()) {
            throw new IllegalArgumentException();
        }
        indexes[i] = index;
    }

    return VectorShuffle.fromArray(species, indexes, 0);
}

Essentially, you give it a "template", such as {1, 0} to indicate you want
those two lanes rearranged, and the function creates a shuffle array that
replicates that pattern up to the length of the vector for the given
species.

Maybe it's too niche, but without something of that sort it seems
impossible to write code that uses rearrange() and works with
SPECIES_PREFERRED.

- Martin


On Tue, Jul 9, 2024 at 10:33 AM Vladimir Ivanov <
vladimir.x.ivanov at oracle.com> wrote:

> Hi Martin,
>
> What about loading indices as a Vector first (using
> IntVector.fromArray()) and then converting it to VectorShuffle using
> Vector.toShuffle()?
>
> Best regards,
> Vladimir Ivanov
>
> On 7/9/24 09:59, Martin Traverso wrote:
> > Hi,
> >
> > I've been experimenting with the Vector API in an attempt to port the
> > XXH3 hashing algorithm to Java. One of the steps requires rearranging
> > the elements of a vector by swapping adjacent values. I.e., (v0, v1, v2,
> > v3, ...) -> (v1, v0, v3, v2, ...).
> >
> > One usability issue I'm running into is creating the VectorShuffle in a
> > species-agnostic manner. The VectorShuffle.fromArray() expects a number
> > of indices that matches *exactly* the length of the vector for the given
> > species, so it's harder to create a shuffle that uses SPECIES_PREFERRED
> > without additional logic to construct that array of indices dynamically.
> >
> > To illustrate, I need to do the following for each species:
> >
> >      VectorShuffle.fromArray(LongVector.SPECIES_128, new int[] { 1, 0 },
> 0)
> >      VectorShuffle.fromArray(LongVector.SPECIES_256, new int[] { 1, 0,
> > 3, 2 }, 0)
> >      VectorShuffle.fromArray(LongVector.SPECIES_512, new int[] { 1, 0,
> > 3, 2, 5, 4, 7, 6 }, 0)
> >
> > Of course, I could make something like this
> >
> >      VectorShuffle.fromArray(LongVector.SPECIES_PREFERRED,
> > makeIndexesFromTemplate(new int[] { 1, 0 }), 0)
> >
> > with my own custom "makeIndexesFromTemplate" function to create the
> > array of indices of the appropriate length.
> >
> > Is there anything like that in the Vector APIs that I haven't been able
> > to find? If not, would it make sense to add something like it, or allow
> > VectorShuffle.fromXXX to derive such mapping?
> >
> > - Martin
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/panama-dev/attachments/20240709/d21c1d38/attachment.htm>


More information about the panama-dev mailing list