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(int[], int[], int[]) loop_optimization (before)
23   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                 loop:<<Loop:B\d+>> outer_loop:none
24   /// CHECK-DAG: <<Get1:i\d+>> ArrayGet                            loop:<<Loop>>      outer_loop:none
25   /// CHECK-DAG: <<Get2:i\d+>> ArrayGet                            loop:<<Loop>>      outer_loop:none
26   /// CHECK-DAG: <<Min:i\d+>>  InvokeStaticOrDirect [<<Get1>>,<<Get2>>] intrinsic:MathMinIntInt loop:<<Loop>> outer_loop:none
27   /// CHECK-DAG:               ArraySet [{{l\d+}},<<Phi>>,<<Min>>] loop:<<Loop>>      outer_loop:none
28   //
29   /// CHECK-START-{ARM,ARM64,MIPS64}: void Main.doitMin(int[], int[], int[]) loop_optimization (after)
30   /// CHECK-DAG: <<Get1:d\d+>> VecLoad                                      loop:<<Loop:B\d+>> outer_loop:none
31   /// CHECK-DAG: <<Get2:d\d+>> VecLoad                                      loop:<<Loop>>      outer_loop:none
32   /// CHECK-DAG: <<Min:d\d+>>  VecMin [<<Get1>>,<<Get2>>] packed_type:Int32 loop:<<Loop>>      outer_loop:none
33   /// CHECK-DAG:               VecStore [{{l\d+}},{{i\d+}},<<Min>>]         loop:<<Loop>>      outer_loop:none
doitMin(int[] x, int[] y, int[] z)34   private static void doitMin(int[] x, int[] y, int[] z) {
35     int min = Math.min(x.length, Math.min(y.length, z.length));
36     for (int i = 0; i < min; i++) {
37       x[i] = Math.min(y[i], z[i]);
38     }
39   }
40 
41   /// CHECK-START: void Main.doitMax(int[], int[], int[]) loop_optimization (before)
42   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                 loop:<<Loop:B\d+>> outer_loop:none
43   /// CHECK-DAG: <<Get1:i\d+>> ArrayGet                            loop:<<Loop>>      outer_loop:none
44   /// CHECK-DAG: <<Get2:i\d+>> ArrayGet                            loop:<<Loop>>      outer_loop:none
45   /// CHECK-DAG: <<Max:i\d+>>  InvokeStaticOrDirect [<<Get1>>,<<Get2>>] intrinsic:MathMaxIntInt loop:<<Loop>> outer_loop:none
46   /// CHECK-DAG:               ArraySet [{{l\d+}},<<Phi>>,<<Max>>] loop:<<Loop>>      outer_loop:none
47   //
48   /// CHECK-START-{ARM,ARM64,MIPS64}: void Main.doitMax(int[], int[], int[]) loop_optimization (after)
49   /// CHECK-DAG: <<Get1:d\d+>> VecLoad                                      loop:<<Loop:B\d+>> outer_loop:none
50   /// CHECK-DAG: <<Get2:d\d+>> VecLoad                                      loop:<<Loop>>      outer_loop:none
51   /// CHECK-DAG: <<Max:d\d+>>  VecMax [<<Get1>>,<<Get2>>] packed_type:Int32 loop:<<Loop>>      outer_loop:none
52   /// CHECK-DAG:               VecStore [{{l\d+}},{{i\d+}},<<Max>>]         loop:<<Loop>>      outer_loop:none
doitMax(int[] x, int[] y, int[] z)53   private static void doitMax(int[] x, int[] y, int[] z) {
54     int min = Math.min(x.length, Math.min(y.length, z.length));
55     for (int i = 0; i < min; i++) {
56       x[i] = Math.max(y[i], z[i]);
57     }
58   }
59 
main(String[] args)60   public static void main(String[] args) {
61     int[] interesting = {
62       0x00000000, 0x00000001, 0x00007fff, 0x00008000, 0x00008001, 0x0000ffff,
63       0x00010000, 0x00010001, 0x00017fff, 0x00018000, 0x00018001, 0x0001ffff,
64       0x7fff0000, 0x7fff0001, 0x7fff7fff, 0x7fff8000, 0x7fff8001, 0x7fffffff,
65       0x80000000, 0x80000001, 0x80007fff, 0x80008000, 0x80008001, 0x8000ffff,
66       0x80010000, 0x80010001, 0x80017fff, 0x80018000, 0x80018001, 0x8001ffff,
67       0xffff0000, 0xffff0001, 0xffff7fff, 0xffff8000, 0xffff8001, 0xffffffff
68     };
69     // Initialize cross-values for the interesting values.
70     int total = interesting.length * interesting.length;
71     int[] x = new int[total];
72     int[] y = new int[total];
73     int[] z = new int[total];
74     int k = 0;
75     for (int i = 0; i < interesting.length; i++) {
76       for (int j = 0; j < interesting.length; j++) {
77         x[k] = 0;
78         y[k] = interesting[i];
79         z[k] = interesting[j];
80         k++;
81       }
82     }
83 
84     // And test.
85     doitMin(x, y, z);
86     for (int i = 0; i < total; i++) {
87       int expected = Math.min(y[i], z[i]);
88       expectEquals(expected, x[i]);
89     }
90     doitMax(x, y, z);
91     for (int i = 0; i < total; i++) {
92       int expected = Math.max(y[i], z[i]);
93       expectEquals(expected, x[i]);
94     }
95 
96     System.out.println("passed");
97   }
98 
expectEquals(int expected, int result)99   private static void expectEquals(int expected, int result) {
100     if (expected != result) {
101       throw new Error("Expected: " + expected + ", found: " + result);
102     }
103   }
104 }
105