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