Level: Resident

Joined: Fri. Feb 24, 2006

Location: http://avr.x.am

Posted by RetroDan:
Fri. Mar 24, 2006 - 04:37 AM
****** DIRTY MATH PACK for AVR/ASM ******

While trying to help someone in another FORUM with a division-by-60 routine to convert seconds to minutes, someone made the remark:

Quote:

What RetroDan has is a quick and dirty approximation.

I don't know if it was an insult or compliment, but it gave me a great name for the type of binary routines I am interested in: "Dirty Math," Quick & Dirty Routines that are small and fast.

Quote:

DIRTY DAN'S RULE #1: ALWAYS USE A POWER OF TWO

If you are writing any program or routine, even if the savings are not obvious at first, pick numbers that are powers of two. If you need to take a average don't take 10 readings and divide by 10, take 8 or 16. If you're setting up a table, don't make entries 12 bytes long, make them 8 or 16. It will greatly simplify things at the hardware-level.

Quote:

DIRTY DAN'S RULE #2: AVOID DIVISION!

While Addition, Subtraction and Multiplication can be done is a little as 1 clock cycle, most division involves looping and uses a lot of processor time. If you must divide try to do it by powers of two (See Rule #1) which can be accomplished with a few Right-Shifts. If you must divide more than once, try and re-arrange equation so you only divide once, for example if you need to divide by 10 and later divide by 12, perhaps you can just divide once by 120.

## Tags:

Last Edited: Sun. Mar 26, 2006 - 04:43 AM