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 test 18 // it does compile the method. 19 public class MulTest { 20 expectEquals(int expected, int result)21 public static void expectEquals(int expected, int result) { 22 if (expected != result) { 23 throw new Error("Expected: " + expected + ", found: " + result); 24 } 25 } 26 expectEquals(long expected, long result)27 public static void expectEquals(long expected, long result) { 28 if (expected != result) { 29 throw new Error("Expected: " + expected + ", found: " + result); 30 } 31 } 32 expectEquals(float expected, float result)33 public static void expectEquals(float expected, float result) { 34 if (expected != result) { 35 throw new Error("Expected: " + expected + ", found: " + result); 36 } 37 } 38 expectEquals(double expected, double result)39 public static void expectEquals(double expected, double result) { 40 if (expected != result) { 41 throw new Error("Expected: " + expected + ", found: " + result); 42 } 43 } 44 expectApproxEquals(float a, float b, float maxDelta)45 public static void expectApproxEquals(float a, float b, float maxDelta) { 46 boolean aproxEquals = (a > b) 47 ? ((a - b) < maxDelta) 48 : ((b - a) < maxDelta); 49 if (!aproxEquals) { 50 throw new Error("Expected: " + a + ", found: " + b + ", with delta: " + maxDelta); 51 } 52 } 53 expectApproxEquals(double a, double b, double maxDelta)54 public static void expectApproxEquals(double a, double b, double maxDelta) { 55 boolean aproxEquals = (a > b) 56 ? ((a - b) < maxDelta) 57 : ((b - a) < maxDelta); 58 if (!aproxEquals) { 59 throw new Error("Expected: " + a + ", found: " + b + ", with delta: " + maxDelta); 60 } 61 } 62 expectNaN(float a)63 public static void expectNaN(float a) { 64 if (a == a) { 65 throw new Error("Expected NaN: " + a); 66 } 67 } 68 expectNaN(double a)69 public static void expectNaN(double a) { 70 if (a == a) { 71 throw new Error("Expected NaN: " + a); 72 } 73 } 74 main()75 public static void main() { 76 mulInt(); 77 mulLong(); 78 mulFloat(); 79 mulDouble(); 80 } 81 mulInt()82 private static void mulInt() { 83 expectEquals(15, $opt$Mul(5, 3)); 84 expectEquals(0, $opt$Mul(0, 0)); 85 expectEquals(0, $opt$Mul(0, 3)); 86 expectEquals(0, $opt$Mul(3, 0)); 87 expectEquals(-3, $opt$Mul(1, -3)); 88 expectEquals(36, $opt$Mul(-12, -3)); 89 expectEquals(33, $opt$Mul(1, 3) * 11); 90 expectEquals(671088645, $opt$Mul(134217729, 5)); // (2^27 + 1) * 5 91 } 92 mulLong()93 private static void mulLong() { 94 expectEquals(15L, $opt$Mul(5L, 3L)); 95 expectEquals(0L, $opt$Mul(0L, 0L)); 96 expectEquals(0L, $opt$Mul(0L, 3L)); 97 expectEquals(0L, $opt$Mul(3L, 0L)); 98 expectEquals(-3L, $opt$Mul(1L, -3L)); 99 expectEquals(36L, $opt$Mul(-12L, -3L)); 100 expectEquals(33L, $opt$Mul(1L, 3L) * 11L); 101 expectEquals(240518168583L, $opt$Mul(34359738369L, 7L)); // (2^35 + 1) * 7 102 } 103 mulFloat()104 private static void mulFloat() { 105 expectApproxEquals(15F, $opt$Mul(5F, 3F), 0.0001F); 106 expectApproxEquals(0F, $opt$Mul(0F, 0F), 0.0001F); 107 expectApproxEquals(0F, $opt$Mul(0F, 3F), 0.0001F); 108 expectApproxEquals(0F, $opt$Mul(3F, 0F), 0.0001F); 109 expectApproxEquals(-3F, $opt$Mul(1F, -3F), 0.0001F); 110 expectApproxEquals(36F, $opt$Mul(-12F, -3F), 0.0001F); 111 expectApproxEquals(33F, $opt$Mul(1F, 3F) * 11F, 0.0001F); 112 expectApproxEquals(0.02F, 0.1F * 0.2F, 0.0001F); 113 expectApproxEquals(-0.1F, -0.5F * 0.2F, 0.0001F); 114 115 expectNaN($opt$Mul(0F, Float.POSITIVE_INFINITY)); 116 expectNaN($opt$Mul(0F, Float.NEGATIVE_INFINITY)); 117 expectNaN($opt$Mul(Float.NaN, 11F)); 118 expectNaN($opt$Mul(Float.NaN, -11F)); 119 expectNaN($opt$Mul(Float.NaN, Float.NEGATIVE_INFINITY)); 120 expectNaN($opt$Mul(Float.NaN, Float.POSITIVE_INFINITY)); 121 122 expectEquals(Float.POSITIVE_INFINITY, $opt$Mul(2F, 3.40282346638528860e+38F)); 123 expectEquals(Float.POSITIVE_INFINITY, $opt$Mul(2F, Float.POSITIVE_INFINITY)); 124 expectEquals(Float.NEGATIVE_INFINITY, $opt$Mul(-2F, Float.POSITIVE_INFINITY)); 125 expectEquals(Float.NEGATIVE_INFINITY, $opt$Mul(-2F, 3.40282346638528860e+38F)); 126 expectEquals(Float.NEGATIVE_INFINITY, $opt$Mul(2F, Float.NEGATIVE_INFINITY)); 127 expectEquals(Float.POSITIVE_INFINITY, $opt$Mul(-2F, Float.NEGATIVE_INFINITY)); 128 expectEquals(Float.NEGATIVE_INFINITY, 129 $opt$Mul(Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY)); 130 expectEquals(Float.POSITIVE_INFINITY, 131 $opt$Mul(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY)); 132 expectEquals(Float.POSITIVE_INFINITY, 133 $opt$Mul(Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY)); 134 } 135 mulDouble()136 private static void mulDouble() { 137 expectApproxEquals(15D, $opt$Mul(5D, 3D), 0.0001D); 138 expectApproxEquals(0D, $opt$Mul(0D, 0D), 0.0001D); 139 expectApproxEquals(0D, $opt$Mul(0D, 3D), 0.0001D); 140 expectApproxEquals(0D, $opt$Mul(3D, 0D), 0.0001D); 141 expectApproxEquals(-3D, $opt$Mul(1D, -3D), 0.0001D); 142 expectApproxEquals(36D, $opt$Mul(-12D, -3D), 0.0001D); 143 expectApproxEquals(33D, $opt$Mul(1D, 3D) * 11D, 0.0001D); 144 expectApproxEquals(0.02D, 0.1D * 0.2D, 0.0001D); 145 expectApproxEquals(-0.1D, -0.5D * 0.2D, 0.0001D); 146 147 expectNaN($opt$Mul(0D, Double.POSITIVE_INFINITY)); 148 expectNaN($opt$Mul(0D, Double.NEGATIVE_INFINITY)); 149 expectNaN($opt$Mul(Double.NaN, 11D)); 150 expectNaN($opt$Mul(Double.NaN, -11D)); 151 expectNaN($opt$Mul(Double.NaN, Double.NEGATIVE_INFINITY)); 152 expectNaN($opt$Mul(Double.NaN, Double.POSITIVE_INFINITY)); 153 154 expectEquals(Double.POSITIVE_INFINITY, $opt$Mul(2D, 1.79769313486231570e+308)); 155 expectEquals(Double.POSITIVE_INFINITY, $opt$Mul(2D, Double.POSITIVE_INFINITY)); 156 expectEquals(Double.NEGATIVE_INFINITY, $opt$Mul(-2D, Double.POSITIVE_INFINITY)); 157 expectEquals(Double.NEGATIVE_INFINITY, $opt$Mul(-2D, 1.79769313486231570e+308)); 158 expectEquals(Double.NEGATIVE_INFINITY, $opt$Mul(2D, Double.NEGATIVE_INFINITY)); 159 expectEquals(Double.POSITIVE_INFINITY, $opt$Mul(-2D, Double.NEGATIVE_INFINITY)); 160 expectEquals(Double.NEGATIVE_INFINITY, $opt$Mul(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY)); 161 expectEquals(Double.POSITIVE_INFINITY, $opt$Mul(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY)); 162 expectEquals(Double.POSITIVE_INFINITY, $opt$Mul(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY)); 163 } 164 $opt$Mul(int a, int b)165 static int $opt$Mul(int a, int b) { 166 return a * b; 167 } 168 $opt$Mul(long a, long b)169 static long $opt$Mul(long a, long b) { 170 return a * b; 171 } 172 $opt$Mul(float a, float b)173 static float $opt$Mul(float a, float b) { 174 return a * b; 175 } 176 $opt$Mul(double a, double b)177 static double $opt$Mul(double a, double b) { 178 return a * b; 179 } 180 } 181