blob: e2850ca760618c80d0b50dd56b08cc678ae06264 [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
17// Note that $opt$ is a marker for the optimizing compiler to ensure
18// 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
39 public static void assertEquals(double expected, double result) {
40 if (expected != result) {
41 throw new Error("Expected: " + expected + ", found: " + result);
42 }
43 }
44
45 public static void assertIsNaN(float result) {
46 if (!Float.isNaN(result)) {
47 throw new Error("Expected NaN: " + result);
48 }
49 }
50
51 public static void assertIsNaN(double result) {
52 if (!Double.isNaN(result)) {
53 throw new Error("Expected NaN: " + result);
54 }
55 }
56
Roland Levillain2e07b4f2014-10-23 18:12:09 +010057 public static void main(String[] args) {
58 negInt();
59 $opt$InplaceNegOneInt(1);
60
61 negLong();
62 $opt$InplaceNegOneLong(1L);
Roland Levillain3dbcb382014-10-28 17:30:07 +000063
64 negFloat();
65 negDouble();
Roland Levillain2e07b4f2014-10-23 18:12:09 +010066 }
67
68 private static void negInt() {
Roland Levillain3dbcb382014-10-28 17:30:07 +000069 assertEquals(-1, $opt$NegInt(1));
70 assertEquals(1, $opt$NegInt(-1));
71 assertEquals(0, $opt$NegInt(0));
72 assertEquals(51, $opt$NegInt(-51));
73 assertEquals(-51, $opt$NegInt(51));
74 assertEquals(2147483647, $opt$NegInt(-2147483647)); // (2^31 - 1)
75 assertEquals(-2147483647, $opt$NegInt(2147483647)); // -(2^31 - 1)
Roland Levillain2e07b4f2014-10-23 18:12:09 +010076 // From the Java 7 SE Edition specification:
77 // http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.15.4
78 //
79 // For integer values, negation is the same as subtraction from
80 // zero. The Java programming language uses two's-complement
81 // representation for integers, and the range of two's-complement
82 // values is not symmetric, so negation of the maximum negative
83 // int or long results in that same maximum negative number.
84 // Overflow occurs in this case, but no exception is thrown.
85 // For all integer values x, -x equals (~x)+1.''
Roland Levillain3dbcb382014-10-28 17:30:07 +000086 assertEquals(-2147483648, $opt$NegInt(-2147483648)); // -(2^31)
Roland Levillain2e07b4f2014-10-23 18:12:09 +010087 }
88
89 private static void $opt$InplaceNegOneInt(int a) {
90 a = -a;
Roland Levillain3dbcb382014-10-28 17:30:07 +000091 assertEquals(-1, a);
Roland Levillain2e07b4f2014-10-23 18:12:09 +010092 }
93
94 private static void negLong() {
Roland Levillain3dbcb382014-10-28 17:30:07 +000095 assertEquals(-1L, $opt$NegLong(1L));
96 assertEquals(1L, $opt$NegLong(-1L));
97 assertEquals(0L, $opt$NegLong(0L));
98 assertEquals(51L, $opt$NegLong(-51L));
99 assertEquals(-51L, $opt$NegLong(51L));
Roland Levillain2e07b4f2014-10-23 18:12:09 +0100100
Roland Levillain3dbcb382014-10-28 17:30:07 +0000101 assertEquals(2147483647L, $opt$NegLong(-2147483647L)); // (2^31 - 1)
102 assertEquals(-2147483647L, $opt$NegLong(2147483647L)); // -(2^31 - 1)
103 assertEquals(2147483648L, $opt$NegLong(-2147483648L)); // 2^31
104 assertEquals(-2147483648L, $opt$NegLong(2147483648L)); // -(2^31)
Roland Levillain2e07b4f2014-10-23 18:12:09 +0100105
Roland Levillain3dbcb382014-10-28 17:30:07 +0000106 assertEquals(9223372036854775807L, $opt$NegLong(-9223372036854775807L)); // (2^63 - 1)
107 assertEquals(-9223372036854775807L, $opt$NegLong(9223372036854775807L)); // -(2^63 - 1)
Roland Levillain2e07b4f2014-10-23 18:12:09 +0100108 // See remark regarding the negation of the maximum negative
109 // (long) value in negInt().
Roland Levillain3dbcb382014-10-28 17:30:07 +0000110 assertEquals(-9223372036854775808L, $opt$NegLong(-9223372036854775808L)); // -(2^63)
Roland Levillain2e07b4f2014-10-23 18:12:09 +0100111 }
112
113 private static void $opt$InplaceNegOneLong(long a) {
114 a = -a;
Roland Levillain3dbcb382014-10-28 17:30:07 +0000115 assertEquals(-1L, a);
116 }
117
118 private static void negFloat() {
119 assertEquals(-1F, $opt$NegFloat(1F));
120 assertEquals(1F, $opt$NegFloat(-1F));
121 assertEquals(0F, $opt$NegFloat(0F));
122 assertEquals(51F, $opt$NegFloat(-51F));
123 assertEquals(-51F, $opt$NegFloat(51F));
124
125 assertEquals(-0.1F, $opt$NegFloat(0.1F));
126 assertEquals(0.1F, $opt$NegFloat(-0.1F));
127 assertEquals(343597.38362F, $opt$NegFloat(-343597.38362F));
128 assertEquals(-343597.38362F, $opt$NegFloat(343597.38362F));
129
130 assertEquals(-Float.MIN_NORMAL, $opt$NegFloat(Float.MIN_NORMAL));
131 assertEquals(Float.MIN_NORMAL, $opt$NegFloat(-Float.MIN_NORMAL));
132 assertEquals(-Float.MIN_VALUE, $opt$NegFloat(Float.MIN_VALUE));
133 assertEquals(Float.MIN_VALUE, $opt$NegFloat(-Float.MIN_VALUE));
134 assertEquals(-Float.MAX_VALUE, $opt$NegFloat(Float.MAX_VALUE));
135 assertEquals(Float.MAX_VALUE, $opt$NegFloat(-Float.MAX_VALUE));
136
137 assertEquals(Float.NEGATIVE_INFINITY, $opt$NegFloat(Float.POSITIVE_INFINITY));
138 assertEquals(Float.POSITIVE_INFINITY, $opt$NegFloat(Float.NEGATIVE_INFINITY));
139 assertIsNaN($opt$NegFloat(Float.NaN));
140 }
141
142 private static void negDouble() {
143 assertEquals(-1D, $opt$NegDouble(1D));
144 assertEquals(1D, $opt$NegDouble(-1D));
145 assertEquals(0D, $opt$NegDouble(0D));
146 assertEquals(51D, $opt$NegDouble(-51D));
147 assertEquals(-51D, $opt$NegDouble(51D));
148
149 assertEquals(-0.1D, $opt$NegDouble(0.1D));
150 assertEquals(0.1D, $opt$NegDouble(-0.1D));
151 assertEquals(343597.38362D, $opt$NegDouble(-343597.38362D));
152 assertEquals(-343597.38362D, $opt$NegDouble(343597.38362D));
153
154 assertEquals(-Double.MIN_NORMAL, $opt$NegDouble(Double.MIN_NORMAL));
155 assertEquals(Double.MIN_NORMAL, $opt$NegDouble(-Double.MIN_NORMAL));
156 assertEquals(-Double.MIN_VALUE, $opt$NegDouble(Double.MIN_VALUE));
157 assertEquals(Double.MIN_VALUE, $opt$NegDouble(-Double.MIN_VALUE));
158 assertEquals(-Double.MAX_VALUE, $opt$NegDouble(Double.MAX_VALUE));
159 assertEquals(Double.MAX_VALUE, $opt$NegDouble(-Double.MAX_VALUE));
160
161 assertEquals(Double.NEGATIVE_INFINITY, $opt$NegDouble(Double.POSITIVE_INFINITY));
162 assertEquals(Double.POSITIVE_INFINITY, $opt$NegDouble(Double.NEGATIVE_INFINITY));
163 assertIsNaN($opt$NegDouble(Double.NaN));
Roland Levillain2e07b4f2014-10-23 18:12:09 +0100164 }
165
166 static int $opt$NegInt(int a){
167 return -a;
168 }
169
170 static long $opt$NegLong(long a){
171 return -a;
172 }
Roland Levillain3dbcb382014-10-28 17:30:07 +0000173
174 static float $opt$NegFloat(float a){
175 return -a;
176 }
177
178 static double $opt$NegDouble(double a){
179 return -a;
180 }
Roland Levillain2e07b4f2014-10-23 18:12:09 +0100181}