diff options
author | Mark Raynsford <[email protected]> | 2013-10-31 01:07:48 +0000 |
---|---|---|
committer | Mark Raynsford <[email protected]> | 2013-10-31 01:07:48 +0000 |
commit | bca7777fa507a509f413c6dc8919bab641fe0d15 (patch) | |
tree | ed5a73e0734d6d1a938083927efe9538697c56de | |
parent | d2690939b46a91b346bdd2d4ce750ddb0d2136fd (diff) |
Add functions for converting to and from binary16 half-precision floating point values. Derived from http://mvn.io7m.com/ieee754b16, of which I am the original author.mark
6 files changed, 1672 insertions, 0 deletions
diff --git a/src/jogl/classes/com/jogamp/opengl/math/Binary16.java b/src/jogl/classes/com/jogamp/opengl/math/Binary16.java new file mode 100644 index 000000000..33add46c2 --- /dev/null +++ b/src/jogl/classes/com/jogamp/opengl/math/Binary16.java @@ -0,0 +1,569 @@ +/** + * Copyright 2013 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ + +package com.jogamp.opengl.math; + +/** + * <p> + * Functions to convert values to/from the <code>binary16</code> format + * specified in <code>IEEE 754 2008</code>. + * </p> + */ + +public final class Binary16 +{ + /** + * The encoded form of negative infinity <code>-∞</code>. + */ + + public static final char NEGATIVE_INFINITY; + + /** + * The encoded form of positive infinity <code>∞</code>. + */ + + public static final char POSITIVE_INFINITY; + + /** + * The encoded form of positive zero <code>0</code>. + */ + + public static final char POSITIVE_ZERO; + + /** + * The encoded form of negative zero <code>-0</code>. + */ + + public static final char NEGATIVE_ZERO; + + /** + * The <i>bias</i> value used to offset the encoded exponent. A given + * exponent <code>e</code> is encoded as <code>{@link #BIAS} + e</code>. + */ + + public static final int BIAS; + + static { + NEGATIVE_INFINITY = 0xFC00; + POSITIVE_INFINITY = 0x7C00; + POSITIVE_ZERO = 0x0000; + NEGATIVE_ZERO = 0x8000; + BIAS = 15; + } + + private static final int MASK_SIGN; + private static final int MASK_EXPONENT; + private static final int MASK_SIGNIFICAND; + + static { + MASK_SIGN = 0x8000; + MASK_EXPONENT = 0x7C00; + MASK_SIGNIFICAND = 0x03FF; + } + + /** + * One possible not-a-number value. + */ + + public static char exampleNaN() + { + final int n = + Binary16.packSetExponentUnbiasedUnchecked(16) + | Binary16.packSetSignificandUnchecked(1); + final char c = (char) n; + return c; + } + + /** + * Return <code>true</code> if the given packed <code>binary16</code> value + * is infinite. + */ + + public static boolean isInfinite( + final char k) + { + if (Binary16.unpackGetExponentUnbiased(k) == 16) { + if (Binary16.unpackGetSignificand(k) == 0) { + return true; + } + } + return false; + } + + /** + * Return <code>true</code> if the given packed <code>binary16</code> value + * is not a number (<code>NaN</code>). + */ + + public static boolean isNaN( + final char k) + { + final int e = Binary16.unpackGetExponentUnbiased(k); + final int s = Binary16.unpackGetSignificand(k); + return (e == 16) && (s > 0); + } + + /** + * <p> + * Convert a double precision floating point value to a packed + * <code>binary16</code> value. + * </p> + * <p> + * For the following specific cases, the function returns: + * </p> + * <ul> + * <li><code>NaN</code> iff <code>isNaN(k)</code></li> + * <li>{@link #POSITIVE_INFINITY} iff + * <code>k == {@link Double#POSITIVE_INFINITY}</code></li> + * <li>{@link #NEGATIVE_INFINITY} iff + * <code>k == {@link Double#NEGATIVE_INFINITY}</code></li> + * <li>{@link #NEGATIVE_ZERO} iff <code>k == -0.0</code></li> + * <li>{@link #POSITIVE_ZERO} iff <code>k == 0.0</code></li> + * </ul> + * <p> + * Otherwise, the <code>binary16</code> value that most closely represents + * <code>k</code> is returned. This may obviously be an infinite value as + * the interval of double precision values is far larger than that of the + * <code>binary16</code> type. + * </p> + * + * @see #unpackDouble(char) + */ + + public static char packDouble( + final double k) + { + if (Double.isNaN(k)) { + return Binary16.exampleNaN(); + } + if (k == Double.POSITIVE_INFINITY) { + return Binary16.POSITIVE_INFINITY; + } + if (k == Double.NEGATIVE_INFINITY) { + return Binary16.NEGATIVE_INFINITY; + } + if (Double.doubleToLongBits(k) == Binary64.NEGATIVE_ZERO_BITS) { + return Binary16.NEGATIVE_ZERO; + } + if (k == 0.0) { + return Binary16.POSITIVE_ZERO; + } + + final long de = Binary64.unpackGetExponentUnbiased(k); + final long ds = Binary64.unpackGetSign(k); + final long dn = Binary64.unpackGetSignificand(k); + final char rsr = Binary16.packSetSignUnchecked((int) ds); + + /** + * Extract the 5 least-significant bits of the exponent. + */ + + final int rem = (int) (de & 0x001F); + final char rer = Binary16.packSetExponentUnbiasedUnchecked(rem); + + /** + * Extract the 10 most-significant bits of the significand. + */ + + final long rnm = dn & 0xFFC0000000000L; + final long rns = rnm >> 42; + final char rnr = Binary16.packSetSignificandUnchecked((int) rns); + + /** + * Combine the results. + */ + + return (char) (rsr | rer | rnr); + } + + /** + * <p> + * Convert a single precision floating point value to a packed + * <code>binary16</code> value. + * </p> + * <p> + * For the following specific cases, the function returns: + * </p> + * <ul> + * <li><code>NaN</code> iff <code>isNaN(k)</code></li> + * <li>{@link #POSITIVE_INFINITY} iff + * <code>k == {@link Float#POSITIVE_INFINITY}</code></li> + * <li>{@link #NEGATIVE_INFINITY} iff + * <code>k == {@link Float#NEGATIVE_INFINITY}</code></li> + * <li>{@link #NEGATIVE_ZERO} iff <code>k == -0.0</code></li> + * <li>{@link #POSITIVE_ZERO} iff <code>k == 0.0</code></li> + * </ul> + * <p> + * Otherwise, the <code>binary16</code> value that most closely represents + * <code>k</code> is returned. This may obviously be an infinite value as + * the interval of single precision values is far larger than that of the + * <code>binary16</code> type. + * </p> + * + * @see #unpackFloat(char) + */ + + public static char packFloat( + final float k) + { + if (Float.isNaN(k)) { + return Binary16.exampleNaN(); + } + if (k == Float.POSITIVE_INFINITY) { + return Binary16.POSITIVE_INFINITY; + } + if (k == Float.NEGATIVE_INFINITY) { + return Binary16.NEGATIVE_INFINITY; + } + if (Float.floatToIntBits(k) == Binary32.NEGATIVE_ZERO_BITS) { + return Binary16.NEGATIVE_ZERO; + } + if (k == 0.0) { + return Binary16.POSITIVE_ZERO; + } + + final long de = Binary32.unpackGetExponentUnbiased(k); + final long ds = Binary32.unpackGetSign(k); + final long dn = Binary32.unpackGetSignificand(k); + final char rsr = Binary16.packSetSignUnchecked((int) ds); + + /** + * Extract the 5 least-significant bits of the exponent. + */ + + final int rem = (int) (de & 0x001F); + final char rer = Binary16.packSetExponentUnbiasedUnchecked(rem); + + /** + * Extract the 10 most-significant bits of the significand. + */ + + final long rnm = dn & 0x7FE000L; + final long rns = rnm >> 13; + final char rnr = Binary16.packSetSignificandUnchecked((int) rns); + + /** + * Combine the results. + */ + + return (char) (rsr | rer | rnr); + } + + /** + * <p> + * Encode the unbiased exponent <code>e</code>. Values should be in the + * range <code>[-15, 16]</code> - values outside of this range will be + * truncated. + * </p> + * + * @see #unpackGetExponentUnbiased(char) + */ + + public static char packSetExponentUnbiasedUnchecked( + final int e) + { + final int eb = e + Binary16.BIAS; + final int es = eb << 10; + final int em = es & Binary16.MASK_EXPONENT; + return (char) em; + } + + /** + * <p> + * Encode the significand <code>s</code>. Values should be in the range + * <code>[0, 1023]</code>. Values outside of this range will be truncated. + * </p> + * + * @see #unpackGetSignificand(char) + */ + + public static char packSetSignificandUnchecked( + final int s) + { + final int sm = s & Binary16.MASK_SIGNIFICAND; + return (char) sm; + } + + /** + * <p> + * Encode the sign bit <code>s</code>. Values should be in the range + * <code>[0, 1]</code>, with <code>0</code> ironically denoting a positive + * value. Values outside of this range will be truncated. + * </p> + * + * @see #unpackGetSign(char) + */ + + public static char packSetSignUnchecked( + final int s) + { + final int ss = s << 15; + final int sm = ss & Binary16.MASK_SIGN; + return (char) sm; + } + + /** + * Show the given raw packed <code>binary16</code> value as a string of + * binary digits. + */ + + public static String toRawBinaryString( + final char k) + { + final StringBuilder b = new StringBuilder(); + int z = k; + for (int i = 0; i < 16; ++i) { + if ((z & 1) == 1) { + b.insert(0, "1"); + } else { + b.insert(0, "0"); + } + z >>= 1; + } + return b.toString(); + } + + /** + * <p> + * Convert a packed <code>binary16</code> value <code>k</code> to a + * double-precision floating point value. + * </p> + * <p> + * The function returns: + * </p> + * <ul> + * <li><code>NaN</code> iff <code>isNaN(k)</code></li> + * <li>{@link Double#POSITIVE_INFINITY} iff + * <code>k == {@link #POSITIVE_INFINITY}</code></li> + * <li>{@link Double#NEGATIVE_INFINITY} iff + * <code>k == {@link #NEGATIVE_INFINITY}</code></li> + * <li><code>-0.0</code> iff <code>k == {@link #NEGATIVE_ZERO}</code></li> + * <li><code>0.0</code> iff <code>k == {@link #POSITIVE_ZERO}</code></li> + * <li><code>(-1.0 * n) * (2 ^ e) * 1.s</code>, for the decoded sign + * <code>n</code> of <code>k</code>, the decoded exponent <code>e</code> of + * <code>k</code>, and the decoded significand <code>s</code> of + * <code>k</code>.</li> + * </ul> + * + * @see #packDouble(double) + */ + + public static double unpackDouble( + final char k) + { + if (Binary16.isNaN(k)) { + return Double.NaN; + } + if (k == Binary16.POSITIVE_INFINITY) { + return Double.POSITIVE_INFINITY; + } + if (k == Binary16.NEGATIVE_INFINITY) { + return Double.NEGATIVE_INFINITY; + } + if (k == Binary16.NEGATIVE_ZERO) { + return -0.0; + } + if (k == Binary16.POSITIVE_ZERO) { + return 0.0; + } + + final long e = Binary16.unpackGetExponentUnbiased(k); + final long s = Binary16.unpackGetSign(k); + final long n = Binary16.unpackGetSignificand(k); + + /** + * Shift the sign bit to the position at which it will appear in the + * resulting value. + */ + + final long rsr = s << 63; + + /** + * 1. Bias the exponent. + * + * 2. Shift the result left to the position at which it will appear in the + * resulting value. + */ + + final long reb = (e + Binary64.BIAS); + final long rer = reb << 52; + + /** + * Shift the significand left to the position at which it will appear in + * the resulting value. + */ + + final long rnr = n << 42; + return Double.longBitsToDouble(rsr | rer | rnr); + } + + /** + * <p> + * Convert a packed <code>binary16</code> value <code>k</code> to a + * single-precision floating point value. + * </p> + * <p> + * The function returns: + * </p> + * <ul> + * <li><code>NaN</code> iff <code>isNaN(k)</code></li> + * <li>{@link Float#POSITIVE_INFINITY} iff + * <code>k == {@link #POSITIVE_INFINITY}</code></li> + * <li>{@link Float#NEGATIVE_INFINITY} iff + * <code>k == {@link #NEGATIVE_INFINITY}</code></li> + * <li><code>-0.0</code> iff <code>k == {@link #NEGATIVE_ZERO}</code></li> + * <li><code>0.0</code> iff <code>k == {@link #POSITIVE_ZERO}</code></li> + * <li><code>(-1.0 * n) * (2 ^ e) * 1.s</code>, for the decoded sign + * <code>n</code> of <code>k</code>, the decoded exponent <code>e</code> of + * <code>k</code>, and the decoded significand <code>s</code> of + * <code>k</code>.</li> + * </ul> + * + * @see #packFloat(float) + */ + + public static float unpackFloat( + final char k) + { + if (Binary16.isNaN(k)) { + return Float.NaN; + } + if (k == Binary16.POSITIVE_INFINITY) { + return Float.POSITIVE_INFINITY; + } + if (k == Binary16.NEGATIVE_INFINITY) { + return Float.NEGATIVE_INFINITY; + } + if (k == Binary16.NEGATIVE_ZERO) { + return -0.0f; + } + if (k == Binary16.POSITIVE_ZERO) { + return 0.0f; + } + + final int e = Binary16.unpackGetExponentUnbiased(k); + final int s = Binary16.unpackGetSign(k); + final int n = Binary16.unpackGetSignificand(k); + + /** + * Shift the sign bit to the position at which it will appear in the + * resulting value. + */ + + final int rsr = s << 31; + + /** + * 1. Bias the exponent. + * + * 2. Shift the result left to the position at which it will appear in the + * resulting value. + */ + + final int reb = (e + Binary32.BIAS); + final int rer = reb << 23; + + /** + * Shift the significand left to the position at which it will appear in + * the resulting value. + */ + + final int rnr = n << 13; + return Float.intBitsToFloat(rsr | rer | rnr); + } + + /** + * <p> + * Extract and unbias the exponent of the given packed <code>binary16</code> + * value. + * </p> + * <p> + * The exponent is encoded <i>biased</i> as a number in the range + * <code>[0, 31]</code>, with <code>0</code> indicating that the number is + * <i>subnormal</i> and <code>[1, 30]</code> denoting the actual exponent + * plus {@link #BIAS}. Infinite and <code>NaN</code> values always have an + * exponent of <code>31</code>. + * </p> + * <p> + * This function will therefore return: + * </p> + * <ul> + * <li> + * <code>0 - {@link #BIAS} = -15</code> iff the input is a <i>subnormal</i> + * number.</li> + * <li>An integer in the range + * <code>[1 - {@link #BIAS}, 30 - {@link #BIAS}] = [-14, 15]</code> iff the + * input is a <i>normal</i> number.</li> + * <li> + * <code>16</code> iff the input is {@link #POSITIVE_INFINITY}, + * {@link #NEGATIVE_INFINITY}, or <code>NaN</code>.</li> + * </ul> + * + * @see #packSetExponentUnbiasedUnchecked(int) + */ + + public static int unpackGetExponentUnbiased( + final char k) + { + final int em = k & Binary16.MASK_EXPONENT; + final int es = em >> 10; + return es - Binary16.BIAS; + } + + /** + * Retrieve the sign bit of the given packed <code>binary16</code> value, as + * an integer in the range <code>[0, 1]</code>. + * + * @see Binary16#packSetSignUnchecked(int) + */ + + public static int unpackGetSign( + final char k) + { + return (k & Binary16.MASK_SIGN) >> 15; + } + + /** + * <p> + * Return the significand of the given packed <code>binary16</code> value as + * an integer in the range <code>[0, 1023]</code>. + * </p> + * + * @see Binary16#packSetSignificandUnchecked(int) + */ + + public static int unpackGetSignificand( + final char k) + { + return k & Binary16.MASK_SIGNIFICAND; + } + + private Binary16() + { + throw new AssertionError("Unreachable code, report this bug!"); + } +} diff --git a/src/jogl/classes/com/jogamp/opengl/math/Binary32.java b/src/jogl/classes/com/jogamp/opengl/math/Binary32.java new file mode 100644 index 000000000..d98815d9f --- /dev/null +++ b/src/jogl/classes/com/jogamp/opengl/math/Binary32.java @@ -0,0 +1,116 @@ +/** + * Copyright 2013 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ + +package com.jogamp.opengl.math; + +/** + * Functions for interrogating <code>binary32</code> (float) values. + */ + +public final class Binary32 +{ + static final int NEGATIVE_ZERO_BITS; + static final int MASK_SIGN; + static final int MASK_EXPONENT; + static final int MASK_SIGNIFICAND; + static final int BIAS; + + static { + NEGATIVE_ZERO_BITS = 0x80000000; + MASK_SIGN = 0x80000000; + MASK_EXPONENT = 0x7ff00000; + MASK_SIGNIFICAND = 0x7fffff; + BIAS = 127; + } + + /** + * <p> + * Extract and unbias the exponent of the given packed <code>float</code> + * value. + * </p> + * <p> + * The exponent is encoded <i>biased</i> as a number in the range + * <code>[0, 255]</code>, with <code>0</code> indicating that the number is + * <i>subnormal</i> and <code>[1, 254]</code> denoting the actual exponent + * plus {@link #BIAS}. Infinite and <code>NaN</code> values always have a + * biased exponent of <code>255</code>. + * </p> + * <p> + * This function will therefore return: + * </p> + * <ul> + * <li> + * <code>0 - {@link #BIAS} = -127</code> iff the input is a <i>subnormal</i> + * number.</li> + * <li>An integer in the range + * <code>[1 - {@link #BIAS}, 254 - {@link #BIAS}] = [-126, 127]</code> iff + * the input is a <i>normal</i> number.</li> + * <li> + * <code>255 - {@link #BIAS} = 128</code> iff the input is + * {@link #POSITIVE_INFINITY}, {@link #NEGATIVE_INFINITY}, or + * <code>NaN</code>.</li> + * </ul> + * + * @see #packSetExponentUnbiasedUnchecked(int) + */ + + public static int unpackGetExponentUnbiased( + final float d) + { + final int b = Float.floatToRawIntBits(d); + final int em = b & Binary32.MASK_EXPONENT; + final int es = em >> 23; + return es - Binary32.BIAS; + } + + /** + * <p> + * Return the sign of the given float value. + * </p> + */ + + public static int unpackGetSign( + final float d) + { + final int b = Float.floatToRawIntBits(d); + return ((b & Binary32.MASK_SIGN) >> 31) & 1; + } + + /** + * <p> + * Return the significand of the given float value. + * </p> + */ + + public static int unpackGetSignificand( + final float d) + { + final int b = Float.floatToRawIntBits(d); + return b & Binary32.MASK_SIGNIFICAND; + } +} diff --git a/src/jogl/classes/com/jogamp/opengl/math/Binary64.java b/src/jogl/classes/com/jogamp/opengl/math/Binary64.java new file mode 100644 index 000000000..5efad433a --- /dev/null +++ b/src/jogl/classes/com/jogamp/opengl/math/Binary64.java @@ -0,0 +1,116 @@ +/** + * Copyright 2013 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ + +package com.jogamp.opengl.math; + +/** + * Functions for interrogating <code>binary64</code> (double) values. + */ + +public final class Binary64 +{ + static final long NEGATIVE_ZERO_BITS; + static final long MASK_SIGN; + static final long MASK_EXPONENT; + static final long MASK_SIGNIFICAND; + static final long BIAS; + + static { + NEGATIVE_ZERO_BITS = 0x8000000000000000L; + MASK_SIGN = 0x8000000000000000L; + MASK_EXPONENT = 0x7ff0000000000000L; + MASK_SIGNIFICAND = 0x000fffffffffffffL; + BIAS = 1023; + } + + /** + * <p> + * Extract and unbias the exponent of the given packed <code>double</code> + * value. + * </p> + * <p> + * The exponent is encoded <i>biased</i> as a number in the range + * <code>[0, 2047]</code>, with <code>0</code> indicating that the number is + * <i>subnormal</i> and <code>[1, 2046]</code> denoting the actual exponent + * plus {@link #BIAS}. Infinite and <code>NaN</code> values always have a + * biased exponent of <code>2047</code>. + * </p> + * <p> + * This function will therefore return: + * </p> + * <ul> + * <li> + * <code>0 - {@link #BIAS} = -1023</code> iff the input is a + * <i>subnormal</i> number.</li> + * <li>An integer in the range + * <code>[1 - {@link #BIAS}, 2046 - {@link #BIAS}] = [-1022, 1023]</code> + * iff the input is a <i>normal</i> number.</li> + * <li> + * <code>2047 - {@link #BIAS} = 1024</code> iff the input is + * {@link #POSITIVE_INFINITY}, {@link #NEGATIVE_INFINITY}, or + * <code>NaN</code>.</li> + * </ul> + * + * @see #packSetExponentUnbiasedUnchecked(int) + */ + + public static long unpackGetExponentUnbiased( + final double d) + { + final long b = Double.doubleToRawLongBits(d); + final long em = b & Binary64.MASK_EXPONENT; + final long es = em >> 52; + return es - Binary64.BIAS; + } + + /** + * <p> + * Return the significand of the given double value. + * </p> + */ + + public static long unpackGetSignificand( + final double d) + { + final long b = Double.doubleToRawLongBits(d); + return b & Binary64.MASK_SIGNIFICAND; + } + + /** + * <p> + * Return the sign of the given double value. + * </p> + */ + + public static long unpackGetSign( + final double d) + { + final long b = Double.doubleToRawLongBits(d); + return ((b & Binary64.MASK_SIGN) >> 63) & 1; + } +} diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/math/Binary16Test.java b/src/test/com/jogamp/opengl/test/junit/jogl/math/Binary16Test.java new file mode 100644 index 000000000..d9e84eeab --- /dev/null +++ b/src/test/com/jogamp/opengl/test/junit/jogl/math/Binary16Test.java @@ -0,0 +1,685 @@ +/** + * Copyright 2013 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ + +package com.jogamp.opengl.test.junit.jogl.math; + +import org.junit.Assert; +import org.junit.Test; + +import com.jogamp.opengl.math.Binary16; + +public final class Binary16Test +{ + /** + * Exponents in the range [-15, 16] are encoded and decoded correctly. + */ + + @SuppressWarnings("static-method") @Test public void testExponentIdentity() + { + System.out.println("-- Exponent identities"); + for (int e = -15; e <= 16; ++e) { + final char p = Binary16.packSetExponentUnbiasedUnchecked(e); + final int u = Binary16.unpackGetExponentUnbiased(p); + System.out.println("e: " + e); + System.out.println("p: " + Integer.toHexString(p)); + System.out.println("u: " + u); + Assert.assertEquals(e, u); + } + } + + /** + * Infinities are infinite. + */ + + @SuppressWarnings("static-method") @Test public void testInfinite() + { + Assert.assertTrue(Binary16.isInfinite(Binary16.POSITIVE_INFINITY)); + Assert.assertTrue(Binary16.isInfinite(Binary16.NEGATIVE_INFINITY)); + Assert.assertFalse(Binary16.isInfinite(Binary16.exampleNaN())); + + for (int i = 0; i <= 65535; ++i) { + Assert.assertFalse(Binary16.isInfinite(Binary16.packDouble(i))); + } + } + + /** + * The unencoded exponent of infinity is 16. + */ + + @SuppressWarnings("static-method") @Test public void testInfinityExponent() + { + Assert.assertEquals( + 16, + Binary16.unpackGetExponentUnbiased(Binary16.POSITIVE_INFINITY)); + } + + /** + * The unencoded exponent of infinity is 16. + */ + + @SuppressWarnings("static-method") @Test public + void + testInfinityNegativeExponent() + { + Assert.assertEquals( + 16, + Binary16.unpackGetExponentUnbiased(Binary16.NEGATIVE_INFINITY)); + } + + /** + * The sign of negative infinity is 1. + */ + + @SuppressWarnings("static-method") @Test public + void + testInfinityNegativeSign() + { + Assert + .assertEquals(1, Binary16.unpackGetSign(Binary16.NEGATIVE_INFINITY)); + } + + /** + * The significand of infinity is 0. + */ + + @SuppressWarnings("static-method") @Test public + void + testInfinityNegativeSignificand() + { + Assert.assertEquals( + 0, + Binary16.unpackGetSignificand(Binary16.NEGATIVE_INFINITY)); + } + + /** + * The sign of positive infinity is 0. + */ + + @SuppressWarnings("static-method") @Test public void testInfinitySign() + { + Assert + .assertEquals(0, Binary16.unpackGetSign(Binary16.POSITIVE_INFINITY)); + } + + /** + * The significand of infinity is 0. + */ + + @SuppressWarnings("static-method") @Test public + void + testInfinitySignificand() + { + Assert.assertEquals( + 0, + Binary16.unpackGetSignificand(Binary16.POSITIVE_INFINITY)); + } + + /** + * NaN is NaN. + */ + + @SuppressWarnings("static-method") @Test public void testNaN() + { + final int n = + Binary16.packSetExponentUnbiasedUnchecked(16) + | Binary16.packSetSignificandUnchecked(1); + final char c = (char) n; + Assert.assertEquals(16, Binary16.unpackGetExponentUnbiased(c)); + Assert.assertEquals(1, Binary16.unpackGetSignificand(c)); + Assert.assertEquals( + 16, + Binary16.unpackGetExponentUnbiased(Binary16.exampleNaN())); + Assert.assertEquals( + 1, + Binary16.unpackGetSignificand(Binary16.exampleNaN())); + Assert.assertTrue(Binary16.isNaN(c)); + Assert.assertTrue(Binary16.isNaN(Binary16.exampleNaN())); + } + + /** + * Packing NaN results in NaN. + */ + + @SuppressWarnings("static-method") @Test public void testPackDoubleNaN() + { + final double k = Double.NaN; + final char r = Binary16.packDouble(k); + Assert.assertTrue(Binary16.isNaN(r)); + } + + /** + * Packing negative infinity results in negative infinity. + */ + + @SuppressWarnings("static-method") @Test public + void + testPackDoubleNegativeInfinity() + { + Assert.assertTrue(Binary16.NEGATIVE_INFINITY == Binary16 + .packDouble(Double.NEGATIVE_INFINITY)); + } + + /** + * Packing negative zero results in negative zero. + */ + + @SuppressWarnings("static-method") @Test public + void + testPackDoubleNegativeZero() + { + Assert.assertTrue(Binary16.NEGATIVE_ZERO == Binary16.packDouble(-0.0)); + } + + /** + * Packing positive infinity results in positive infinity. + */ + + @SuppressWarnings("static-method") @Test public + void + testPackDoublePositiveInfinity() + { + Assert.assertTrue(Binary16.POSITIVE_INFINITY == Binary16 + .packDouble(Double.POSITIVE_INFINITY)); + } + + /** + * Packing positive zero results in positive zero. + */ + + @SuppressWarnings("static-method") @Test public + void + testPackDoublePositiveZero() + { + Assert.assertTrue(Binary16.POSITIVE_ZERO == Binary16.packDouble(0.0)); + } + + /** + * Integers in the range [0, 65520] should be representable. + */ + + @SuppressWarnings({ "static-method", "boxing" }) @Test public + void + testPackDoubleUnpackFloat() + { + for (int i = 0; i <= 65536; ++i) { + final double in = i; + final char packed = Binary16.packDouble(in); + final float r = Binary16.unpackFloat(packed); + System.out.println(String.format( + "packed: 0x%04x 0b%s in: %f unpacked: %f", + (int) packed, + Binary16.toRawBinaryString(packed), + in, + r)); + + if (i <= 2048) { + Assert.assertEquals(in, r, 0.0); + } + if ((i > 2048) && (i <= 4096)) { + Assert.assertTrue((r % 2) == 0); + } + if ((i > 4096) && (i <= 8192)) { + Assert.assertTrue((r % 4) == 0); + } + if ((i > 8192) && (i <= 16384)) { + Assert.assertTrue((r % 8) == 0); + } + if ((i > 16384) && (i <= 32768)) { + Assert.assertTrue((r % 16) == 0); + } + if ((i > 32768) && (i < 65536)) { + Assert.assertTrue((r % 32) == 0); + } + if (i == 65536) { + Assert.assertTrue(Double.isInfinite(r)); + } + } + } + + /** + * Integers in the range [0, 65520] should be representable. + */ + + @SuppressWarnings({ "static-method", "boxing" }) @Test public + void + testPackFloatDoubleEquivalent() + { + for (int i = 0; i <= 65536; ++i) { + final float f_in = i; + final double d_in = i; + final char pf = Binary16.packFloat(f_in); + final char pd = Binary16.packDouble(d_in); + + System.out.println("i: " + i); + System.out.println(String.format( + "pack_f: 0x%04x 0b%s", + (int) pf, + Binary16.toRawBinaryString(pf))); + System.out.println(String.format( + "pack_d: 0x%04x 0b%s", + (int) pd, + Binary16.toRawBinaryString(pd))); + + Assert.assertEquals(pf, pd); + } + } + + /** + * Packing NaN results in NaN. + */ + + @SuppressWarnings("static-method") @Test public void testPackFloatNaN() + { + final float k = Float.NaN; + final char r = Binary16.packFloat(k); + Assert.assertTrue(Binary16.isNaN(r)); + } + + /** + * Packing negative infinity results in negative infinity. + */ + + @SuppressWarnings("static-method") @Test public + void + testPackFloatNegativeInfinity() + { + Assert.assertTrue(Binary16.NEGATIVE_INFINITY == Binary16 + .packFloat(Float.NEGATIVE_INFINITY)); + } + + /** + * Packing negative zero results in negative zero. + */ + + @SuppressWarnings("static-method") @Test public + void + testPackFloatNegativeZero() + { + Assert.assertTrue(Binary16.NEGATIVE_ZERO == Binary16.packFloat(-0.0f)); + } + + /** + * Packing positive infinity results in positive infinity. + */ + + @SuppressWarnings("static-method") @Test public + void + testPackFloatPositiveInfinity() + { + Assert.assertTrue(Binary16.POSITIVE_INFINITY == Binary16 + .packFloat(Float.POSITIVE_INFINITY)); + } + + /** + * Packing positive zero results in positive zero. + */ + + @SuppressWarnings("static-method") @Test public + void + testPackFloatPositiveZero() + { + Assert.assertTrue(Binary16.POSITIVE_ZERO == Binary16.packFloat(0.0f)); + } + + /** + * Integers in the range [0, 65520] should be representable. + */ + + @SuppressWarnings({ "static-method", "boxing" }) @Test public + void + testPackFloatUnpackDouble() + { + for (int i = 0; i <= 65536; ++i) { + final float in = i; + final char packed = Binary16.packFloat(in); + final double r = Binary16.unpackDouble(packed); + System.out.println(String.format( + "packed: 0x%04x 0b%s in: %f unpacked: %f", + (int) packed, + Binary16.toRawBinaryString(packed), + in, + r)); + + if (i <= 2048) { + Assert.assertEquals(in, r, 0.0); + } + if ((i > 2048) && (i <= 4096)) { + Assert.assertTrue((r % 2) == 0); + } + if ((i > 4096) && (i <= 8192)) { + Assert.assertTrue((r % 4) == 0); + } + if ((i > 8192) && (i <= 16384)) { + Assert.assertTrue((r % 8) == 0); + } + if ((i > 16384) && (i <= 32768)) { + Assert.assertTrue((r % 16) == 0); + } + if ((i > 32768) && (i < 65536)) { + Assert.assertTrue((r % 32) == 0); + } + if (i == 65536) { + Assert.assertTrue(Double.isInfinite(r)); + } + } + } + + /** + * Integers in the range [0, 65520] should be representable. + */ + + @SuppressWarnings({ "static-method", "boxing" }) @Test public + void + testPackUnpackDouble() + { + for (int i = 0; i <= 65536; ++i) { + final double in = i; + final char packed = Binary16.packDouble(in); + final double r = Binary16.unpackDouble(packed); + System.out.println(String.format( + "packed: 0x%04x 0b%s in: %f unpacked: %f", + (int) packed, + Binary16.toRawBinaryString(packed), + in, + r)); + + if (i <= 2048) { + Assert.assertEquals(in, r, 0.0); + } + if ((i > 2048) && (i <= 4096)) { + Assert.assertTrue((r % 2) == 0); + } + if ((i > 4096) && (i <= 8192)) { + Assert.assertTrue((r % 4) == 0); + } + if ((i > 8192) && (i <= 16384)) { + Assert.assertTrue((r % 8) == 0); + } + if ((i > 16384) && (i <= 32768)) { + Assert.assertTrue((r % 16) == 0); + } + if ((i > 32768) && (i < 65536)) { + Assert.assertTrue((r % 32) == 0); + } + if (i == 65536) { + Assert.assertTrue(Double.isInfinite(r)); + } + } + } + + /** + * Integers in the range [0, 65520] should be representable. + */ + + @SuppressWarnings({ "static-method", "boxing" }) @Test public + void + testPackUnpackFloat() + { + for (int i = 0; i <= 65536; ++i) { + final float in = i; + final char packed = Binary16.packFloat(in); + final float r = Binary16.unpackFloat(packed); + System.out.println(String.format( + "packed: 0x%04x 0b%s in: %f unpacked: %f", + (int) packed, + Binary16.toRawBinaryString(packed), + in, + r)); + if (i <= 2048) { + Assert.assertEquals(in, r, 0.0); + } + if ((i > 2048) && (i <= 4096)) { + Assert.assertTrue((r % 2) == 0); + } + if ((i > 4096) && (i <= 8192)) { + Assert.assertTrue((r % 4) == 0); + } + if ((i > 8192) && (i <= 16384)) { + Assert.assertTrue((r % 8) == 0); + } + if ((i > 16384) && (i <= 32768)) { + Assert.assertTrue((r % 16) == 0); + } + if ((i > 32768) && (i < 65536)) { + Assert.assertTrue((r % 32) == 0); + } + if (i == 65536) { + Assert.assertTrue(Float.isInfinite(r)); + } + } + } + + /** + * Signs in the range [0, 1] are encoded and decoded correctly. + */ + + @SuppressWarnings("static-method") @Test public void testSignIdentity() + { + System.out.println("-- Sign identities"); + for (int e = 0; e <= 1; ++e) { + final char p = Binary16.packSetSignUnchecked(e); + final int u = Binary16.unpackGetSign(p); + System.out.println("e: " + e); + System.out.println("p: " + Integer.toHexString(p)); + System.out.println("u: " + u); + Assert.assertEquals(e, u); + } + } + + /** + * Significands in the range [0, 1023] are encoded and decoded correctly. + */ + + @SuppressWarnings("static-method") @Test public + void + testSignificandIdentity() + { + System.out.println("-- Significand identities"); + for (int e = 0; e <= 1023; ++e) { + final char p = Binary16.packSetSignificandUnchecked(e); + final int u = Binary16.unpackGetSignificand(p); + System.out.println("e: " + e); + System.out.println("p: " + Integer.toHexString(p)); + System.out.println("u: " + u); + Assert.assertEquals(e, u); + } + } + + /** + * Unpacking NaN results in NaN. + */ + + @SuppressWarnings("static-method") @Test public void testUnpackDoubleNaN() + { + final double k = Binary16.unpackDouble(Binary16.exampleNaN()); + Assert.assertTrue(Double.isNaN(k)); + } + + /** + * Unpacking negative infinity results in negative infinity. + */ + + @SuppressWarnings("static-method") @Test public + void + testUnpackDoubleNegativeInfinity() + { + Assert.assertTrue(Double.NEGATIVE_INFINITY == Binary16 + .unpackDouble(Binary16.NEGATIVE_INFINITY)); + } + + /** + * Unpacking negative zero results in negative zero. + */ + + @SuppressWarnings("static-method") @Test public + void + testUnpackDoubleNegativeZero() + { + Assert.assertTrue(-0.0 == Binary16.unpackDouble(Binary16.NEGATIVE_ZERO)); + } + + /** + * Unpacking 1.0 results in 1.0. + */ + + @SuppressWarnings({ "static-method", "boxing" }) @Test public + void + testUnpackDoubleOne() + { + final char one = 0x3C00; + final double r = Binary16.unpackDouble(one); + System.out.println(String.format("0x%04x -> %f", (int) one, r)); + Assert.assertEquals(r, 1.0, 0.0); + } + + /** + * Unpacking -1.0 results in -1.0. + */ + + @SuppressWarnings({ "static-method", "boxing" }) @Test public + void + testUnpackDoubleOneNegative() + { + final char one = 0xBC00; + final double r = Binary16.unpackDouble(one); + System.out.println(String.format("0x%04x -> %f", (int) one, r)); + Assert.assertEquals(r, -1.0, 0.0); + } + + /** + * Unpacking positive infinity results in positive infinity. + */ + + @SuppressWarnings("static-method") @Test public + void + testUnpackDoublePositiveInfinity() + { + Assert.assertTrue(Double.POSITIVE_INFINITY == Binary16 + .unpackDouble(Binary16.POSITIVE_INFINITY)); + } + + /** + * Unpacking positive zero results in positive zero. + */ + + @SuppressWarnings("static-method") @Test public + void + testUnpackDoublePositiveZero() + { + Assert.assertTrue(0.0 == Binary16.unpackDouble(Binary16.POSITIVE_ZERO)); + } + + /** + * Unpacking 2.0 results in 2.0. + */ + + @SuppressWarnings({ "static-method", "boxing" }) @Test public + void + testUnpackDoubleTwo() + { + final char one = 0x4000; + final double r = Binary16.unpackDouble(one); + System.out.println(String.format("%04x -> %f", (int) one, r)); + Assert.assertEquals(r, 2.0, 0.0); + } + + /** + * Unpacking -2.0 results in -2.0. + */ + + @SuppressWarnings({ "static-method", "boxing" }) @Test public + void + testUnpackDoubleTwoNegative() + { + final char one = 0xC000; + final double r = Binary16.unpackDouble(one); + System.out.println(String.format("%04x -> %f", (int) one, r)); + Assert.assertEquals(r, -2.0, 0.0); + } + + /** + * Unpacking NaN results in NaN. + */ + + @SuppressWarnings("static-method") @Test public void testUnpackFloatNaN() + { + final float k = Binary16.unpackFloat(Binary16.exampleNaN()); + Assert.assertTrue(Float.isNaN(k)); + } + + /** + * Unpacking negative infinity results in negative infinity. + */ + + @SuppressWarnings("static-method") @Test public + void + testUnpackFloatNegativeInfinity() + { + Assert.assertTrue(Float.NEGATIVE_INFINITY == Binary16 + .unpackFloat(Binary16.NEGATIVE_INFINITY)); + } + + /** + * Unpacking negative zero results in negative zero. + */ + + @SuppressWarnings("static-method") @Test public + void + testUnpackFloatNegativeZero() + { + Assert.assertTrue(-0.0 == Binary16.unpackFloat(Binary16.NEGATIVE_ZERO)); + } + + /** + * Unpacking 1.0 results in 1.0. + */ + + @SuppressWarnings({ "static-method", "boxing" }) @Test public + void + testUnpackFloatOne() + { + final char one = 0x3C00; + final float r = Binary16.unpackFloat(one); + System.out.println(String.format("0x%04x -> %f", (int) one, r)); + Assert.assertEquals(r, 1.0, 0.0); + } + + /** + * Unpacking -1.0 results in -1.0. + */ + + @SuppressWarnings({ "static-method", "boxing" }) @Test public + void + testUnpackFloatOneNegative() + { + final char one = 0xBC00; + final float r = Binary16.unpackFloat(one); + System.out.println(String.format("0x%04x -> %f", (int) one, r)); + Assert.assertEquals(r, -1.0, 0.0); + } +} diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/math/Binary32Test.java b/src/test/com/jogamp/opengl/test/junit/jogl/math/Binary32Test.java new file mode 100644 index 000000000..42e7f08a1 --- /dev/null +++ b/src/test/com/jogamp/opengl/test/junit/jogl/math/Binary32Test.java @@ -0,0 +1,93 @@ +/** + * Copyright 2013 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ + +package com.jogamp.opengl.test.junit.jogl.math; + +import org.junit.Assert; +import org.junit.Test; + +import com.jogamp.opengl.math.Binary32; + +public class Binary32Test +{ + @SuppressWarnings("static-method") @Test public void testInfinityExponent() + { + Assert.assertEquals( + 128, + Binary32.unpackGetExponentUnbiased(Float.POSITIVE_INFINITY)); + } + + @SuppressWarnings("static-method") @Test public + void + testInfinityNegativeExponent() + { + Assert.assertEquals( + 128, + Binary32.unpackGetExponentUnbiased(Float.NEGATIVE_INFINITY)); + } + + @SuppressWarnings("static-method") @Test public + void + testInfinityNegativeSign() + { + Assert.assertEquals(1, Binary32.unpackGetSign(Float.NEGATIVE_INFINITY)); + } + + @SuppressWarnings("static-method") @Test public + void + testInfinityNegativeSignificand() + { + Assert.assertEquals( + 0, + Binary32.unpackGetSignificand(Float.NEGATIVE_INFINITY)); + } + + @SuppressWarnings("static-method") @Test public void testInfinitySign() + { + Assert.assertEquals(0, Binary32.unpackGetSign(Float.POSITIVE_INFINITY)); + } + + @SuppressWarnings("static-method") @Test public + void + testInfinitySignificand() + { + Assert.assertEquals( + 0, + Binary32.unpackGetSignificand(Float.POSITIVE_INFINITY)); + } + + @SuppressWarnings("static-method") @Test public void testNaNExponent() + { + Assert.assertEquals(128, Binary32.unpackGetExponentUnbiased(Float.NaN)); + } + + @SuppressWarnings("static-method") @Test public void testNaNSignificand() + { + Assert.assertTrue(Binary32.unpackGetSignificand(Float.NaN) > 0); + } +} diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/math/Binary64Test.java b/src/test/com/jogamp/opengl/test/junit/jogl/math/Binary64Test.java new file mode 100644 index 000000000..78806985f --- /dev/null +++ b/src/test/com/jogamp/opengl/test/junit/jogl/math/Binary64Test.java @@ -0,0 +1,93 @@ +/** + * Copyright 2013 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ + +package com.jogamp.opengl.test.junit.jogl.math; + +import org.junit.Assert; +import org.junit.Test; + +import com.jogamp.opengl.math.Binary64; + +public class Binary64Test +{ + @SuppressWarnings("static-method") @Test public void testInfinityExponent() + { + Assert.assertEquals( + 1024, + Binary64.unpackGetExponentUnbiased(Double.POSITIVE_INFINITY)); + } + + @SuppressWarnings("static-method") @Test public + void + testInfinityNegativeExponent() + { + Assert.assertEquals( + 1024, + Binary64.unpackGetExponentUnbiased(Double.NEGATIVE_INFINITY)); + } + + @SuppressWarnings("static-method") @Test public + void + testInfinityNegativeSign() + { + Assert.assertEquals(1, Binary64.unpackGetSign(Double.NEGATIVE_INFINITY)); + } + + @SuppressWarnings("static-method") @Test public + void + testInfinityNegativeSignificand() + { + Assert.assertEquals( + 0, + Binary64.unpackGetSignificand(Double.NEGATIVE_INFINITY)); + } + + @SuppressWarnings("static-method") @Test public void testInfinitySign() + { + Assert.assertEquals(0, Binary64.unpackGetSign(Double.POSITIVE_INFINITY)); + } + + @SuppressWarnings("static-method") @Test public + void + testInfinitySignificand() + { + Assert.assertEquals( + 0, + Binary64.unpackGetSignificand(Double.POSITIVE_INFINITY)); + } + + @SuppressWarnings("static-method") @Test public void testNaNExponent() + { + Assert.assertEquals(1024, Binary64.unpackGetExponentUnbiased(Double.NaN)); + } + + @SuppressWarnings("static-method") @Test public void testNaNSignificand() + { + Assert.assertTrue(Binary64.unpackGetSignificand(Double.NaN) > 0); + } +} |