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 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 expectEquals(float expected, float result) { 34 if (expected != result) { 35 throw new Error("Expected: " + expected + ", found: " + result); 36 } 37 } 38 39 public static void expectEquals(double expected, double result) { 40 if (expected != result) { 41 throw new Error("Expected: " + expected + ", found: " + result); 42 } 43 } 44 45 public static void expectApproxEquals(float a, float b) { 46 float maxDelta = 0.0001F; 47 boolean aproxEquals = (a > b) ? ((a - b) < maxDelta) : ((b - a) < maxDelta); 48 if (!aproxEquals) { 49 throw new Error("Expected: " + a + ", found: " + b + ", with delta: " + maxDelta + " " + (a - b)); 50 } 51 } 52 53 public static void expectApproxEquals(double a, double b) { 54 double maxDelta = 0.00001D; 55 boolean aproxEquals = (a > b) ? ((a - b) < maxDelta) : ((b - a) < maxDelta); 56 if (!aproxEquals) { 57 throw new Error("Expected: " + a + ", found: " + b + ", with delta: " + maxDelta + " " + (a - b)); 58 } 59 } 60 61 public static void expectNaN(float a) { 62 if (a == a) { 63 throw new Error("Expected NaN: " + a); 64 } 65 } 66 67 public static void expectNaN(double a) { 68 if (a == a) { 69 throw new Error("Expected NaN: " + a); 70 } 71 } 72 73 public static void main(String[] args) { 74 subInt(); 75 subLong(); 76 subFloat(); 77 subDouble(); 78 } 79 80 private static void subInt() { 81 expectEquals(2, $opt$Sub(5, 3)); 82 expectEquals(0, $opt$Sub(0, 0)); 83 expectEquals(-3, $opt$Sub(0, 3)); 84 expectEquals(3, $opt$Sub(3, 0)); 85 expectEquals(4, $opt$Sub(1, -3)); 86 expectEquals(-9, $opt$Sub(-12, -3)); 87 expectEquals(134217724, $opt$Sub(134217729, 5)); // (2^27 + 1) - 5 88 } 89 90 private static void subLong() { 91 expectEquals(2L, $opt$Sub(5L, 3L)); 92 expectEquals(0L, $opt$Sub(0L, 0L)); 93 expectEquals(-3L, $opt$Sub(0L, 3L)); 94 expectEquals(3L, $opt$Sub(3L, 0L)); 95 expectEquals(4L, $opt$Sub(1L, -3L)); 96 expectEquals(-9L, $opt$Sub(-12L, -3L)); 97 expectEquals(134217724L, $opt$Sub(134217729L, 5L)); // (2^27 + 1) - 5 98 expectEquals(34359738362L, $opt$Sub(34359738369L, 7L)); // (2^35 + 1) - 7 99 } 100 101 private static void subFloat() { 102 expectApproxEquals(2F, $opt$Sub(5F, 3F)); 103 expectApproxEquals(0F, $opt$Sub(0F, 0F)); 104 expectApproxEquals(-3F, $opt$Sub(0F, 3F)); 105 expectApproxEquals(3F, $opt$Sub(3F, 0F)); 106 expectApproxEquals(4F, $opt$Sub(1F, -3F)); 107 expectApproxEquals(-9F, $opt$Sub(-12F, -3F)); 108 expectApproxEquals(34359738362F, $opt$Sub(34359738369F, 7F)); // (2^35 + 1) - 7 109 expectApproxEquals(-0.1F, $opt$Sub(0.1F, 0.2F)); 110 expectApproxEquals(0.2F, $opt$Sub(-0.5F, -0.7F)); 111 112 expectNaN($opt$Sub(Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY)); 113 expectNaN($opt$Sub(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY)); 114 expectNaN($opt$Sub(Float.NaN, 11F)); 115 expectNaN($opt$Sub(Float.NaN, -11F)); 116 expectNaN($opt$Sub(Float.NaN, Float.NEGATIVE_INFINITY)); 117 expectNaN($opt$Sub(Float.NaN, Float.POSITIVE_INFINITY)); 118 119 expectEquals(Float.NEGATIVE_INFINITY, $opt$Sub(-Float.MAX_VALUE, Float.MAX_VALUE)); 120 expectEquals(Float.NEGATIVE_INFINITY, $opt$Sub(2F, Float.POSITIVE_INFINITY)); 121 expectEquals(Float.POSITIVE_INFINITY, $opt$Sub(Float.MAX_VALUE, -Float.MAX_VALUE)); 122 expectEquals(Float.POSITIVE_INFINITY, $opt$Sub(2F, Float.NEGATIVE_INFINITY)); 123 expectEquals(Float.POSITIVE_INFINITY, $opt$Sub(Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY)); 124 expectEquals(Float.NEGATIVE_INFINITY, $opt$Sub(Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY)); 125 } 126 127 private static void subDouble() { 128 expectApproxEquals(2D, $opt$Sub(5D, 3D)); 129 expectApproxEquals(0D, $opt$Sub(0D, 0D)); 130 expectApproxEquals(-3D, $opt$Sub(0D, 3D)); 131 expectApproxEquals(3D, $opt$Sub(3D, 0D)); 132 expectApproxEquals(4D, $opt$Sub(1D, -3D)); 133 expectApproxEquals(-9D, $opt$Sub(-12D, -3D)); 134 expectApproxEquals(134217724D, $opt$Sub(134217729D, 5D)); // (2^27 + 1) - 5 135 expectApproxEquals(34359738362D, $opt$Sub(34359738369D, 7D)); // (2^35 + 1) - 7 136 expectApproxEquals(-0.1D, $opt$Sub(0.1D, 0.2D)); 137 expectApproxEquals(0.2D, $opt$Sub(-0.5D, -0.7D)); 138 139 expectNaN($opt$Sub(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY)); 140 expectNaN($opt$Sub(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY)); 141 expectNaN($opt$Sub(Double.NaN, 11D)); 142 expectNaN($opt$Sub(Double.NaN, -11D)); 143 expectNaN($opt$Sub(Double.NaN, Double.NEGATIVE_INFINITY)); 144 expectNaN($opt$Sub(Double.NaN, Double.POSITIVE_INFINITY)); 145 146 expectEquals(Double.NEGATIVE_INFINITY, $opt$Sub(-Double.MAX_VALUE, Double.MAX_VALUE)); 147 expectEquals(Double.NEGATIVE_INFINITY, $opt$Sub(2D, Double.POSITIVE_INFINITY)); 148 expectEquals(Double.POSITIVE_INFINITY, $opt$Sub(Double.MAX_VALUE, -Double.MAX_VALUE)); 149 expectEquals(Double.POSITIVE_INFINITY, $opt$Sub(2D, Double.NEGATIVE_INFINITY)); 150 expectEquals(Double.POSITIVE_INFINITY, $opt$Sub(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY)); 151 expectEquals(Double.NEGATIVE_INFINITY, $opt$Sub(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY)); 152 } 153 154 static int $opt$Sub(int a, int b) { 155 return a - b; 156 } 157 158 static long $opt$Sub(long a, long b) { 159 return a - b; 160 } 161 162 static float $opt$Sub(float a, float b) { 163 return a - b; 164 } 165 166 static double $opt$Sub(double a, double b) { 167 return a - b; 168 } 169 170 } 171