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