ATLAS Offline Software
|
Pack/unpack floating-point data from/to a given number of bits. More...
#include <FloatPacker.h>
Public Types | |
typedef uint32_t | Packdest |
Type into which we pack. More... | |
Public Member Functions | |
FloatPacker (int nbits, int nmantissa, double scale=1, bool is_signed=true, bool round=false) | |
Constructor. More... | |
Packdest | pack (double src, std::string *err=nullptr) const |
Pack a value. More... | |
double | unpack (Packdest val, std::string *err=nullptr) const |
Unpack the value VAL . More... | |
Private Attributes | |
int | m_nmantissa |
Number of bits in the mantissa + sign bit. More... | |
double | m_scale |
Scale factor for stored numbers. More... | |
double | m_invscale |
Inverse of scale. More... | |
bool | m_is_signed |
Should we use a sign bit? More... | |
bool | m_round |
Should we round instead of truncating? More... | |
int | m_npack |
Number of bits in mantissa (exclusive of any sign bit). More... | |
Packdest | m_npack_ones |
Mask with that many low bits set. More... | |
Packdest | m_signmask |
Mask containing the sign bit (or 0 if there's no sign bit). More... | |
int | m_nexp |
Number of exponent bits. More... | |
Packdest | m_nexp_ones |
Mask with that many low bits set. More... | |
int | m_min_exp |
Minimum exponent value. More... | |
int | m_max_exp |
Maximum exponent value. More... | |
Pack/unpack floating-point data from/to a given number of bits.
The format is specified by the following parameters.
nbits - The total number of bits in the representation. scale - Scale factor to apply before storing. nmantissa - The number of bits to use for the mantissa and sign bit. is_signed - Flag to tell if we should use a sign bit. round - Flag to tell if we should round or truncate.
From these we derive:
npack = nmantissa, if is_signed is false. = nmantissa-1 if is_signed is true. nexp = nbits - nmantissa
The format consists of, in order from high bits to low bits:
The number is stored in normalized form, with an exponent bias of 2^(nexp-1). But if the (biased) exponent is zero, then the mantissa is stored in denormalized form. If nexp==0, this gives a fixed-point representation in the range [0,1). 0 is represented by all bits 0; if we have a sign bit, we can also represent -0 by all bits 0 except for the sign bit.
Definition at line 57 of file FloatPacker.h.
typedef uint32_t CxxUtils::FloatPacker::Packdest |
Type into which we pack.
Definition at line 61 of file FloatPacker.h.
CxxUtils::FloatPacker::FloatPacker | ( | int | nbits, |
int | nmantissa, | ||
double | scale = 1 , |
||
bool | is_signed = true , |
||
bool | round = false |
||
) |
Constructor.
nbits | The number of bits in the packed representation. |
nmantissa | The number of bits to use for the mantissa and sign bit. |
scale | Divide the input number by this before packing. |
is_signed | If true, then one mantissa bit is used for a sign. |
round | If true, numbers will be rounded. Otherwise, they will be truncated. |
Definition at line 213 of file FloatPacker.cxx.
FloatPacker::Packdest CxxUtils::FloatPacker::pack | ( | double | src, |
std::string * | err = nullptr |
||
) | const |
Pack a value.
src | Value to pack. |
err | If non-null, then this string will be set to a description of any error that occurs. |
For now, we convert floats to doubles before packing.
Definition at line 271 of file FloatPacker.cxx.
double CxxUtils::FloatPacker::unpack | ( | Packdest | val, |
std::string * | err = nullptr |
||
) | const |
Unpack the value VAL
.
val | The packed data. It should start with the low bit, and any extraneous bits should have been masked off. |
err | If non-null, then this string will be set to a description of any error that occurs. |
Definition at line 405 of file FloatPacker.cxx.
|
private |
Inverse of scale.
Definition at line 111 of file FloatPacker.h.
|
private |
Should we use a sign bit?
Definition at line 114 of file FloatPacker.h.
|
private |
Maximum exponent value.
Definition at line 138 of file FloatPacker.h.
|
private |
Minimum exponent value.
Definition at line 135 of file FloatPacker.h.
|
private |
Number of exponent bits.
Definition at line 129 of file FloatPacker.h.
|
private |
Mask with that many low bits set.
Definition at line 132 of file FloatPacker.h.
|
private |
Number of bits in the mantissa + sign bit.
Definition at line 105 of file FloatPacker.h.
|
private |
Number of bits in mantissa (exclusive of any sign bit).
Definition at line 120 of file FloatPacker.h.
|
private |
Mask with that many low bits set.
Definition at line 123 of file FloatPacker.h.
|
private |
Should we round instead of truncating?
Definition at line 117 of file FloatPacker.h.
|
private |
Scale factor for stored numbers.
Definition at line 108 of file FloatPacker.h.
|
private |
Mask containing the sign bit (or 0 if there's no sign bit).
Definition at line 126 of file FloatPacker.h.