import jdk.incubator.vector.*;

public class TestReverse {
    private static final VectorSpecies<Integer> SPECIES_I    = IntVector.SPECIES_PREFERRED;

    public static void main(String[] args) {
        System.out.println("Warmup");
        int[] aI = new int[100_000];
        int[] rI = new int[100_000];
        for (int i = 0; i < 10_000; i++) {
            test(aI, rI);
        }
        long t0 = System.nanoTime();
        System.out.println("Benchmark");
        for (int i = 0; i < 100_000; i++) {
            test(aI, rI);
        }
        long t1 = System.nanoTime();
        System.out.println("Elapsed: " + (t1 - t0));
    }

    private static final VectorShuffle<Integer> REVERSE_SHUFFLE_I = SPECIES_I.iotaShuffle(SPECIES_I.length()-1, -1, true);

    public static Object test(int[] a, int[] r) {
        // Note: to avoid the random alignment/misalignment issues, which can lead to 2x perf differences,
        //       I just start the iteration at element 1, which means we are always misaligned and get
        //       consistent performance
        int i = 1;
        for (; i < SPECIES_I.loopBound(a.length-1); i += SPECIES_I.length()) {
            IntVector v = IntVector.fromArray(SPECIES_I, a, i);
            v = v.rearrange(REVERSE_SHUFFLE_I);
            v.intoArray(r, r.length - SPECIES_I.length() - i);
        }
        // I commented out the tail to keep the example simple.
        //for (; i < a.length; i++) {
        //    r[a.length - i - 1] = a[i];
        //}
        return r;
    }

}
