顯示具有 SystemC 標籤的文章。 顯示所有文章
顯示具有 SystemC 標籤的文章。 顯示所有文章

2009年3月28日 星期六

Complex Fixed-points in ANSI C

As I mentioned before, SystemC provides a series well-define classes for fixed-point. For more details I will summarize them and make a short intro in these days. Here, I'll show you another way, in ANSI C, to write for my own fixed-point simulation. And it's fully compatiable with gcc.
Here is my cmplx_fix.h (header file for complex fixed-point)



//+FHDR------------------------------------------------
// (C) Copyright Company NTHU.EE VLSI Comm Lab.2009
// All Right Reserved
//-----------------------------------------------------
// FILE NAME: cmplx_fix.h
// AUTHOR: stanlly9
// CONTACT INFORMATION: stanlly9.blogspot.com
//-----------------------------------------------------
// RELEASE VERSION: V 1.0
// VERSION DESCRIPTION:
// 1.0 First release
//-----------------------------------------------------
// RELEASE DATE: Mar-25-2009
//-----------------------------------------------------
// PURPOSE: Complex fixed point
//-FHDR------------------------------------------------


#ifndef _CMPLX_FX_HDR
#define _CMPLX_FX_HDR

#define IWL 7//integer word-length
#define DWL 9 //decimal fraction word-length
//IWL+DWL=sizeof(short int)*2=16

typedef struct {
short int re;
short int im;
} cmplx_fix;
//check negaitve
int is_neg(short int);
//cmplx_fix fcns
void cf_print(cmplx_fix);//printer
void cf_seti_re(cmplx_fix*, int);//set rel part by integer values
void cf_seti_im(cmplx_fix*, int);//set img part by integer values
void cf_setf_re(cmplx_fix*, float);//set rel part by float values
void cf_setf_im(cmplx_fix*, float);//set img part by float values

//--cmplx_fix operator fcns--
//----cmplx_fix to cmplx_fix----
cmplx_fix cf_add(cmplx_fix, cmplx_fix);// +
cmplx_fix cf_sub(cmplx_fix, cmplx_fix);// -
cmplx_fix cf_mul(cmplx_fix, cmplx_fix);// *
cmplx_fix cf_div(cmplx_fix, cmplx_fix);// /
//----cmplx_fix to integer values----
cmplx_fix cf_add_re(cmplx_fix, int); // + real
cmplx_fix cf_add_im(cmplx_fix, int); // + imag
//cmplx_fix cf_sub_re(cmplx_fix, int); // - real as _add_re(-int)
//cmplx_fix cf_sub_im(cmplx_fix, int); // - imag as _add_im(-int)
cmplx_fix cf_mul_re(cmplx_fix, int); // * real
cmplx_fix cf_mul_im(cmplx_fix, int); // * imag
cmplx_fix cf_div_re(cmplx_fix, int); // / real
cmplx_fix cf_div_im(cmplx_fix, int); // / imag
#endif


In ANSI C, we don't have much useful programming shortcuts,
no classes, no functional overridding, no operator overridding, etc
All things we can do is function calls, such as the function:
cf_print() is self-made print function.
cf_add() is the func for my cmplx_fix addition.

For the cmplx_fix structure contains two members:
re is the real part
im is the imalge part
They are in a short int datatypes(signed 16bits each) for reducing memory allocation.
The interger word-length is define by IWL, decimal fraction part by DWL

In these version, I do not perform the overflow and stratuation processing.
It may be improved in the future.
The cmplx_fix addition function cf_add() is shwon as an example here:

//cmplx_fix + cmplx_fix
cmplx_fix cf_add(cmplx_fix a, cmplx_fix b)
{
short int real = a.re + b.re;
short int imag = a.im + b.im;
a.re = (short int) real;
a.im = (short int) imag;
return a;
}

2009年3月20日 星期五

How to perform fixed-point simulation in SystemC

When you need to converts a floating-point digital signal processing program written in C or C++ language to a fixed-point program automatically. Maybe the following tips will help.
Recently, I have tried to using SystemC to perform a fixed-point simulation for my DSP program. There are lots of built-in C++ classes [datatypes] for fixed-point representations, which are also very useful. For examples, the support of operator overridding were compeletly done by OSCI. Program designers can multiply the real values they want without considering the representations for integer part and the decimal part. All the rest jobs like scalling, signed extension, rounding are automatically performed by default.

SystemC support many datatypes for fixed-point representations, such as :

sc_fix
(signed values)
sc_ufix
(unsigned values)
sc_fixed (signed values, fixed word-length)
sc_ufixed (unsigned values, fixed word-length)

The difference between sc_fix and sc_fixed is that the sc_fixed version could only have a specificaly fixed word-length while compiling. It also means that the sc_fix version can perform the dynamic fixed-point precision in run-time(you may change the word-length in some place if you want).

Besides, there are _fast versions for above 4 datatypes, the _fast version's precisions are limitted to a maximun length for 53-bits in order to speed up the simulation task. I think the _fast version will be a help. Since there is no need for high persions while perfoming fixed-point simulations.

If you want to know the quatization noise effect on your application, I think the
sc_fixed_fast and sc_fixed might be the most useful datatypes.

Here are some usage about sc_fixed_fast:

#define SC_INCLUDE_FX
sc_fixed_fast<8,3> x;

First of all, the definition of SC_INCLUDE_FX enables the SystemC bulit-in library for fixed-point datatpyes. Without this definition, the library will not reconize the sc_fix* datatypes. You may see the release notes in the SystemC download resources.

The <8,3> represents the total word-length and the integer word-length.
It follows the format as:

, where WL is the word-length and IWL is the integer word-length.

You may see the x variable as "saa.bbbbb" in which the 'saa' are the signed bit and the integer bits. And the 5-bits 'bbbbb' are the decimal fraction part.

If you want to assign a real value '2.333' to x, you just use:
x=2.333;
The rounding result for x will be 2.3125. Here is an example for FIR filter...