Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

Re: [AVR-Chat] Faster arithmetic

2005-06-14 by Peter Harrison

Paul Curtis wrote:
> Peter, 
> 
> [ snip ]
> 
> 
>>In my application, I only wanted the most significant 16 
>>bits. there are two easy ways to shed them:
>>
>>w = ((long)x * y) / 65536;  //takes 782 cycles (toooo long at 16MHz)
>>
>>This seems to trigger a full divide with the compiler missing 
>>the optimisation, so I gave it a clue:
> 
> Actually, the compiler is doing the correct thing in implementing a full
> divide.
> 
> Consider (1 * -1) / 65536.  That's -1L / 65536 which is 0.
> 
> 
In those cases, CodeVision generates the following:

          ;     152 z = (1*-1)/65536;36
           _0xA:
00014b e0e0      	LDI  R30,0
00014c 93e0 0169 	STS  _z,R30
00014e 93e0 016a 	STS  _z+1,R30
000150 93e0 016b 	STS  _z+2,R30
000152 93e0 016c 	STS  _z+3,R30
          ;     153 z = -1L/65536;
000154 e0e0      	LDI  R30,0
000155 93e0 0169 	STS  _z,R30
000157 93e0 016a 	STS  _z+1,R30
000159 93e0 016b 	STS  _z+2,R30
00015b 93e0 016c 	STS  _z+3,R30

So it can spot optimisations - I am not really complaining, just 
pointing out that it pays to look at what is going on. I am sure it is 
quite correct in doing the divide. I just expected the shortcut on first 
sight.

> 
> Consider (1 * -1) >> 16.  That's -1L >> 16 which is still -1.

And CodeVision gets that right as you would expect but I am using 
unsigned quantities

> 
> (Actually, there *is* a way to implement a signed divide by a power of
> two without using the clockwork method.)
> 

What is that?

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.