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 public static int max(final int a, final int b) { 43 return (a >= b) ? a : b; 44 } 45 46 public static int min(final int a, final int b) { 47 return (a <= b) ? a : b; 48 } 49 50 /** 51 * Faster alternative to ceil(float) optimized for the integer domain 52 * and supporting NaN and +/-Infinity. 53 * 54 * @param a a value. 55 * @return the largest (closest to positive infinity) integer value 56 * that less than or equal to the argument and is equal to a mathematical 57 * integer. 58 */ 59 public static int ceil_int(final float a) { 60 final int intpart = (int) a; 61 62 if (a <= intpart 63 || (CHECK_OVERFLOW && intpart == Integer.MAX_VALUE) 64 || CHECK_NAN && Float.isNaN(a)) { 65 return intpart; 66 } 67 return intpart + 1; 68 } 69 70 /** 71 * Faster alternative to ceil(double) optimized for the integer domain 72 * and supporting NaN and +/-Infinity. 73 * 74 * @param a a value. 75 * @return the largest (closest to positive infinity) integer value 76 * that less than or equal to the argument and is equal to a mathematical 77 * integer. 78 */ 79 public static int ceil_int(final double a) { 80 final int intpart = (int) a; 81 82 if (a <= intpart 83 || (CHECK_OVERFLOW && intpart == Integer.MAX_VALUE) 84 || CHECK_NAN && Double.isNaN(a)) { 85 return intpart; 86 } 87 return intpart + 1; 88 } 89 90 /** 91 * Faster alternative to floor(float) optimized for the integer domain 92 * and supporting NaN and +/-Infinity. 93 * 94 * @param a a value. 95 * @return the largest (closest to positive infinity) floating-point value 96 * that less than or equal to the argument and is equal to a mathematical 97 * integer. 98 */ 99 public static int floor_int(final float a) { 100 final int intpart = (int) a; 101 102 if (a >= intpart 103 || (CHECK_OVERFLOW && intpart == Integer.MIN_VALUE) 104 || CHECK_NAN && Float.isNaN(a)) { 105 return intpart; 106 } 107 return intpart - 1; 108 } 109 110 /** 111 * Faster alternative to floor(double) optimized for the integer domain 112 * and supporting NaN and +/-Infinity. 113 * 114 * @param a a value. 115 * @return the largest (closest to positive infinity) floating-point value 116 * that less than or equal to the argument and is equal to a mathematical 117 * integer. 118 */ 119 public static int floor_int(final double a) { 120 final int intpart = (int) a; 121 122 if (a >= intpart 123 || (CHECK_OVERFLOW && intpart == Integer.MIN_VALUE) 124 || CHECK_NAN && Double.isNaN(a)) { 125 return intpart; 126 } 127 return intpart - 1; 128 } 129 }