Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » AVR-GCC
  • » [avr-gcc-list] How to (efficeiently !!!) test a bit within amulti-byte integer ? [RSS Feed]

#1 Nov. 4, 2005 09:13:24

Jurek S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[avr-gcc-list] How to (efficeiently !!!) test a bit within amulti-byte integer ?


>>
>> unsigned char temp;
>>
>> temp = (address >> 16) & 0xFF;
>>
>> if(temp & 0x04)
>
>

Hi Vince !
You could also try to retrieve directly the 2. ( from 0) byte of ulong and check
the bit , something like this ( -Os) :

if( *((uchar*)&long1+2) & 0x4) PORTB = 0x1;
8c: 80 91 65 00 lds r24, 0x0065
90: 82 ff sbrs r24, 2
92: 02 c0 rjmp .+4 ; 0x98
94: 81 e0 ldi r24, 0x01 ; 1
96: 88 bb out 0x18, r24 ; 24

(long1 located at 0x63-0x66 here - Atmega8)

HTH
Best regards Jurek S.


_______________________________________________
AVR-GCC-list mailing list
AVR-GCC-list@nongnu.orghttp://lists.nongnu.org/mailman/listinfo/avr-gcc-list

Offline

#2 Nov. 4, 2005 12:17:48

Vincent T.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[avr-gcc-list] How to (efficeiently !!!) test a bit within amulti-byte integer ?


On Fri, 2005-11-04 at 10:04 +0100, Jurek Szczesiul wrote:
> Hi Vince !
> You could also try to retrieve directly the 2. ( from 0) byte of ulong and
> check
> the bit , something like this ( -Os) :
>
> if( *((uchar*)&long1+2) & 0x4) PORTB = 0x1;
> 8c: 80 91 65 00 lds r24, 0x0065
> 90: 82 ff sbrs r24, 2
> 92: 02 c0 rjmp .+4 ; 0x98
> 94: 81 e0 ldi r24, 0x01 ; 1
> 96: 88 bb out 0x18, r24 ; 24

Using pointers to access directly the required byte, I did think of it,
but didn't dare ! ;-) It's technically elegant I find, but sadly the
code is not very easy to read I find.

About the -Os flag, I noticed this morning that it managed MASSIVE code
size reduction ! SO far I had been using just -O (I guess that means no
particular optimisation ?), and the program is about 13KB in size. With
-Os, it's only 10KB !!! Sadly, for some reason, when compiled with this
flag, my program misbehaves badly (I get massive corruption of the LCD
display) !! Too bad... :o(



--
Vince



_______________________________________________
AVR-GCC-list mailing list
AVR-GCC-list@nongnu.orghttp://lists.nongnu.org/mailman/listinfo/avr-gcc-list

Offline

#3 Nov. 4, 2005 15:48:47

n.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[avr-gcc-list] How to (efficeiently !!!) test a bit within amulti-byte integer ?


Great!
But you still have to use the correct operator:
& is bit-wise and
&& is logical and

You probably want "&" otherwise why compare with 0x04?

/niklo

-----Original Message-----
From:
On Behalf Of David
Kelly
Sent: den 4 november 2005 14:37
To: Vincent Trouilliez
Cc: avr-gcc
Subject: Re: How to (efficeiently !!!) test a bit within
amulti-byte integer ?


On Nov 3, 2005, at 11:35 PM, Vincent Trouilliez wrote:
>
> static void log_address_set( uint32_t address )
> {
> ...
> ...
> if (address & 0x00040000)
> PORTB |= LOG_SDA;
> else
> PORTB &= ~LOG_SDA;
> ...
> }


On Nov 4, 2005, at 1:20 AM, Ian Caddy wrote:
>
> I haven't tried this, but it might be better:
>
> unsigned char temp;
>
> temp = (address >> 16) & 0xFF;
>
> if(temp & 0x04)
> {
> /* Your code */
> }

In other messages & has been replaced with &&.

Ian is close to how I would have handled it. I use a "union32_t" type
a fair bit in my code for no other purpose than to pluck values out
of the middle painlessly.

typedef union union32_t {
struct __attribute__ ((packed)) {
uint8_t a, b, c, d;
} u8;

struct __attribute__ ((packed)) {
uint8_t a;
uint16_t bc;
uint8_t d;
} mixed;

struct __attribute__ ((packed)) {
uint16_t ab, cd;
} u16;

uint32_t u32;
};

union32_t address;

if( address.u8.c && 0x04 )
...

avr-gcc does the expected without "__attribute__ ((packed))" but if
you get to playing in Linux or FreeBSD its needed to do the same as
what avr-gcc does.

Also note the above is sensitive to machine endianess. Macintosh,
Sun, and Irix are big-endian.

--
David Kelly N4HHE,
========================================================================
Whom computers would destroy, they must first drive mad.



_______________________________________________
AVR-GCC-list mailing list
AVR-GCC-l***@*ongnu.orghttp://lists.nongnu.org/mailman/listinfo/avr-gcc-list_______________________________________________
AVR-GCC-list mailing list
AVR-GCC-list@nongnu.orghttp://lists.nongnu.org/mailman/listinfo/avr-gcc-list

Offline

#4 Nov. 4, 2005 16:05:55

Martijn v.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[avr-gcc-list] How to (efficeiently !!!) test a bit within amulti-byte integer ?


Vincent,I just remembered something that might be of use.
This is a quotefrom your first message:>I just ran into a weird problem that I hardly
expected.>I wrote a trivial routine that takes a 32bit unsigned int, which holds
a>memory address, and shifts the lower 19 bits, out to a port pin (this>feeds cascaded shift registers, that in turn drive the 19 address lines>of a memory chip).>For a long time I always put address bit 0 on address
pin A0 of the memory,bit 1 on A1, etc. But this is not always needed! Especially
for a RAMdevice where you the AVR is the only one having access,
the address linescan be swapped as you like. The same holds for the
data lines. For ROM memoryI have use this as well to enable better PCB routing
and just swapped the contentsbefore flashing.In your case if your reverse the order of A0-A18 to
A18-A0, you could start withoutputting bit 0. I'm not sure if this results in
smaller/faster code in your case,but I found it a nice extra design parameter I could
play with.Regards,Martijn._______________________________________________
AVR-GCC-list mailing list
AVR-GCC-list@nongnu.orghttp://lists.nongnu.org/mailman/listinfo/avr-gcc-list

Offline

#5 Nov. 5, 2005 04:15:38

Derric T.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[avr-gcc-list] How to (efficeiently !!!) test a bit within amulti-byte integer ?


Empty loops and optimization don't go together. Also
check to ensure you're using 'volatile' where
appropriate. I'm running into this same problem with
some inherited code at work right now.

Tubbs

--- Vincent Trouilliez
<> wrote:

> On Fri, 2005-11-04 at 10:04 +0100, Jurek Szczesiul
> wrote:
> > Hi Vince !
> > You could also try to retrieve directly the 2. (
> from 0) byte of ulong and check
> > the bit , something like this ( -Os) :
> >
> > if( *((uchar*)&long1+2) & 0x4) PORTB = 0x1;
> > 8c: 80 91 65 00 lds r24, 0x0065
> > 90: 82 ff sbrs r24, 2
> > 92: 02 c0 rjmp .+4 ; 0x98
> > 94: 81 e0 ldi r24, 0x01 ; 1
> > 96: 88 bb out 0x18, r24 ; 24
>
> Using pointers to access directly the required byte,
> I did think of it,
> but didn't dare ! ;-) It's technically elegant I
> find, but sadly the
> code is not very easy to read I find.
>
> About the -Os flag, I noticed this morning that it
> managed MASSIVE code
> size reduction ! SO far I had been using just -O (I
> guess that means no
> particular optimisation ?), and the program is about
> 13KB in size. With
> -Os, it's only 10KB !!! Sadly, for some reason,
> when compiled with this
> flag, my program misbehaves badly (I get massive
> corruption of the LCD
> display) !! Too bad... :o(
>
>
>
> --
> Vince
>
>
>
> _______________________________________________
> AVR-GCC-list mailing list
> AVR-GCC-l***@*ongnu.org
>http://lists.nongnu.org/mailman/listinfo/avr-gcc-list>



_______________________________________________
AVR-GCC-list mailing list
AVR-GCC-list@nongnu.orghttp://lists.nongnu.org/mailman/listinfo/avr-gcc-list

Offline

#6 Nov. 5, 2005 18:04:05

Vincent T.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[avr-gcc-list] How to (efficeiently !!!) test a bit within amulti-byte integer ?


> For a long time I always put address bit 0 on address pin A0 of the
> memory,
> bit 1 on A1, etc. But this is not always needed! Especially for a RAM
> device where you the AVR is the only one having access, the address
> lines
> can be swapped as you like. The same holds for the data lines. For ROM
> memory
> I have use this as well to enable better PCB routing and just swapped
> the contents
> before flashing.
> but I found it a nice extra design parameter I could play with.
>
> Regards,
> Martijn.


Interesting, thanks :o)


--
Vince



_______________________________________________
AVR-GCC-list mailing list
AVR-GCC-list@nongnu.orghttp://lists.nongnu.org/mailman/listinfo/avr-gcc-list

Offline

#7 Nov. 5, 2005 18:43:08

Niklas L.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[avr-gcc-list] How to (efficeiently !!!) test a bit within amulti-byte integer ?


That's interesting it makes that much differnce between the minors. So how
much "better" is gcc 4.x.x?
And also why isn't the newer gcc versions included in a winavr release?

/niklo

-----Original Message-----
From:
On Behalf Of
Vincent Trouilliez
Sent: den 5 november 2005 01:28
To: avr-gcc
Subject: Re: How to (efficeiently !!!) test a bit within
amulti-byte integer ?

> >I compiled 3.4.3 ... does 3.4.4 or 4.0 do a better job at it ?
> >
> >
> With gcc-3.4.4 and -Os, the compiler loads 4 registers with the 32 bits
> value (address) and then use 'sbrs <register>,2' to perform the test.
> Hence it is very fast. It would be faster if it needs only one register
> to test however. I did not test with gcc-4.x
>
> Bernard


Hmmm, it's interesting to see that 3.4.4 is that much better than
3.4.3 ! It means that avr-gcc is being actively improved, that's rather
encouraging, at this pace, in 2 or 3 years, it will rival or better the
most expensive commercial compilers :o) Long may live Unix/Linux and
open source...


--
Vince



_______________________________________________
AVR-GCC-list mailing list
AVR-GCC-l***@*ongnu.orghttp://lists.nongnu.org/mailman/listinfo/avr-gcc-list_______________________________________________
AVR-GCC-list mailing list
AVR-GCC-list@nongnu.orghttp://lists.nongnu.org/mailman/listinfo/avr-gcc-list

Offline

#8 Nov. 8, 2005 22:59:38

Eric W.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[avr-gcc-list] How to (efficeiently !!!) test a bit within amulti-byte integer ?


Niklas Lövgren wrote:That's interesting it makes that much differnce between the minors. So howmuch "better" is gcc 4.x.x?And also why isn't the newer gcc versions included in a winavr release?1. Previously, there have been bugs in that tree affecting the AVR portwhich has made it a "no-go".2. Because I haven't done a new WinAVR release yet. I'm planning toinclude 4.0.2 as soon as I can come up for air.Eric


_______________________________________________
AVR-GCC-list mailing list
AVR-GCC-list@nongnu.orghttp://lists.nongnu.org/mailman/listinfo/avr-gcc-list

Offline

  • Root
  • » AVR-GCC
  • » [avr-gcc-list] How to (efficeiently !!!) test a bit within amulti-byte integer ? [RSS Feed]

Board footer

Moderator control

Enjoy the 18th of November
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support