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 SadChar {
21 
22   /// CHECK-START: int SadChar.sad1(char, char) instruction_simplifier$after_gvn (before)
23   /// CHECK-DAG: <<Select:i\d+>> Select
24   /// CHECK-DAG:                 Return [<<Select>>]
25   //
26   /// CHECK-START: int SadChar.sad1(char, char) instruction_simplifier$after_gvn (after)
27   /// CHECK-DAG: <<Intrin:i\d+>> Abs
28   /// CHECK-DAG:                 Return [<<Intrin>>]
sad1(char x, char y)29   static int sad1(char x, char y) {
30     return x >= y ? x - y : y - x;
31   }
32 
33   /// CHECK-START: int SadChar.sad2(char, char) instruction_simplifier$after_gvn (before)
34   /// CHECK-DAG: <<Select:i\d+>> Select
35   /// CHECK-DAG:                 Return [<<Select>>]
36   //
37   /// CHECK-START: int SadChar.sad2(char, char) instruction_simplifier$after_gvn (after)
38   /// CHECK-DAG: <<Intrin:i\d+>> Abs
39   /// CHECK-DAG:                 Return [<<Intrin>>]
sad2(char x, char y)40   static int sad2(char x, char y) {
41     int diff = x - y;
42     if (diff < 0) diff = -diff;
43     return diff;
44   }
45 
46   /// CHECK-START: int SadChar.sad3(char, char) instruction_simplifier$after_gvn (before)
47   /// CHECK-DAG: <<Select:i\d+>> Select
48   /// CHECK-DAG:                 Return [<<Select>>]
49   //
50   /// CHECK-START: int SadChar.sad3(char, char) instruction_simplifier$after_gvn (after)
51   /// CHECK-DAG: <<Intrin:i\d+>> Abs
52   /// CHECK-DAG:                 Return [<<Intrin>>]
sad3(char x, char y)53   static int sad3(char x, char y) {
54     int diff = x - y;
55     return diff >= 0 ? diff : -diff;
56   }
57 
58   /// CHECK-START: int SadChar.sad3Alt(char, char) instruction_simplifier$after_gvn (before)
59   /// CHECK-DAG: <<Select:i\d+>> Select
60   /// CHECK-DAG:                 Return [<<Select>>]
61   //
62   /// CHECK-START: int SadChar.sad3Alt(char, char) instruction_simplifier$after_gvn (after)
63   /// CHECK-DAG: <<Intrin:i\d+>> Abs
64   /// CHECK-DAG:                 Return [<<Intrin>>]
sad3Alt(char x, char y)65   static int sad3Alt(char x, char y) {
66     int diff = x - y;
67     return 0 <= diff ? diff : -diff;
68   }
69 
70   /// CHECK-START: long SadChar.sadL1(char, char) instruction_simplifier$after_gvn (before)
71   /// CHECK-DAG: <<Select:j\d+>> Select
72   /// CHECK-DAG:                 Return [<<Select>>]
73   //
74   /// CHECK-START: long SadChar.sadL1(char, char) instruction_simplifier$after_gvn (after)
75   /// CHECK-DAG: <<Intrin:j\d+>> Abs
76   /// CHECK-DAG:                 Return [<<Intrin>>]
sadL1(char x, char y)77   static long sadL1(char x, char y) {
78     long xl = x;
79     long yl = y;
80     return xl >= yl ? xl - yl : yl - xl;
81   }
82 
83   /// CHECK-START: long SadChar.sadL2(char, char) instruction_simplifier$after_gvn (before)
84   /// CHECK-DAG: <<Select:j\d+>> Select
85   /// CHECK-DAG:                 Return [<<Select>>]
86   //
87   /// CHECK-START: long SadChar.sadL2(char, char) instruction_simplifier$after_gvn (after)
88   /// CHECK-DAG: <<Intrin:j\d+>> Abs
89   /// CHECK-DAG:                 Return [<<Intrin>>]
sadL2(char x, char y)90   static long sadL2(char x, char y) {
91     long diff = x - y;
92     if (diff < 0L) diff = -diff;
93     return diff;
94   }
95 
96   /// CHECK-START: long SadChar.sadL3(char, char) instruction_simplifier$after_gvn (before)
97   /// CHECK-DAG: <<Select:j\d+>> Select
98   /// CHECK-DAG:                 Return [<<Select>>]
99   //
100   /// CHECK-START: long SadChar.sadL3(char, char) instruction_simplifier$after_gvn (after)
101   /// CHECK-DAG: <<Intrin:j\d+>> Abs
102   /// CHECK-DAG:                 Return [<<Intrin>>]
sadL3(char x, char y)103   static long sadL3(char x, char y) {
104     long diff = x - y;
105     return diff >= 0L ? diff : -diff;
106   }
107 
108   /// CHECK-START: long SadChar.sadL3Alt(char, char) instruction_simplifier$after_gvn (before)
109   /// CHECK-DAG: <<Select:j\d+>> Select
110   /// CHECK-DAG:                 Return [<<Select>>]
111   //
112   /// CHECK-START: long SadChar.sadL3Alt(char, char) instruction_simplifier$after_gvn (after)
113   /// CHECK-DAG: <<Intrin:j\d+>> Abs
114   /// CHECK-DAG:                 Return [<<Intrin>>]
sadL3Alt(char x, char y)115   static long sadL3Alt(char x, char 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     char[] interesting = {
123       (char) 0x0000, (char) 0x0001, (char) 0x007f,
124       (char) 0x0080, (char) 0x0081, (char) 0x00ff,
125       (char) 0x0100, (char) 0x0101, (char) 0x017f,
126       (char) 0x0180, (char) 0x0181, (char) 0x01ff,
127       (char) 0x7f00, (char) 0x7f01, (char) 0x7f7f,
128       (char) 0x7f80, (char) 0x7f81, (char) 0x7fff,
129       (char) 0x8000, (char) 0x8001, (char) 0x807f,
130       (char) 0x8080, (char) 0x8081, (char) 0x80ff,
131       (char) 0x8100, (char) 0x8101, (char) 0x817f,
132       (char) 0x8180, (char) 0x8181, (char) 0x81ff,
133       (char) 0xff00, (char) 0xff01, (char) 0xff7f,
134       (char) 0xff80, (char) 0xff81, (char) 0xffff
135     };
136     for (int i = 0; i < interesting.length; i++) {
137       for (int j = 0; j < interesting.length; j++) {
138         char x = interesting[i];
139         char 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("SadChar 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