Roland Levillain | 2e07b4f | 2014-10-23 18:12:09 +0100 | [diff] [blame^] | 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 | |
| 21 | public static void expectEquals(int expected, int result) { |
| 22 | if (expected != result) { |
| 23 | throw new Error("Expected: " + expected + ", found: " + result); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | public static void expectEquals(long expected, long result) { |
| 28 | if (expected != result) { |
| 29 | throw new Error("Expected: " + expected + ", found: " + result); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | public static void main(String[] args) { |
| 34 | negInt(); |
| 35 | $opt$InplaceNegOneInt(1); |
| 36 | |
| 37 | negLong(); |
| 38 | $opt$InplaceNegOneLong(1L); |
| 39 | } |
| 40 | |
| 41 | private static void negInt() { |
| 42 | expectEquals(-1, $opt$NegInt(1)); |
| 43 | expectEquals(1, $opt$NegInt(-1)); |
| 44 | expectEquals(0, $opt$NegInt(0)); |
| 45 | expectEquals(51, $opt$NegInt(-51)); |
| 46 | expectEquals(-51, $opt$NegInt(51)); |
| 47 | expectEquals(2147483647, $opt$NegInt(-2147483647)); // (2^31 - 1) |
| 48 | expectEquals(-2147483647, $opt$NegInt(2147483647)); // -(2^31 - 1) |
| 49 | // From the Java 7 SE Edition specification: |
| 50 | // http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.15.4 |
| 51 | // |
| 52 | // For integer values, negation is the same as subtraction from |
| 53 | // zero. The Java programming language uses two's-complement |
| 54 | // representation for integers, and the range of two's-complement |
| 55 | // values is not symmetric, so negation of the maximum negative |
| 56 | // int or long results in that same maximum negative number. |
| 57 | // Overflow occurs in this case, but no exception is thrown. |
| 58 | // For all integer values x, -x equals (~x)+1.'' |
| 59 | expectEquals(-2147483648, $opt$NegInt(-2147483648)); // -(2^31) |
| 60 | } |
| 61 | |
| 62 | private static void $opt$InplaceNegOneInt(int a) { |
| 63 | a = -a; |
| 64 | expectEquals(-1, a); |
| 65 | } |
| 66 | |
| 67 | private static void negLong() { |
| 68 | expectEquals(-1L, $opt$NegLong(1L)); |
| 69 | expectEquals(1L, $opt$NegLong(-1L)); |
| 70 | expectEquals(0L, $opt$NegLong(0L)); |
| 71 | expectEquals(51L, $opt$NegLong(-51L)); |
| 72 | expectEquals(-51L, $opt$NegLong(51L)); |
| 73 | |
| 74 | expectEquals(2147483647L, $opt$NegLong(-2147483647L)); // (2^31 - 1) |
| 75 | expectEquals(-2147483647L, $opt$NegLong(2147483647L)); // -(2^31 - 1) |
| 76 | expectEquals(2147483648L, $opt$NegLong(-2147483648L)); // 2^31 |
| 77 | expectEquals(-2147483648L, $opt$NegLong(2147483648L)); // -(2^31) |
| 78 | |
| 79 | expectEquals(9223372036854775807L, $opt$NegLong(-9223372036854775807L)); // (2^63 - 1) |
| 80 | expectEquals(-9223372036854775807L, $opt$NegLong(9223372036854775807L)); // -(2^63 - 1) |
| 81 | // See remark regarding the negation of the maximum negative |
| 82 | // (long) value in negInt(). |
| 83 | expectEquals(-9223372036854775808L, $opt$NegLong(-9223372036854775808L)); // -(2^63) |
| 84 | } |
| 85 | |
| 86 | private static void $opt$InplaceNegOneLong(long a) { |
| 87 | a = -a; |
| 88 | expectEquals(-1L, a); |
| 89 | } |
| 90 | |
| 91 | static int $opt$NegInt(int a){ |
| 92 | return -a; |
| 93 | } |
| 94 | |
| 95 | static long $opt$NegLong(long a){ |
| 96 | return -a; |
| 97 | } |
| 98 | } |