1 /*
2  * Copyright (C) 2014 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 // Note that $opt$ is a marker for the optimizing compiler to test
18 // it does compile the method.
19 public class Main {
20 
expectEquals(int expected, int result)21   public static void expectEquals(int expected, int result) {
22     if (expected != result) {
23       throw new Error("Expected: " + expected + ", found: " + result);
24     }
25   }
26 
expectEquals(long expected, long result)27   public static void expectEquals(long expected, long result) {
28     if (expected != result) {
29       throw new Error("Expected: " + expected + ", found: " + result);
30     }
31   }
32 
main(String[] args)33   public static void main(String[] args) {
34     andInt();
35     andLong();
36 
37     orInt();
38     orLong();
39 
40     xorInt();
41     xorLong();
42   }
43 
andInt()44   private static void andInt() {
45     expectEquals(1, $opt$And(5, 3));
46     expectEquals(0, $opt$And(0, 0));
47     expectEquals(0, $opt$And(0, 3));
48     expectEquals(0, $opt$And(3, 0));
49     expectEquals(1, $opt$And(1, -3));
50     expectEquals(-12, $opt$And(-12, -3));
51 
52     expectEquals(1, $opt$AndLit8(1));
53     expectEquals(0, $opt$AndLit8(0));
54     expectEquals(0, $opt$AndLit8(0));
55     expectEquals(3, $opt$AndLit8(3));
56     expectEquals(4, $opt$AndLit8(-12));
57 
58     expectEquals(0, $opt$AndLit16(1));
59     expectEquals(0, $opt$AndLit16(0));
60     expectEquals(0, $opt$AndLit16(0));
61     expectEquals(0, $opt$AndLit16(3));
62     expectEquals(65280, $opt$AndLit16(-12));
63   }
64 
andLong()65   private static void andLong() {
66     expectEquals(1L, $opt$And(5L, 3L));
67     expectEquals(0L, $opt$And(0L, 0L));
68     expectEquals(0L, $opt$And(0L, 3L));
69     expectEquals(0L, $opt$And(3L, 0L));
70     expectEquals(1L, $opt$And(1L, -3L));
71     expectEquals(-12L, $opt$And(-12L, -3L));
72 
73     expectEquals(1L, $opt$AndLit8(1L));
74     expectEquals(0L, $opt$AndLit8(0L));
75     expectEquals(0L, $opt$AndLit8(0L));
76     expectEquals(3L, $opt$AndLit8(3L));
77     expectEquals(4L, $opt$AndLit8(-12L));
78 
79     expectEquals(0L, $opt$AndLit16(1L));
80     expectEquals(0L, $opt$AndLit16(0L));
81     expectEquals(0L, $opt$AndLit16(0L));
82     expectEquals(0L, $opt$AndLit16(3L));
83     expectEquals(65280L, $opt$AndLit16(-12L));
84   }
85 
$opt$And(int a, int b)86   static int $opt$And(int a, int b) {
87     return a & b;
88   }
89 
$opt$AndLit8(int a)90   static int $opt$AndLit8(int a) {
91     return a & 0xF;
92   }
93 
$opt$AndLit16(int a)94   static int $opt$AndLit16(int a) {
95     return a & 0xFF00;
96   }
97 
$opt$And(long a, long b)98   static long $opt$And(long a, long b) {
99     return a & b;
100   }
101 
$opt$AndLit8(long a)102   static long $opt$AndLit8(long a) {
103     return a & 0xF;
104   }
105 
$opt$AndLit16(long a)106   static long $opt$AndLit16(long a) {
107     return a & 0xFF00;
108   }
109 
orInt()110   private static void orInt() {
111     expectEquals(7, $opt$Or(5, 3));
112     expectEquals(0, $opt$Or(0, 0));
113     expectEquals(3, $opt$Or(0, 3));
114     expectEquals(3, $opt$Or(3, 0));
115     expectEquals(-3, $opt$Or(1, -3));
116     expectEquals(-3, $opt$Or(-12, -3));
117 
118     expectEquals(15, $opt$OrLit8(1));
119     expectEquals(15, $opt$OrLit8(0));
120     expectEquals(15, $opt$OrLit8(3));
121     expectEquals(-1, $opt$OrLit8(-12));
122 
123     expectEquals(0xFF01, $opt$OrLit16(1));
124     expectEquals(0xFF00, $opt$OrLit16(0));
125     expectEquals(0xFF03, $opt$OrLit16(3));
126     expectEquals(-12, $opt$OrLit16(-12));
127   }
128 
orLong()129   private static void orLong() {
130     expectEquals(7L, $opt$Or(5L, 3L));
131     expectEquals(0L, $opt$Or(0L, 0L));
132     expectEquals(3L, $opt$Or(0L, 3L));
133     expectEquals(3L, $opt$Or(3L, 0L));
134     expectEquals(-3L, $opt$Or(1L, -3L));
135     expectEquals(-3L, $opt$Or(-12L, -3L));
136 
137     expectEquals(15L, $opt$OrLit8(1L));
138     expectEquals(15L, $opt$OrLit8(0L));
139     expectEquals(15L, $opt$OrLit8(3L));
140     expectEquals(-1L, $opt$OrLit8(-12L));
141 
142     expectEquals(0xFF01L, $opt$OrLit16(1L));
143     expectEquals(0xFF00L, $opt$OrLit16(0L));
144     expectEquals(0xFF03L, $opt$OrLit16(3L));
145     expectEquals(-12L, $opt$OrLit16(-12L));
146   }
147 
$opt$Or(int a, int b)148   static int $opt$Or(int a, int b) {
149     return a | b;
150   }
151 
$opt$OrLit8(int a)152   static int $opt$OrLit8(int a) {
153     return a | 0xF;
154   }
155 
$opt$OrLit16(int a)156   static int $opt$OrLit16(int a) {
157     return a | 0xFF00;
158   }
159 
$opt$Or(long a, long b)160   static long $opt$Or(long a, long b) {
161     return a | b;
162   }
163 
$opt$OrLit8(long a)164   static long $opt$OrLit8(long a) {
165     return a | 0xF;
166   }
167 
$opt$OrLit16(long a)168   static long $opt$OrLit16(long a) {
169     return a | 0xFF00;
170   }
171 
xorInt()172   private static void xorInt() {
173     expectEquals(6, $opt$Xor(5, 3));
174     expectEquals(0, $opt$Xor(0, 0));
175     expectEquals(3, $opt$Xor(0, 3));
176     expectEquals(3, $opt$Xor(3, 0));
177     expectEquals(-4, $opt$Xor(1, -3));
178     expectEquals(9, $opt$Xor(-12, -3));
179 
180     expectEquals(14, $opt$XorLit8(1));
181     expectEquals(15, $opt$XorLit8(0));
182     expectEquals(12, $opt$XorLit8(3));
183     expectEquals(-5, $opt$XorLit8(-12));
184 
185     expectEquals(0xFF01, $opt$XorLit16(1));
186     expectEquals(0xFF00, $opt$XorLit16(0));
187     expectEquals(0xFF03, $opt$XorLit16(3));
188     expectEquals(-0xFF0c, $opt$XorLit16(-12));
189   }
190 
xorLong()191   private static void xorLong() {
192     expectEquals(6L, $opt$Xor(5L, 3L));
193     expectEquals(0L, $opt$Xor(0L, 0L));
194     expectEquals(3L, $opt$Xor(0L, 3L));
195     expectEquals(3L, $opt$Xor(3L, 0L));
196     expectEquals(-4L, $opt$Xor(1L, -3L));
197     expectEquals(9L, $opt$Xor(-12L, -3L));
198 
199     expectEquals(14L, $opt$XorLit8(1L));
200     expectEquals(15L, $opt$XorLit8(0L));
201     expectEquals(12L, $opt$XorLit8(3L));
202     expectEquals(-5L, $opt$XorLit8(-12L));
203 
204     expectEquals(0xFF01L, $opt$XorLit16(1L));
205     expectEquals(0xFF00L, $opt$XorLit16(0L));
206     expectEquals(0xFF03L, $opt$XorLit16(3L));
207     expectEquals(-0xFF0cL, $opt$XorLit16(-12L));
208   }
209 
$opt$Xor(int a, int b)210   static int $opt$Xor(int a, int b) {
211     return a ^ b;
212   }
213 
$opt$XorLit8(int a)214   static int $opt$XorLit8(int a) {
215     return a ^ 0xF;
216   }
217 
$opt$XorLit16(int a)218   static int $opt$XorLit16(int a) {
219     return a ^ 0xFF00;
220   }
221 
$opt$Xor(long a, long b)222   static long $opt$Xor(long a, long b) {
223     return a ^ b;
224   }
225 
$opt$XorLit8(long a)226   static long $opt$XorLit8(long a) {
227     return a ^ 0xF;
228   }
229 
$opt$XorLit16(long a)230   static long $opt$XorLit16(long a) {
231     return a ^ 0xFF00;
232   }
233 }
234