Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/java2d/marlin/FloatMath.java
38918 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package sun.java2d.marlin;2526import sun.misc.DoubleConsts;27import sun.misc.FloatConsts;2829/**30* Faster Math ceil / floor routines derived from StrictMath31*/32public final class FloatMath implements MarlinConst {3334// overflow / NaN handling enabled:35static final boolean CHECK_OVERFLOW = true;36static final boolean CHECK_NAN = true;3738private FloatMath() {39// utility class40}4142// faster inlined min/max functions in the branch prediction is high43static float max(final float a, final float b) {44// no NaN handling45return (a >= b) ? a : b;46}4748static int max(final int a, final int b) {49return (a >= b) ? a : b;50}5152static int min(final int a, final int b) {53return (a <= b) ? a : b;54}5556/**57* Returns the smallest (closest to negative infinity) {@code float} value58* that is greater than or equal to the argument and is equal to a59* mathematical integer. Special cases:60* <ul><li>If the argument value is already equal to a mathematical integer,61* then the result is the same as the argument. <li>If the argument is NaN62* or an infinity or positive zero or negative zero, then the result is the63* same as the argument. <li>If the argument value is less than zero but64* greater than -1.0, then the result is negative zero.</ul> Note that the65* value of {@code StrictMath.ceil(x)} is exactly the value of66* {@code -StrictMath.floor(-x)}.67*68* @param a a value.69* @return the smallest (closest to negative infinity) floating-point value70* that is greater than or equal to the argument and is equal to a71* mathematical integer.72*/73public static float ceil_f(final float a) {74// Derived from StrictMath.ceil(double):7576// Inline call to Math.getExponent(a) to77// compute only once Float.floatToRawIntBits(a)78final int doppel = Float.floatToRawIntBits(a);7980final int exponent = ((doppel & FloatConsts.EXP_BIT_MASK)81>> (FloatConsts.SIGNIFICAND_WIDTH - 1))82- FloatConsts.EXP_BIAS;8384if (exponent < 0) {85/*86* Absolute value of argument is less than 1.87* floorOrceil(-0.0) => -0.088* floorOrceil(+0.0) => +0.089*/90return ((a == 0) ? a :91( (a < 0f) ? -0f : 1f) );92}93if (CHECK_OVERFLOW && (exponent >= 23)) { // 52 for double94/*95* Infinity, NaN, or a value so large it must be integral.96*/97return a;98}99// Else the argument is either an integral value already XOR it100// has to be rounded to one.101assert exponent >= 0 && exponent <= 22; // 51 for double102103final int intpart = doppel104& (~(FloatConsts.SIGNIF_BIT_MASK >> exponent));105106if (intpart == doppel) {107return a; // integral value (including 0)108}109110// 0 handled above as an integer111// sign: 1 for negative, 0 for positive numbers112// add : 0 for negative and 1 for positive numbers113return Float.intBitsToFloat(intpart) + ((~intpart) >>> 31);114}115116/**117* Returns the largest (closest to positive infinity) {@code float} value118* that is less than or equal to the argument and is equal to a mathematical119* integer. Special cases:120* <ul><li>If the argument value is already equal to a mathematical integer,121* then the result is the same as the argument. <li>If the argument is NaN122* or an infinity or positive zero or negative zero, then the result is the123* same as the argument.</ul>124*125* @param a a value.126* @return the largest (closest to positive infinity) floating-point value127* that less than or equal to the argument and is equal to a mathematical128* integer.129*/130public static float floor_f(final float a) {131// Derived from StrictMath.floor(double):132133// Inline call to Math.getExponent(a) to134// compute only once Float.floatToRawIntBits(a)135final int doppel = Float.floatToRawIntBits(a);136137final int exponent = ((doppel & FloatConsts.EXP_BIT_MASK)138>> (FloatConsts.SIGNIFICAND_WIDTH - 1))139- FloatConsts.EXP_BIAS;140141if (exponent < 0) {142/*143* Absolute value of argument is less than 1.144* floorOrceil(-0.0) => -0.0145* floorOrceil(+0.0) => +0.0146*/147return ((a == 0) ? a :148( (a < 0f) ? -1f : 0f) );149}150if (CHECK_OVERFLOW && (exponent >= 23)) { // 52 for double151/*152* Infinity, NaN, or a value so large it must be integral.153*/154return a;155}156// Else the argument is either an integral value already XOR it157// has to be rounded to one.158assert exponent >= 0 && exponent <= 22; // 51 for double159160final int intpart = doppel161& (~(FloatConsts.SIGNIF_BIT_MASK >> exponent));162163if (intpart == doppel) {164return a; // integral value (including 0)165}166167// 0 handled above as an integer168// sign: 1 for negative, 0 for positive numbers169// add : -1 for negative and 0 for positive numbers170return Float.intBitsToFloat(intpart) + (intpart >> 31);171}172173/**174* Faster alternative to ceil(float) optimized for the integer domain175* and supporting NaN and +/-Infinity.176*177* @param a a value.178* @return the largest (closest to positive infinity) integer value179* that less than or equal to the argument and is equal to a mathematical180* integer.181*/182public static int ceil_int(final float a) {183final int intpart = (int) a;184185if (a <= intpart186|| (CHECK_OVERFLOW && intpart == Integer.MAX_VALUE)187|| CHECK_NAN && Float.isNaN(a)) {188return intpart;189}190return intpart + 1;191}192193/**194* Faster alternative to floor(float) optimized for the integer domain195* and supporting NaN and +/-Infinity.196*197* @param a a value.198* @return the largest (closest to positive infinity) floating-point value199* that less than or equal to the argument and is equal to a mathematical200* integer.201*/202public static int floor_int(final float a) {203final int intpart = (int) a;204205if (a >= intpart206|| (CHECK_OVERFLOW && intpart == Integer.MIN_VALUE)207|| CHECK_NAN && Float.isNaN(a)) {208return intpart;209}210return intpart - 1;211}212}213214215