blob: 2b3ba33a6a3cc92a61f3acdb05ff1a51cacf8d27 [file] [log] [blame]
Calin Juravle34bacdf2014-10-07 20:23:36 +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.
19
20public 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 Levillain88cb1752014-10-20 16:36:47 +010036 neg();
Calin Juravle34bacdf2014-10-07 20:23:36 +010037 }
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 Levillain88cb1752014-10-20 16:36:47 +010055
56 $opt$InplaceNegOne(1);
57 }
58
59 public static void neg() {
60 expectEquals(-1, $opt$Neg(1));
61 expectEquals(1, $opt$Neg(-1));
62 expectEquals(0, $opt$Neg(0));
63 expectEquals(51, $opt$Neg(-51));
64 expectEquals(-51, $opt$Neg(51));
65 expectEquals(2147483647, $opt$Neg(-2147483647)); // (2^31 - 1)
66 expectEquals(-2147483647, $opt$Neg(2147483647)); // -(2^31 - 1)
67 // From the Java 7 SE Edition specification:
68 // http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.15.4
69 //
70 // For integer values, negation is the same as subtraction from
71 // zero. The Java programming language uses two's-complement
72 // representation for integers, and the range of two's-complement
73 // values is not symmetric, so negation of the maximum negative
74 // int or long results in that same maximum negative number.
75 // Overflow occurs in this case, but no exception is thrown.
76 // For all integer values x, -x equals (~x)+1.''
77 expectEquals(-2147483648, $opt$Neg(-2147483648)); // -(2^31)
78 }
79
80 public static void $opt$InplaceNegOne(int a) {
81 a = -a;
82 expectEquals(-1, a);
Calin Juravle34bacdf2014-10-07 20:23:36 +010083 }
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 Levillain88cb1752014-10-20 16:36:47 +010093 static int $opt$Neg(int a){
94 return -a;
95 }
Calin Juravle34bacdf2014-10-07 20:23:36 +010096}