1 /* 2 * Copyright (c) 2015, 2016, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.marlin; 27 28 /** 29 * Faster Math ceil / floor routines derived from StrictMath 30 */ 31 public final class FloatMath implements MarlinConst { 32 33 // overflow / NaN handling enabled: 34 static final boolean CHECK_OVERFLOW = true; 35 static final boolean CHECK_NAN = true; 36 37 private FloatMath() { 38 // utility class 39 } 40 41 // faster inlined min/max functions in the branch prediction is high 42 static float max(final float a, final float b) { 43 // no NaN handling 44 return (a >= b) ? a : b; 45 } 46 47 public static int max(final int a, final int b) { 48 return (a >= b) ? a : b; 49 } 50 51 public static int min(final int a, final int b) { 52 return (a <= b) ? a : b; 53 } 54 55 /** 56 * Faster alternative to ceil(float) optimized for the integer domain 57 * and supporting NaN and +/-Infinity. 58 * 59 * @param a a value. 60 * @return the largest (closest to positive infinity) integer value 61 * that less than or equal to the argument and is equal to a mathematical 62 * integer. 63 */ 64 public static int ceil_int(final float a) { 65 final int intpart = (int) a; 66 67 if (a <= intpart 68 || (CHECK_OVERFLOW && intpart == Integer.MAX_VALUE) 69 || CHECK_NAN && Float.isNaN(a)) { 70 return intpart; 71 } 72 return intpart + 1; 73 } 74 75 /** 76 * Faster alternative to floor(float) optimized for the integer domain 77 * and supporting NaN and +/-Infinity. 78 * 79 * @param a a value. 80 * @return the largest (closest to positive infinity) floating-point value 81 * that less than or equal to the argument and is equal to a mathematical 82 * integer. 83 */ 84 public static int floor_int(final float a) { 85 final int intpart = (int) a; 86 87 if (a >= intpart 88 || (CHECK_OVERFLOW && intpart == Integer.MIN_VALUE) 89 || CHECK_NAN && Float.isNaN(a)) { 90 return intpart; 91 } 92 return intpart - 1; 93 } 94 }