/** * 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 to convert values to/from the binary16
format
* specified in IEEE 754 2008
.
*
-∞
.
*/
public static final char NEGATIVE_INFINITY;
/**
* The encoded form of positive infinity ∞
.
*/
public static final char POSITIVE_INFINITY;
/**
* The encoded form of positive zero 0
.
*/
public static final char POSITIVE_ZERO;
/**
* The encoded form of negative zero -0
.
*/
public static final char NEGATIVE_ZERO;
/**
* The bias value used to offset the encoded exponent. A given
* exponent e
is encoded as {@link #BIAS} + e
.
*/
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 true
if the given packed binary16
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 true
if the given packed binary16
value
* is not a number (NaN
).
*/
public static boolean isNaN(
final char k)
{
final int e = Binary16.unpackGetExponentUnbiased(k);
final int s = Binary16.unpackGetSignificand(k);
return (e == 16) && (s > 0);
}
/**
*
* Convert a double precision floating point value to a packed
* binary16
value.
*
* For the following specific cases, the function returns: *
*NaN
iff isNaN(k)
k == {@link Double#POSITIVE_INFINITY}
k == {@link Double#NEGATIVE_INFINITY}
k == -0.0
k == 0.0
* Otherwise, the binary16
value that most closely represents
* k
is returned. This may obviously be an infinite value as
* the interval of double precision values is far larger than that of the
* binary16
type.
*
* Convert a single precision floating point value to a packed
* binary16
value.
*
* For the following specific cases, the function returns: *
*NaN
iff isNaN(k)
k == {@link Float#POSITIVE_INFINITY}
k == {@link Float#NEGATIVE_INFINITY}
k == -0.0
k == 0.0
* Otherwise, the binary16
value that most closely represents
* k
is returned. This may obviously be an infinite value as
* the interval of single precision values is far larger than that of the
* binary16
type.
*
* Encode the unbiased exponent e
. Values should be in the
* range [-15, 16]
- values outside of this range will be
* truncated.
*
* Encode the significand s
. Values should be in the range
* [0, 1023]
. Values outside of this range will be truncated.
*
* Encode the sign bit s
. Values should be in the range
* [0, 1]
, with 0
ironically denoting a positive
* value. Values outside of this range will be truncated.
*
binary16
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();
}
/**
*
* Convert a packed binary16
value k
to a
* double-precision floating point value.
*
* The function returns: *
*NaN
iff isNaN(k)
k == {@link #POSITIVE_INFINITY}
k == {@link #NEGATIVE_INFINITY}
-0.0
iff k == {@link #NEGATIVE_ZERO}
0.0
iff k == {@link #POSITIVE_ZERO}
(-1.0 * n) * (2 ^ e) * 1.s
, for the decoded sign
* n
of k
, the decoded exponent e
of
* k
, and the decoded significand s
of
* k
.
* Convert a packed binary16
value k
to a
* single-precision floating point value.
*
* The function returns: *
*NaN
iff isNaN(k)
k == {@link #POSITIVE_INFINITY}
k == {@link #NEGATIVE_INFINITY}
-0.0
iff k == {@link #NEGATIVE_ZERO}
0.0
iff k == {@link #POSITIVE_ZERO}
(-1.0 * n) * (2 ^ e) * 1.s
, for the decoded sign
* n
of k
, the decoded exponent e
of
* k
, and the decoded significand s
of
* k
.
* Extract and unbias the exponent of the given packed binary16
* value.
*
* The exponent is encoded biased as a number in the range
* [0, 31]
, with 0
indicating that the number is
* subnormal and [1, 30]
denoting the actual exponent
* plus {@link #BIAS}. Infinite and NaN
values always have an
* exponent of 31
.
*
* This function will therefore return: *
*0 - {@link #BIAS} = -15
iff the input is a subnormal
* number.[1 - {@link #BIAS}, 30 - {@link #BIAS}] = [-14, 15]
iff the
* input is a normal number.16
iff the input is {@link #POSITIVE_INFINITY},
* {@link #NEGATIVE_INFINITY}, or NaN
.binary16
value, as
* an integer in the range [0, 1]
.
*
* @see Binary16#packSetSignUnchecked(int)
*/
public static int unpackGetSign(
final char k)
{
return (k & Binary16.MASK_SIGN) >> 15;
}
/**
*
* Return the significand of the given packed binary16
value as
* an integer in the range [0, 1023]
.
*