fix_base.cpp
Go to the documentation of this file.
#include <itpp/fixed/fix_base.h>
#include <itpp/base/itassert.h>
#include <iostream>
namespace itpp
{
// Definition and initialization of static data member
output_mode Fix_Base::outputmode = OUTPUT_FIX_SHIFT;
void Fix_Base::set_output_mode(std::string o)
{
if (o == "OUTPUT_FIX")
outputmode = OUTPUT_FIX;
else if (o == "OUTPUT_FIX_SHIFT")
outputmode = OUTPUT_FIX_SHIFT;
else if (o == "OUTPUT_FLOAT")
outputmode = OUTPUT_FLOAT;
else if (o == "OUTPUT_FLOAT_SHIFT")
outputmode = OUTPUT_FLOAT_SHIFT;
else
it_error("Fix_Base::set_output_mode: Illegal output mode!");
}
void Fix_Base::print() const
{
std::cout << "shift = " << shift << std::endl
<< "wordlen = " << wordlen << std::endl
<< "int(emode) = " << int(emode) << std::endl
<< "int(omode) = " << int(omode) << std::endl
<< "int(qmode) = " << int(qmode) << std::endl
<< "stat_ptr = " << stat_ptr << std::endl
<< "min = " << min << std::endl
<< "max = " << max << std::endl
<< "n_unused_bits = " << n_unused_bits << std::endl;
}
void Fix_Base::init()
{
switch (emode) {
case TC:
it_assert_debug(wordlen >= 1 && wordlen <= 64, "Fix_Base::calc_apply_o_modes: Illegal word length!");
max = fixrep(UINT64_POW2[wordlen - 1] - 1);
min = -max - 1;
break;
case US:
it_assert_debug(wordlen >= 0 && wordlen <= 63, "Fix_Base::calc_apply_o_modes: Illegal word length!");
min = 0;
max = fixrep(UINT64_POW2[wordlen] - 1);
break;
default:
it_error("Fix_Base::init: Illegal sign encoding mode!");
break;
}
n_unused_bits = MAX_WORDLEN - wordlen;
}
fixrep Fix_Base::apply_o_mode(fixrep x) const
{
fixrep ret = x;
bool overflow = false;
if (ret < min) {
overflow = true;
switch (omode) {
case WRAP:
ret = fixrep((fixrep(ret) << n_unused_bits) >> n_unused_bits);
break;
case SAT:
ret = min;
break;
default:
it_error("Fix_Base::apply_o_mode: Illegal overflow mode!");
break;
}
}
else if (ret > max) {
overflow = true;
switch (omode) {
case WRAP:
ret = fixrep((fixrep(ret) << n_unused_bits) >> n_unused_bits);
break;
case SAT:
ret = max;
break;
default:
it_error("Fix_Base::apply_o_mode: Illegal overflow mode!");
break;
}
}
if (stat_ptr != 0)
stat_ptr->sample(double(ret), overflow);
return ret;
}
fixrep Fix_Base::scale_and_apply_modes(double x, q_mode q) const
{
it_assert_debug(shift >= -64 && shift <= 63, "Fix_Base::scale_and_apply_modes: Illegal shift!");
fixrep ret = 0;
double scaled_value = x * DOUBLE_POW2[shift + 64];
switch (q) {
case RND:
ret = apply_o_mode(fixrep(std::floor(scaled_value + 0.5)));
break;
case RND_ZERO:
if (x < 0)
ret = apply_o_mode(fixrep(std::floor(scaled_value + 0.5)));
else
ret = apply_o_mode(fixrep(-std::floor(-scaled_value + 0.5)));
break;
case RND_MIN_INF:
ret = apply_o_mode(fixrep(-std::floor(-scaled_value + 0.5)));
break;
case RND_INF:
if (x < 0)
ret = apply_o_mode(fixrep(scaled_value - 0.5));
else
ret = apply_o_mode(fixrep(scaled_value + 0.5));
break;
case RND_CONV:
if (scaled_value == std::floor(scaled_value) + 0.5)
ret = apply_o_mode((fixrep(round(scaled_value)) >> 1) << 1);
else
ret = apply_o_mode(fixrep(std::floor(scaled_value + 0.5)));
break;
case RND_CONV_ODD:
if (scaled_value == std::floor(scaled_value) + 0.5)
if (scaled_value < 0)
ret = apply_o_mode(((fixrep(std::ceil(scaled_value)) >> 1) << 1) - 1);
else
ret = apply_o_mode(((fixrep(std::floor(scaled_value)) >> 1) << 1) + 1);
else
ret = apply_o_mode(fixrep(std::floor(scaled_value + 0.5)));
break;
case TRN:
ret = apply_o_mode(fixrep(std::floor(scaled_value)));
break;
case TRN_ZERO:
ret = apply_o_mode(fixrep(scaled_value));
break;
default:
it_error("Fix_Base::scale_and_apply_modes: Illegal quantization mode!");
break;
}
return ret;
}
fixrep Fix_Base::rshift_and_apply_q_mode(fixrep x, int n, q_mode q) const
{
it_assert_debug(n >= 0, "Fix_Base::rshift_and_apply_q_mode: n cannot be negative!");
fixrep ret = 0;
if (n == 0) {
ret = x;
}
else {
switch (q) {
case RND:
// Add the most significant deleted bit to the remaining bits
ret = ((x >> (n - 1)) + 1) >> 1;
break;
case RND_ZERO:
// If the most significant deleted bit is 1,
// and either the sign bit or at least one other deleted bit is 1,
// add 1 to the remaining bits
if ((x & (fixrep(1) << (n - 1))) && ((x < 0) || (x & ((fixrep(1) << (n - 1)) - 1))))
ret = (x >> n) + 1;
else
ret = x >> n;
break;
case RND_MIN_INF:
// If the most significant deleted bit is 1,
// and at least one other deleted bit is 1,
// add 1 to the remaining bits
if ((x & (fixrep(1) << (n - 1))) && (x & ((fixrep(1) << (n - 1)) - 1)))
ret = (x >> n) + 1;
else
ret = x >> n;
break;
case RND_INF:
// If the most significant deleted bit is 1,
// and either the inverted value of the sign bit or at least one other deleted bit is 1,
// add 1 to the remaining bits
if ((x & (fixrep(1) << (n - 1))) && ((x >= 0) || (x & ((fixrep(1) << (n - 1)) - 1))))
ret = (x >> n) + 1;
else
ret = x >> n;
break;
case RND_CONV:
// If the most significant deleted bit is 1,
// and either the least significant of the remaining bits or at least one other deleted bit is 1,
// add 1 to the remaining bits
if ((x & (fixrep(1) << (n - 1))) && ((x & (fixrep(1) << n)) || (x & ((fixrep(1) << (n - 1)) - 1))))
ret = (x >> n) + 1;
else
ret = x >> n;
break;
case RND_CONV_ODD:
// If the most significant deleted bit is 1,
// and either the least significant of the remaining bits is 0 or at least one other deleted bit is 1,
// add 1 to the remaining bits
if ((x & (fixrep(1) << (n - 1))) && (!(x & (fixrep(1) << n)) || (x & ((fixrep(1) << (n - 1)) - 1))))
ret = (x >> n) + 1;
else
ret = x >> n;
break;
case TRN:
// Just copy the remaining bits
ret = x >> n;
break;
case TRN_ZERO:
// If the sign bit is 1,
// and either the most significant deleted bit or at least one other deleted bit is 1,
// add 1 to the remaining bits
if ((x < 0) && (x & ((fixrep(1) << n) - 1)))
ret = (x >> n) + 1;
else
ret = x >> n;
break;
default:
it_error("Fix_Base::rshift_and_apply_q_mode: Illegal quantization mode!");
break;
}
}
if (stat_ptr != 0)
stat_ptr->sample(double(ret), false);
return ret;
}
} // namespace itpp
itpp::Array
General array class.
Definition array.h:105
itpp::Fix_Base::print
virtual void print() const
Print restrictions.
Definition fix_base.cpp:54
itpp::Fix_Base::set_output_mode
static void set_output_mode(output_mode o)
Set output mode to OUTPUT_FIX, OUTPUT_FIX_SHIFT, OUTPUT_FLOAT or OUTPUT_FLOAT_SHIFT....
Definition fix_base.h:996
itpp::Fix_Base::apply_o_mode
fixrep apply_o_mode(fixrep x) const
Handle overflows using overflow mode omode and make call to statistics object (if any)
Definition fix_base.cpp:88
itpp::Fix_Base::scale_and_apply_modes
fixrep scale_and_apply_modes(double x) const
Convert from double to fixrep using shift and quantization mode qmode, then call limit()
Definition fix_base.h:1044
itpp::Fix_Base::n_unused_bits
int n_unused_bits
Number of unused (MSB) bits (help variable to speed up calculations)
Definition fix_base.h:1037
itpp::Fix_Base::rshift_and_apply_q_mode
fixrep rshift_and_apply_q_mode(fixrep x, int n) const
Right shift n bits using quantization mode qmode and make call to statistics object (if any)
Definition fix_base.h:1048
itpp::Fix_Base::wordlen
int wordlen
Word length.
Definition fix_base.h:1023
itpp::Fix_Base::min
fixrep min
Minimum allowed value (help variable to speed up calculations)
Definition fix_base.h:1033
itpp::Fix_Base::init
void init()
Calculate help variables min, max and n_unused_bits.
Definition fix_base.cpp:67
itpp::Fix_Base::emode
e_mode emode
Sign encoding mode.
Definition fix_base.h:1025
itpp::Fix_Base::stat_ptr
Stat * stat_ptr
Pointer to statistics object.
Definition fix_base.h:1031
itpp::Fix_Base::max
fixrep max
Maximum allowed value (help variable to speed up calculations)
Definition fix_base.h:1035
itpp::Fix_Base::shift
int shift
Accumulated bitshift (positive means left-shifted, negative means right-shifted)
Definition fix_base.h:1021
itpp::Fix_Base::qmode
q_mode qmode
Quantization mode.
Definition fix_base.h:1029
itpp::Fix_Base::omode
o_mode omode
Overflow mode.
Definition fix_base.h:1027
itpp::Stat::sample
virtual void sample(const double s, const bool overflow=false)
Register a sample and flag for overflow.
Definition misc_stat.h:68
fix_base.h
Definitions of a base class for fixed-point data types.
it_error
#define it_error(s)
Abort unconditionally.
Definition itassert.h:126
it_assert_debug
#define it_assert_debug(t, s)
Abort if t is not true and NDEBUG is not defined.
Definition itassert.h:107
itpp::MAX_WORDLEN
const int MAX_WORDLEN
Max word length.
Definition fix_base.h:886
itpp::DOUBLE_POW2
const double DOUBLE_POW2[128]
Table for fast multiplication by 2^(n-64)
Definition fix_base.h:906
itpp::UINT64_POW2
const uint64_t UINT64_POW2[64]
Table for fast multiplication or division by 2^n.
Definition fix_base.h:889
itpp::q_mode
q_mode
Quantization modes (aligned with SystemC)
Definition fix_base.h:957
itpp::output_mode
output_mode
Output modes.
Definition fix_base.h:969
itpp::fixrep
int64_t fixrep
Representation for fixed-point data types.
Definition fix_base.h:884
itpp::SAT
@ SAT
Saturation.
Definition fix_base.h:949
itpp::WRAP
@ WRAP
Wrap-around.
Definition fix_base.h:952
itpp::US
@ US
Unsigned.
Definition fix_base.h:944
itpp::TC
@ TC
Two's complement.
Definition fix_base.h:943
itpp::TRN
@ TRN
Truncation.
Definition fix_base.h:964
itpp::TRN_ZERO
@ TRN_ZERO
Truncation to zero.
Definition fix_base.h:965
itpp::RND
@ RND
Rounding to plus infinity.
Definition fix_base.h:958
itpp::RND_INF
@ RND_INF
Rounding to infinity.
Definition fix_base.h:961
itpp::RND_CONV
@ RND_CONV
Convergent rounding with half-way value rounded to even value.
Definition fix_base.h:962
itpp::RND_MIN_INF
@ RND_MIN_INF
Rounding to minus infinity.
Definition fix_base.h:960
itpp::RND_ZERO
@ RND_ZERO
Rounding to zero.
Definition fix_base.h:959
itpp::RND_CONV_ODD
@ RND_CONV_ODD
Convergent rounding with half-way value rounded to odd value (not defined in SystemC)
Definition fix_base.h:963
itpp::OUTPUT_FIX
@ OUTPUT_FIX
Output fixed-point representation only.
Definition fix_base.h:970
itpp::OUTPUT_FLOAT
@ OUTPUT_FLOAT
Output floating-point value.
Definition fix_base.h:972
itpp::OUTPUT_FIX_SHIFT
@ OUTPUT_FIX_SHIFT
Output fixed-point representation followed by <shift> (default)
Definition fix_base.h:971
itpp::OUTPUT_FLOAT_SHIFT
@ OUTPUT_FLOAT_SHIFT
Output floating-point value followed by <<shift.
Definition fix_base.h:973
itassert.h
Error handling functions - header file.
itpp
itpp namespace
Definition itmex.h:37
itpp::round
ITPP_EXPORT double round(double x)
Round to nearest integer, return result in double.

Generated on Mon Jun 10 2024 11:49:23 for IT++ by Doxygen 1.9.8