Aart Bik | c8e93c7 | 2017-05-10 10:49:22 -0700 | [diff] [blame^] | 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(byte[], byte[], byte[]) loop_optimization (before) |
| 23 | /// CHECK-DAG: <<Phi:i\d+>> Phi loop:<<Loop:B\d+>> outer_loop:none |
| 24 | /// CHECK-DAG: <<Get1:b\d+>> ArrayGet loop:<<Loop>> outer_loop:none |
| 25 | /// CHECK-DAG: <<Get2:b\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:b\d+>> TypeConversion [<<Min>>] loop:<<Loop>> outer_loop:none |
| 28 | /// CHECK-DAG: ArraySet [{{l\d+}},<<Phi>>,<<Cnv>>] loop:<<Loop>> outer_loop:none |
| 29 | // |
| 30 | // TODO: narrow type vectorization. |
| 31 | /// CHECK-START: void Main.doitMin(byte[], byte[], byte[]) loop_optimization (after) |
| 32 | /// CHECK-NOT: VecMin |
| 33 | private static void doitMin(byte[] x, byte[] y, byte[] z) { |
| 34 | int min = Math.min(x.length, Math.min(y.length, z.length)); |
| 35 | for (int i = 0; i < min; i++) { |
| 36 | x[i] = (byte) Math.min(y[i], z[i]); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /// CHECK-START: void Main.doitMax(byte[], byte[], byte[]) loop_optimization (before) |
| 41 | /// CHECK-DAG: <<Phi:i\d+>> Phi loop:<<Loop:B\d+>> outer_loop:none |
| 42 | /// CHECK-DAG: <<Get1:b\d+>> ArrayGet loop:<<Loop>> outer_loop:none |
| 43 | /// CHECK-DAG: <<Get2:b\d+>> ArrayGet loop:<<Loop>> outer_loop:none |
| 44 | /// CHECK-DAG: <<Max:i\d+>> InvokeStaticOrDirect [<<Get1>>,<<Get2>>] intrinsic:MathMaxIntInt loop:<<Loop>> outer_loop:none |
| 45 | /// CHECK-DAG: <<Cnv:b\d+>> TypeConversion [<<Max>>] loop:<<Loop>> outer_loop:none |
| 46 | /// CHECK-DAG: ArraySet [{{l\d+}},<<Phi>>,<<Cnv>>] loop:<<Loop>> outer_loop:none |
| 47 | // |
| 48 | // TODO: narrow type vectorization. |
| 49 | /// CHECK-START: void Main.doitMax(byte[], byte[], byte[]) loop_optimization (after) |
| 50 | /// CHECK-NOT: VecMax |
| 51 | private static void doitMax(byte[] x, byte[] y, byte[] z) { |
| 52 | int min = Math.min(x.length, Math.min(y.length, z.length)); |
| 53 | for (int i = 0; i < min; i++) { |
| 54 | x[i] = (byte) Math.max(y[i], z[i]); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | public static void main(String[] args) { |
| 59 | // Initialize cross-values for all possible values. |
| 60 | int total = 256 * 256; |
| 61 | byte[] x = new byte[total]; |
| 62 | byte[] y = new byte[total]; |
| 63 | byte[] z = new byte[total]; |
| 64 | int k = 0; |
| 65 | for (int i = 0; i < 256; i++) { |
| 66 | for (int j = 0; j < 256; j++) { |
| 67 | x[k] = 0; |
| 68 | y[k] = (byte) i; |
| 69 | z[k] = (byte) j; |
| 70 | k++; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // And test. |
| 75 | doitMin(x, y, z); |
| 76 | for (int i = 0; i < total; i++) { |
| 77 | byte expected = (byte) Math.min(y[i], z[i]); |
| 78 | expectEquals(expected, x[i]); |
| 79 | } |
| 80 | doitMax(x, y, z); |
| 81 | for (int i = 0; i < total; i++) { |
| 82 | byte expected = (byte) Math.max(y[i], z[i]); |
| 83 | expectEquals(expected, x[i]); |
| 84 | } |
| 85 | |
| 86 | System.out.println("passed"); |
| 87 | } |
| 88 | |
| 89 | private static void expectEquals(byte expected, byte result) { |
| 90 | if (expected != result) { |
| 91 | throw new Error("Expected: " + expected + ", found: " + result); |
| 92 | } |
| 93 | } |
| 94 | } |