1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @modules jdk.incubator.vector
  27  */
  28 
  29 import jdk.incubator.vector.FloatVector;
  30 import jdk.incubator.vector.Vector.Shape;
  31 import jdk.incubator.vector.Vector.Species;
  32 import jdk.incubator.vector.Vector;
  33 
  34 import java.util.Arrays;
  35 import java.util.stream.IntStream;
  36 
  37 public class AddTest {
  38     static final Species<Float> SPECIES =
  39             FloatVector.SPECIES_256;
  40 
  41     static final int SIZE = 1024;
  42     static float[] a = new float[SIZE];
  43     static float[] b = new float[SIZE];
  44     static float[] c = new float[SIZE];
  45 
  46     static {
  47         for (int i = 0; i < SIZE; i++) {
  48             a[i] = 1f;
  49             b[i] = 2f;
  50         }
  51     }
  52 
  53     static void workload() {
  54         for (int i = 0; i < a.length; i += SPECIES.length()) {
  55             FloatVector av = FloatVector.fromArray(SPECIES, a, i);
  56             FloatVector bv = FloatVector.fromArray(SPECIES, b, i);
  57             av.add(bv).intoArray(c, i);
  58         }
  59     }
  60 
  61     static final int[] IDENTITY_INDEX_MAPPING = IntStream.range(0, SPECIES.length()).toArray();
  62 
  63     static void workloadIndexMapped() {
  64         for (int i = 0; i < a.length; i += SPECIES.length()) {
  65             FloatVector av = FloatVector.fromArray(SPECIES, a, i, IDENTITY_INDEX_MAPPING, 0);
  66             FloatVector bv = FloatVector.fromArray(SPECIES, b, i, IDENTITY_INDEX_MAPPING, 0);
  67             av.add(bv).intoArray(c, i, IDENTITY_INDEX_MAPPING, 0);
  68         }
  69     }
  70 
  71     public static void main(String args[]) {
  72         for (int i = 0; i < 30_0000; i++) {
  73             workload();
  74         }
  75         for (int i = 0; i < a.length; i++) {
  76             if (c[i] != a[i] + b[i])
  77                 throw new AssertionError();
  78         }
  79 
  80         Arrays.fill(c, 0.0f);
  81 
  82         for (int i = 0; i < 30_0000; i++) {
  83             workloadIndexMapped();
  84         }
  85         for (int i = 0; i < a.length; i++) {
  86             if (c[i] != a[i] + b[i])
  87                 throw new AssertionError();
  88         }
  89     }
  90 }