casacore
Loading...
Searching...
No Matches
casacore::BitFloat Class Reference

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 BitFloatoperator+= (const BitFloat &rhs)
 Adds the value rhs to this.
constexpr BitFloatoperator-= (const BitFloat &rhs)
 Like operator+=, but subtracts rhs.
constexpr BitFloatoperator*= (unsigned factor)
 Multiplies the value by an integer factor.
constexpr BitFloatoperator/= (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< BitFloatMatch (const BitFloat &input, int8_t value_exponent)
 Shifts the input value such that its exponent matches the specified exponent.

Detailed Description

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.

Constructor & Destructor Documentation

◆ BitFloat() [1/3]

casacore::BitFloat::BitFloat ( )
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==.

◆ BitFloat() [2/3]

casacore::BitFloat::BitFloat ( float f)
inlineexplicitconstexpr

Constructs a BitFloat by decomposing the specified floating point value.

Definition at line 61 of file BitFloat.h.

References exponent_, mantissa_, and sign_.

◆ BitFloat() [3/3]

casacore::BitFloat::BitFloat ( uint32_t mantissa,
int8_t exponent,
bool sign )
inlineconstexpr

Definition at line 70 of file BitFloat.h.

References exponent_, mantissa_, casacore::sign(), and sign_.

Member Function Documentation

◆ AllowsMath() [1/2]

bool casacore::BitFloat::AllowsMath ( ) const
inlineconstexpr

Definition at line 220 of file BitFloat.h.

References AllowsMath(), and exponent_.

Referenced by AllowsMath().

◆ AllowsMath() [2/2]

constexpr bool casacore::BitFloat::AllowsMath ( int8_t exponent)
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().

◆ Exponent()

int8_t casacore::BitFloat::Exponent ( ) const
inlineconstexpr

Definition at line 75 of file BitFloat.h.

References exponent_.

Referenced by Match, and casacore::sisco::Predict().

◆ FromCompressed()

constexpr BitFloat casacore::BitFloat::FromCompressed ( uint32_t mantissa_with_sign,
int8_t exponent )
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().

◆ GetKind()

constexpr BitFloatKind casacore::BitFloat::GetKind ( float f)
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.

◆ Mantissa()

uint32_t casacore::BitFloat::Mantissa ( ) const
inlineconstexpr

Definition at line 73 of file BitFloat.h.

References mantissa_.

Referenced by Match, and casacore::sisco::Predict().

◆ MantissaOverflow()

bool casacore::BitFloat::MantissaOverflow ( ) const
inlineconstexpr

Returns true if bit 32 is set.

Definition at line 267 of file BitFloat.h.

References mantissa_.

Referenced by PackMantissa().

◆ operator float()

casacore::BitFloat::operator float ( ) const
inlineexplicitconstexpr

Definition at line 205 of file BitFloat.h.

References ToFloat().

◆ operator*=()

BitFloat & casacore::BitFloat::operator*= ( unsigned factor)
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_.

◆ operator+=()

BitFloat & casacore::BitFloat::operator+= ( const BitFloat & rhs)
inlineconstexpr

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_.

◆ operator-=()

BitFloat & casacore::BitFloat::operator-= ( const BitFloat & rhs)
inlineconstexpr

Like operator+=, but subtracts rhs.

Definition at line 144 of file BitFloat.h.

References BitFloat(), mantissa_, and sign_.

◆ operator/=()

BitFloat & casacore::BitFloat::operator/= ( unsigned factor)
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_.

◆ PackMantissa()

uint32_t casacore::BitFloat::PackMantissa ( ) const
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().

◆ Sign()

bool casacore::BitFloat::Sign ( ) const
inlineconstexpr

Definition at line 122 of file BitFloat.h.

References sign_.

Referenced by Match, and casacore::sisco::Predict().

◆ ToFloat()

float casacore::BitFloat::ToFloat ( ) const
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().

◆ UnpackMantissa()

constexpr std::pair< uint32_t, bool > casacore::BitFloat::UnpackMantissa ( uint32_t mantissa_with_sign)
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().

◆ Match

std::optional< BitFloat > Match ( const BitFloat & input,
int8_t value_exponent )
friend

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().

◆ operator-

BitFloat operator- ( const BitFloat & input)
friend

Negation; flips the sign of the value.

Definition at line 225 of file BitFloat.h.

References BitFloat(), exponent_, mantissa_, and sign_.

◆ operator==

bool operator== ( const BitFloat & lhs,
const BitFloat & rhs )
friend

Definition at line 229 of file BitFloat.h.

References BitFloat(), exponent_, mantissa_, and sign_.

Member Data Documentation

◆ exponent_

int8_t casacore::BitFloat::exponent_
private

◆ mantissa_

uint32_t casacore::BitFloat::mantissa_
private

◆ sign_

bool casacore::BitFloat::sign_
private

The documentation for this class was generated from the following file: