From: Frank on
Hey guys,

I was wondering if you could help me optimize the code below (if possible):

R = (B*X);
tmp = (int16_t) (R>>15);
R = ((int32_t)tmp)+Z;
tmp = (int16_t)R;
R = (A*((int32_t)Y));
tmp2 = -(int16_t) (R>>15);
Rout=tmp+tmp2;

Rout is a SINT16 variable containing a Q1.15 value
A and B will be replaced by a number between -32768 and 32767
tmp and tmp2 are SINT16 variables; each containing a Q1.15 value
Z is a SINT32 variable containing a Q1.15 value
Y is a SINT16 variable containing a Q1.15 value
R is a SINT32 variable containing the result of an operation on two Q1.15
values
X is a SINT32 variable containing a Q1.15 value

The above code looks too messy to me and I'm sure it can be optimized..Or
maybe I am wrong?
Basically I just want to do some operations on Q1.15 numbers in a 32-bit
cell.

Thank you in advance.


From: robert bristow-johnson on
On Jun 8, 5:49 am, "Frank" <Fr...(a)invalidmail.com> wrote:
>
> Basically I just want to do some operations on Q1.15 numbers in a 32-bit
> cell.

so left-justify it and call it a Q1.31 number (where the 16 LSBs are
zero). or right justify it and call it a Q16.16 number.

the code looks like C where you've typedeffed some things.

r b-j
From: Tim Wescott on
On 06/08/2010 02:49 AM, Frank wrote:
> Hey guys,
>
> I was wondering if you could help me optimize the code below (if possible):
>
> R = (B*X);
> tmp = (int16_t) (R>>15);
> R = ((int32_t)tmp)+Z;
> tmp = (int16_t)R;
> R = (A*((int32_t)Y));
> tmp2 = -(int16_t) (R>>15);
> Rout=tmp+tmp2;
>
> Rout is a SINT16 variable containing a Q1.15 value
> A and B will be replaced by a number between -32768 and 32767
> tmp and tmp2 are SINT16 variables; each containing a Q1.15 value
> Z is a SINT32 variable containing a Q1.15 value
> Y is a SINT16 variable containing a Q1.15 value
> R is a SINT32 variable containing the result of an operation on two
> Q1.15 values
> X is a SINT32 variable containing a Q1.15 value
>
> The above code looks too messy to me and I'm sure it can be
> optimized..Or maybe I am wrong?
> Basically I just want to do some operations on Q1.15 numbers in a 32-bit
> cell.
>
> Thank you in advance.
>
>
Is this C? Do you have to stick to C? C really doesn't like
Q1.anything -- it's only native fixed-point data type is integer, and it
sticks to it like glue. You can do fractional fixed-point arithmetic
_much_ faster in assembly -- if I'm working with a processor that
doesn't have really fast floating point math then I'll write one -- it
usually takes less than a day.

See chapter 10 of my book --
http://www.wescottdesign.com/actfes/actfes.html -- it presents a Q1.31
library for the x86; if you can understand assembly language programming
at all it should make clear what you need to do for Q1.whatever math on
your processor.

--
Tim Wescott
Control system and signal processing consulting
www.wescottdesign.com
From: Frank on
> Is this C? Do you have to stick to C?

It is C and I have to stick to C.


>C really doesn't like Q1.anything -- it's only native fixed-point data type
>is integer, and it sticks to it like glue. You can do fractional
>fixed-point arithmetic _much_ faster in assembly -- if I'm working with a
>processor that doesn't have really fast floating point math then I'll write
>one -- it usually takes less than a day.

I was trying to do it with some defines like these:

#define TOQ15(x32) (((int16_t)(x32>>15)) & (int16_t)(((x32>>31)<<15) |
65535))

The idea here is to shift a 32 bit value 15 bits down set bit 15 (the sign
bit) correctly.

#define MULXY(x16,y16) TOQ15(((int32_t)x16)*((int32_t)y16))
#define ADDXY(x16,y16) TOQ15(((int32_t)x16)+((int32_t)y16))

Some basic operations. However, there seems to be an error in the above
defines...Have to debug it...


> See chapter 10 of my book --
> http://www.wescottdesign.com/actfes/actfes.html -- it presents a Q1.31
> library for the x86; if you can understand assembly language programming
> at all it should make clear what you need to do for Q1.whatever math on
> your processor.

Thank you. I will have a look at it (even though I still have to do the
operations
in C)....


From: Frank on
>so left-justify it and call it a Q1.31 number (where the 16 LSBs are
>zero). or right justify it and call it a Q16.16 number.

Yeah...i guess...but how does that answer my question? :o)