• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(char[], char[], char[]) loop_optimization (before)
23   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                 loop:<<Loop:B\d+>> outer_loop:none
24   /// CHECK-DAG: <<Get1:c\d+>> ArrayGet                            loop:<<Loop>>      outer_loop:none
25   /// CHECK-DAG: <<Get2:c\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: <<Cnv:c\d+>>  TypeConversion [<<Min>>]            loop:<<Loop>>      outer_loop:none
28   /// CHECK-DAG:               ArraySet [{{l\d+}},<<Phi>>,<<Cnv>>] loop:<<Loop>>      outer_loop:none
29   //
30   /// CHECK-START-ARM: void Main.doitMin(char[], char[], char[]) loop_optimization (after)
31   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                 loop:<<Loop:B\d+>> outer_loop:none
32   /// CHECK-DAG: <<Get1:d\d+>> VecLoad                             loop:<<Loop>>      outer_loop:none
33   /// CHECK-DAG: <<Get2:d\d+>> VecLoad                             loop:<<Loop>>      outer_loop:none
34   /// CHECK-DAG: <<Min:d\d+>>  VecMin [<<Get1>>,<<Get2>>] unsigned:true loop:<<Loop>> outer_loop:none
35   /// CHECK-DAG:               VecStore [{{l\d+}},<<Phi>>,<<Min>>] loop:<<Loop>>      outer_loop:none
36   //
37   /// CHECK-START-ARM64: void Main.doitMin(char[], char[], char[]) loop_optimization (after)
38   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                 loop:<<Loop:B\d+>> outer_loop:none
39   /// CHECK-DAG: <<Get1:d\d+>> VecLoad                             loop:<<Loop>>      outer_loop:none
40   /// CHECK-DAG: <<Get2:d\d+>> VecLoad                             loop:<<Loop>>      outer_loop:none
41   /// CHECK-DAG: <<Min:d\d+>>  VecMin [<<Get1>>,<<Get2>>] unsigned:true loop:<<Loop>> outer_loop:none
42   /// CHECK-DAG:               VecStore [{{l\d+}},<<Phi>>,<<Min>>] loop:<<Loop>>      outer_loop:none
43   //
44   /// CHECK-START-MIPS64: void Main.doitMin(char[], char[], char[]) loop_optimization (after)
45   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                 loop:<<Loop:B\d+>> outer_loop:none
46   /// CHECK-DAG: <<Get1:d\d+>> VecLoad                             loop:<<Loop>>      outer_loop:none
47   /// CHECK-DAG: <<Get2:d\d+>> VecLoad                             loop:<<Loop>>      outer_loop:none
48   /// CHECK-DAG: <<Min:d\d+>>  VecMin [<<Get1>>,<<Get2>>] unsigned:true loop:<<Loop>> outer_loop:none
49   /// CHECK-DAG:               VecStore [{{l\d+}},<<Phi>>,<<Min>>] loop:<<Loop>>      outer_loop:none
doitMin(char[] x, char[] y, char[] z)50   private static void doitMin(char[] x, char[] y, char[] z) {
51     int min = Math.min(x.length, Math.min(y.length, z.length));
52     for (int i = 0; i < min; i++) {
53       x[i] = (char) Math.min(y[i], z[i]);
54     }
55   }
56 
57   /// CHECK-START: void Main.doitMax(char[], char[], char[]) loop_optimization (before)
58   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                 loop:<<Loop:B\d+>> outer_loop:none
59   /// CHECK-DAG: <<Get1:c\d+>> ArrayGet                            loop:<<Loop>>      outer_loop:none
60   /// CHECK-DAG: <<Get2:c\d+>> ArrayGet                            loop:<<Loop>>      outer_loop:none
61   /// CHECK-DAG: <<Max:i\d+>>  InvokeStaticOrDirect [<<Get1>>,<<Get2>>] intrinsic:MathMaxIntInt loop:<<Loop>> outer_loop:none
62   /// CHECK-DAG: <<Cnv:c\d+>>  TypeConversion [<<Max>>]            loop:<<Loop>>      outer_loop:none
63   /// CHECK-DAG:               ArraySet [{{l\d+}},<<Phi>>,<<Cnv>>] loop:<<Loop>>      outer_loop:none
64   //
65   /// CHECK-START-ARM: void Main.doitMax(char[], char[], char[]) loop_optimization (after)
66   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                 loop:<<Loop:B\d+>> outer_loop:none
67   /// CHECK-DAG: <<Get1:d\d+>> VecLoad                             loop:<<Loop>>      outer_loop:none
68   /// CHECK-DAG: <<Get2:d\d+>> VecLoad                             loop:<<Loop>>      outer_loop:none
69   /// CHECK-DAG: <<Max:d\d+>>  VecMax [<<Get1>>,<<Get2>>] unsigned:true loop:<<Loop>> outer_loop:none
70   /// CHECK-DAG:               VecStore [{{l\d+}},<<Phi>>,<<Max>>] loop:<<Loop>>      outer_loop:none
71   //
72   /// CHECK-START-ARM64: void Main.doitMax(char[], char[], char[]) loop_optimization (after)
73   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                 loop:<<Loop:B\d+>> outer_loop:none
74   /// CHECK-DAG: <<Get1:d\d+>> VecLoad                             loop:<<Loop>>      outer_loop:none
75   /// CHECK-DAG: <<Get2:d\d+>> VecLoad                             loop:<<Loop>>      outer_loop:none
76   /// CHECK-DAG: <<Max:d\d+>>  VecMax [<<Get1>>,<<Get2>>] unsigned:true loop:<<Loop>> outer_loop:none
77   /// CHECK-DAG:               VecStore [{{l\d+}},<<Phi>>,<<Max>>] loop:<<Loop>>      outer_loop:none
78   //
79   /// CHECK-START-MIPS64: void Main.doitMax(char[], char[], char[]) loop_optimization (after)
80   /// CHECK-DAG: <<Phi:i\d+>>  Phi                                 loop:<<Loop:B\d+>> outer_loop:none
81   /// CHECK-DAG: <<Get1:d\d+>> VecLoad                             loop:<<Loop>>      outer_loop:none
82   /// CHECK-DAG: <<Get2:d\d+>> VecLoad                             loop:<<Loop>>      outer_loop:none
83   /// CHECK-DAG: <<Max:d\d+>>  VecMax [<<Get1>>,<<Get2>>] unsigned:true loop:<<Loop>> outer_loop:none
84   /// CHECK-DAG:               VecStore [{{l\d+}},<<Phi>>,<<Max>>] loop:<<Loop>>      outer_loop:none
doitMax(char[] x, char[] y, char[] z)85   private static void doitMax(char[] x, char[] y, char[] z) {
86     int min = Math.min(x.length, Math.min(y.length, z.length));
87     for (int i = 0; i < min; i++) {
88       x[i] = (char) Math.max(y[i], z[i]);
89     }
90   }
91 
main(String[] args)92   public static void main(String[] args) {
93     char[] interesting = {
94       0x0000, 0x0001, 0x007f, 0x0080, 0x0081, 0x00ff,
95       0x0100, 0x0101, 0x017f, 0x0180, 0x0181, 0x01ff,
96       0x7f00, 0x7f01, 0x7f7f, 0x7f80, 0x7f81, 0x7fff,
97       0x8000, 0x8001, 0x807f, 0x8080, 0x8081, 0x80ff,
98       0x8100, 0x8101, 0x817f, 0x8180, 0x8181, 0x81ff,
99       0xff00, 0xff01, 0xff7f, 0xff80, 0xff81, 0xffff
100     };
101     // Initialize cross-values for the interesting values.
102     int total = interesting.length * interesting.length;
103     char[] x = new char[total];
104     char[] y = new char[total];
105     char[] z = new char[total];
106     int k = 0;
107     for (int i = 0; i < interesting.length; i++) {
108       for (int j = 0; j < interesting.length; j++) {
109         x[k] = 0;
110         y[k] = interesting[i];
111         z[k] = interesting[j];
112         k++;
113       }
114     }
115 
116     // And test.
117     doitMin(x, y, z);
118     for (int i = 0; i < total; i++) {
119       char expected = (char) Math.min(y[i], z[i]);
120       expectEquals(expected, x[i]);
121     }
122     doitMax(x, y, z);
123     for (int i = 0; i < total; i++) {
124       char expected = (char) Math.max(y[i], z[i]);
125       expectEquals(expected, x[i]);
126     }
127 
128     System.out.println("passed");
129   }
130 
expectEquals(char expected, char result)131   private static void expectEquals(char expected, char result) {
132     if (expected != result) {
133       throw new Error("Expected: " + expected + ", found: " + result);
134     }
135   }
136 }
137