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 SAD (sum of absolute differences). 19 */ 20 public class SadShort { 21 22 /// CHECK-START: int SadShort.sad1(short, short) instruction_simplifier$after_gvn (before) 23 /// CHECK-DAG: <<Select:i\d+>> Select 24 /// CHECK-DAG: Return [<<Select>>] 25 // 26 /// CHECK-START: int SadShort.sad1(short, short) instruction_simplifier$after_gvn (after) 27 /// CHECK-DAG: <<Intrin:i\d+>> Abs 28 /// CHECK-DAG: Return [<<Intrin>>] sad1(short x, short y)29 static int sad1(short x, short y) { 30 return x >= y ? x - y : y - x; 31 } 32 33 /// CHECK-START: int SadShort.sad2(short, short) instruction_simplifier$after_gvn (before) 34 /// CHECK-DAG: <<Select:i\d+>> Select 35 /// CHECK-DAG: Return [<<Select>>] 36 // 37 /// CHECK-START: int SadShort.sad2(short, short) instruction_simplifier$after_gvn (after) 38 /// CHECK-DAG: <<Intrin:i\d+>> Abs 39 /// CHECK-DAG: Return [<<Intrin>>] sad2(short x, short y)40 static int sad2(short x, short y) { 41 int diff = x - y; 42 if (diff < 0) diff = -diff; 43 return diff; 44 } 45 46 /// CHECK-START: int SadShort.sad3(short, short) instruction_simplifier$after_gvn (before) 47 /// CHECK-DAG: <<Select:i\d+>> Select 48 /// CHECK-DAG: Return [<<Select>>] 49 // 50 /// CHECK-START: int SadShort.sad3(short, short) instruction_simplifier$after_gvn (after) 51 /// CHECK-DAG: <<Intrin:i\d+>> Abs 52 /// CHECK-DAG: Return [<<Intrin>>] sad3(short x, short y)53 static int sad3(short x, short y) { 54 int diff = x - y; 55 return diff >= 0 ? diff : -diff; 56 } 57 58 /// CHECK-START: int SadShort.sad3Alt(short, short) instruction_simplifier$after_gvn (before) 59 /// CHECK-DAG: <<Select:i\d+>> Select 60 /// CHECK-DAG: Return [<<Select>>] 61 // 62 /// CHECK-START: int SadShort.sad3Alt(short, short) instruction_simplifier$after_gvn (after) 63 /// CHECK-DAG: <<Intrin:i\d+>> Abs 64 /// CHECK-DAG: Return [<<Intrin>>] sad3Alt(short x, short y)65 static int sad3Alt(short x, short y) { 66 int diff = x - y; 67 return 0 <= diff ? diff : -diff; 68 } 69 70 /// CHECK-START: long SadShort.sadL1(short, short) instruction_simplifier$after_gvn (before) 71 /// CHECK-DAG: <<Select:j\d+>> Select 72 /// CHECK-DAG: Return [<<Select>>] 73 // 74 /// CHECK-START: long SadShort.sadL1(short, short) instruction_simplifier$after_gvn (after) 75 /// CHECK-DAG: <<Intrin:j\d+>> Abs 76 /// CHECK-DAG: Return [<<Intrin>>] sadL1(short x, short y)77 static long sadL1(short x, short y) { 78 long xl = x; 79 long yl = y; 80 return xl >= yl ? xl - yl : yl - xl; 81 } 82 83 /// CHECK-START: long SadShort.sadL2(short, short) instruction_simplifier$after_gvn (before) 84 /// CHECK-DAG: <<Select:j\d+>> Select 85 /// CHECK-DAG: Return [<<Select>>] 86 // 87 /// CHECK-START: long SadShort.sadL2(short, short) instruction_simplifier$after_gvn (after) 88 /// CHECK-DAG: <<Intrin:j\d+>> Abs 89 /// CHECK-DAG: Return [<<Intrin>>] sadL2(short x, short y)90 static long sadL2(short x, short y) { 91 long diff = x - y; 92 if (diff < 0L) diff = -diff; 93 return diff; 94 } 95 96 /// CHECK-START: long SadShort.sadL3(short, short) instruction_simplifier$after_gvn (before) 97 /// CHECK-DAG: <<Select:j\d+>> Select 98 /// CHECK-DAG: Return [<<Select>>] 99 // 100 /// CHECK-START: long SadShort.sadL3(short, short) instruction_simplifier$after_gvn (after) 101 /// CHECK-DAG: <<Intrin:j\d+>> Abs 102 /// CHECK-DAG: Return [<<Intrin>>] sadL3(short x, short y)103 static long sadL3(short x, short y) { 104 long diff = x - y; 105 return diff >= 0L ? diff : -diff; 106 } 107 108 /// CHECK-START: long SadShort.sadL3Alt(short, short) instruction_simplifier$after_gvn (before) 109 /// CHECK-DAG: <<Select:j\d+>> Select 110 /// CHECK-DAG: Return [<<Select>>] 111 // 112 /// CHECK-START: long SadShort.sadL3Alt(short, short) instruction_simplifier$after_gvn (after) 113 /// CHECK-DAG: <<Intrin:j\d+>> Abs 114 /// CHECK-DAG: Return [<<Intrin>>] sadL3Alt(short x, short y)115 static long sadL3Alt(short x, short y) { 116 long diff = x - y; 117 return 0L <= diff ? diff : -diff; 118 } 119 main()120 public static void main() { 121 // Use cross-values to test all cases. 122 short[] interesting = { 123 (short) 0x0000, (short) 0x0001, (short) 0x007f, 124 (short) 0x0080, (short) 0x0081, (short) 0x00ff, 125 (short) 0x0100, (short) 0x0101, (short) 0x017f, 126 (short) 0x0180, (short) 0x0181, (short) 0x01ff, 127 (short) 0x7f00, (short) 0x7f01, (short) 0x7f7f, 128 (short) 0x7f80, (short) 0x7f81, (short) 0x7fff, 129 (short) 0x8000, (short) 0x8001, (short) 0x807f, 130 (short) 0x8080, (short) 0x8081, (short) 0x80ff, 131 (short) 0x8100, (short) 0x8101, (short) 0x817f, 132 (short) 0x8180, (short) 0x8181, (short) 0x81ff, 133 (short) 0xff00, (short) 0xff01, (short) 0xff7f, 134 (short) 0xff80, (short) 0xff81, (short) 0xffff 135 }; 136 for (int i = 0; i < interesting.length; i++) { 137 for (int j = 0; j < interesting.length; j++) { 138 short x = interesting[i]; 139 short y = interesting[j]; 140 int e = Math.abs(x - y); 141 expectEquals(e, sad1(x, y)); 142 expectEquals(e, sad2(x, y)); 143 expectEquals(e, sad3(x, y)); 144 expectEquals(e, sad3Alt(x, y)); 145 expectEquals(e, sadL1(x, y)); 146 expectEquals(e, sadL2(x, y)); 147 expectEquals(e, sadL3(x, y)); 148 expectEquals(e, sadL3Alt(x, y)); 149 } 150 } 151 System.out.println("SadShort passed"); 152 } 153 expectEquals(int expected, int result)154 private static void expectEquals(int expected, int result) { 155 if (expected != result) { 156 throw new Error("Expected: " + expected + ", found: " + result); 157 } 158 } 159 expectEquals(long expected, long result)160 private static void expectEquals(long expected, long result) { 161 if (expected != result) { 162 throw new Error("Expected: " + expected + ", found: " + result); 163 } 164 } 165 } 166