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