Shifting on m2 picaxes - fastest way?

PaulRB

Senior Member
I want to run some code written for x2 on m2. The only function used that believe I can't use on m2 is shifting: >> and <<. I only need to shift by 1 bit.

Instead of

W0 = W0 << 1

I can use

W0 = W0 + W0

Which will be pretty quick. But for

W0 = W0 >> 1

the only thing I can think of is

W0 = W0 / 2

which will be slower. Speed is critial. Any suggestions please?

Thanks,

Paul
 

AllyCat

Senior Member
Hi Paul,

There's probably less difference in speed between + - * and / than you think. I've only measured byte operations but I believe that the execution time of "/" versus "+" is less than 2:1. Where PICaxe really gets rather messy is if you want/need to maintain the Carry/Borrow/Overflow bit.

However, you are (probably) correct that w0 = w0 + w0 is faster than w0 = w0 * 2, but by less than 10%. The only alternative that I can suggest for w0 = w0 / 2 is w0 = w0 ** $8000, but I don't know if it's any faster. Maybe I'll test it sometime, or you can try yourself by timing the operations within a loop, or I described a PICaxe-only method in Post #12 of this thread. Note that M2 devices appear to be significantly slower than previous versions (at the same clock speed), presumably beacause of the larger instruction set and address space.

Cheers, Alan.
 

PaulRB

Senior Member
Thanks Alan! Sounds like I shouldn't worry about it too much then. I was imagining the / might take 16 times longer for a word variable compared to shift or add.

Thanks,

Paul
 

hippy

Ex-Staff (retired)
I would suspect W0 = W0 / 2 might be your fastest option. It's optimal in token fetches and possibly not that long in execution. The only other thing I could think of is an EEPROM table ...

Eeprom ( 0,0, 1,1, 2,2, 3,3 ... 127,127 )

Read b0, b0
bit7 = bit8
Read b1, b1
 
Top