fix.cpp
Go to the documentation of this file.
#include <itpp/fixed/fix.h>
#include <itpp/base/itassert.h>
#include <cstdio>
#include <iostream>
namespace itpp
{
Fix& Fix::operator=(const Fix &x)
{
shift = x.shift;
re = apply_o_mode(x.re);
return *this;
}
Fix& Fix::operator=(const int x)
{
shift = 0;
re = apply_o_mode(x);
return *this;
}
Fix& Fix::operator+=(const Fix &x)
{
shift = assert_shifts(*this, x);
re = apply_o_mode(re + x.re);
return *this;
}
Fix& Fix::operator+=(const int x)
{
assert_shifts(*this, x);
re = apply_o_mode(re + x);
return *this;
}
Fix& Fix::operator-=(const Fix &x)
{
shift = assert_shifts(*this, x);
re = apply_o_mode(re - x.re);
return *this;
}
Fix& Fix::operator-=(const int x)
{
assert_shifts(*this, x);
re = apply_o_mode(re - x);
return *this;
}
Fix& Fix::operator*=(const Fix &x)
{
shift += x.shift;
re = apply_o_mode(re * x.re);
return *this;
}
Fix& Fix::operator*=(const int x)
{
re = apply_o_mode(re * x);
return *this;
}
Fix& Fix::operator/=(const Fix &x)
{
shift -= x.shift;
re = apply_o_mode(re / x.re);
return *this;
}
Fix& Fix::operator/=(const int x)
{
re = apply_o_mode(re / x);
return *this;
}
Fix Fix::operator-() const
{
return Fix(-re, shift, 0, 0);
}
Fix& Fix::operator<<=(const int n)
{
it_assert_debug(n >= 0, "Fix::operator<<=: n cannot be negative!");
shift += n;
re = apply_o_mode(re << n);
return *this;
}
Fix& Fix::operator>>=(const int n)
{
shift -= n;
re = rshift_and_apply_q_mode(re, n);
return *this;
}
void Fix::set(double x, int n)
{
shift = n;
re = scale_and_apply_modes(x);
}
void Fix::set(double x, int n, q_mode q)
{
shift = n;
re = scale_and_apply_modes(x, q);
}
void Fix::lshift(int n)
{
it_assert_debug(n >= 0, "Fix::lshift: n cannot be negative!");
shift += n;
re = apply_o_mode(re << n);
}
void Fix::rshift(int n)
{
shift -= n;
re = rshift_and_apply_q_mode(re, n);
}
void Fix::rshift(int n, q_mode q)
{
shift -= n;
re = rshift_and_apply_q_mode(re, n, q);
}
double Fix::unfix() const
{
it_assert_debug(shift >= -63 && shift <= 64, "Fix::unfix: Illegal shift!");
return double(re)*DOUBLE_POW2[64 - shift];
}
void Fix::print() const
{
Fix_Base::print();
std::cout << "re = " << re << std::endl;
}
int assert_shifts(const Fix &x, const Fix &y)
{
int ret = 0;
if (x.shift == y.shift)
ret = x.shift;
else if (x.re == 0)
ret = y.shift;
else if (y.re == 0)
ret = x.shift;
else
it_error("assert_shifts: Different shifts not allowed!");
return ret;
}
int assert_shifts(const Fix &x, int y)
{
it_error_if((x.shift != 0) && (x.re != 0) && (y != 0),
"assert_shifts: Different shifts not allowed!");
return x.shift;
}
std::istream &operator>>(std::istream &is, Fix &x)
{
double value;
is >> value;
if (!is.eof() && (is.peek() == '<')) {
int shift;
is.get(); // Swallow '<' sign
if (is.peek() == '<') {
is.get(); // Swallow '<' sign
is >> shift;
x.set(value, shift);
}
else {
is >> shift;
is.get(); // Swallow '>' sign
x.set_re(fixrep(value));
x.set_shift(shift);
}
}
else {
// Change data representation but keep shift
x.set_re(fixrep(value));
}
return is;
}
std::ostream &operator<<(std::ostream &os, const Fix &x)
{
switch (x.get_output_mode()) {
case OUTPUT_FIX:
os << x.get_re();
break;
case OUTPUT_FIX_SHIFT:
os << x.get_re() << '<' << x.get_shift() << '>';
break;
case OUTPUT_FLOAT:
os << double(x);
break;
case OUTPUT_FLOAT_SHIFT:
os << double(x) << "<<" << x.get_shift();
break;
default:
it_error("operator<<: Illegal output mode!");
}
return os;
}
// Specialization of template definition in vec.cpp
template<>
void fixvec::set(const char *values)
{
std::istringstream buffer(values);
int b = 0, c = 0;
int default_shift = 0, pos = 0, maxpos = 10;
if (datasize > 0) {
// Assume that all elements have the same shift
default_shift = data[0].get_shift();
}
alloc(maxpos);
while (buffer.peek() != EOF) {
switch (buffer.peek()) {
case ':': // reads format a:b:c or a:b
buffer.get();
if (!buffer.eof()) {
buffer >> b;
}
if (!buffer.eof() && buffer.peek() == ':') {
buffer.get();
if (!buffer.eof()) {
buffer >> c;
while (int(double(data[pos-1])) + b - c <= 0) {
pos++;
if (pos > maxpos) {
maxpos = maxpos * 2;
set_size(maxpos, true);
}
data[pos-1] = data[pos-2];
data[pos-1] += b;
}
}
}
else {
while (int(double(data[pos-1])) < b) {
pos++;
if (pos > maxpos) {
maxpos = maxpos * 2;
set_size(maxpos, true);
}
data[pos-1] = data[pos-2];
data[pos-1] += 1;
}
}
break;
case ',':
buffer.get();
break;
default:
pos++;
if (pos > maxpos) {
maxpos *= 2;
set_size(maxpos, true);
}
data[pos-1].set_shift(default_shift);
buffer >> data[pos-1]; // May override default_shift
while (buffer.peek() == ' ') { buffer.get(); }
break;
}
}
set_size(pos, true);
}
// Specialization of template definition in mat.cpp
template<>
void fixmat::set(const char *values)
{
std::istringstream buffer(values);
int default_shift = 0, rows = 0, maxrows = 10, cols = 0, nocols = 0, maxcols = 10;
if (datasize > 0) {
// Assume that all elements have the same shift
default_shift = data[0].get_shift();
}
alloc(maxrows, maxcols);
while (buffer.peek() != EOF) {
rows++;
if (rows > maxrows) {
maxrows = maxrows * 2;
set_size(maxrows, maxcols, true);
}
cols = 0;
while ((buffer.peek() != ';') && (buffer.peek() != EOF)) {
if (buffer.peek() == ',') {
buffer.get();
}
else {
cols++;
if (cols > nocols) {
nocols = cols;
if (cols > maxcols) {
maxcols = maxcols * 2;
set_size(maxrows, maxcols, true);
}
}
this->operator()(rows-1, cols - 1).set_shift(default_shift);
buffer >> this->operator()(rows-1, cols - 1); // May override default_shift
while (buffer.peek() == ' ') { buffer.get(); }
}
}
if (!buffer.eof())
buffer.get();
}
set_size(rows, nocols, true);
}
} //namespace itpp
itpp::Fix_Base::print
virtual void print() const
Print restrictions.
Definition fix_base.cpp:54
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::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::get_output_mode
output_mode get_output_mode() const
Get output mode.
Definition fix_base.h:1011
itpp::Fix_Base::set_shift
void set_shift(int s)
Set shift (without shifting).
Definition fix_base.h:994
itpp::Fix_Base::get_shift
int get_shift() const
Get shift.
Definition fix_base.h:1001
itpp::Fix_Base::shift
int shift
Accumulated bitshift (positive means left-shifted, negative means right-shifted).
Definition fix_base.h:1021
itpp::Fix
Fixed-point data type.
Definition fix.h:52
itpp::Fix::Fix
Fix(double x=0.0, int s=0, int w=MAX_WORDLEN, e_mode e=TC, o_mode o=WRAP, q_mode q=TRN, Stat *ptr=0)
Default constructor.
Definition fix.h:58
itpp::Fix::lshift
void lshift(int n)
Left shift n bits.
Definition fix.cpp:138
itpp::Fix::operator/=
Fix & operator/=(const Fix &x)
Division with Fix using quantization mode TRN.
Definition fix.cpp:93
itpp::Fix::re
fixrep re
Data representation.
Definition fix.h:137
itpp::Fix::rshift
void rshift(int n)
Right shift n bits using quantization mode qmode (constructor argument).
Definition fix.cpp:145
itpp::Fix::set
void set(double x, int n)
Set to x * pow2(n) using quantization mode qmode (constructor argument).
Definition fix.cpp:126
itpp::Fix::operator=
Fix & operator=(const Fix &x)
Assignment from Fix.
Definition fix.cpp:38
itpp::Fix::operator+=
Fix & operator+=(const Fix &x)
Addition of Fix.
Definition fix.cpp:52
itpp::Fix::operator>>=
Fix & operator>>=(const int n)
Right shift n bits using quantization mode qmode (constructor argument).
Definition fix.cpp:119
itpp::Fix::get_re
fixrep get_re() const
Get data representation (mainly for internal use since it reveals the representation type).
Definition fix.h:116
itpp::Fix::operator-
Fix operator-() const
Unary negative of Fix.
Definition fix.cpp:106
itpp::Fix::unfix
double unfix() const
Conversion to double.
Definition fix.cpp:157
itpp::Fix::operator-=
Fix & operator-=(const Fix &x)
Subtraction of Fix.
Definition fix.cpp:66
itpp::Fix::operator<<=
Fix & operator<<=(const int n)
Left shift n bits.
Definition fix.cpp:111
itpp::Fix::operator*=
Fix & operator*=(const Fix &x)
Multiplication with Fix.
Definition fix.cpp:80
itpp::Fix::set_re
void set_re(fixrep x)
Set data representation (mainly for internal use since it reveals the representation type).
Definition fix.h:104
itpp::Fix::assert_shifts
friend ITPP_EXPORT int assert_shifts(const CFix &x, const Fix &y)
Check that x.shift==y.shift OR x==0 OR y==0 and return the shift (for the non-zero argument).
Definition cfix.cpp:265
itpp::Fix::print
virtual void print() const
Print restrictions.
Definition fix.cpp:163
itpp::Mat::set_size
void set_size(int rows, int cols, bool copy=false)
Set size of matrix. If copy = true then keep the data before resizing.
Definition mat.h:647
itpp::Mat< Fix >::set
void set(const std::string &str)
itpp::Mat< Fix >::rows
int rows() const
Definition mat.h:237
itpp::Mat< Fix >::data
Fix * data
Definition mat.h:457
itpp::Mat< Fix >::cols
int cols() const
Definition mat.h:235
itpp::Mat::operator()
const Num_T & operator()(int r, int c) const
Get element (r,c) from matrix.
Definition mat.h:712
itpp::Mat< Fix >::datasize
int datasize
Definition mat.h:454
itpp::Mat::alloc
void alloc(int rows, int cols)
Allocate memory for the matrix.
Definition mat.h:548
itpp::Vec< Fix >::datasize
int datasize
Definition vec.h:503
itpp::Vec< Fix >::set
void set(const char *str)
Definition fix.cpp:241
itpp::Vec< Fix >::data
Fix * data
Definition vec.h:505
itpp::Vec::set_size
void set_size(int size, bool copy=false)
Set length of vector. if copy = true then keeping the old values.
Definition vec.h:663
itpp::Vec::alloc
void alloc(int size)
Allocate storage for a vector of length size.
Definition vec.h:593
fix.h
Definitions of a fixed-point data type Fix.
it_error_if
#define it_error_if(t, s)
Abort if t is true.
Definition itassert.h:117
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::DOUBLE_POW2
const double DOUBLE_POW2[128]
Table for fast multiplication by 2^(n-64).
Definition fix_base.h:906
itpp::assert_shifts
int assert_shifts(const CFix &x, const CFix &y)
Check that x.shift==y.shift OR x==0 OR y==0 and return the shift (for the non-zero argument).
Definition cfix.cpp:249
itpp::q_mode
q_mode
Quantization modes (aligned with SystemC).
Definition fix_base.h:957
itpp::fixrep
int64_t fixrep
Representation for fixed-point data types.
Definition fix_base.h:884
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::operator<<
std::ostream & operator<<(std::ostream &output, const bin &inbin)
Output stream of bin.
Definition binary.cpp:36
itpp::operator>>
std::istream & operator>>(std::istream &input, bin &outbin)
Input stream of bin.
Definition binary.cpp:42

Generated on for IT++ by Doxygen 1.15.0