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(); |
| 36 | } |
| 37 | |
| 38 | public static void mul() { |
| 39 | expectEquals(15, $opt$Mul(5, 3)); |
| 40 | expectEquals(0, $opt$Mul(0, 3)); |
| 41 | expectEquals(0, $opt$Mul(3, 0)); |
| 42 | expectEquals(-3, $opt$Mul(1, -3)); |
| 43 | expectEquals(36, $opt$Mul(-12, -3)); |
| 44 | expectEquals(33, $opt$Mul(1, 3) * 11); |
| 45 | expectEquals(671088645, $opt$Mul(134217729, 5)); // (2^27 + 1) * 5 |
| 46 | |
| 47 | expectEquals(15L, $opt$Mul(5L, 3L)); |
| 48 | expectEquals(0L, $opt$Mul(0L, 3L)); |
| 49 | expectEquals(0L, $opt$Mul(3L, 0L)); |
| 50 | expectEquals(-3L, $opt$Mul(1L, -3L)); |
| 51 | expectEquals(36L, $opt$Mul(-12L, -3L)); |
| 52 | expectEquals(33L, $opt$Mul(1L, 3L) * 11); |
| 53 | expectEquals(240518168583L, $opt$Mul(34359738369L, 7L)); // (2^35 + 1) * 7 |
| 54 | } |
| 55 | |
| 56 | static int $opt$Mul(int a, int b) { |
| 57 | return a * b; |
| 58 | } |
| 59 | |
| 60 | static long $opt$Mul(long a, long b) { |
| 61 | return a * b; |
| 62 | } |
| 63 | |
| 64 | } |