1 /*
   2  * Copyright (c) 2015, 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 /**
  26  * @test
  27  * @bug 8074981
  28  * @summary Add C2 x86 Superword support for scalar product reduction optimizations : int test
  29  *
  30  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+SuperWordReductions -XX:LoopUnrollLimit=250 -XX:CompileThresholdScaling=0.1 ReductionPerf
  31  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-SuperWordReductions -XX:LoopUnrollLimit=250 -XX:CompileThresholdScaling=0.1 ReductionPerf
  32  */
  33 
  34 public class ReductionPerf {
  35   public static void main(String[] args) throws Exception {
  36     int[] a1 = new int[8*1024];
  37     int[] a2 = new int[8*1024];
  38     int[] a3 = new int[8*1024];
  39     long[] b1 = new long[8*1024];
  40     long[] b2 = new long[8*1024];
  41     long[] b3 = new long[8*1024];
  42     float[] c1 = new float[8*1024];
  43     float[] c2 = new float[8*1024];
  44     float[] c3 = new float[8*1024];
  45     double[] d1 = new double[8*1024];
  46     double[] d2 = new double[8*1024];
  47     double[] d3 = new double[8*1024];
  48 
  49     ReductionInit(a1,a2,a3,b1,b2,b3,c1,c2,c3,d1,d2,d3);
  50 
  51     int    sumIv = sumInt(a1,a2,a3);
  52     long   sumLv = sumLong(b1,b2,b3);
  53     float  sumFv = sumFloat(c1,c2,c3);
  54     double sumDv = sumDouble(d1,d2,d3);
  55     int    mulIv = prodInt(a1,a2,a3);
  56     long   mulLv = prodLong(b1,b2,b3);
  57     float  mulFv = prodFloat(c1,c2,c3);
  58     double mulDv = prodDouble(d1,d2,d3);
  59 
  60     int    sumI = 0;
  61     long   sumL = 0;
  62     float  sumF = 0.f;
  63     double sumD = 0.;
  64     int    mulI = 0;
  65     long   mulL = 0;
  66     float  mulF = 0.f;
  67     double mulD = 0.;
  68 
  69     System.out.println("Warmup ...");
  70     long  start = System.currentTimeMillis();
  71 
  72     for(int j = 0; j < 20000; j++) {
  73       sumI = sumInt(a1,a2,a3);
  74       sumL = sumLong(b1,b2,b3);
  75       sumF = sumFloat(c1,c2,c3);
  76       sumD = sumDouble(d1,d2,d3);
  77       mulI = prodInt(a1,a2,a3);
  78       mulL = prodLong(b1,b2,b3);
  79       mulF = prodFloat(c1,c2,c3);
  80       mulD = prodDouble(d1,d2,d3);
  81     }
  82 
  83     long stop = System.currentTimeMillis();
  84     System.out.println(" Warmup is done in " + (stop - start) + " msec");
  85 
  86     if (sumIv != sumI) {
  87       System.out.println("sum int:    " + sumIv + " != " + sumI);
  88     }
  89     if (sumLv != sumL) {
  90       System.out.println("sum long:   " + sumLv + " != " + sumL);
  91     }
  92     if (sumFv != sumF) {
  93       System.out.println("sum float:  " + sumFv + " != " + sumF);
  94     }
  95     if (sumDv != sumD) {
  96       System.out.println("sum double: " + sumDv + " != " + sumD);
  97     }
  98     if (mulIv != mulI) {
  99       System.out.println("prod int:    " + mulIv + " != " + mulI);
 100     }
 101     if (mulLv != mulL) {
 102       System.out.println("prod long:   " + mulLv + " != " + mulL);
 103     }
 104     if (mulFv != mulF) {
 105       System.out.println("prod float:  " + mulFv + " != " + mulF);
 106     }
 107     if (mulDv != mulD) {
 108       System.out.println("prod double: " + mulDv + " != " + mulD);
 109     }
 110 
 111     start = System.currentTimeMillis();
 112     for (int j = 0; j < 50000; j++) {
 113       sumI = sumInt(a1, a2 ,a3);
 114     }
 115     stop = System.currentTimeMillis();
 116     System.out.println("sum int:    " + (stop - start));
 117 
 118     start = System.currentTimeMillis();
 119     for (int j = 0; j < 50000; j++) {
 120       sumL = sumLong(b1, b2, b3);
 121     }
 122     stop = System.currentTimeMillis();
 123     System.out.println("sum long:   " + (stop - start));
 124 
 125     start = System.currentTimeMillis();
 126     for (int j = 0; j < 50000; j++) {
 127       sumF = sumFloat(c1, c2, c3);
 128     }
 129     stop = System.currentTimeMillis();
 130     System.out.println("sum float:  " + (stop - start));
 131 
 132     start = System.currentTimeMillis();
 133     for (int j = 0; j < 50000; j++) {
 134       sumD = sumDouble(d1, d2, d3);
 135     }
 136     stop = System.currentTimeMillis();
 137     System.out.println("sum double: " + (stop - start));
 138 
 139     start = System.currentTimeMillis();
 140     for (int j = 0; j < 50000; j++) {
 141       mulI = prodInt(a1, a2, a3);
 142     }
 143     stop = System.currentTimeMillis();
 144     System.out.println("prod int:    " + (stop - start));
 145 
 146     start = System.currentTimeMillis();
 147     for (int j = 0; j < 50000; j++) {
 148       mulL = prodLong(b1, b2 ,b3);
 149     }
 150     stop = System.currentTimeMillis();
 151     System.out.println("prod long:   " + (stop - start));
 152 
 153     start = System.currentTimeMillis();
 154     for (int j = 0; j < 50000; j++) {
 155       mulF = prodFloat(c1, c2, c3);
 156     }
 157     stop = System.currentTimeMillis();
 158     System.out.println("prod float:  " + (stop - start));
 159 
 160     start = System.currentTimeMillis();
 161     for (int j = 0; j < 50000; j++) {
 162       mulD = prodDouble(d1, d2, d3);
 163     }
 164     stop = System.currentTimeMillis();
 165     System.out.println("prod double: " + (stop - start));
 166 
 167   }
 168 
 169   public static void ReductionInit(int[]    a1, int[]    a2, int[]    a3,
 170                                    long[]   b1, long[]   b2, long[]   b3,
 171                                    float[]  c1, float[]  c2, float[]  c3,
 172                                    double[] d1, double[] d2, double[] d3 ) {
 173     for(int i = 0; i < a1.length; i++) {
 174       a1[i] =          (i + 0);
 175       a2[i] =          (i + 1);
 176       a3[i] =          (i + 2);
 177       b1[i] =   (long) (i + 0);
 178       b2[i] =   (long) (i + 1);
 179       b3[i] =   (long) (i + 2);
 180       c1[i] =  (float) (i + 0);
 181       c2[i] =  (float) (i + 1);
 182       c3[i] =  (float) (i + 2);
 183       d1[i] = (double) (i + 0);
 184       d2[i] = (double) (i + 1);
 185       d3[i] = (double) (i + 2);
 186     }
 187   }
 188 
 189   public static int sumInt(int[] a1, int[] a2, int[] a3) {
 190     int total = 0;
 191     for(int i = 0; i < a1.length; i++) {
 192       total += (a1[i] * a2[i]) + (a1[i] * a3[i]) + (a2[i] * a3[i]);
 193     }
 194     return total;
 195   }
 196 
 197   public static long sumLong(long[] b1, long[] b2, long[] b3) {
 198     long total = 0;
 199     for(int i = 0; i < b1.length; i++) {
 200       total += (b1[i] * b2[i]) + (b1[i] * b3[i]) + (b2[i] * b3[i]);
 201     }
 202     return total;
 203   }
 204 
 205   public static float sumFloat(float[] c1, float[] c2, float[] c3) {
 206     float total = 0;
 207     for(int i = 0; i < c1.length; i++) {
 208       total += (c1[i] * c2[i]) + (c1[i] * c3[i]) + (c2[i] * c3[i]);
 209     }
 210     return total;
 211   }
 212 
 213   public static double sumDouble(double[] d1, double[] d2, double[] d3) {
 214     double total = 0;
 215     for(int i = 0; i < d1.length; i++) {
 216       total += (d1[i] * d2[i]) + (d1[i] * d3[i]) + (d2[i] * d3[i]);
 217     }
 218     return total;
 219   }
 220 
 221   public static int prodInt(int[] a1, int[] a2, int[] a3) {
 222     int total = 1;
 223     for(int i = 0; i < a1.length; i++) {
 224       total *= (a1[i] * a2[i]) + (a1[i] * a3[i]) + (a2[i] * a3[i]);
 225     }
 226     return total;
 227   }
 228 
 229   public static long prodLong(long[] b1, long[] b2, long[] b3) {
 230     long total = 1;
 231     for(int i = 0; i < b1.length; i++) {
 232       total *= (b1[i] * b2[i]) + (b1[i] * b3[i]) + (b2[i] * b3[i]);
 233     }
 234     return total;
 235   }
 236 
 237   public static float prodFloat(float[] c1, float[] c2, float[] c3) {
 238     float total = 1;
 239     for(int i = 0; i < c1.length; i++) {
 240       total *= (c1[i] * c2[i]) + (c1[i] * c3[i]) + (c2[i] * c3[i]);
 241     }
 242     return total;
 243   }
 244 
 245   public static double prodDouble(double[] d1, double[] d2, double[] d3) {
 246     double total = 1;
 247     for(int i = 0; i < d1.length; i++) {
 248       total *= (d1[i] * d2[i]) + (d1[i] * d3[i]) + (d2[i] * d3[i]);
 249     }
 250     return total;
 251   }
 252 }