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 Main { 21 22 /// CHECK-START: int Main.sad1(byte, byte) instruction_simplifier$after_gvn (before) 23 /// CHECK-DAG: <<Select:i\d+>> Select 24 /// CHECK-DAG: Return [<<Select>>] 25 // 26 /// CHECK-START: int Main.sad1(byte, byte) instruction_simplifier$after_gvn (after) 27 /// CHECK-DAG: <<Intrin:i\d+>> Abs 28 /// CHECK-DAG: Return [<<Intrin>>] 29 static int sad1(byte x, byte y) { 30 return x >= y ? x - y : y - x; 31 } 32 33 /// CHECK-START: int Main.sad2(byte, byte) instruction_simplifier$after_gvn (before) 34 /// CHECK-DAG: <<Select:i\d+>> Select 35 /// CHECK-DAG: Return [<<Select>>] 36 // 37 /// CHECK-START: int Main.sad2(byte, byte) instruction_simplifier$after_gvn (after) 38 /// CHECK-DAG: <<Intrin:i\d+>> Abs 39 /// CHECK-DAG: Return [<<Intrin>>] 40 static int sad2(byte x, byte y) { 41 int diff = x - y; 42 if (diff < 0) diff = -diff; 43 return diff; 44 } 45 46 /// CHECK-START: int Main.sad3(byte, byte) instruction_simplifier$after_gvn (before) 47 /// CHECK-DAG: <<Select:i\d+>> Select 48 /// CHECK-DAG: Return [<<Select>>] 49 // 50 /// CHECK-START: int Main.sad3(byte, byte) instruction_simplifier$after_gvn (after) 51 /// CHECK-DAG: <<Intrin:i\d+>> Abs 52 /// CHECK-DAG: Return [<<Intrin>>] 53 static int sad3(byte x, byte y) { 54 int diff = x - y; 55 return diff >= 0 ? diff : -diff; 56 } 57 58 /// CHECK-START: int Main.sad3Alt(byte, byte) instruction_simplifier$after_gvn (before) 59 /// CHECK-DAG: <<Select:i\d+>> Select 60 /// CHECK-DAG: Return [<<Select>>] 61 // 62 /// CHECK-START: int Main.sad3Alt(byte, byte) instruction_simplifier$after_gvn (after) 63 /// CHECK-DAG: <<Intrin:i\d+>> Abs 64 /// CHECK-DAG: Return [<<Intrin>>] 65 static int sad3Alt(byte x, byte y) { 66 int diff = x - y; 67 return 0 <= diff ? diff : -diff; 68 } 69 70 /// CHECK-START: long Main.sadL1(byte, byte) instruction_simplifier$after_gvn (before) 71 /// CHECK-DAG: <<Select:j\d+>> Select 72 /// CHECK-DAG: Return [<<Select>>] 73 // 74 /// CHECK-START: long Main.sadL1(byte, byte) instruction_simplifier$after_gvn (after) 75 /// CHECK-DAG: <<Intrin:j\d+>> Abs 76 /// CHECK-DAG: Return [<<Intrin>>] 77 static long sadL1(byte x, byte y) { 78 long xl = x; 79 long yl = y; 80 return xl >= yl ? xl - yl : yl - xl; 81 } 82 83 /// CHECK-START: long Main.sadL2(byte, byte) instruction_simplifier$after_gvn (before) 84 /// CHECK-DAG: <<Select:j\d+>> Select 85 /// CHECK-DAG: Return [<<Select>>] 86 // 87 /// CHECK-START: long Main.sadL2(byte, byte) instruction_simplifier$after_gvn (after) 88 /// CHECK-DAG: <<Intrin:j\d+>> Abs 89 /// CHECK-DAG: Return [<<Intrin>>] 90 static long sadL2(byte x, byte y) { 91 long diff = x - y; 92 if (diff < 0L) diff = -diff; 93 return diff; 94 } 95 96 /// CHECK-START: long Main.sadL3(byte, byte) instruction_simplifier$after_gvn (before) 97 /// CHECK-DAG: <<Select:j\d+>> Select 98 /// CHECK-DAG: Return [<<Select>>] 99 // 100 /// CHECK-START: long Main.sadL3(byte, byte) instruction_simplifier$after_gvn (after) 101 /// CHECK-DAG: <<Intrin:j\d+>> Abs 102 /// CHECK-DAG: Return [<<Intrin>>] 103 static long sadL3(byte x, byte y) { 104 long diff = x - y; 105 return diff >= 0L ? diff : -diff; 106 } 107 108 /// CHECK-START: long Main.sadL3Alt(byte, byte) instruction_simplifier$after_gvn (before) 109 /// CHECK-DAG: <<Select:j\d+>> Select 110 /// CHECK-DAG: Return [<<Select>>] 111 // 112 /// CHECK-START: long Main.sadL3Alt(byte, byte) instruction_simplifier$after_gvn (after) 113 /// CHECK-DAG: <<Intrin:j\d+>> Abs 114 /// CHECK-DAG: Return [<<Intrin>>] 115 static long sadL3Alt(byte x, byte y) { 116 long diff = x - y; 117 return 0L <= diff ? diff : -diff; 118 } 119 120 public static void main(String[] args) { 121 // Use cross-values to test all cases. 122 int n = 256; 123 for (int i = 0; i < n; i++) { 124 for (int j = 0; j < n; j++) { 125 byte x = (byte) i; 126 byte y = (byte) j; 127 int e = Math.abs(x - y); 128 expectEquals(e, sad1(x, y)); 129 expectEquals(e, sad2(x, y)); 130 expectEquals(e, sad3(x, y)); 131 expectEquals(e, sad3Alt(x, y)); 132 expectEquals(e, sadL2(x, y)); 133 expectEquals(e, sadL3(x, y)); 134 expectEquals(e, sadL3Alt(x, y)); 135 } 136 } 137 System.out.println("passed"); 138 } 139 140 private static void expectEquals(int expected, int result) { 141 if (expected != result) { 142 throw new Error("Expected: " + expected + ", found: " + result); 143 } 144 } 145 146 private static void expectEquals(long expected, long result) { 147 if (expected != result) { 148 throw new Error("Expected: " + expected + ", found: " + result); 149 } 150 } 151 } 152