Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 /**
     18  * Functional tests for SIMD vectorization. Note that this class provides a mere
     19  * functional test, not a precise numerical verifier.
     20  */
     21 public class Main {
     22 
     23   static double[] a;
     24 
     25   //
     26   // Arithmetic operations.
     27   //
     28 
     29   /// CHECK-START: void Main.add(double) loop_optimization (before)
     30   /// CHECK-DAG: Phi      loop:<<Loop:B\d+>> outer_loop:none
     31   /// CHECK-DAG: ArrayGet loop:<<Loop>>      outer_loop:none
     32   /// CHECK-DAG: ArraySet loop:<<Loop>>      outer_loop:none
     33   //
     34   /// CHECK-START-ARM64: void Main.add(double) loop_optimization (after)
     35   /// CHECK-DAG: Phi      loop:<<Loop:B\d+>> outer_loop:none
     36   /// CHECK-DAG: VecLoad  loop:<<Loop>>      outer_loop:none
     37   /// CHECK-DAG: VecAdd   loop:<<Loop>>      outer_loop:none
     38   /// CHECK-DAG: VecStore loop:<<Loop>>      outer_loop:none
     39   static void add(double x) {
     40     for (int i = 0; i < 128; i++)
     41       a[i] += x;
     42   }
     43 
     44   /// CHECK-START: void Main.sub(double) loop_optimization (before)
     45   /// CHECK-DAG: Phi      loop:<<Loop:B\d+>> outer_loop:none
     46   /// CHECK-DAG: ArrayGet loop:<<Loop>>      outer_loop:none
     47   /// CHECK-DAG: ArraySet loop:<<Loop>>      outer_loop:none
     48   //
     49   /// CHECK-START-ARM64: void Main.sub(double) loop_optimization (after)
     50   /// CHECK-DAG: Phi      loop:<<Loop:B\d+>> outer_loop:none
     51   /// CHECK-DAG: VecLoad  loop:<<Loop>>      outer_loop:none
     52   /// CHECK-DAG: VecSub   loop:<<Loop>>      outer_loop:none
     53   /// CHECK-DAG: VecStore loop:<<Loop>>      outer_loop:none
     54   static void sub(double x) {
     55     for (int i = 0; i < 128; i++)
     56       a[i] -= x;
     57   }
     58 
     59   /// CHECK-START: void Main.mul(double) loop_optimization (before)
     60   /// CHECK-DAG: Phi      loop:<<Loop:B\d+>> outer_loop:none
     61   /// CHECK-DAG: ArrayGet loop:<<Loop>>      outer_loop:none
     62   /// CHECK-DAG: ArraySet loop:<<Loop>>      outer_loop:none
     63   //
     64   /// CHECK-START-ARM64: void Main.mul(double) loop_optimization (after)
     65   /// CHECK-DAG: Phi      loop:<<Loop:B\d+>> outer_loop:none
     66   /// CHECK-DAG: VecLoad  loop:<<Loop>>      outer_loop:none
     67   /// CHECK-DAG: VecMul   loop:<<Loop>>      outer_loop:none
     68   /// CHECK-DAG: VecStore loop:<<Loop>>      outer_loop:none
     69   static void mul(double x) {
     70     for (int i = 0; i < 128; i++)
     71       a[i] *= x;
     72   }
     73 
     74   /// CHECK-START: void Main.div(double) loop_optimization (before)
     75   /// CHECK-DAG: Phi      loop:<<Loop:B\d+>> outer_loop:none
     76   /// CHECK-DAG: ArrayGet loop:<<Loop>>      outer_loop:none
     77   /// CHECK-DAG: ArraySet loop:<<Loop>>      outer_loop:none
     78   //
     79   /// CHECK-START-ARM64: void Main.div(double) loop_optimization (after)
     80   /// CHECK-DAG: Phi      loop:<<Loop:B\d+>> outer_loop:none
     81   /// CHECK-DAG: VecLoad  loop:<<Loop>>      outer_loop:none
     82   /// CHECK-DAG: VecDiv   loop:<<Loop>>      outer_loop:none
     83   /// CHECK-DAG: VecStore loop:<<Loop>>      outer_loop:none
     84   static void div(double x) {
     85     for (int i = 0; i < 128; i++)
     86       a[i] /= x;
     87   }
     88 
     89   /// CHECK-START: void Main.neg() loop_optimization (before)
     90   /// CHECK-DAG: Phi      loop:<<Loop:B\d+>> outer_loop:none
     91   /// CHECK-DAG: ArrayGet loop:<<Loop>>      outer_loop:none
     92   /// CHECK-DAG: ArraySet loop:<<Loop>>      outer_loop:none
     93   //
     94   /// CHECK-START-ARM64: void Main.neg() loop_optimization (after)
     95   /// CHECK-DAG: Phi      loop:<<Loop:B\d+>> outer_loop:none
     96   /// CHECK-DAG: VecLoad  loop:<<Loop>>      outer_loop:none
     97   /// CHECK-DAG: VecNeg   loop:<<Loop>>      outer_loop:none
     98   /// CHECK-DAG: VecStore loop:<<Loop>>      outer_loop:none
     99   static void neg() {
    100     for (int i = 0; i < 128; i++)
    101       a[i] = -a[i];
    102   }
    103 
    104   /// CHECK-START: void Main.abs() loop_optimization (before)
    105   /// CHECK-DAG: Phi      loop:<<Loop:B\d+>> outer_loop:none
    106   /// CHECK-DAG: ArrayGet loop:<<Loop>>      outer_loop:none
    107   /// CHECK-DAG: ArraySet loop:<<Loop>>      outer_loop:none
    108   //
    109   /// CHECK-START-ARM64: void Main.abs() loop_optimization (after)
    110   /// CHECK-DAG: Phi      loop:<<Loop:B\d+>> outer_loop:none
    111   /// CHECK-DAG: VecLoad  loop:<<Loop>>      outer_loop:none
    112   /// CHECK-DAG: VecAbs   loop:<<Loop>>      outer_loop:none
    113   /// CHECK-DAG: VecStore loop:<<Loop>>      outer_loop:none
    114   static void abs() {
    115     for (int i = 0; i < 128; i++)
    116       a[i] = Math.abs(a[i]);
    117   }
    118 
    119   /// CHECK-START: void Main.conv(long[]) loop_optimization (before)
    120   /// CHECK-DAG: Phi      loop:<<Loop:B\d+>> outer_loop:none
    121   /// CHECK-DAG: ArrayGet loop:<<Loop>>      outer_loop:none
    122   /// CHECK-DAG: ArraySet loop:<<Loop>>      outer_loop:none
    123   //
    124   /// CHECK-START-ARM64: void Main.conv(long[]) loop_optimization (after)
    125   //
    126   // TODO: fill in when supported
    127   static void conv(long[] b) {
    128     for (int i = 0; i < 128; i++)
    129       a[i] = b[i];
    130   }
    131 
    132   //
    133   // Loop bounds.
    134   //
    135 
    136   static void bounds() {
    137     for (int i = 1; i < 127; i++)
    138       a[i] += 11;
    139   }
    140 
    141   //
    142   // Test Driver.
    143   //
    144 
    145   public static void main(String[] args) {
    146     // Set up.
    147     a = new double[128];
    148     for (int i = 0; i < 128; i++) {
    149       a[i] = i;
    150     }
    151     // Arithmetic operations.
    152     add(2.0);
    153     for (int i = 0; i < 128; i++) {
    154       expectEquals(i + 2, a[i], "add");
    155     }
    156     sub(2.0);
    157     for (int i = 0; i < 128; i++) {
    158       expectEquals(i, a[i], "sub");
    159     }
    160     mul(2.0);
    161     for (int i = 0; i < 128; i++) {
    162       expectEquals(i + i, a[i], "mul");
    163     }
    164     div(2.0);
    165     for (int i = 0; i < 128; i++) {
    166       expectEquals(i, a[i], "div");
    167     }
    168     neg();
    169     for (int i = 0; i < 128; i++) {
    170       expectEquals(-i, a[i], "neg");
    171     }
    172     // Loop bounds.
    173     bounds();
    174     expectEquals(0, a[0], "bounds0");
    175     for (int i = 1; i < 127; i++) {
    176       expectEquals(11 - i, a[i], "bounds");
    177     }
    178     expectEquals(-127, a[127], "bounds127");
    179     // Abs.
    180     abs();
    181     expectEquals(0, a[0], "abs0");
    182     for (int i = 1; i <= 11; i++) {
    183       expectEquals(11 - i, a[i], "abs_lo");
    184     }
    185     for (int i = 12; i < 127; i++) {
    186       expectEquals(i - 11, a[i], "abs_hi");
    187     }
    188     expectEquals(127, a[127], "abs127");
    189     // Conversion.
    190     long[] b = new long[128];
    191     for (int i = 0; i < 128; i++) {
    192       b[i] = 1000 * i;
    193     }
    194     conv(b);
    195     for (int i = 1; i < 127; i++) {
    196       expectEquals(1000.0 * i, a[i], "conv");
    197     }
    198     // Done.
    199     System.out.println("passed");
    200   }
    201 
    202   private static void expectEquals(double expected, double result, String action) {
    203     if (expected != result) {
    204       throw new Error("Expected: " + expected + ", found: " + result + " for " + action);
    205     }
    206   }
    207 }
    208