[vector] Hash code and working with a single shape
Paul Sandoz
paul.sandoz at oracle.com
Fri Mar 16 00:06:29 UTC 2018
Hi,
When thinking about coding to a fixed shape, perhaps defined generically, the hash code of a byte[] is a useful example. Load as many bytes into the byte vector of shape S then operate over it four times in the integer domain for an int vector of shape S.
The challenge is to efficiently transform from the byte vector to the int vector.
Here’s one solution non-generic-in-shape solution:
static int hashCodeVector512Shift(byte[] a) {
int h = 1;
int i = 0;
for (; i < (a.length & ~(BYTE_512_SPECIES.length() - 1)); i += BYTE_512_SPECIES.length()) {
ByteVector<Shapes.S512Bit> b = BYTE_512_SPECIES.fromArray(a, i);
for (int j = 0; j < BYTE_512_SPECIES.length() / INT_512_Species.length(); j++) {
IntVector<Shapes.S512Bit> x = (IntVector<Shapes.S512Bit>) b.cast(INT_512_Species);
h = h * COEFF_31_TO_16 + x.mul(H_COEFF_16).addAll();
b = (ByteVector<Shapes.S512Bit>) b.shiftEL(INT_512_Species.length());
}
}
for (; i < a.length; i++) {
h = 31 * h + a[i];
}
return h;
}
I doubt this is optimal right now, vectorization of the shiftEL (perhaps a swizzle is a better approach), and also vectorization of the cast.
Alternatively masks might be the longer term preferred approach to gather bytes from the byte vector into the int vector. A mask would initially be defined with the first N bits set, the cast accepts the mask, then the mask is shifted so only the next N bits of set. I don’t know how optimal this would be regarding vector instructions.
Thoughts?
Paul.
More information about the panama-dev
mailing list