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_bce (after) 96 /// CHECK-DAG: <<Arg:z\d+>> ParameterValue 97 /// CHECK-DAG: <<Zero:i\d+>> IntConstant 0 98 /// CHECK-DAG: <<Result:i\d+>> Compare [<<Arg>>,<<Zero>>] 99 /// CHECK-DAG: Return [<<Result>>] 100 101 /// CHECK-START: int TestSignum.signBoolean(boolean) instruction_simplifier$after_bce (after) 102 /// CHECK-NOT: Select 103 signBoolean(boolean x)104 private static int signBoolean(boolean x) { 105 // Note: D8 would replace the ternary expression `x ? 1 : 0` with `x` 106 // but explicit `if` is preserved. 107 int src_x; 108 if (x) { 109 src_x = 1; 110 } else { 111 src_x = 0; 112 } 113 return Integer.signum(src_x); 114 } 115 testSignBoolean()116 public static void testSignBoolean() { 117 expectEquals(0, signBoolean(false)); 118 expectEquals(1, signBoolean(true)); 119 } 120 testSignByte()121 public static void testSignByte() { 122 expectEquals(-1, signByte((byte)Byte.MIN_VALUE)); 123 expectEquals(-1, signByte((byte)-64)); 124 expectEquals(-1, signByte((byte)-1)); 125 expectEquals(0, signByte((byte)0)); 126 expectEquals(1, signByte((byte)1)); 127 expectEquals(1, signByte((byte)64)); 128 expectEquals(1, signByte((byte)Byte.MAX_VALUE)); 129 } 130 testSignShort()131 public static void testSignShort() { 132 expectEquals(-1, signShort((short)Short.MIN_VALUE)); 133 expectEquals(-1, signShort((short)-12345)); 134 expectEquals(-1, signShort((short)-1)); 135 expectEquals(0, signShort((short)0)); 136 expectEquals(1, signShort((short)1)); 137 expectEquals(1, signShort((short)12345)); 138 expectEquals(1, signShort((short)Short.MAX_VALUE)); 139 } 140 testSignChar()141 public static void testSignChar() { 142 expectEquals(0, signChar((char)0)); 143 expectEquals(1, signChar((char)1)); 144 expectEquals(1, signChar((char)12345)); 145 expectEquals(1, signChar((char)Character.MAX_VALUE)); 146 } 147 testSignInt()148 public static void testSignInt() { 149 expectEquals(-1, signInt(Integer.MIN_VALUE)); 150 expectEquals(-1, signInt(-12345)); 151 expectEquals(-1, signInt(-1)); 152 expectEquals(0, signInt(0)); 153 expectEquals(1, signInt(1)); 154 expectEquals(1, signInt(12345)); 155 expectEquals(1, signInt(Integer.MAX_VALUE)); 156 157 for (int i = -11; i <= 11; i++) { 158 int expected = 0; 159 if (i < 0) expected = -1; 160 else if (i > 0) expected = 1; 161 expectEquals(expected, signInt(i)); 162 } 163 } 164 testSignLong()165 public static void testSignLong() { 166 expectEquals(-1, signLong(Long.MIN_VALUE)); 167 expectEquals(-1, signLong(-12345L)); 168 expectEquals(-1, signLong(-1L)); 169 expectEquals(0, signLong(0L)); 170 expectEquals(1, signLong(1L)); 171 expectEquals(1, signLong(12345L)); 172 expectEquals(1, signLong(Long.MAX_VALUE)); 173 174 expectEquals(-1, signLong(0x800000007FFFFFFFL)); 175 expectEquals(-1, signLong(0x80000000FFFFFFFFL)); 176 expectEquals(1, signLong(0x000000007FFFFFFFL)); 177 expectEquals(1, signLong(0x00000000FFFFFFFFL)); 178 expectEquals(1, signLong(0x7FFFFFFF7FFFFFFFL)); 179 expectEquals(1, signLong(0x7FFFFFFFFFFFFFFFL)); 180 181 for (long i = -11L; i <= 11L; i++) { 182 int expected = 0; 183 if (i < 0) expected = -1; 184 else if (i > 0) expected = 1; 185 expectEquals(expected, signLong(i)); 186 } 187 188 for (long i = Long.MIN_VALUE; i <= Long.MIN_VALUE + 11L; i++) { 189 expectEquals(-1, signLong(i)); 190 } 191 192 for (long i = Long.MAX_VALUE; i >= Long.MAX_VALUE - 11L; i--) { 193 expectEquals(1, signLong(i)); 194 } 195 } 196 197 main()198 public static void main() { 199 testSignBoolean(); 200 testSignByte(); 201 testSignShort(); 202 testSignChar(); 203 testSignInt(); 204 testSignLong(); 205 206 System.out.println("TestSignum passed"); 207 } 208 expectEquals(int expected, int result)209 private static void expectEquals(int expected, int result) { 210 if (expected != result) { 211 throw new Error("Expected: " + expected + ", found: " + result); 212 } 213 } 214 } 215