|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object ca.idms.io.FloatConverter
public class FloatConverter
Converter class for IEEE BFP and IBM HFP.
This class provides methods to convert between the Hexadecimal Floating Point
(HFP) numbers traditionally used on IBM S/390 (and z900) systems and the
IEEE Binary Floating Point (BFP) used in Java. The methods in this class
are modeled (loosely) after the z900 CONVERT BFP TO HFP
and
CONVERT HFP TO BFP
instructions. They support conversion
of single and double precision numbers and a subset of the rounding methods
used by the analagous z900 conversion instructions.
Single Precision Floating Point Representations
IEEE Binary Floating Point (BFP)
The format of the Java float
type is
4 bytes, consisting of a sign bit, an 8 bit binary exponent
in excess-127 notation, and a 23 bit binary mantissa (or fraction),
arranged as:
+---------------+---------------+---------------+---------------+ | s1,e7...e1 | e0,m22...m16 | m15...m8 | m7...m0 | +---------------+---------------+---------------+---------------+ where: s1 = sign bit e7...e0 = exponent bits m22...m0 = mantissa bits
The value of a normalized number is:
(-1)^(sign)*(1.mantissa)*(2^(exponent-127))
For a normalized number an implicit 1 is inserted in the fraction.
Example: 300.0 = 0x43960000
A denormalized (or subnormal) number can represent numbers whose magnitude is smaller than the lowest exponent (-126) at the cost of a loss of precision. Denormalized numbers have an exponent of all zeros and an explicit most significant bit in the mantissa.
Example: 1E-45 = 0x00000001
All bit patterns do not represent a numeric value. Non-numeric values include +/-Infinity and NaN (Not a Number) and have an exponent of all ones.
S/390 and z900 Hexadecimal Floating Point (HFP)
The format of the HFP float
type is
4 bytes, consisting of a sign bit, a 7 bit hexadecimal exponent
in excess-64 notation, and a 24 bit binary mantissa (or fraction),
arranged as:
+---------------+---------------+---------------+---------------+ | s1,e6...e0 | m23...m16 | m15...m8 | m7...m0 | +---------------+---------------+---------------+---------------+ where: s1 = sign bit e6...e0 = exponent bits m23...m0 = mantissa bits
The value of the number is
(-1^(sign)*(.mantissa)*(16^(exponent-64))
In a "normalized" number the first one to three bits may be zero. The most significant bit is explicit. Denormalized numbers are also supported. All bit patterns represent a numeric value.
Example: 300 = 4312C000
Double Precision Floating Point Representations
IEEE Binary Floating Point (BFP)
The format of the BFP double
type is
8 bytes, consisting of a sign bit, an 11 bit binary exponent
in excess-1023 notation, and a 52 bit binary mantissa,
arranged as:
+---------------+---------------+---------------+---------------+ | s1,e10...e4 |e3..e0,m51..m48| m47...m40 | m39...m32 | +---------------+---------------+---------------+---------------+ | m31...m24 | m23...m16 | m15...m8 | m7...m0 | +---------------+---------------+---------------+---------------+ where: s1 = sign bit e11...e0 = exponent bits m52...m0 = mantissa bits
The value of the number is
(-1)^(sign)*(1.mantissa)*(2^(exponent-1023))
For a normalized number an implicit 1 is inserted in the fraction.
Example: 300.0 = 0x4072C00000000000
A denormalized (or subnormal) number can represent numbers whose magnitude is smaller than the lowest exponent (-1022) at the cost of a loss of precision. Denormalized numbers have an exponent of all zeros and an explicit most significant bit in the mantissa.
Example: 4.9E-324 = 0x0000000000000001
All bit [atterns do not represent a numeric value. Non-numeric values include +/-Infinity and NaN (Not a Number) and have an exponent of all ones.
S/390 and z900 Hexadecimal Floating Point (HFP)
The format of the S/390 double
type is
8 bytes, consisting of a sign bit, a 7 bit hexadecimal exponent
in excess-64 notation, and a 56 bit binary mantissa,
arranged as:
+---------------+---------------+---------------+---------------+ | s1,e6...e0 | m55...m48 | m47...m40 | m39...m32 | +---------------+---------------+---------------+---------------+ | m31...m24 | m23...m16 | m15...m8 | m7...m0 | +---------------+---------------+---------------+---------------+ where: s1 = sign bit e6...e0 = exponent bits m55...m0 = mantissa bits
The value of the number is
(-1^(sign)*(.mantissa)*(16^(exponent-64))
In a "normalized" number the first one to three bits may be zero. The most significant bit is explicit. Denormalized numbers are also supported. All bit patterns represent a numeric value.
Field Summary | |
---|---|
static boolean |
DEF_NORM_HFP
setNormalizeHFP(boolean) default. |
static int |
DEF_ROUND_BFP
setRoundBFP(int) default. |
static int |
DEF_ROUND_HFP
setRoundHFP(int) default. |
static long |
HMAXD
Largest (in magnitude) double precision HFP number. |
static int |
HMAXF
Largest (in magnitude) single precision HFP number. |
static int |
ROUND_0
Specifies "round toward 0". |
static int |
ROUND_BIAS
Specifies "biased round to nearest". |
static int |
ROUND_NEAR
Specifies "round to nearest". |
static boolean |
trace
Trace flag. |
Constructor Summary | |
---|---|
FloatConverter()
|
Method Summary | |
---|---|
long |
doubleToLongBits(double value)
Converts a double precision IEEE BFP value to an S/390 or z900 HFP bit representation. |
int |
floatToIntBits(float value)
Converts a single precision IEEE BFP value to an S/390 or z900 HFP bit representation. |
boolean |
getNormalizeHFP()
Gets the BFP-->HFP normalization state. |
int |
getRoundBFP()
Gets the HFP-->BFP rounding mode. |
int |
getRoundHFP()
Gets the BFP-->HFP rounding mode. |
float |
intBitsToFloat(int bits)
Converts a single precision S/390 or z900 HFP bit representation to an IEEE BFP value. |
double |
longBitsToDouble(long bits)
Converts a double precision S/390 or z900 HFP bit representation to an IEEE BFP value. |
void |
setNormalizeHFP(boolean enable)
Enables or disable BFP-->HFP normalization. |
void |
setRoundBFP(int mode)
Sets the HFP-->BFP rounding mode. |
void |
setRoundHFP(int mode)
Sets the BFP-->HFP rounding mode. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int HMAXF
public static final long HMAXD
public static final int ROUND_0
public static final int ROUND_BIAS
public static final int ROUND_NEAR
public static int DEF_ROUND_BFP
setRoundBFP(int)
default.
public static int DEF_ROUND_HFP
setRoundHFP(int)
default.
public static boolean DEF_NORM_HFP
setNormalizeHFP(boolean)
default.
public static boolean trace
Constructor Detail |
---|
public FloatConverter()
Method Detail |
---|
public void setRoundBFP(int mode)
mode
- rounding mode, ignored if invalid.ROUND_0
,
ROUND_BIAS
,
ROUND_NEAR
,
DEF_ROUND_BFP
public int getRoundBFP()
setRoundBFP(int)
public void setRoundHFP(int mode)
mode
- rounding mode, ignored if invalid.ROUND_0
,
ROUND_BIAS
,
ROUND_NEAR
,
DEF_ROUND_HFP
public int getRoundHFP()
setRoundBFP(int)
public void setNormalizeHFP(boolean enable)
true
, double precision BFP numbers
that would result in unnormalized HFP numbers will be treated as
underflow and converted to 0. This is consistent with the behavior
of the CONVERT BFP TO HFP
instruction. When set to
false
, the default, double precision BFP numbers that
can be represented as unnormalized HFP numbers will be rounded and
converted.
enable
- true or falseDEF_NORM_HFP
,
doubleToLongBits(double)
public boolean getNormalizeHFP()
setNormalizeHFP(boolean)
public int floatToIntBits(float value)
LE R2,VALUE load short BFP THDER R0,R2 convert BFP to long HFP STE R0,RESULT return short HFP
The z900 Principles of Operation manual states that conversion from BFP to HFP is exact (and overflow and underflow cannot occur for short formats because of the difference in exponent width). This is because the z900 CONVERT BFP TO HFP instruction converts a short BFP to a long HFP, and thus does not need to round the result. Of course the application does need to deal with rounding to use a short format result. Using the left half of the target floating point register as a short HFP is equivalent to "round toward 0", or truncation. The z900 LOAD ROUNDED instruction is equivalent to "bias round to nearest". This method supports a subset of the z900 rounding modes to simulate either technique, with truncation as the default.
The following table describes how it handles the various special
cases that occur when converting IEEE binary floating point (BFP)
to z900 hexadecimal floating point (HFP). The rounding method is
specified by setRoundHFP(int)
BFP (a) HFP -Infinity <= a < -Hmax -Hmax (1) -Hmax <= a <= -Hmin rounded HFP -Hmin < a < 0 -0 (1) -0 -0 +0 +0 0 < a < +Hmin +0 (1) +Hmin <= a <= +Hmax rounded HFP +Hmax < a <= +Infinity +Hmax (1) NaN +HmaxNotes:
value
- BFP number
public float intBitsToFloat(int bits)
LZDR R2 clear right half of register LE R2,VALUE load z900 short HFP, extended to long TBEDR R0,M3,R2 convert HFP to short BFP, M3 = rounding mode STE R0,RESULT return resultThis method supports a subset of the explict rounding modes available for the S/390 CONVERT HFP TO BFP instruction.
The following table describes how it handles the various special
cases that occur when converting S/390 hexadecimal floating point (HFP)
to IEEE binary floating point (BFP). The rounding method is specified
by setRoundBFP(int)
.
HFP (a) BFP a < -Nmax -Infinity (1) a < -Nmax -Nmax (2) -Nmax <= a <= -Nmin rounded BFP -Nmin < a <= -Dmin denormalized BFP -Dmin < a < 0 denormalized BFP -0 -0 +0 +0 0 < a < +Dmin denormalized BFP +Dmin <= a < +Nmin denormalized BFP +Nmin <= a <= +Nmax rounded BFP +Nmax < a +Nmax (2) +Nmax < a +Infinity (1)Notes:
ROUND_BIAS
or ROUND_NEAR
ROUND_0
bits
- S/390 HFP bit representation
public long doubleToLongBits(double value)
LD R2,VALUE load long BFP THDR R0,R2 convert long BFP to long HFP STD R0,RESULT return long HFPThe z900 Principles of Operation manual states that conversion from BFP to HFP is exact (although overflow and underflow can occur for long formats because of the difference in exponent width). The CONVERT BFP TO HFP instruction always produces a normalized result, and by default this method works the same way. When the normalizeBFP option is disable, this method will use the effective rounding mode to attempt to return result for numbers that are smaller in magnitude than the smallest normalized representable HFP number, yet still representable as an unnormalized HFP number.
The following table describes how it handles the various special
cases that occur when converting IEEE binary floating point (BFP)
to S/390 hexadecimal floating point (HFP). The rounding method is
specified by setRoundHFP(int)
. Normalization is enabled and
disabled with setNormalizeHFP(boolean)
.
BFP (a) HFP -Infinity <= a < -Hmax -Hmax -Hmax <= a <= -Hmin HFP -Hmin < a <= -Dmin rounded HFP (1) -Hmin < a <= -Dmin -0 (2) -Dmin < a < 0 -0 -0 -0 +0 +0 0 < a < +Dmin +0 +Dmin <= a < +Hmin +0 (2) +Dmin <= a < +Hmin rounded HFP (1) +Hmin <= a <= +Hmax HFP +Hmax < a <= +Infinity +Hmax NaN +Hmax
Notes:
value
- IEEE BFP number
setNormalizeHFP(boolean)
public double longBitsToDouble(long bits)
LD R2,VALUE load z900 long HFP TBDR R0,M3,R2 convert HFP to BFP, M3 = rounding mode STD R0,RESULT return resultThis method supports a subset of the explict rounding modes available for the S/390 CONVERT HFP TO BFP instruction.
The following table describes how it handles the various special
cases that occur when converting S/390 hexadecimal floating point (HFP)
to IEEE binary floating point (BFP). Since the BFP exponent is wider
than the HFP exponent, there is no possibity of overflow or underflow
when converting HFP to BFP. Since the BFP mantissa is narrower than
the HFP mantissa, there is the possiblity of loss of precision when
it is rounded to fit. The rounding method is specified by
setRoundBFP(int)
. Since there are really no special cases, this
table becomes trivial.
HFP (a) BFP a < 0 rounded BFP -0 -0 +0 +0 0 < a rounded BFPNotes:
ROUND_BIAS
or ROUND_NEAR
ROUND_0
bits
- S/390 HFP bit representation
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |