Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > Re: Example of ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 9 Topic 26045 of 26821
Post > Topic >>

Re: Example of the optimiser recognising a pattern

by =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= <toe@[EMAIL PROTECTED] > May 2, 2008 at 04:55 PM

Ian Collins:

> > Suprisingly, the compiler produced more efficient code for the latter,
> > presumably because it recognises the pattern of " x &= ~y" for
> > clearing a single bit.
>
> Odd, is x an unsigned 8 bit type?


Yes, it is.


> If so, the two expressions should
> generate identical code.


If I do:

    y &= ~0x08u;

then I get the following assembler:

    BCF y, 0x3    /* Clear the 4th bit of y */

If I do:

    y &= 0x7Fu;

then I get the following assembler:

    MOVLW 0x7f     /* Load the accumulator with 0x7f */
    ANDWF y, F     /* AND y with the accumulator
                      and store the result in y      */

The former is one instruction, while the latter is two, and as all
instructions take the same amount of CPU cycles, the latter version is
exactly twice as slow.

Not only that, but things get even worse if you do the following:

    if (whatever) y &= ~0x08u;

versus:

    if (whatever) y &= 0x7Fu;

On the PIC micrcontroller, there's an instruction that does the
following: "Check whether the last arithmetic operation resulted in
zero, and if so, skip the next instruction". Since the former version
is comprised of a single instruction, this single instruction can be
skipped by the conditional. However, in the case of the latter form
which consists of two instructions, there has to be an interleaving
goto statement. Result: WAY slower.
 




 9 Posts in Topic:
Example of the optimiser recognising a pattern
=?ISO-8859-1?Q?Tom=E1s_=D  2008-05-02 14:59:33 
Re: Example of the optimiser recognising a pattern
=?ISO-8859-1?Q?Tom=E1s_=D  2008-05-02 15:20:39 
Re: Example of the optimiser recognising a pattern
Ian Collins <ian-news@  2008-05-03 11:23:54 
Re: Example of the optimiser recognising a pattern
=?ISO-8859-1?Q?Tom=E1s_=D  2008-05-02 16:55:43 
Re: Example of the optimiser recognising a pattern
"Bartc" <bc@  2008-05-03 08:12:42 
Re: Example of the optimiser recognising a pattern
vippstar@[EMAIL PROTECTED  2008-05-03 07:16:16 
Re: Example of the optimiser recognising a pattern
Andrey Tarasevich <and  2008-05-03 09:45:25 
Re: Example of the optimiser recognising a pattern
vippstar@[EMAIL PROTECTED  2008-05-03 09:48:17 
Re: Example of the optimiser recognising a pattern
Andrey Tarasevich <and  2008-05-03 10:09:19 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Jul 9 0:49:42 CDT 2008.