Bernard Fouché | 13 Aug 10:50 2004

How to control the optimizer from the C source?

I met an optimization problem: while porting code from icc to gcc, I had the
following routine:

void set_clkdiv(int mode)
{
    CLI();
    switch(mode)
    {
       case 0:
       case 1:
          CLKPR=0x80;
          CLKPR=0x00;
          break;
       case 2:
          CLKPR=0x80;
          CLKPR=0x01;
          break;
       case 4:
          CLKPR=0x80;
          CLKPR=0x02;
          break;
       case 8:
          CLKPR=0x80;
          CLKPR=0x03;
          break;
       case 16:
          CLKPR=0x80;
          CLKPR=0x04;
          break;
       case 32:
(Continue reading)

Joerg Wunsch | 13 Aug 11:19 2004
Picon

Re: How to control the optimizer from the C source?

Bernard Fouché <bernard.fouche <at> kuantic.com> wrote:

> the fun thing is that the optimizer does not spare many 'ldi r24,0x80' but
> change the code to have at the end of the function 'sts 0x0061,r24' once: it
> delayed the second setting of 0x0061 to the end of the function. The problem
> is that the clock prescaler register must be touched twice within 4 clock
> ticks. If we look at line 237a (for instance), There is:

How about that?
#include <avr/io.h>
#include <inttypes.h>

void set_clkdiv(int mode)
{
    uint8_t clkpr;

    switch(mode)
    {
       case 0:
       case 1:
          clkpr=0x00;
          break;
       case 2:
          clkpr=0x01;
          break;
       case 4:
          clkpr=0x02;
          break;
       case 8:
          clkpr=0x03;
(Continue reading)

Bernard Fouché | 13 Aug 12:00 2004

RE: How to control the optimizer from the C source?

Perfect, it stands any -O value and it's also okay without '-O' (just in
time however!).

Would be nice however if something would warn the programmer that a time
critical register is touched. But well, we have to justify the monthly pay
check :-)

Thanks!

-----Message d'origine-----
De : avr-gcc-list-bounces <at> avr1.org
[mailto:avr-gcc-list-bounces <at> avr1.org]De la part de Joerg Wunsch
Envoyé : vendredi 13 août 2004 11:20
À : avr-gcc-list <at> avr1.org
Objet : Re: [avr-gcc-list] How to control the optimizer from the C
source?

Bernard Fouché <bernard.fouche <at> kuantic.com> wrote:

> the fun thing is that the optimizer does not spare many 'ldi r24,0x80' but
> change the code to have at the end of the function 'sts 0x0061,r24' once:
it
> delayed the second setting of 0x0061 to the end of the function. The
problem
> is that the clock prescaler register must be touched twice within 4 clock
> ticks. If we look at line 237a (for instance), There is:

How about that?
#include <avr/io.h>
#include <inttypes.h>
(Continue reading)

Anton Erasmus | 15 Aug 22:51 2004

Re: How to control the optimizer from the C source?

On 13 Aug 2004 at 10:50, Bernard Fouché wrote:

> I met an optimization problem: while porting code from icc to gcc, I had the
> following routine:
[routine etc snipped]

Hi,

Try the following - It produces to concecutive writes to the CLKPR register.
It also allows you to keep your critical section much shorter.

 void set_clkdiv(int mode)
{
    unsigned char tmp1,tmp2;

    switch(mode)
    {
       case 0:
       case 1:
          tmp1=0x80;
          tmp2=0x00;
          break;
       case 2:
          tmp1=0x80;
          tmp2=0x01;
          break;
       case 4:
          tmp1=0x80;
          tmp2=0x02;
          break;
(Continue reading)


Gmane