Yahoo Groups archive

AVR-Chat

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

Message

Re: [AVR-Chat] Faster arithmetic

2005-06-15 by Peter Harrison

Paul Curtis wrote:
> Peter, 
> 
> 
>>
>>And CodeVision gets that right as you would expect but I am using 
>>unsigned quantities
> 
> 
> ...but you're not, you've told the compiler that you're using signed
> quantities, you said "long" not "unsigned long" in your code snippet.
> If you has said "unsigned long" in the cast, the division by 65536 would
> decay to a shift.
> 
> 

Very true - how daft of me now I feel silly :) My apologies.

I just went back and redid them as (unsigned long) casts and the 
compiler still performed the divide! Only the first timimg quoted 
changed - it reduced to 71 cycles.

>>>(Actually, there *is* a way to implement a signed divide by 
>>
>>a power of
>>
>>>two without using the clockwork method.)
>>
>>What is that?
> 
> 
> x /= 2**n can be coded using shifts like this:
> 
> if (x < 0)
>   x += (2**n)-1;
> x >>= n;
> 
Ah - handy.

Pete

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.