12 const int FixedPointConverter::zero = 0;
15 std::string variableName,
unsigned int nBits,
int fractionalBits,
bool isSignedFlag)
16 : _variableName(std::move(variableName)), _nBits(nBits), _fractionalBits(fractionalBits), _isSigned(isSignedFlag),
17 _fractionalBitsCoefficient(pow(2., -fractionalBits)), _inverseFractionalBitsCoefficient(pow(2., fractionalBits)) {
25 _fractionalBits = fractionalBits;
26 _isSigned = isSignedFlag;
27 _fractionalBitsCoefficient = pow(2., -fractionalBits);
28 _inverseFractionalBitsCoefficient = pow(2., fractionalBits);
30 _bitShiftMaskSigned = 0;
33 std::stringstream errorMessage;
34 errorMessage <<
"The number of bits must be <= 32, but is " << nBits;
41 if((fractionalBits > -std::numeric_limits<double>::min_exponent -
static_cast<int>(nBits)) ||
42 (fractionalBits < -std::numeric_limits<double>::max_exponent +
static_cast<int>(nBits))) {
43 std::stringstream errorMessage;
44 errorMessage <<
"The number of fractional bits exceeds the dynamic"
45 <<
" range of a double.";
53 _signBitMask = (_isSigned ? 1 << (nBits - 1) : 0x0);
62 _usedBitsMask =
static_cast<int32_t
>((1L << nBits) - 1L);
64 _unusedBitsMask = ~_usedBitsMask;
69 _bitShiftMask =
static_cast<int32_t
>(~(0xFFFFFFFF >> abs(_fractionalBits)));
73 _maxRawValue = _usedBitsMask ^ _signBitMask;
75 _minRawValue = _signBitMask;
90 uint32_t FixedPointConverter::toRaw<std::string>(std::string cookedValue)
const {
91 if(_fractionalBits == 0) {
93 return toRaw(std::stoi(cookedValue));
95 return toRaw(
static_cast<uint32_t
>(std::stoul(cookedValue)));
98 return toRaw(std::stod(cookedValue));
102 uint32_t FixedPointConverter::toRaw<Boolean>(
Boolean cookedValue)
const {
103 if((
bool)cookedValue) {
110 uint32_t FixedPointConverter::toRaw<Void>(__attribute__((unused))
Void cookedValue)
const {