Jump to content

Multiplying by 3 in C


robinfawell
 Share

Recommended Posts

Thorsten kindly provided me with code to address Bankstick memory locations. Part of this code includes the following line.

unsigned int addr = dump_table[dump_index*3 + 1];

Bearing in mind that multiplying gobbles up bytes.  Would the following work? And would it be more efficient?

unsigned int addr = dump_table[dump_index*2 +dump index + 1];

unsigned int addr = dump_table[(dump_index << 1) + dump_index + 1];

Or would the compiler carry out the *3 function efficiently in any case?

Robin

Link to comment
Share on other sites

Bitshift operations are most useful on the PIC18s for divide operations, because of the hardware multiply module used for MULWF they can actually be slower for multiplications.

It depends on the size of the multiples and how it's done in ASM after the compilation... the 18f will do MULWF in a single cycle just like a bitshift, so it might not be any faster provided that the multiples are <=8 bits long, and the compiler uses MULWF. You'll just have to try both methods and check out the code.

Still, it's always good to consider another way to do things in your code :) You never know what you'll think up.

Link to comment
Share on other sites

Well, I must admit that I would probably give better advice if I read your post correctly the first time! I gave advice about how *fast* it would be, rather than how *big* ..derrr... I guess code optimising is getting to me :-\

At least it's a good reminder that 'efficient' often means a tradeoff :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...