ive got to multiply a variable by 25 and i was wondering which of these 2 would be quicker
x = x*25
or
x = x<<4 + x<<3 + x
I have a suspicion that the latter will be quicker, but take up more code space.
ive got to multiply a variable by 25 and i was wondering which of these 2 would be quicker
x = x*25
or
x = x<<4 + x<<3 + x
I have a suspicion that the latter will be quicker, but take up more code space.
"I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams
ok, i just did some tests and i was half right and half wrong.
x=x<<4+x<<3+x was quicker (taking 80% of the time that x=x*25 took to execute)
However, what surprised me was that x=x*25 used MORE code space than x=x<<4+x<<3+x. Replacing x=x<<4+x<<3+x with x=x*25 in my program adds 338 bytes.
It appears that x=x<<4+x<<3+x wins on both counts.
"I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams
PBP uses a subroutine for performing the multiplication. (For all 16F * calculations, and for 18F * calcs involving more than 8 bits.) If you perform the calculation multiple times, an individual line may be shorter than shifting. However, on just a one-time multiplication, the PBP subroutine will add significant code.
No other code, just Variable declaration and END,for a F877:
For BYTE variable:
For WORD variable:Code:x=x*25 '40 bytes x=x<<4+x<<3+x '43 bytes
IoannisCode:x=x*25 '43 bytes x=x<<4+x<<3+x '49 bytes x=x<<25 '27 bytes!!!
whats the point of the last one? shift a word variable 25 times either way and you will just get 0.
i was multiplying a word var and saving into a long var
"I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams
I know 25 shift will not give the result you want.
Just tested to see how much less program space and how faster a shift will be. Useless for you but quite impressive!
Which PIC are you using? Is there any chance to use one with hardware multiplier?
Ioannis
Bookmarks