DSPRelated.com
Forums
Online · Live + on demand
February 9-11, 2027
Price goes up Jan 1: register now to lock in today's rate
Register Now →

optimizing C code /switching to asembler

Started by Bernhard 'Gustl' Bauer ●October 12, 2004

Qing Li wrote:
>
> Hi there,

Hi,

I back at this problem now.

BTW have I mantioned that I'm using CCS2.2 on a TMS320C6713?

> 1. Do what Andraw suggested.
> Use LDDW and STDW to reduce the pressure of
> load/store, and check the assembly to make sure TI
> compiler is using a register to store the loop result
> instead of store instruction is used in each
> iteration.

I get the meaning of LDDW and STDW, but I'm not so sure how to implement
the instrinsics in C. Is there an example code out there?

> 4. Use in_array directly
> It seems fil can be get rid of at a glance.

Unfortunatly not, fil has to store them for the next round. in_array is
used outside and can't do that.

Thanks a lot

Gustl



Hello again,

I tried a different loop. With the help of your comments I was able to
reduce the time for this loop to 25%! Thanks a lot.

To measure the time it needs I set/clr a MCASP pin. Here is the source:

------------------
MCASP_RSET(PDSET1,0x00000020);
for (i=0;i<16;i++) {
loop_in_array[i][0]=*coeff*r_in_array[0][0];
loop_in_array[i][1]=*coeff*r_in_array[0][1];
loop_in_array[i][0]+=*(coeff+1)*r_in_array[1][0];
loop_in_array[i][1]+=*(coeff+1)*r_in_array[1][1];
loop_in_array[i][2]=*coeff*r_in_array[0][2];
loop_in_array[i][3]=*coeff*r_in_array[0][3];
loop_in_array[i][2]+=*(coeff+1)*r_in_array[1][2];
loop_in_array[i][3]+=*(coeff+1)*r_in_array[1][3];
loop_in_array[i][4]=*coeff*r_in_array[0][4];
loop_in_array[i][5]=*coeff*r_in_array[0][5];
loop_in_array[i][4]+=*(coeff+1)*r_in_array[1][4];
loop_in_array[i][5]+=*(coeff+1)*r_in_array[1][5];
loop_in_array[i][6]=*coeff*r_in_array[0][6];
loop_in_array[i][7]=*coeff*r_in_array[0][7];
loop_in_array[i][6]+=*(coeff+1)*r_in_array[1][6];
loop_in_array[i][7]+=*(coeff+1)*r_in_array[1][7];
coeff+=8;
}
MCASP_RSET(PDCLR1,0x00000020);
------------------

I measured the time from rise to fall: 2.8s
I went trough list file and counted the cycles: 348 cyc
I run it a 225 MHz, so 348 cyc. last 1.546s

I run the code from internal RAM and all data is in internal RAM to. The
code is part of an IRQ, so nothing can interrupt this code. How can the
time difference of 1.254s (almost 50%!) be explained?

I think TMS320C6713 with 8 .units should be capable of doing 2 MACs in
one cycle. This code has 8 MYPs, 8 MACs, 1 DEC 1ADD. So this code should
be able to run in 16*(8+8+1+1)/24 cycles, isn't it? At lest this is
the calculation my boss does :-(

Looking forward to read your comments.

Gustl



Bernhard-

One comment, not in the area of optimizing your code, but more along the lines
of
"debug of the debug": GPIO pins may not set/clear that fast. Changing external
state on a McASP pin might take several cycles, depending on how fast is your
processor. For a vanilla example, on 150 MHz C6711, it can take up to 90 nsec
for a
McBSP pin being used as GPIO to change states. I found this out the hard way
before,
when I tried to toggle McBSP pins very quickly. I found that even using
hand-asm,
the code stalled and waited for a chip-level state machine.

-Jeff

Bernhard 'Gustl' Bauer wrote:
>
> Hello again,
>
> I tried a different loop. With the help of your comments I was able to
> reduce the time for this loop to 25%! Thanks a lot.
>
> To measure the time it needs I set/clr a MCASP pin. Here is the source:
>
> ------------------
> MCASP_RSET(PDSET1,0x00000020);
> for (i=0;i<16;i++) {
> loop_in_array[i][0]=*coeff*r_in_array[0][0];
> loop_in_array[i][1]=*coeff*r_in_array[0][1];
> loop_in_array[i][0]+=*(coeff+1)*r_in_array[1][0];
> loop_in_array[i][1]+=*(coeff+1)*r_in_array[1][1];
> loop_in_array[i][2]=*coeff*r_in_array[0][2];
> loop_in_array[i][3]=*coeff*r_in_array[0][3];
> loop_in_array[i][2]+=*(coeff+1)*r_in_array[1][2];
> loop_in_array[i][3]+=*(coeff+1)*r_in_array[1][3];
> loop_in_array[i][4]=*coeff*r_in_array[0][4];
> loop_in_array[i][5]=*coeff*r_in_array[0][5];
> loop_in_array[i][4]+=*(coeff+1)*r_in_array[1][4];
> loop_in_array[i][5]+=*(coeff+1)*r_in_array[1][5];
> loop_in_array[i][6]=*coeff*r_in_array[0][6];
> loop_in_array[i][7]=*coeff*r_in_array[0][7];
> loop_in_array[i][6]+=*(coeff+1)*r_in_array[1][6];
> loop_in_array[i][7]+=*(coeff+1)*r_in_array[1][7];
> coeff+=8;
> }
> MCASP_RSET(PDCLR1,0x00000020);
> ------------------
>
> I measured the time from rise to fall: 2.8s
> I went trough list file and counted the cycles: 348 cyc
> I run it a 225 MHz, so 348 cyc. last 1.546s
>
> I run the code from internal RAM and all data is in internal RAM to. The
> code is part of an IRQ, so nothing can interrupt this code. How can the
> time difference of 1.254s (almost 50%!) be explained?
>
> I think TMS320C6713 with 8 .units should be capable of doing 2 MACs in
> one cycle. This code has 8 MYPs, 8 MACs, 1 DEC 1ADD. So this code should
> be able to run in 16*(8+8+1+1)/24 cycles, isn't it? At lest this is
> the calculation my boss does :-(
>
> Looking forward to read your comments.
>
> Gustl
>
> _____________________________________
> Note: If you do a simple "reply" with your email client, only the author of
this message will receive your answer. You need to do a "reply all" if you want
your answer to be distributed to the entire group.
>
> _____________________________________
> About this discussion group:
>
> To Join: Send an email to
>
> To Post: Send an email to
>
> To Leave: Send an email to
>
> Archives: http://www.yahoogroups.com/group/c6x
>
> Other Groups: http://www.dsprelated.com
>
> Yahoo! Groups Links >
>




Gustl-

> I'm aware that the signal on the McASP might be delayed to the set/clr
> command. But both commands (set and clr) should be delayed so the amount
> of delay is about the same doesn't matter that much.
>
> I have a difference of 1.254us or 282 cycles. I don't think that the
> delay of the clr command is 282 cyc. longer than that of the set command.
>
> You are writing of 90ns@150MHz which are 15 cycles. This is amount I can
> live with.

Ok. I was just tossing a thought in case it made a difference.

-Jeff

> Jeff Brower wrote:
> > Bernhard-
> >
> > One comment, not in the area of optimizing your code, but more along
> > the lines of "debug of the debug": GPIO pins may not set/clear that
> > fast. Changing external state on a McASP pin might take several
> > cycles, depending on how fast is your processor. For a vanilla
> > example, on 150 MHz C6711, it can take up to 90 nsec for a McBSP pin
> > being used as GPIO to change states. I found this out the hard way
> > before, when I tried to toggle McBSP pins very quickly. I found that
> > even using hand-asm, the code stalled and waited for a chip-level
> > state machine.
> >
> > -Jeff



Gustl,

Please see my comment below.

mikedunn

----- Original Message -----
From: "Bernhard Gustl Bauer" <>
To: "Mike Dunn" <>
Cc: "Jeff Brower" <>; <>
Sent: Sunday, October 24, 2004 4:24 AM
Subject: Re: [c6x] optimizing C code /switching to asembler >
>
> Mike Dunn wrote:
>
> > Gustl,
>
> Mike,
>
> >
> > I agree with Jeff on the I/O pins. If you have an
> > unused CE space [or a used one set up with max async
> > speed] it will cut down on the overhead.
>
> I don't know what you mean. Do you suggest to use a CE pin instead of a
> MCASP pin?

Yes, if you are trying to get precise timing to trigger a scope, CE space
signals are better than I/O pins. You may have enough accuracy for what you
are doing.

mikedunn
>
> >
> > Also, if your code is such that you can set a
> > breakpoint before and after it and use the simple
> > profile function to count cpu clocks you can get an
> > accurate measurement [when you count from one
> > breakpoint to the next, you are using a hardware
> > counter on the chip]. If you have doubts, you might
> > try to correlate it with your scope measurement.
>
> I'll try that
>
> Gustl > _____________________________________
> Note: If you do a simple "reply" with your email client, only the author
of this message will receive your answer. You need to do a "reply all" if
you want your answer to be distributed to the entire group.
>
> _____________________________________
> About this discussion group:
>
> To Join: Send an email to
>
> To Post: Send an email to
>
> To Leave: Send an email to
>
> Archives: http://www.yahoogroups.com/group/c6x
>
> Other Groups: http://www.dsprelated.com
>
> Yahoo! Groups Links >
>