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  * Tests for MIN/MAX vectorization.
     19  */
     20 public class Main {
     21 
     22   /// CHECK-START: void Main.doitMin(long[], long[], long[]) loop_optimization (before)
     23   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                 loop:<<Loop:B\d+>> outer_loop:none
     24   /// CHECK-DAG: <<Get1:j\d+>> ArrayGet                            loop:<<Loop>>      outer_loop:none
     25   /// CHECK-DAG: <<Get2:j\d+>> ArrayGet                            loop:<<Loop>>      outer_loop:none
     26   /// CHECK-DAG: <<Min:j\d+>>  InvokeStaticOrDirect [<<Get1>>,<<Get2>>] intrinsic:MathMinLongLong loop:<<Loop>> outer_loop:none
     27   /// CHECK-DAG:               ArraySet [{{l\d+}},<<Phi>>,<<Min>>] loop:<<Loop>>      outer_loop:none
     28   //
     29   // Not directly supported for longs.
     30   //
     31   /// CHECK-START-ARM64: void Main.doitMin(long[], long[], long[]) loop_optimization (after)
     32   /// CHECK-NOT: VecMin
     33   //
     34   /// CHECK-START-MIPS64: void Main.doitMin(long[], long[], long[]) loop_optimization (after)
     35   /// CHECK-DAG: <<Get1:d\d+>> VecLoad                              loop:<<Loop:B\d+>> outer_loop:none
     36   /// CHECK-DAG: <<Get2:d\d+>> VecLoad                              loop:<<Loop>>      outer_loop:none
     37   /// CHECK-DAG: <<Min:d\d+>>  VecMin [<<Get1>>,<<Get2>>]           loop:<<Loop>>      outer_loop:none
     38   /// CHECK-DAG:               VecStore [{{l\d+}},{{i\d+}},<<Min>>] loop:<<Loop>>      outer_loop:none
     39 
     40   private static void doitMin(long[] x, long[] y, long[] z) {
     41     int min = Math.min(x.length, Math.min(y.length, z.length));
     42     for (int i = 0; i < min; i++) {
     43       x[i] = Math.min(y[i], z[i]);
     44     }
     45   }
     46 
     47   /// CHECK-START: void Main.doitMax(long[], long[], long[]) loop_optimization (before)
     48   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                 loop:<<Loop:B\d+>> outer_loop:none
     49   /// CHECK-DAG: <<Get1:j\d+>> ArrayGet                            loop:<<Loop>>      outer_loop:none
     50   /// CHECK-DAG: <<Get2:j\d+>> ArrayGet                            loop:<<Loop>>      outer_loop:none
     51   /// CHECK-DAG: <<Max:j\d+>>  InvokeStaticOrDirect [<<Get1>>,<<Get2>>] intrinsic:MathMaxLongLong loop:<<Loop>> outer_loop:none
     52   /// CHECK-DAG:               ArraySet [{{l\d+}},<<Phi>>,<<Max>>] loop:<<Loop>>      outer_loop:none
     53   //
     54   // Not directly supported for longs.
     55   //
     56   /// CHECK-START-ARM64: void Main.doitMax(long[], long[], long[]) loop_optimization (after)
     57   /// CHECK-NOT: VecMax
     58   //
     59   /// CHECK-START-MIPS64: void Main.doitMax(long[], long[], long[]) loop_optimization (after)
     60   /// CHECK-DAG: <<Get1:d\d+>> VecLoad                              loop:<<Loop:B\d+>> outer_loop:none
     61   /// CHECK-DAG: <<Get2:d\d+>> VecLoad                              loop:<<Loop>>      outer_loop:none
     62   /// CHECK-DAG: <<Max:d\d+>>  VecMax [<<Get1>>,<<Get2>>]           loop:<<Loop>>      outer_loop:none
     63   /// CHECK-DAG:               VecStore [{{l\d+}},{{i\d+}},<<Max>>] loop:<<Loop>>      outer_loop:none
     64   private static void doitMax(long[] x, long[] y, long[] z) {
     65     int min = Math.min(x.length, Math.min(y.length, z.length));
     66     for (int i = 0; i < min; i++) {
     67       x[i] = Math.max(y[i], z[i]);
     68     }
     69   }
     70 
     71   public static void main(String[] args) {
     72     long[] interesting = {
     73       0x0000000000000000L, 0x0000000000000001L, 0x000000007fffffffL,
     74       0x0000000080000000L, 0x0000000080000001L, 0x00000000ffffffffL,
     75       0x0000000100000000L, 0x0000000100000001L, 0x000000017fffffffL,
     76       0x0000000180000000L, 0x0000000180000001L, 0x00000001ffffffffL,
     77       0x7fffffff00000000L, 0x7fffffff00000001L, 0x7fffffff7fffffffL,
     78       0x7fffffff80000000L, 0x7fffffff80000001L, 0x7fffffffffffffffL,
     79       0x8000000000000000L, 0x8000000000000001L, 0x800000007fffffffL,
     80       0x8000000080000000L, 0x8000000080000001L, 0x80000000ffffffffL,
     81       0x8000000100000000L, 0x8000000100000001L, 0x800000017fffffffL,
     82       0x8000000180000000L, 0x8000000180000001L, 0x80000001ffffffffL,
     83       0xffffffff00000000L, 0xffffffff00000001L, 0xffffffff7fffffffL,
     84       0xffffffff80000000L, 0xffffffff80000001L, 0xffffffffffffffffL
     85     };
     86     // Initialize cross-values for the interesting values.
     87     int total = interesting.length * interesting.length;
     88     long[] x = new long[total];
     89     long[] y = new long[total];
     90     long[] z = new long[total];
     91     int k = 0;
     92     for (int i = 0; i < interesting.length; i++) {
     93       for (int j = 0; j < interesting.length; j++) {
     94         x[k] = 0;
     95         y[k] = interesting[i];
     96         z[k] = interesting[j];
     97         k++;
     98       }
     99     }
    100 
    101     // And test.
    102     doitMin(x, y, z);
    103     for (int i = 0; i < total; i++) {
    104       long expected = Math.min(y[i], z[i]);
    105       expectEquals(expected, x[i]);
    106     }
    107     doitMax(x, y, z);
    108     for (int i = 0; i < total; i++) {
    109       long expected = Math.max(y[i], z[i]);
    110       expectEquals(expected, x[i]);
    111     }
    112 
    113     System.out.println("passed");
    114   }
    115 
    116   private static void expectEquals(long expected, long result) {
    117     if (expected != result) {
    118       throw new Error("Expected: " + expected + ", found: " + result);
    119     }
    120   }
    121 }
    122