cfix.cpp
Go to the documentation of this file.
#include <itpp/fixed/cfix.h>
#include <itpp/base/itassert.h>
#include <cstdio>
#include <iostream>
namespace itpp
{
CFix& CFix::operator=(const CFix &x)
{
shift = x.shift;
re = apply_o_mode(x.re);
im = apply_o_mode(x.im);
return *this;
}
CFix& CFix::operator=(const Fix &x)
{
shift = x.shift;
re = apply_o_mode(x.re);
im = 0;
return *this;
}
CFix& CFix::operator=(const std::complex<double> &x)
{
shift = 0;
re = apply_o_mode(fixrep(std::real(x)));
im = apply_o_mode(fixrep(std::imag(x)));
return *this;
}
CFix& CFix::operator=(const int x)
{
shift = 0;
re = apply_o_mode(x);
im = 0;
return *this;
}
CFix& CFix::operator+=(const CFix &x)
{
shift = assert_shifts(*this, x);
re = apply_o_mode(re + x.re);
im = apply_o_mode(im + x.im);
return *this;
}
CFix& CFix::operator+=(const Fix &x)
{
shift = assert_shifts(*this, x);
re = apply_o_mode(re + x.re);
return *this;
}
CFix& CFix::operator+=(const int x)
{
assert_shifts(*this, x);
re = apply_o_mode(re + x);
return *this;
}
CFix& CFix::operator-=(const CFix &x)
{
shift = assert_shifts(*this, x);
re = apply_o_mode(re - x.re);
im = apply_o_mode(im - x.im);
return *this;
}
CFix& CFix::operator-=(const Fix &x)
{
shift = assert_shifts(*this, x);
re = apply_o_mode(re - x.re);
return *this;
}
CFix& CFix::operator-=(const int x)
{
assert_shifts(*this, x);
re = apply_o_mode(re - x);
return *this;
}
CFix& CFix::operator*=(const CFix &x)
{
shift += x.shift;
fixrep tmp_re = apply_o_mode(re * x.re - im * x.im);
im = apply_o_mode(re * x.im + im * x.re);
re = tmp_re;
return *this;
}
CFix& CFix::operator*=(const Fix &x)
{
shift += x.shift;
re = apply_o_mode(re * x.re);
im = apply_o_mode(im * x.re);
return *this;
}
CFix& CFix::operator*=(const int x)
{
re = apply_o_mode(re * x);
im = apply_o_mode(im * x);
return *this;
}
CFix& CFix::operator/=(const CFix &x)
{
shift -= x.shift;
fixrep denominator = x.re * x.re + x.im * x.im;
fixrep tmp_re = apply_o_mode((re * x.re + im * x.im) / denominator);
im = apply_o_mode((im * x.re - re * x.im) / denominator);
re = tmp_re;
return *this;
}
CFix& CFix::operator/=(const Fix &x)
{
shift -= x.shift;
re = apply_o_mode(re / x.re);
im = apply_o_mode(im / x.re);
return *this;
}
CFix& CFix::operator/=(const int x)
{
re = apply_o_mode(re / x);
im = apply_o_mode(im / x);
return *this;
}
CFix CFix::operator-() const
{
return CFix(-re, -im, shift, 0, 0);
}
CFix& CFix::operator<<=(const int n)
{
it_assert_debug(n >= 0, "CFix::operator<<=: n cannot be negative!");
shift += n;
re = apply_o_mode(re << n);
im = apply_o_mode(im << n);
return *this;
}
CFix& CFix::operator>>=(const int n)
{
shift -= n;
re = rshift_and_apply_q_mode(re, n);
im = rshift_and_apply_q_mode(im, n);
return *this;
}
void CFix::set(double real, double imag, int n)
{
shift = n;
re = scale_and_apply_modes(real);
im = scale_and_apply_modes(imag);
}
void CFix::set(double real, double imag, int n, q_mode q)
{
shift = n;
re = scale_and_apply_modes(real, q);
im = scale_and_apply_modes(imag, q);
}
void CFix::set(const std::complex<double> &x, int n)
{
shift = n;
re = scale_and_apply_modes(std::real(x));
im = scale_and_apply_modes(std::imag(x));
}
void CFix::set(const std::complex<double> &x, int n, q_mode q)
{
shift = n;
re = scale_and_apply_modes(std::real(x), q);
im = scale_and_apply_modes(std::imag(x), q);
}
void CFix::lshift(int n)
{
it_assert_debug(n >= 0, "CFix::lshift: n cannot be negative!");
shift += n;
re = apply_o_mode(re << n);
im = apply_o_mode(im << n);
}
void CFix::rshift(int n)
{
shift -= n;
re = rshift_and_apply_q_mode(re, n);
im = rshift_and_apply_q_mode(im, n);
}
void CFix::rshift(int n, q_mode q)
{
shift -= n;
re = rshift_and_apply_q_mode(re, n, q);
im = rshift_and_apply_q_mode(im, n, q);
}
std::complex<double> CFix::unfix() const
{
it_assert_debug(shift >= -63 && shift <= 64, "CFix::unfix: Illegal shift!");
return std::complex<double>(double(re)*DOUBLE_POW2[64 - shift],
double(im)*DOUBLE_POW2[64 - shift]);
}
void CFix::print() const
{
Fix_Base::print();
std::cout << "re = " << re << std::endl;
std::cout << "im = " << im << std::endl;
}
int assert_shifts(const CFix &x, const CFix &y)
{
int ret = 0;
if (x.shift == y.shift)
ret = x.shift;
else if (x.re == 0 && x.im == 0)
ret = y.shift;
else if (y.re == 0 && y.im == 0)
ret = x.shift;
else
it_error("assert_shifts: Different shifts not allowed!");
return ret;
}
int assert_shifts(const CFix &x, const Fix &y)
{
int ret = 0;
if (x.shift == y.shift)
ret = x.shift;
else if (x.re == 0 && x.im == 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 CFix &x, int y)
{
it_error_if((x.shift != 0) && !(x.re == 0 && x.im == 0)
&& (y != 0), "assert_shifts: Different shifts not allowed!");
return x.shift;
}
std::istream &operator>>(std::istream &is, CFix &x)
{
std::complex<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(std::real(value)));
x.set_im(fixrep(std::imag(value)));
x.set_shift(shift);
}
}
else {
// Change data representation but keep shift
x.set_re(fixrep(std::real(value)));
x.set_im(fixrep(std::imag(value)));
}
return is;
}
std::ostream &operator<<(std::ostream &os, const CFix &x)
{
switch (x.get_output_mode()) {
case OUTPUT_FIX:
if (x.get_im() < 0)
os << x.get_re() << x.get_im() << 'i';
else
os << x.get_re() << '+' << x.get_im() << 'i';
break;
case OUTPUT_FIX_SHIFT:
if (x.get_im() < 0)
os << x.get_re() << x.get_im() << 'i';
else
os << x.get_re() << '+' << x.get_im() << 'i';
os << '<' << x.get_shift() << '>';
break;
case OUTPUT_FLOAT:
os << std::complex<double>(x);
break;
case OUTPUT_FLOAT_SHIFT:
os << std::complex<double>(x) << "<<" << x.get_shift();
break;
default:
it_error("operator<<: Illegal output mode!");
}
return os;
}
// Specialization of template definition in vec.cpp
template<>
void cfixvec::set(const char *values)
{
std::istringstream buffer(values);
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 ':':
it_error("set: expressions with ':' are not valid for cfixvec");
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 cfixmat::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
it_error_if
#define it_error_if(t, s)
Abort if t is true.
Definition: itassert.h:117
itpp::Mat::alloc
void alloc(int rows, int cols)
Allocate memory for the matrix.
Definition: mat.h:548
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::cols
int cols() const
The number of columns.
Definition: mat.h:235
itpp::Mat::data
Num_T * data
Protected data pointer.
Definition: mat.h:457
itpp::Fix_Base::get_output_mode
output_mode get_output_mode() const
Get output mode.
Definition: fix_base.h:1011
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::CFix::operator*=
CFix & operator*=(const CFix &x)
Multiplication with CFix. Temporary variables use the maximum word length (64 bits) ...
Definition: cfix.cpp:114
itpp::Mat::datasize
int datasize
Definition: mat.h:454
itpp::CFix::rshift
void rshift(int n)
Right shift n bits using quantization mode qmode (constructor argument)
Definition: cfix.cpp:221
itpp::imag
vec imag(const cvec &data)
Imaginary part of complex values.
Definition: elem_math.cpp:180
itpp::CFix::operator-=
CFix & operator-=(const CFix &x)
Subtraction of CFix.
Definition: cfix.cpp:92
itpp::CFix::operator/=
CFix & operator/=(const CFix &x)
Division with CFix using quantization mode TRN. Temporary variables use the maximum word length (64 b...
Definition: cfix.cpp:138
itpp::Mat::rows
int rows() const
The number of rows.
Definition: mat.h:237
itpp::Vec::set
void set(const char *str)
Set the vector equal to the values in the str string.
Definition: vec.h:814
itpp::CFix::assert_shifts
friend ITPP_EXPORT 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::CFix::operator+=
CFix & operator+=(const CFix &x)
Addition of CFix.
Definition: cfix.cpp:70
itpp::OUTPUT_FLOAT
Output floating-point value.
Definition: fix_base.h:972
itpp::operator<<
std::ostream & operator<<(std::ostream &output, const bin &inbin)
Output stream of bin.
Definition: binary.cpp:36
itpp::Vec::data
Num_T * data
A pointer to the data area.
Definition: vec.h:505
itpp::CFix::operator>>=
CFix & operator>>=(const int n)
Right shift n bits using quantization mode qmode (constructor argument)
Definition: cfix.cpp:177
itpp::CFix::unfix
std::complex< double > unfix() const
Conversion to std::complex<double>
Definition: cfix.cpp:235
itpp::Fix_Base::set_shift
void set_shift(int s)
Set shift (without shifting)
Definition: fix_base.h:994
itpp::CFix::re
fixrep re
Real data part.
Definition: cfix.h:161
itpp::Mat::set
void set(const std::string &str)
Set matrix equal to values in the string str.
Definition: mat.h:762
itpp::Fix::re
fixrep re
Data representation.
Definition: fix.h:137
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::Fix_Base::print
virtual void print() const
Print restrictions.
Definition: fix_base.cpp:54
itpp::Vec::datasize
int datasize
The current number of elements in the vector.
Definition: vec.h:503
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::CFix::operator-
CFix operator-() const
Unary negative of CFix.
Definition: cfix.cpp:163
itpp::DOUBLE_POW2
const double DOUBLE_POW2[128]
Table for fast multiplication by 2^(n-64)
Definition: fix_base.h:906
itpp::CFix::operator<<=
CFix & operator<<=(const int n)
Left shift n bits.
Definition: cfix.cpp:168
itpp::CFix
Complex fixed-point data type.
Definition: cfix.h:51
itpp::CFix::print
virtual void print() const
Print restrictions.
Definition: cfix.cpp:242
itpp::OUTPUT_FIX
Output fixed-point representation only.
Definition: fix_base.h:970
itpp::CFix::get_re
fixrep get_re() const
Get data representation for real part (mainly for internal use since it reveals the representation ty...
Definition: cfix.h:138
itpp::Mat::operator()
const Num_T & operator()(int r, int c) const
Get element (r,c) from matrix.
Definition: mat.h:712
itpp::CFix::set_im
void set_im(fixrep x)
Set data representation for imaginary part (mainly for internal use since it reveals the representati...
Definition: cfix.h:126
itpp
itpp namespace
Definition: itmex.h:36
itpp::CFix::lshift
void lshift(int n)
Left shift n bits.
Definition: cfix.cpp:213
itpp::Fix_Base::shift
int shift
Accumulated bitshift (positive means left-shifted, negative means right-shifted)
Definition: fix_base.h:1021
itassert.h
Error handling functions - header file.
itpp::CFix::set
void set(double real, double imag, int n)
Set to (real + i*imag) * pow2(n) using quantization mode qmode (constructor argument) ...
Definition: cfix.cpp:185
itpp::CFix::operator=
CFix & operator=(const CFix &x)
Assignment from CFix.
Definition: cfix.cpp:38
itpp::CFix::set_re
void set_re(fixrep x)
Set data representation for real part (mainly for internal use since it reveals the representation ty...
Definition: cfix.h:124
itpp::CFix::im
fixrep im
Imaginary data part.
Definition: cfix.h:162
itpp::OUTPUT_FLOAT_SHIFT
Output floating-point value followed by <<shift.
Definition: fix_base.h:973
itpp::Fix
Fixed-point data type.
Definition: fix.h:51
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::CFix::get_im
fixrep get_im() const
Get data representation for imaginary part (mainly for internal use since it reveals the representati...
Definition: cfix.h:140
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
it_error
#define it_error(s)
Abort unconditionally.
Definition: itassert.h:126
itpp::Vec::alloc
void alloc(int size)
Allocate storage for a vector of length size.
Definition: vec.h:593
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::get_shift
int get_shift() const
Get shift.
Definition: fix_base.h:1001
itpp::CFix::CFix
CFix(double r=0.0, double i=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: cfix.h:56
itpp::fixrep
int64_t fixrep
Representation for fixed-point data types.
Definition: fix_base.h:884
itpp::OUTPUT_FIX_SHIFT
Output fixed-point representation followed by <shift> (default)
Definition: fix_base.h:971
itpp::operator>>
std::istream & operator>>(std::istream &input, bin &outbin)
Input stream of bin.
Definition: binary.cpp:42
itpp::real
vec real(const cvec &data)
Real part of complex values.
Definition: elem_math.cpp:157
itpp::q_mode
q_mode
Quantization modes (aligned with SystemC)
Definition: fix_base.h:957
cfix.h
Definitions of a complex fixed-point data type CFix.

Generated on Thu Aug 20 2015 21:02:51 for IT++ by Doxygen 1.8.9.1