package shared; import jdk.incubator.vector.ByteVector; import jdk.incubator.vector.DoubleVector; import jdk.incubator.vector.FloatVector; import jdk.incubator.vector.IntVector; import jdk.incubator.vector.LongVector; import jdk.incubator.vector.ShortVector; import jdk.incubator.vector.VectorMask; import jdk.incubator.vector.VectorOperators; import jdk.incubator.vector.VectorSpecies; import ml.Cell; /** * Holds SIMD methods. * @author Brian Bushnell * @date Sep 12, 2013 * */ final class SIMD { //Example from https://medium.com/@Styp/java-18-vector-api-do-we-get-free-speed-up-c4510eda50d2 @SuppressWarnings("restriction") private static final VectorSpecies FSPECIES=FloatVector.SPECIES_256;//FloatVector.SPECIES_PREFERRED; //This needs to be final or performance drops. private static final int FWIDTH=FSPECIES.length(); // private static final int boundMask=~(FWIDTH-1); @SuppressWarnings("restriction") private static final VectorSpecies BSPECIES=ByteVector.SPECIES_256; private static final int BWIDTH=BSPECIES.length(); @SuppressWarnings("restriction") private static final VectorSpecies ISPECIES=IntVector.SPECIES_256; private static final int IWIDTH=ISPECIES.length(); @SuppressWarnings("restriction") private static final VectorSpecies SSPECIES=ShortVector.SPECIES_256; private static final int SWIDTH=SSPECIES.length(); @SuppressWarnings("restriction") private static final VectorSpecies DSPECIES=DoubleVector.SPECIES_256; private static final int DWIDTH=DSPECIES.length(); @SuppressWarnings("restriction") private static final VectorSpecies LSPECIES=LongVector.SPECIES_256; private static final int LWIDTH=LSPECIES.length(); @SuppressWarnings("restriction") /** * Vectorized version of "c+=a[i]*b[i]" where a and b are equal-length arrays. * @param a A vector to multiply. * @param b A vector to multiply. * @return Sum of products of vector elements. */ static final float fma(final float[] a, final float[] b){ assert(a.length==b.length); //Note: FSPECIES=FloatVector.SPECIES_256 and FWIDTH=8 final int limit=FSPECIES.loopBound(a.length); FloatVector sum=FloatVector.zero(FSPECIES); int i=0; for(; i=a.length) {return;}//Shortcut when length is divisible by 8. for(int cnum=0; cnum x=v1.eq(v2); matches+=x.trueCount();//This might be slow, or might not } for(; j<=b2; i++, j++) { final byte x=s1[i], y=s2[j]; final int m=((x==y) ? 1 : 0); matches+=m; } return matches; } /** Returns index of symbol */ @SuppressWarnings("restriction") static final int find(final byte[] a, final byte symbol, final int from, final int to){//15% Slower than scalar code, at least for ByteFile1 final int length=to-from;//Intentionally exclusive final int limit0=BSPECIES.loopBound(length); final int limit=from+limit0; int pos=from; for(; pos x=v.eq(symbol); int t=x.firstTrue(); if(tc ? x : c); } return c; } @SuppressWarnings("restriction") /** * Finds the maximum value. * @param a A vector. * @return The max. */ static final long max(final long[] a, final int from, final int to){ final int length=to-from+1; final int limit0=LSPECIES.loopBound(length); final int limit=from+limit0; int i=from; LongVector max=LongVector.broadcast(LSPECIES, a[from]); for(; ic ? x : c); } return c; } @SuppressWarnings("restriction") /** * Finds the maximum value. * @param a A vector. * @return The max. */ static final float max(final float[] a, final int from, final int to){ final int length=to-from+1; final int limit0=FSPECIES.loopBound(length); final int limit=from+limit0; int i=from; FloatVector max=FloatVector.broadcast(FSPECIES, a[from]); for(; ic ? x : c); } return c; } }