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