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 ensure 18 // it does compile the method. 19 public class Main { 20 assertEquals(int expected, int result)21 public static void assertEquals(int expected, int result) { 22 if (expected != result) { 23 throw new Error("Expected: " + expected + ", found: " + result); 24 } 25 } 26 assertEquals(long expected, long result)27 public static void assertEquals(long expected, long result) { 28 if (expected != result) { 29 throw new Error("Expected: " + expected + ", found: " + result); 30 } 31 } 32 assertEquals(float expected, float result)33 public static void assertEquals(float expected, float result) { 34 if (expected != result) { 35 throw new Error("Expected: " + expected + ", found: " + result); 36 } 37 } 38 assertEquals(String expected, float result)39 public static void assertEquals(String expected, float result) { 40 if (!expected.equals(new Float(result).toString())) { 41 throw new Error("Expected: " + expected + ", found: " + result); 42 } 43 } 44 assertEquals(double expected, double result)45 public static void assertEquals(double expected, double result) { 46 if (expected != result) { 47 throw new Error("Expected: " + expected + ", found: " + result); 48 } 49 } 50 assertEquals(String expected, double result)51 public static void assertEquals(String expected, double result) { 52 if (!expected.equals(new Double(result).toString())) { 53 throw new Error("Expected: " + expected + ", found: " + result); 54 } 55 } 56 assertIsNaN(float result)57 public static void assertIsNaN(float result) { 58 if (!Float.isNaN(result)) { 59 throw new Error("Expected NaN: " + result); 60 } 61 } 62 assertIsNaN(double result)63 public static void assertIsNaN(double result) { 64 if (!Double.isNaN(result)) { 65 throw new Error("Expected NaN: " + result); 66 } 67 } 68 main(String[] args)69 public static void main(String[] args) { 70 negInt(); 71 $opt$InplaceNegOneInt(1); 72 73 negLong(); 74 $opt$InplaceNegOneLong(1L); 75 76 negFloat(); 77 negDouble(); 78 } 79 negInt()80 private static void negInt() { 81 assertEquals(-1, $opt$NegInt(1)); 82 assertEquals(1, $opt$NegInt(-1)); 83 assertEquals(0, $opt$NegInt(0)); 84 assertEquals(51, $opt$NegInt(-51)); 85 assertEquals(-51, $opt$NegInt(51)); 86 assertEquals(2147483647, $opt$NegInt(-2147483647)); // -(2^31 - 1) 87 assertEquals(-2147483647, $opt$NegInt(2147483647)); // 2^31 - 1 88 // From the Java 7 SE Edition specification: 89 // http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.15.4 90 // 91 // For integer values, negation is the same as subtraction from 92 // zero. The Java programming language uses two's-complement 93 // representation for integers, and the range of two's-complement 94 // values is not symmetric, so negation of the maximum negative 95 // int or long results in that same maximum negative number. 96 // Overflow occurs in this case, but no exception is thrown. 97 // For all integer values x, -x equals (~x)+1.'' 98 assertEquals(-2147483648, $opt$NegInt(-2147483648)); // -(2^31) 99 } 100 $opt$InplaceNegOneInt(int a)101 private static void $opt$InplaceNegOneInt(int a) { 102 a = -a; 103 assertEquals(-1, a); 104 } 105 negLong()106 private static void negLong() { 107 assertEquals(-1L, $opt$NegLong(1L)); 108 assertEquals(1L, $opt$NegLong(-1L)); 109 assertEquals(0L, $opt$NegLong(0L)); 110 assertEquals(51L, $opt$NegLong(-51L)); 111 assertEquals(-51L, $opt$NegLong(51L)); 112 113 assertEquals(2147483647L, $opt$NegLong(-2147483647L)); // -(2^31 - 1) 114 assertEquals(-2147483647L, $opt$NegLong(2147483647L)); // (2^31 - 1) 115 assertEquals(2147483648L, $opt$NegLong(-2147483648L)); // -(2^31) 116 assertEquals(-2147483648L, $opt$NegLong(2147483648L)); // 2^31 117 118 assertEquals(9223372036854775807L, $opt$NegLong(-9223372036854775807L)); // -(2^63 - 1) 119 assertEquals(-9223372036854775807L, $opt$NegLong(9223372036854775807L)); // 2^63 - 1 120 // See remark regarding the negation of the maximum negative 121 // (long) value in negInt(). 122 assertEquals(-9223372036854775808L, $opt$NegLong(-9223372036854775808L)); // -(2^63) 123 } 124 $opt$InplaceNegOneLong(long a)125 private static void $opt$InplaceNegOneLong(long a) { 126 a = -a; 127 assertEquals(-1L, a); 128 } 129 negFloat()130 private static void negFloat() { 131 assertEquals("-0.0", $opt$NegFloat(0F)); 132 assertEquals("0.0", $opt$NegFloat(-0F)); 133 assertEquals(-1F, $opt$NegFloat(1F)); 134 assertEquals(1F, $opt$NegFloat(-1F)); 135 assertEquals(51F, $opt$NegFloat(-51F)); 136 assertEquals(-51F, $opt$NegFloat(51F)); 137 138 assertEquals(-0.1F, $opt$NegFloat(0.1F)); 139 assertEquals(0.1F, $opt$NegFloat(-0.1F)); 140 assertEquals(343597.38362F, $opt$NegFloat(-343597.38362F)); 141 assertEquals(-343597.38362F, $opt$NegFloat(343597.38362F)); 142 143 assertEquals(-Float.MIN_NORMAL, $opt$NegFloat(Float.MIN_NORMAL)); 144 assertEquals(Float.MIN_NORMAL, $opt$NegFloat(-Float.MIN_NORMAL)); 145 assertEquals(-Float.MIN_VALUE, $opt$NegFloat(Float.MIN_VALUE)); 146 assertEquals(Float.MIN_VALUE, $opt$NegFloat(-Float.MIN_VALUE)); 147 assertEquals(-Float.MAX_VALUE, $opt$NegFloat(Float.MAX_VALUE)); 148 assertEquals(Float.MAX_VALUE, $opt$NegFloat(-Float.MAX_VALUE)); 149 150 assertEquals(Float.NEGATIVE_INFINITY, $opt$NegFloat(Float.POSITIVE_INFINITY)); 151 assertEquals(Float.POSITIVE_INFINITY, $opt$NegFloat(Float.NEGATIVE_INFINITY)); 152 assertIsNaN($opt$NegFloat(Float.NaN)); 153 } 154 negDouble()155 private static void negDouble() { 156 assertEquals("-0.0", $opt$NegDouble(0D)); 157 assertEquals("0.0", $opt$NegDouble(-0D)); 158 assertEquals(-1D, $opt$NegDouble(1D)); 159 assertEquals(1D, $opt$NegDouble(-1D)); 160 assertEquals(51D, $opt$NegDouble(-51D)); 161 assertEquals(-51D, $opt$NegDouble(51D)); 162 163 assertEquals(-0.1D, $opt$NegDouble(0.1D)); 164 assertEquals(0.1D, $opt$NegDouble(-0.1D)); 165 assertEquals(343597.38362D, $opt$NegDouble(-343597.38362D)); 166 assertEquals(-343597.38362D, $opt$NegDouble(343597.38362D)); 167 168 assertEquals(-Double.MIN_NORMAL, $opt$NegDouble(Double.MIN_NORMAL)); 169 assertEquals(Double.MIN_NORMAL, $opt$NegDouble(-Double.MIN_NORMAL)); 170 assertEquals(-Double.MIN_VALUE, $opt$NegDouble(Double.MIN_VALUE)); 171 assertEquals(Double.MIN_VALUE, $opt$NegDouble(-Double.MIN_VALUE)); 172 assertEquals(-Double.MAX_VALUE, $opt$NegDouble(Double.MAX_VALUE)); 173 assertEquals(Double.MAX_VALUE, $opt$NegDouble(-Double.MAX_VALUE)); 174 175 assertEquals(Double.NEGATIVE_INFINITY, $opt$NegDouble(Double.POSITIVE_INFINITY)); 176 assertEquals(Double.POSITIVE_INFINITY, $opt$NegDouble(Double.NEGATIVE_INFINITY)); 177 assertIsNaN($opt$NegDouble(Double.NaN)); 178 } 179 $opt$NegInt(int a)180 static int $opt$NegInt(int a){ 181 return -a; 182 } 183 $opt$NegLong(long a)184 static long $opt$NegLong(long a){ 185 return -a; 186 } 187 $opt$NegFloat(float a)188 static float $opt$NegFloat(float a){ 189 return -a; 190 } 191 $opt$NegDouble(double a)192 static double $opt$NegDouble(double a){ 193 return -a; 194 } 195 } 196