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