Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » AVR-GCC
  • » [avr-gcc-list] Problem with Bit Shift using WinAVR20071221rc1 [RSS Feed]

#1 March 25, 2008 15:41:48

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

[avr-gcc-list] Problem with Bit Shift using WinAVR20071221rc1


Hello everyone!
I got a little Problem on this simple Code:

/-------------code start-------------------/

uint32_t i; t;

for(i=0; i<20; i++)
t = (1<<i);

/-------------code stop-------------------/

The results of this should be:
1
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
-->4294934528

after the 16384 i get 4294934528, I dont know where the problem is.

I cant code in asm, but i watched the produced asm code in AVR-Studios
Debugger and i noticed that only 2 of the 4 bits are copied in the used
registers.
This might be a Compiler failure, or my Code is wrong, or my Compiler Flags
are not correct.


Have you got any solutions for me? I'm not able to write inline asm code :(


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

Offline

#2 March 25, 2008 15:55:41

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

[avr-gcc-list] Problem with Bit Shift using WinAVR20071221rc1


Looks like a bug but

Cut and paste assembler from .lss file into email.

Also try same thing with different optimisations

-O0 (none) -O1 (some) -O2 -O3 -OsIf you are using make file look for optimsation variable about 20 linesor so from top.This will give us a clue :-)


Andy



-----Original Message-----
From: Moritz Federspiel <>
To: avr-gcc-l***@*ongnu.org
Sent: Tue, 25 Mar 2008 9:41 am
Subject: Problem with Bit Shift using WinAVR20071221rc1


Hello everyone!
I got a little Problem on this simple Code:

/-------------code start-------------------/

uint32_t i; t;

for(i=0; i<20; i++)
  t = (1<<i);

/-------------code stop-------------------/

The results of this should be:
1
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
-->4294934528

after the 16384 i get 4294934528, I dont know where the problem is.I cant code in asm, but i watched the produced asm code in AVR-StudiosDebugger and i noticed that only 2 of the 4 bits are copied in the usedregisters.This might be a Compiler failure, or my Code is wrong, or my CompilerFlags are not correct.Have you got any solutions for me? I'm not able to write inline asmcode :(regards MoFe



_______________________________________________
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

#3 March 25, 2008 16:03:16

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

[avr-gcc-list] Problem with Bit Shift using WinAVR20071221rc1


Moritz Federspiel wrote:Hello everyone!Hi, MoritzI got a little Problem on this simple Code:

/-------------code start-------------------/

uint32_t i; t;

for(i=0; i<20; i++)
t = (1<<i);This isn't actual compilable code... you should send a compilableexample so that others can test it.The code you shown as several problems:- the for loop as no side effects, so the compiler could just optimizeit to: t = (1 << 19)- "t" isn't being used afterwards, so the compiler could just optimizeeverything awayPlease send a small compilable example so that we can see what is going on.

--
Paulo Marques
Software Development Department - Grupo PIE, S.A.
Phone: +351 252 290600, Fax: +351 252 290601
Web: www.grupopie.com

"All generalizations are false."


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

Offline

#4 March 25, 2008 16:04:47

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

[avr-gcc-list] Problem with Bit Shift using WinAVR20071221rc1


On Tuesday 25 March 2008, Moritz Federspiel wrote:
> Hello everyone!
> I got a little Problem on this simple Code:
>
> /-------------code start-------------------/
>
> uint32_t i; t;
>
> for(i=0; i<20; i++)
> t = (1<<i);
>
> /-------------code stop-------------------/
>
> The results of this should be:
> 1
> 2
> 4
> 8
> 16
> 32
> 64
> 128
> 256
> 512
> 1024
> 2048
> 4096
> 8192
> 16384
> -->4294934528
>
> after the 16384 i get 4294934528, I dont know where the problem is.
>

The problem is that (1<<i) assumes that '1' is an integer, or 16 bits. And
it's signed, so when it shifts 15 times, you get -32768 (or so), and that
converts to an unsigned long as the big number you see.

((uint32_t)1 << i) (or something like that) should work.

Blake


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

Offline

#5 March 25, 2008 17:17:21

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

[avr-gcc-list] Problem with Bit Shift using WinAVR20071221rc1


Blake Leverett wrote:On Tuesday 25 March 2008, Moritz Federspiel wrote:Hello everyone!
I got a little Problem on this simple Code:

/-------------code start-------------------/

uint32_t i; t;

for(i=0; i<20; i++)
t = (1<<i);

/-------------code stop-------------------/

The results of this should be:
1
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
-->4294934528

after the 16384 i get 4294934528, I dont know where the problem is.The problem is that (1<<i) assumes that '1' is an integer, or 16 bits. Andit's signed, so when it shifts 15 times, you get -32768 (or so), and thatconverts to an unsigned long as the big number you see.((uint32_t)1 << i) (or something like that) should work.

BlakeJust (1UL << i) should work too.

Philipp_______________________________________________
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

  • Root
  • » AVR-GCC
  • » [avr-gcc-list] Problem with Bit Shift using WinAVR20071221rc1 [RSS Feed]

Board footer

Moderator control

Enjoy the 16th of December
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