1 /*
2  * Copyright (C) 2016 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 public class Main {
18 
19   /// CHECK-START: int Main.signBoolean(boolean) intrinsics_recognition (after)
20   /// CHECK-DAG:     <<Method:[ij]\d+>> CurrentMethod
21   /// CHECK-DAG:     <<Zero:i\d+>>   IntConstant 0
22   /// CHECK-DAG:     <<One:i\d+>>    IntConstant 1
23   /// CHECK-DAG:     <<Phi:i\d+>>    Phi [<<One>>,<<Zero>>]
24   /// CHECK-DAG:     <<Result:i\d+>> InvokeStaticOrDirect [<<Phi>>,<<Method>>] intrinsic:IntegerSignum
25   /// CHECK-DAG:                     Return [<<Result>>]
26 
27   /// CHECK-START: int Main.signBoolean(boolean) instruction_simplifier (after)
28   /// CHECK-DAG:     <<Zero:i\d+>>   IntConstant 0
29   /// CHECK-DAG:     <<One:i\d+>>    IntConstant 1
30   /// CHECK-DAG:     <<Phi:i\d+>>    Phi [<<One>>,<<Zero>>]
31   /// CHECK-DAG:     <<Result:i\d+>> Compare [<<Phi>>,<<Zero>>]
32   /// CHECK-DAG:                     Return [<<Result>>]
33 
34   /// CHECK-START: int Main.signBoolean(boolean) instruction_simplifier (after)
35   /// CHECK-NOT:                     InvokeStaticOrDirect
36 
37   /// CHECK-START: int Main.signBoolean(boolean) select_generator (after)
38   /// CHECK-DAG:     <<Arg:z\d+>>    ParameterValue
39   /// CHECK-DAG:     <<Zero:i\d+>>   IntConstant 0
40   /// CHECK-DAG:     <<One:i\d+>>    IntConstant 1
41   /// CHECK-DAG:     <<Sel:i\d+>>    Select [<<Zero>>,<<One>>,<<Arg>>]
42   /// CHECK-DAG:     <<Result:i\d+>> Compare [<<Sel>>,<<Zero>>]
43   /// CHECK-DAG:                     Return [<<Result>>]
44 
45   /// CHECK-START: int Main.signBoolean(boolean) select_generator (after)
46   /// CHECK-NOT:                     Phi
47 
48   /// CHECK-START: int Main.signBoolean(boolean) instruction_simplifier$after_bce (after)
49   /// CHECK-DAG:     <<Arg:z\d+>>    ParameterValue
50   /// CHECK-DAG:     <<Zero:i\d+>>   IntConstant 0
51   /// CHECK-DAG:     <<Result:i\d+>> Compare [<<Arg>>,<<Zero>>]
52   /// CHECK-DAG:                     Return [<<Result>>]
53 
54   /// CHECK-START: int Main.signBoolean(boolean) instruction_simplifier$after_bce (after)
55   /// CHECK-NOT:                     Select
56 
signBoolean(boolean x)57   private static int signBoolean(boolean x) {
58     return Integer.signum(x ? 1 : 0);
59   }
60 
61   /// CHECK-START: int Main.signByte(byte) intrinsics_recognition (after)
62   /// CHECK-DAG:     <<Result:i\d+>> InvokeStaticOrDirect intrinsic:IntegerSignum
63   /// CHECK-DAG:                     Return [<<Result>>]
64 
65   /// CHECK-START: int Main.signByte(byte) instruction_simplifier (after)
66   /// CHECK-DAG:     <<Result:i\d+>> Compare
67   /// CHECK-DAG:                     Return [<<Result>>]
68 
69   /// CHECK-START: int Main.signByte(byte) instruction_simplifier (after)
70   /// CHECK-NOT:                     InvokeStaticOrDirect
71 
signByte(byte x)72   private static int signByte(byte x) {
73     return Integer.signum(x);
74   }
75 
76   /// CHECK-START: int Main.signShort(short) intrinsics_recognition (after)
77   /// CHECK-DAG:     <<Result:i\d+>> InvokeStaticOrDirect intrinsic:IntegerSignum
78   /// CHECK-DAG:                     Return [<<Result>>]
79 
80   /// CHECK-START: int Main.signShort(short) instruction_simplifier (after)
81   /// CHECK-DAG:     <<Result:i\d+>> Compare
82   /// CHECK-DAG:                     Return [<<Result>>]
83 
84   /// CHECK-START: int Main.signShort(short) instruction_simplifier (after)
85   /// CHECK-NOT:                     InvokeStaticOrDirect
86 
signShort(short x)87   private static int signShort(short x) {
88     return Integer.signum(x);
89   }
90 
91   /// CHECK-START: int Main.signChar(char) intrinsics_recognition (after)
92   /// CHECK-DAG:     <<Result:i\d+>> InvokeStaticOrDirect intrinsic:IntegerSignum
93   /// CHECK-DAG:                     Return [<<Result>>]
94 
95   /// CHECK-START: int Main.signChar(char) instruction_simplifier (after)
96   /// CHECK-DAG:     <<Result:i\d+>> Compare
97   /// CHECK-DAG:                     Return [<<Result>>]
98 
99   /// CHECK-START: int Main.signChar(char) instruction_simplifier (after)
100   /// CHECK-NOT:                     InvokeStaticOrDirect
101 
signChar(char x)102   private static int signChar(char x) {
103     return Integer.signum(x);
104   }
105 
106   /// CHECK-START: int Main.signInt(int) intrinsics_recognition (after)
107   /// CHECK-DAG:     <<Result:i\d+>> InvokeStaticOrDirect intrinsic:IntegerSignum
108   /// CHECK-DAG:                     Return [<<Result>>]
109 
110   /// CHECK-START: int Main.signInt(int) instruction_simplifier (after)
111   /// CHECK-DAG:     <<Result:i\d+>> Compare
112   /// CHECK-DAG:                     Return [<<Result>>]
113 
114   /// CHECK-START: int Main.signInt(int) instruction_simplifier (after)
115   /// CHECK-NOT:                     InvokeStaticOrDirect
116 
signInt(int x)117   private static int signInt(int x) {
118     return Integer.signum(x);
119   }
120 
121   /// CHECK-START: int Main.signLong(long) intrinsics_recognition (after)
122   /// CHECK-DAG:     <<Result:i\d+>> InvokeStaticOrDirect intrinsic:LongSignum
123   /// CHECK-DAG:                     Return [<<Result>>]
124 
125   /// CHECK-START: int Main.signLong(long) instruction_simplifier (after)
126   /// CHECK-DAG:     <<Result:i\d+>> Compare
127   /// CHECK-DAG:                     Return [<<Result>>]
128 
129   /// CHECK-START: int Main.signLong(long) instruction_simplifier (after)
130   /// CHECK-NOT:                     InvokeStaticOrDirect
131 
signLong(long x)132   private static int signLong(long x) {
133     return Long.signum(x);
134   }
135 
136 
testSignBoolean()137   public static void testSignBoolean() {
138     expectEquals(0, signBoolean(false));
139     expectEquals(1, signBoolean(true));
140   }
141 
testSignByte()142   public static void testSignByte() {
143     expectEquals(-1, signByte((byte)Byte.MIN_VALUE));
144     expectEquals(-1, signByte((byte)-64));
145     expectEquals(-1, signByte((byte)-1));
146     expectEquals(0, signByte((byte)0));
147     expectEquals(1, signByte((byte)1));
148     expectEquals(1, signByte((byte)64));
149     expectEquals(1, signByte((byte)Byte.MAX_VALUE));
150   }
151 
testSignShort()152   public static void testSignShort() {
153     expectEquals(-1, signShort((short)Short.MIN_VALUE));
154     expectEquals(-1, signShort((short)-12345));
155     expectEquals(-1, signShort((short)-1));
156     expectEquals(0, signShort((short)0));
157     expectEquals(1, signShort((short)1));
158     expectEquals(1, signShort((short)12345));
159     expectEquals(1, signShort((short)Short.MAX_VALUE));
160   }
161 
testSignChar()162   public static void testSignChar() {
163     expectEquals(0, signChar((char)0));
164     expectEquals(1, signChar((char)1));
165     expectEquals(1, signChar((char)12345));
166     expectEquals(1, signChar((char)Character.MAX_VALUE));
167   }
168 
testSignInt()169   public static void testSignInt() {
170     expectEquals(-1, signInt(Integer.MIN_VALUE));
171     expectEquals(-1, signInt(-12345));
172     expectEquals(-1, signInt(-1));
173     expectEquals(0, signInt(0));
174     expectEquals(1, signInt(1));
175     expectEquals(1, signInt(12345));
176     expectEquals(1, signInt(Integer.MAX_VALUE));
177 
178     for (int i = -11; i <= 11; i++) {
179       int expected = 0;
180       if (i < 0) expected = -1;
181       else if (i > 0) expected = 1;
182       expectEquals(expected, signInt(i));
183     }
184   }
185 
testSignLong()186   public static void testSignLong() {
187     expectEquals(-1, signLong(Long.MIN_VALUE));
188     expectEquals(-1, signLong(-12345L));
189     expectEquals(-1, signLong(-1L));
190     expectEquals(0, signLong(0L));
191     expectEquals(1, signLong(1L));
192     expectEquals(1, signLong(12345L));
193     expectEquals(1, signLong(Long.MAX_VALUE));
194 
195     expectEquals(-1, signLong(0x800000007FFFFFFFL));
196     expectEquals(-1, signLong(0x80000000FFFFFFFFL));
197     expectEquals(1, signLong(0x000000007FFFFFFFL));
198     expectEquals(1, signLong(0x00000000FFFFFFFFL));
199     expectEquals(1, signLong(0x7FFFFFFF7FFFFFFFL));
200     expectEquals(1, signLong(0x7FFFFFFFFFFFFFFFL));
201 
202     for (long i = -11L; i <= 11L; i++) {
203       int expected = 0;
204       if (i < 0) expected = -1;
205       else if (i > 0) expected = 1;
206       expectEquals(expected, signLong(i));
207     }
208 
209     for (long i = Long.MIN_VALUE; i <= Long.MIN_VALUE + 11L; i++) {
210       expectEquals(-1, signLong(i));
211     }
212 
213     for (long i = Long.MAX_VALUE; i >= Long.MAX_VALUE - 11L; i--) {
214       expectEquals(1, signLong(i));
215     }
216   }
217 
218 
main(String args[])219   public static void main(String args[]) {
220     testSignBoolean();
221     testSignByte();
222     testSignShort();
223     testSignChar();
224     testSignInt();
225     testSignLong();
226 
227     System.out.println("passed");
228   }
229 
expectEquals(int expected, int result)230   private static void expectEquals(int expected, int result) {
231     if (expected != result) {
232       throw new Error("Expected: " + expected + ", found: " + result);
233     }
234   }
235 }
236