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 import java.lang.reflect.Method; 18 19 public class Main { 20 21 /// CHECK-START: int Main.signByte(byte) builder (after) 22 /// CHECK-DAG: <<Result:i\d+>> InvokeStaticOrDirect intrinsic:IntegerSignum 23 /// CHECK-DAG: Return [<<Result>>] 24 25 /// CHECK-START: int Main.signByte(byte) instruction_simplifier (after) 26 /// CHECK-DAG: <<Result:i\d+>> Compare 27 /// CHECK-DAG: Return [<<Result>>] 28 29 /// CHECK-START: int Main.signByte(byte) instruction_simplifier (after) 30 /// CHECK-NOT: InvokeStaticOrDirect 31 signByte(byte x)32 private static int signByte(byte x) { 33 return Integer.signum(x); 34 } 35 36 /// CHECK-START: int Main.signShort(short) builder (after) 37 /// CHECK-DAG: <<Result:i\d+>> InvokeStaticOrDirect intrinsic:IntegerSignum 38 /// CHECK-DAG: Return [<<Result>>] 39 40 /// CHECK-START: int Main.signShort(short) instruction_simplifier (after) 41 /// CHECK-DAG: <<Result:i\d+>> Compare 42 /// CHECK-DAG: Return [<<Result>>] 43 44 /// CHECK-START: int Main.signShort(short) instruction_simplifier (after) 45 /// CHECK-NOT: InvokeStaticOrDirect 46 signShort(short x)47 private static int signShort(short x) { 48 return Integer.signum(x); 49 } 50 51 /// CHECK-START: int Main.signChar(char) builder (after) 52 /// CHECK-DAG: <<Result:i\d+>> InvokeStaticOrDirect intrinsic:IntegerSignum 53 /// CHECK-DAG: Return [<<Result>>] 54 55 /// CHECK-START: int Main.signChar(char) instruction_simplifier (after) 56 /// CHECK-DAG: <<Result:i\d+>> Compare 57 /// CHECK-DAG: Return [<<Result>>] 58 59 /// CHECK-START: int Main.signChar(char) instruction_simplifier (after) 60 /// CHECK-NOT: InvokeStaticOrDirect 61 signChar(char x)62 private static int signChar(char x) { 63 return Integer.signum(x); 64 } 65 66 /// CHECK-START: int Main.signInt(int) builder (after) 67 /// CHECK-DAG: <<Result:i\d+>> InvokeStaticOrDirect intrinsic:IntegerSignum 68 /// CHECK-DAG: Return [<<Result>>] 69 70 /// CHECK-START: int Main.signInt(int) instruction_simplifier (after) 71 /// CHECK-DAG: <<Result:i\d+>> Compare 72 /// CHECK-DAG: Return [<<Result>>] 73 74 /// CHECK-START: int Main.signInt(int) instruction_simplifier (after) 75 /// CHECK-NOT: InvokeStaticOrDirect 76 signInt(int x)77 private static int signInt(int x) { 78 return Integer.signum(x); 79 } 80 81 /// CHECK-START: int Main.signLong(long) builder (after) 82 /// CHECK-DAG: <<Result:i\d+>> InvokeStaticOrDirect intrinsic:LongSignum 83 /// CHECK-DAG: Return [<<Result>>] 84 85 /// CHECK-START: int Main.signLong(long) instruction_simplifier (after) 86 /// CHECK-DAG: <<Result:i\d+>> Compare 87 /// CHECK-DAG: Return [<<Result>>] 88 89 /// CHECK-START: int Main.signLong(long) instruction_simplifier (after) 90 /// CHECK-NOT: InvokeStaticOrDirect 91 signLong(long x)92 private static int signLong(long x) { 93 return Long.signum(x); 94 } 95 96 testSignBoolean()97 public static void testSignBoolean() throws Exception { 98 Method signBoolean = Class.forName("Main2").getMethod("signBoolean", boolean.class); 99 expectEquals(0, (int)signBoolean.invoke(null, false)); 100 expectEquals(1, (int)signBoolean.invoke(null, true)); 101 } 102 testSignByte()103 public static void testSignByte() { 104 expectEquals(-1, signByte((byte)Byte.MIN_VALUE)); 105 expectEquals(-1, signByte((byte)-64)); 106 expectEquals(-1, signByte((byte)-1)); 107 expectEquals(0, signByte((byte)0)); 108 expectEquals(1, signByte((byte)1)); 109 expectEquals(1, signByte((byte)64)); 110 expectEquals(1, signByte((byte)Byte.MAX_VALUE)); 111 } 112 testSignShort()113 public static void testSignShort() { 114 expectEquals(-1, signShort((short)Short.MIN_VALUE)); 115 expectEquals(-1, signShort((short)-12345)); 116 expectEquals(-1, signShort((short)-1)); 117 expectEquals(0, signShort((short)0)); 118 expectEquals(1, signShort((short)1)); 119 expectEquals(1, signShort((short)12345)); 120 expectEquals(1, signShort((short)Short.MAX_VALUE)); 121 } 122 testSignChar()123 public static void testSignChar() { 124 expectEquals(0, signChar((char)0)); 125 expectEquals(1, signChar((char)1)); 126 expectEquals(1, signChar((char)12345)); 127 expectEquals(1, signChar((char)Character.MAX_VALUE)); 128 } 129 testSignInt()130 public static void testSignInt() { 131 expectEquals(-1, signInt(Integer.MIN_VALUE)); 132 expectEquals(-1, signInt(-12345)); 133 expectEquals(-1, signInt(-1)); 134 expectEquals(0, signInt(0)); 135 expectEquals(1, signInt(1)); 136 expectEquals(1, signInt(12345)); 137 expectEquals(1, signInt(Integer.MAX_VALUE)); 138 139 for (int i = -11; i <= 11; i++) { 140 int expected = 0; 141 if (i < 0) expected = -1; 142 else if (i > 0) expected = 1; 143 expectEquals(expected, signInt(i)); 144 } 145 } 146 testSignLong()147 public static void testSignLong() { 148 expectEquals(-1, signLong(Long.MIN_VALUE)); 149 expectEquals(-1, signLong(-12345L)); 150 expectEquals(-1, signLong(-1L)); 151 expectEquals(0, signLong(0L)); 152 expectEquals(1, signLong(1L)); 153 expectEquals(1, signLong(12345L)); 154 expectEquals(1, signLong(Long.MAX_VALUE)); 155 156 expectEquals(-1, signLong(0x800000007FFFFFFFL)); 157 expectEquals(-1, signLong(0x80000000FFFFFFFFL)); 158 expectEquals(1, signLong(0x000000007FFFFFFFL)); 159 expectEquals(1, signLong(0x00000000FFFFFFFFL)); 160 expectEquals(1, signLong(0x7FFFFFFF7FFFFFFFL)); 161 expectEquals(1, signLong(0x7FFFFFFFFFFFFFFFL)); 162 163 for (long i = -11L; i <= 11L; i++) { 164 int expected = 0; 165 if (i < 0) expected = -1; 166 else if (i > 0) expected = 1; 167 expectEquals(expected, signLong(i)); 168 } 169 170 for (long i = Long.MIN_VALUE; i <= Long.MIN_VALUE + 11L; i++) { 171 expectEquals(-1, signLong(i)); 172 } 173 174 for (long i = Long.MAX_VALUE; i >= Long.MAX_VALUE - 11L; i--) { 175 expectEquals(1, signLong(i)); 176 } 177 } 178 179 main(String args[])180 public static void main(String args[]) throws Exception { 181 testSignBoolean(); 182 testSignByte(); 183 testSignShort(); 184 testSignChar(); 185 testSignInt(); 186 testSignLong(); 187 188 System.out.println("passed"); 189 } 190 expectEquals(int expected, int result)191 private static void expectEquals(int expected, int result) { 192 if (expected != result) { 193 throw new Error("Expected: " + expected + ", found: " + result); 194 } 195 } 196 } 197