1 /* 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import sun.java2d.marlin.FloatMath; 25 26 /* 27 * @test 28 * @summary Check for correct implementation of FloatMath.ceil/floor 29 * @run main CeilAndFloorTests 30 */ 31 public class CeilAndFloorTests { 32 33 public static String toHexString(float f) { 34 if (!Float.isNaN(f)) 35 return Float.toHexString(f); 36 else 37 return "NaN(0x" + Integer.toHexString(Float.floatToRawIntBits(f)) + ")"; 38 } 39 40 public static int test(String testName, float input, 41 float result, float expected) { 42 if (Float.compare(expected, result) != 0) { 43 System.err.println("Failure for " + testName + ":\n" + 44 "\tFor input " + input + "\t(" + toHexString(input) + ")\n" + 45 "\texpected " + expected + "\t(" + toHexString(expected) + ")\n" + 46 "\tgot " + result + "\t(" + toHexString(result) + ")."); 47 return 1; 48 } 49 else 50 return 0; 51 } 52 53 public static int test_skip_0(String testName, float input, 54 float result, float expected) 55 { 56 // floor_int does not distinguish +0f and -0f 57 // but it is not critical for Marlin 58 if (Float.compare(expected, result) != 0 && (expected != 0f)) 59 { 60 System.err.println("Failure for " + testName + ":\n" + 61 "\tFor input " + input + "\t(" + toHexString(input) + ")\n" + 62 "\texpected " + expected + "\t(" + toHexString(expected) + ")\n" + 63 "\tgot " + result + "\t(" + toHexString(result) + ")."); 64 return 1; 65 } 66 else 67 return 0; 68 } 69 70 private static int testCeilCase(float input, float expected) { 71 int failures = 0; 72 // float result: 73 failures += test("FloatMath.ceil_f", input, FloatMath.ceil_f(input), expected); 74 // int result: 75 failures += test("FloatMath.ceil_int", input, FloatMath.ceil_int(input), (int)expected); 76 failures += test("FloatMath.ceil_f (int)", input, (int)FloatMath.ceil_f(input), (int)expected); 77 return failures; 78 } 79 80 private static int testFloorCase(float input, float expected) { 81 int failures = 0; 82 // float result: 83 failures += test ("FloatMath.floor_f", input, FloatMath.floor_f(input), expected); 84 // ignore difference between +0f and -0f: 85 failures += test_skip_0("FloatMath.floor_int", input, FloatMath.floor_int(input), (int)expected); 86 failures += test_skip_0("FloatMath.floor_f (int)", input, (int)FloatMath.floor_f(input), (int)expected); 87 return failures; 88 } 89 90 private static int nearIntegerTests() { 91 int failures = 0; 92 93 float [] fixedPoints = { 94 -0.0f, 95 0.0f, 96 -1.0f, 97 1.0f, 98 -0x1.0p52f, 99 0x1.0p52f, 100 -Float.MAX_VALUE, 101 Float.MAX_VALUE, 102 Float.NEGATIVE_INFINITY, 103 Float.POSITIVE_INFINITY, 104 Float.NaN, 105 }; 106 107 for(float fixedPoint : fixedPoints) { 108 failures += testCeilCase(fixedPoint, fixedPoint); 109 failures += testFloorCase(fixedPoint, fixedPoint); 110 } 111 112 for(int i = Float.MIN_EXPONENT; i <= Float.MAX_EXPONENT; i++) { 113 float powerOfTwo = Math.scalb(1.0f, i); 114 float neighborDown = Math.nextDown(powerOfTwo); 115 float neighborUp = Math.nextUp(powerOfTwo); 116 117 if (i < 0) { 118 failures += testCeilCase( powerOfTwo, 1.0f); 119 failures += testCeilCase(-powerOfTwo, -0.0f); 120 121 failures += testFloorCase( powerOfTwo, 0.0f); 122 failures += testFloorCase(-powerOfTwo, -1.0f); 123 124 failures += testCeilCase( neighborDown, 1.0f); 125 failures += testCeilCase(-neighborDown, -0.0f); 126 127 failures += testFloorCase( neighborUp, 0.0f); 128 failures += testFloorCase(-neighborUp, -1.0f); 129 } else { 130 failures += testCeilCase(powerOfTwo, powerOfTwo); 131 failures += testFloorCase(powerOfTwo, powerOfTwo); 132 133 if (neighborDown==Math.rint(neighborDown)) { 134 failures += testCeilCase( neighborDown, neighborDown); 135 failures += testCeilCase(-neighborDown, -neighborDown); 136 137 failures += testFloorCase( neighborDown, neighborDown); 138 failures += testFloorCase(-neighborDown,-neighborDown); 139 } else { 140 failures += testCeilCase( neighborDown, powerOfTwo); 141 failures += testFloorCase(-neighborDown, -powerOfTwo); 142 } 143 144 if (neighborUp==Math.rint(neighborUp)) { 145 failures += testCeilCase(neighborUp, neighborUp); 146 failures += testCeilCase(-neighborUp, -neighborUp); 147 148 failures += testFloorCase(neighborUp, neighborUp); 149 failures += testFloorCase(-neighborUp, -neighborUp); 150 } else { 151 failures += testFloorCase(neighborUp, powerOfTwo); 152 failures += testCeilCase(-neighborUp, -powerOfTwo); 153 } 154 } 155 } 156 157 for(int i = -(0x10000); i <= 0x10000; i++) { 158 float f = (float) i; 159 float neighborDown = Math.nextDown(f); 160 float neighborUp = Math.nextUp(f); 161 162 failures += testCeilCase( f, f); 163 failures += testCeilCase(-f, -f); 164 165 failures += testFloorCase( f, f); 166 failures += testFloorCase(-f, -f); 167 168 if (Math.abs(f) > 1.0) { 169 failures += testCeilCase( neighborDown, f); 170 failures += testCeilCase(-neighborDown, -f+1); 171 172 failures += testFloorCase( neighborUp, f); 173 failures += testFloorCase(-neighborUp, -f-1); 174 } 175 } 176 177 return failures; 178 } 179 180 public static int roundingTests() { 181 int failures = 0; 182 float [][] testCases = { 183 { Float.MIN_VALUE, 1.0f}, 184 {-Float.MIN_VALUE, -0.0f}, 185 { Math.nextDown(Float.MIN_NORMAL), 1.0f}, 186 {-Math.nextDown(Float.MIN_NORMAL), -0.0f}, 187 { Float.MIN_NORMAL, 1.0f}, 188 {-Float.MIN_NORMAL, -0.0f}, 189 190 { 0.1f, 1.0f}, 191 {-0.1f, -0.0f}, 192 193 { 0.5f, 1.0f}, 194 {-0.5f, -0.0f}, 195 196 { 1.5f, 2.0f}, 197 {-1.5f, -1.0f}, 198 199 { 2.5f, 3.0f}, 200 {-2.5f, -2.0f}, 201 202 { 12.3456789f, 13.0f}, 203 {-12.3456789f, -12.0f}, 204 205 { Math.nextDown(1.0f), 1.0f}, 206 { Math.nextDown(-1.0f), -1.0f}, 207 208 { Math.nextUp(1.0f), 2.0f}, 209 { Math.nextUp(-1.0f), -0.0f}, 210 211 { 0x1.0p22f, 0x1.0p22f}, 212 {-0x1.0p22f, -0x1.0p22f}, 213 214 { Math.nextDown(0x1.0p22f), 0x1.0p22f}, 215 {-Math.nextUp(0x1.0p22f), -0x1.0p22f}, 216 217 { Math.nextUp(0x1.0p22f), 0x1.0p22f+1f}, 218 {-Math.nextDown(0x1.0p22f), -0x1.0p22f+1f}, 219 220 { Math.nextDown(0x1.0p23f), 0x1.0p23f}, 221 {-Math.nextUp(0x1.0p23f), -0x1.0p23f-1f}, 222 223 { Math.nextUp(0x1.0p23f), 0x1.0p23f+1f}, 224 {-Math.nextDown(0x1.0p23f), -0x1.0p23f+1f}, 225 }; 226 227 for(float[] testCase : testCases) { 228 failures += testCeilCase(testCase[0], testCase[1]); 229 failures += testFloorCase(-testCase[0], -testCase[1]); 230 } 231 return failures; 232 } 233 234 public static void main(String... args) { 235 int failures = 0; 236 237 System.out.println("nearIntegerTests"); 238 failures += nearIntegerTests(); 239 240 System.out.println("roundingTests"); 241 failures += roundingTests(); 242 243 if (failures > 0) { 244 System.err.println("Testing {FloatMath}.ceil/floor incurred " 245 + failures + " failures."); 246 throw new RuntimeException(); 247 } 248 } 249 }