public class Precision
extends java.lang.Object
implements java.lang.Cloneable
Signed fixed point formats are represented in a 2's complement format. As a consequence, a single bit is used to represent the sign of the number. The value of a multi-bit signed number is:
Signed(B) = 2^m x (- b_{n-1} x 2^{n-1} + \sum_{i=0}^{n-2} b_i x 2^i), where b_i is the value of bit i of the bit vector.
Unsigned fixed formats are represented as unsigned binary values. No bits are used to represent the sign and all values are positive. The actual value of a multi-bit unsigned fixed point number is:
Unsigned(B) = 2^m x (\sum_{i=0}^{n-1} b_i x 2^i)
This class supports several different String formats for specifying and
displaying the Precision. These String formats are supported by several
classes that extend the Precision.PrecisionFormat
class. Several such classes
have been created. Any given precision string format can be represented in
any of the valid precision string formats. The four supported Precision
String formats are shown in the table below. A static singleton object of
each of these formats is provided in this class. Each String Representation
column of the table represent equivalent Precision formats:
Format Name | Format Spec | ||||||
Precision.IntegerFractionPrecisionFormat |
[integer bits].[fraction bits] | 3.2 | 0.7 | -2.12 | 12.-4 | 32.0 | U1.7 |
Precision.LengthIntegerPrecisionFormat |
[total bits]/[integer bits] | 5/3 | 7/0 | 10/-2 | 8/12 | 32/32 | U8/1 |
Precision.VHDLPrecisionFormat |
[MSb position]:[LSb position] | 2:-2 | -1:-7 | -3:-12 | 11:4 | 31:0 | U0:-7 |
Precision.LengthExponentPrecisionFormat |
[total bits]e[binary point position] | 5e-2 | 7e-7 | 10e-12 | 8e4 | 32e0 | U8e-7 |
An instance of the class is immutable, meaning that its value is set in the constructor and cannot then be modified.
FixPoint
Red (kienhuis) |
Yellow (kienhuis) |
Modifier and Type | Class and Description |
---|---|
static class |
Precision.ExpressionLanguagePrecisionFormat
Precision format for use with the Expression Language.
|
static class |
Precision.IntegerFractionPrecisionFormat
Defines a Precision string format using the INTEGER.FRACTION
precision format.
|
static class |
Precision.LengthExponentPrecisionFormat
Defines a Precision string format using the LENGTHeEXPONENT
precision format.
|
static class |
Precision.LengthIntegerPrecisionFormat
Defines a Precision string format using the LENGTH/INTEGER
precision format.
|
static class |
Precision.PrecisionFormat
Defines a String format for specifying a Precision object.
|
static class |
Precision.VHDLPrecisionFormat
Defines a Precision string format using the VHDL MSB:LSB
precision format.
|
Modifier and Type | Field and Description |
---|---|
static Precision.PrecisionFormat |
EXPRESSION_LANGUAGE
static ExpressionLanguagePrecisionFormat object.
|
static Precision.PrecisionFormat |
INTEGER_FRACTION
static IntegerFractionPrecisionFormat object.
|
static Precision.PrecisionFormat |
LENGTH_EXPONENT
static LengthExponentPrecisionFormat object.
|
static Precision.PrecisionFormat |
LENGTH_INTEGER
static LengthIntegerPrecisionFormat object.
|
static Precision.PrecisionFormat |
VHDL
static VHDLPrecisionFormat object.
|
Constructor and Description |
---|
Precision(int length,
int integerBits)
Construct a Precision object based on the length/integer
bits format.
|
Precision(int sign,
int length,
int exponent)
Construct a Precision object based on the sign, length, and
exponent format.
|
Precision(java.lang.String str)
Construct a Precision object based on the provided string.
|
Modifier and Type | Method and Description |
---|---|
java.lang.Object |
clone()
Return this, that is, return the reference to this object.
|
boolean |
equals(java.lang.Object object)
Return true if the indicated object is an instance of Precision
and the precision format matches exactly.
|
java.math.BigDecimal |
findMaximum()
Return the maximum obtainable value in this precision.
|
java.math.BigDecimal |
findMinimum()
Return the minimum obtainable value for this precision.
|
java.math.BigDecimal |
getEpsilon()
Return the incremental value between discrete values under
the given Precision format.
|
int |
getExponent()
Return the location of the binary exponent.
|
int |
getFractionBitLength()
Return the number of bits representing the fractional part.
|
int |
getIntegerBitLength()
Return the number of bits representing the integer part.
|
int |
getLeastSignificantBitPosition()
Return the bit position of the least significant bit of the
given fixed point precision.
|
java.math.BigInteger |
getMaximumUnscaledValue()
Return the maximum integer value before scaling so that
quantization levels are represented by adjacent integers.
|
java.math.BigInteger |
getMinimumUnscaledValue()
Return the minimum integer value before scaling so
that quantization levels are represented by adjacent
integers.
|
int |
getMostSignificantBitPosition()
Return the bit position of the most significant bit of the
given fixed point precision.
|
int |
getMostSignificantDataBitPosition()
Return the bit position of the most significant
data bit of the given fixed point precision.
|
int |
getNumberOfBits()
Return the total number of bits.
|
java.math.BigInteger |
getNumberOfLevels()
Return the total number of discrete values possible.
|
int |
getSign()
Return the sign (0 = unsigned, 1 = signed).
|
int |
hashCode()
Return a hash code value for this Precision.
|
boolean |
isSigned()
Determine if the fixed point format is signed or not.
|
static java.math.BigDecimal |
shiftBigDecimal(java.math.BigDecimal val,
int shiftval)
Shift the BigDecimal value either right or left by
a power of 2 value.
|
java.lang.String |
toString()
Return a string representing this precision.
|
java.lang.String |
toString(Precision.PrecisionFormat format)
Return a string representation of this format in one of several
styles.
|
static Precision |
union(Precision precisionA,
Precision precisionB)
Return the precision that is the maximum of the two supplied
precisions in both the length and exponent.
|
public static final Precision.PrecisionFormat INTEGER_FRACTION
public static final Precision.PrecisionFormat LENGTH_EXPONENT
public static final Precision.PrecisionFormat LENGTH_INTEGER
public static final Precision.PrecisionFormat EXPRESSION_LANGUAGE
public static final Precision.PrecisionFormat VHDL
public Precision(java.lang.String str) throws java.lang.IllegalArgumentException
str
- The string representing the precision.java.lang.IllegalArgumentException
- If the precision string
supplied does not match one of the known formats.public Precision(int length, int integerBits) throws java.lang.IllegalArgumentException
length
- The total number of bits.integerBits
- The number of integer bits.java.lang.IllegalArgumentException
- If the given values are
negative or when the integer number of bits is larger than the
total number of bits.public Precision(int sign, int length, int exponent) throws java.lang.IllegalArgumentException
sign
- The presence of a sign bit (1 = signed, 0 = unsigned).length
- The total number of bits.exponent
- The bit location of the exponent.java.lang.IllegalArgumentException
- If the Precision arguments
are inconsistent.public java.lang.Object clone()
clone
in class java.lang.Object
public boolean equals(java.lang.Object object)
equals
in class java.lang.Object
object
- Object to test for equalitypublic java.math.BigDecimal findMaximum()
getMaximumUnscaledValue()
by 2exponent.public java.math.BigDecimal findMinimum()
getMinimumUnscaledValue()
by 2exponent.public java.math.BigDecimal getEpsilon()
public int getExponent()
public int getFractionBitLength()
public int getIntegerBitLength()
public int getLeastSignificantBitPosition()
public java.math.BigInteger getMaximumUnscaledValue()
public java.math.BigInteger getMinimumUnscaledValue()
public int getMostSignificantBitPosition()
public int getMostSignificantDataBitPosition()
public int getNumberOfBits()
public java.math.BigInteger getNumberOfLevels()
public int getSign()
public int hashCode()
hashCode
in class java.lang.Object
public boolean isSigned()
public static Precision union(Precision precisionA, Precision precisionB)
precisionA
- A precisionprecisionB
- Another precisionpublic static java.math.BigDecimal shiftBigDecimal(java.math.BigDecimal val, int shiftval)
val
- BigDecimal value to shift.shiftval
- Amount of "power of 2" shifting to perform
on the BigDecimal valuepublic java.lang.String toString()
toString
in class java.lang.Object
public java.lang.String toString(Precision.PrecisionFormat format)
format
- The String format represented desired for printing.