blob: cabf6355548d7c8925497043e390783fac94d9b4 [file] [log] [blame]
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001/*
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
Roland Levillainc2abe2f2015-08-03 15:20:02 +010017// Note that $opt$ is a marker for the optimizing compiler to test
Roland Levillain2e07b4f2014-10-23 18:12:09 +010018// it does compile the method.
19public class Main {
20
Roland Levillain3dbcb382014-10-28 17:30:07 +000021 public static void assertEquals(int expected, int result) {
Roland Levillain2e07b4f2014-10-23 18:12:09 +010022 if (expected != result) {
23 throw new Error("Expected: " + expected + ", found: " + result);
24 }
25 }
26
Roland Levillain3dbcb382014-10-28 17:30:07 +000027 public static void assertEquals(long expected, long result) {
Roland Levillain2e07b4f2014-10-23 18:12:09 +010028 if (expected != result) {
29 throw new Error("Expected: " + expected + ", found: " + result);
30 }
31 }
32
Roland Levillain3dbcb382014-10-28 17:30:07 +000033 public static void assertEquals(float expected, float result) {
34 if (expected != result) {
35 throw new Error("Expected: " + expected + ", found: " + result);
36 }
37 }
38
Roland Levillain5368c212014-11-27 15:03:41 +000039 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
Roland Levillain3dbcb382014-10-28 17:30:07 +000045 public static void assertEquals(double expected, double result) {
46 if (expected != result) {
47 throw new Error("Expected: " + expected + ", found: " + result);
48 }
49 }
50
Roland Levillain5368c212014-11-27 15:03:41 +000051 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
Roland Levillain3dbcb382014-10-28 17:30:07 +000057 public static void assertIsNaN(float result) {
58 if (!Float.isNaN(result)) {
59 throw new Error("Expected NaN: " + result);
60 }
61 }
62
63 public static void assertIsNaN(double result) {
64 if (!Double.isNaN(result)) {
65 throw new Error("Expected NaN: " + result);
66 }
67 }
68
Roland Levillain2e07b4f2014-10-23 18:12:09 +010069 public static void main(String[] args) {
70 negInt();
71 $opt$InplaceNegOneInt(1);
72
73 negLong();
74 $opt$InplaceNegOneLong(1L);
Roland Levillain3dbcb382014-10-28 17:30:07 +000075
76 negFloat();
77 negDouble();
Roland Levillain2e07b4f2014-10-23 18:12:09 +010078 }
79
80 private static void negInt() {
Roland Levillain3dbcb382014-10-28 17:30:07 +000081 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));
Roland Levillain946e1432014-11-11 17:35:19 +000086 assertEquals(2147483647, $opt$NegInt(-2147483647)); // -(2^31 - 1)
87 assertEquals(-2147483647, $opt$NegInt(2147483647)); // 2^31 - 1
Roland Levillain2e07b4f2014-10-23 18:12:09 +010088 // 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.''
Roland Levillain3dbcb382014-10-28 17:30:07 +000098 assertEquals(-2147483648, $opt$NegInt(-2147483648)); // -(2^31)
Roland Levillain2e07b4f2014-10-23 18:12:09 +010099 }
100
101 private static void $opt$InplaceNegOneInt(int a) {
102 a = -a;
Roland Levillain3dbcb382014-10-28 17:30:07 +0000103 assertEquals(-1, a);
Roland Levillain2e07b4f2014-10-23 18:12:09 +0100104 }
105
106 private static void negLong() {
Roland Levillain3dbcb382014-10-28 17:30:07 +0000107 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));
Roland Levillain2e07b4f2014-10-23 18:12:09 +0100112
Roland Levillain946e1432014-11-11 17:35:19 +0000113 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
Roland Levillain2e07b4f2014-10-23 18:12:09 +0100117
Roland Levillain946e1432014-11-11 17:35:19 +0000118 assertEquals(9223372036854775807L, $opt$NegLong(-9223372036854775807L)); // -(2^63 - 1)
119 assertEquals(-9223372036854775807L, $opt$NegLong(9223372036854775807L)); // 2^63 - 1
Roland Levillain2e07b4f2014-10-23 18:12:09 +0100120 // See remark regarding the negation of the maximum negative
121 // (long) value in negInt().
Roland Levillain3dbcb382014-10-28 17:30:07 +0000122 assertEquals(-9223372036854775808L, $opt$NegLong(-9223372036854775808L)); // -(2^63)
Roland Levillain2e07b4f2014-10-23 18:12:09 +0100123 }
124
125 private static void $opt$InplaceNegOneLong(long a) {
126 a = -a;
Roland Levillain3dbcb382014-10-28 17:30:07 +0000127 assertEquals(-1L, a);
128 }
129
130 private static void negFloat() {
Roland Levillain5368c212014-11-27 15:03:41 +0000131 assertEquals("-0.0", $opt$NegFloat(0F));
132 assertEquals("0.0", $opt$NegFloat(-0F));
Roland Levillain3dbcb382014-10-28 17:30:07 +0000133 assertEquals(-1F, $opt$NegFloat(1F));
134 assertEquals(1F, $opt$NegFloat(-1F));
Roland Levillain3dbcb382014-10-28 17:30:07 +0000135 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
155 private static void negDouble() {
Roland Levillain5368c212014-11-27 15:03:41 +0000156 assertEquals("-0.0", $opt$NegDouble(0D));
157 assertEquals("0.0", $opt$NegDouble(-0D));
Roland Levillain3dbcb382014-10-28 17:30:07 +0000158 assertEquals(-1D, $opt$NegDouble(1D));
159 assertEquals(1D, $opt$NegDouble(-1D));
Roland Levillain3dbcb382014-10-28 17:30:07 +0000160 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));
Roland Levillain2e07b4f2014-10-23 18:12:09 +0100178 }
179
180 static int $opt$NegInt(int a){
181 return -a;
182 }
183
184 static long $opt$NegLong(long a){
185 return -a;
186 }
Roland Levillain3dbcb382014-10-28 17:30:07 +0000187
188 static float $opt$NegFloat(float a){
189 return -a;
190 }
191
192 static double $opt$NegDouble(double a){
193 return -a;
194 }
Roland Levillain2e07b4f2014-10-23 18:12:09 +0100195}