|
casacore
|
Class that understands the bit-representation of single-precision floating point numbers, and that can decompose it into mantissa, exponent, sign and special values. More...
#include <BitFloat.h>
Public Member Functions | |
| constexpr | BitFloat () |
| Constructs a zero-value BitFloat. | |
| constexpr | BitFloat (float f) |
| Constructs a BitFloat by decomposing the specified floating point value. | |
| constexpr | BitFloat (uint32_t mantissa, int8_t exponent, bool sign) |
| constexpr uint32_t | Mantissa () const |
| constexpr int8_t | Exponent () const |
| constexpr uint32_t | PackMantissa () const |
| Combines the sign and the mantissa into one uint32_t. | |
| constexpr bool | Sign () const |
| constexpr BitFloat & | operator+= (const BitFloat &rhs) |
Adds the value rhs to this. | |
| constexpr BitFloat & | operator-= (const BitFloat &rhs) |
Like operator+=, but subtracts rhs. | |
| constexpr BitFloat & | operator*= (unsigned factor) |
| Multiplies the value by an integer factor. | |
| constexpr BitFloat & | operator/= (unsigned factor) |
| Divides the value by an integer factor. | |
| constexpr float | ToFloat () const |
| Compose this value back into a single-precision floating point value. | |
| constexpr | operator float () const |
| constexpr bool | AllowsMath () const |
| constexpr bool | MantissaOverflow () const |
| Returns true if bit 32 is set. | |
Static Public Member Functions | |
| static constexpr std::pair< uint32_t, bool > | UnpackMantissa (uint32_t mantissa_with_sign) |
| Given a result from PackMantissa(), this function reversed the packing. | |
| static constexpr BitFloat | FromCompressed (uint32_t mantissa_with_sign, int8_t exponent) |
| Constructs a BitFloat from a 'packed mantissa' (see PackMantissa()) and the exponent. | |
| static constexpr bool | AllowsMath (int8_t exponent) |
| Based on the exponent, determines if this is a special value and should not be used in mathematical operations like addition or multiplication. | |
| static constexpr BitFloatKind | GetKind (float f) |
| Determine what kind of float the specified value is: normal, nan, inf, etc. | |
Private Attributes | |
| uint32_t | mantissa_ |
| int8_t | exponent_ |
| bool | sign_ |
Friends | |
| constexpr BitFloat | operator- (const BitFloat &input) |
| Negation; flips the sign of the value. | |
| constexpr bool | operator== (const BitFloat &lhs, const BitFloat &rhs) |
| constexpr std::optional< BitFloat > | Match (const BitFloat &input, int8_t value_exponent) |
| Shifts the input value such that its exponent matches the specified exponent. | |
Class that understands the bit-representation of single-precision floating point numbers, and that can decompose it into mantissa, exponent, sign and special values.
It can also do some (limited) mathematical operations on it, while it can do these operations without loss of precision of the source number. It is used to implement compression (Sisco) that is based on predicting the next value in time and frequency.
Definition at line 54 of file BitFloat.h.
|
inlineconstexpr |
Constructs a zero-value BitFloat.
Definition at line 57 of file BitFloat.h.
References exponent_, mantissa_, and sign_.
Referenced by FromCompressed(), Match, operator*=(), operator+=(), operator-, operator-=(), operator/=(), and operator==.
|
inlineexplicitconstexpr |
|
inlineconstexpr |
Definition at line 70 of file BitFloat.h.
References exponent_, mantissa_, casacore::sign(), and sign_.
|
inlineconstexpr |
Definition at line 220 of file BitFloat.h.
References AllowsMath(), and exponent_.
Referenced by AllowsMath().
|
inlinestaticconstexpr |
Based on the exponent, determines if this is a special value and should not be used in mathematical operations like addition or multiplication.
The value 'zero' is also considered to not allow math.
Sisco stores the exponents separately and based on the exponent determines if it can do prediction to reconstruct the mantissa. Hence, the only information available is the exponent.
Definition at line 216 of file BitFloat.h.
Referenced by casacore::sisco::AveragePredict(), casacore::sisco::LinearPredict(), casacore::sisco::Predict(), casacore::sisco::Predict(), casacore::sisco::Predict(), casacore::sisco::Predict(), and casacore::sisco::QuadraticPredict().
|
inlineconstexpr |
Definition at line 75 of file BitFloat.h.
References exponent_.
Referenced by Match, and casacore::sisco::Predict().
|
inlinestaticconstexpr |
Constructs a BitFloat from a 'packed mantissa' (see PackMantissa()) and the exponent.
In Sisco, these are stored separately, and this method is used to reconstruct the BitFloat.
Definition at line 115 of file BitFloat.h.
References BitFloat(), and UnpackMantissa().
|
inlinestaticconstexpr |
Determine what kind of float the specified value is: normal, nan, inf, etc.
exponent is 128
Definition at line 271 of file BitFloat.h.
References casacore::Infinity, casacore::NaN, casacore::NegativeInfinity, casacore::NegativeZero, casacore::NewDelAllocator< T >::value, casacore::Normal, casacore::SignallingNaN, casacore::Subnormal, and casacore::Zero.
|
inlineconstexpr |
Definition at line 73 of file BitFloat.h.
References mantissa_.
Referenced by Match, and casacore::sisco::Predict().
|
inlineconstexpr |
Returns true if bit 32 is set.
Definition at line 267 of file BitFloat.h.
References mantissa_.
Referenced by PackMantissa().
|
inlineexplicitconstexpr |
Definition at line 205 of file BitFloat.h.
References ToFloat().
|
inlineconstexpr |
Multiplies the value by an integer factor.
The exponent is not changed, and overflow or special values aren't checked for.
Definition at line 160 of file BitFloat.h.
References BitFloat(), casacore::factor, and mantissa_.
Adds the value rhs to this.
this and rhs must be exponent-matched beforehand. The function doesn't check this, neither does it check for overflow or non-finite values.
Definition at line 129 of file BitFloat.h.
References BitFloat(), mantissa_, and sign_.
Like operator+=, but subtracts rhs.
Definition at line 144 of file BitFloat.h.
References BitFloat(), mantissa_, and sign_.
|
inlineconstexpr |
Divides the value by an integer factor.
The exponent is not changed, and overflow or special values aren't checked for.
Definition at line 169 of file BitFloat.h.
References BitFloat(), casacore::factor, and mantissa_.
|
inlineconstexpr |
Combines the sign and the mantissa into one uint32_t.
The mantissa of a single-precision floating point values is only 23 bits, and so this would fit easily. However, operations performed on the BitFloat like addition and multiplication may enlarge the mantissa, causing an 'overflow'.
This method checks if mantissa's 32th bit is set (which would conflict with the bit used for the sign), and throws an exception in that case. This is not a full check for the occurence of overflow, as the mantissa might have overflown twice, and thus further overflow checking is necessary when doing operations.
Definition at line 89 of file BitFloat.h.
References exponent_, mantissa_, MantissaOverflow(), sign_, and ToFloat().
|
inlineconstexpr |
Definition at line 122 of file BitFloat.h.
References sign_.
Referenced by Match, and casacore::sisco::Predict().
|
inlineconstexpr |
Compose this value back into a single-precision floating point value.
Since this class allows storing unnormalized floating point values, this function normalizes the value if necessary. If the original value represented a special value (like NaN or inf), the value returned will be the bit-wise equal special value.
Exponent values of +128 and -127 have special meaning. 128 will be wrapped.
The double cast of the exponent is necessary to prevent sign extension.
Definition at line 181 of file BitFloat.h.
References exponent_, mantissa_, and sign_.
Referenced by operator float(), and PackMantissa().
|
inlinestaticconstexpr |
Given a result from PackMantissa(), this function reversed the packing.
Definition at line 103 of file BitFloat.h.
References casacore::sign().
Referenced by FromCompressed().
Shifts the input value such that its exponent matches the specified exponent.
This should be used before operations like addition and multiplication. Note that this may lead to loss of precision if the value_exponent is larger than the input.
In case the shift would lead to overflow, no value is returned. In case the shifting results in the value becoming zero, a BitFloat representing zero is returned, but with the 'unnormalized' exponent still set to the requested exponent, meaning that it can be used in operations.
Definition at line 245 of file BitFloat.h.
References BitFloat(), Exponent(), Mantissa(), and Sign().
Negation; flips the sign of the value.
Definition at line 225 of file BitFloat.h.
References BitFloat(), exponent_, mantissa_, and sign_.
Definition at line 229 of file BitFloat.h.
References BitFloat(), exponent_, mantissa_, and sign_.
|
private |
Definition at line 297 of file BitFloat.h.
Referenced by AllowsMath(), BitFloat(), BitFloat(), BitFloat(), Exponent(), operator-, operator==, PackMantissa(), and ToFloat().
|
private |
Definition at line 296 of file BitFloat.h.
Referenced by BitFloat(), BitFloat(), BitFloat(), Mantissa(), MantissaOverflow(), operator*=(), operator+=(), operator-, operator-=(), operator/=(), operator==, PackMantissa(), and ToFloat().
|
private |
Definition at line 298 of file BitFloat.h.
Referenced by BitFloat(), BitFloat(), BitFloat(), operator+=(), operator-, operator-=(), operator==, PackMantissa(), Sign(), and ToFloat().