RE: [AVR-Chat] Faster arithmetic
2005-06-14 by Paul Curtis
Peter, [ snip ] > In my application, I only wanted the most significant 16 > bits. there are two easy ways to shed them: > > w = ((long)x * y) / 65536; //takes 782 cycles (toooo long at 16MHz) > > This seems to trigger a full divide with the compiler missing > the optimisation, so I gave it a clue: Actually, the compiler is doing the correct thing in implementing a full divide. Consider (1 * -1) / 65536. That's -1L / 65536 which is 0. > w = ((long)x * y) >> 16; // takes 79 cycles (a shade under 5us) Consider (1 * -1) >> 16. That's -1L >> 16 which is still -1. (Actually, there *is* a way to implement a signed divide by a power of two without using the clockwork method.) Just FYI. -- Paul Curtis, Rowley Associates Ltd http://www.rowley.co.uk CrossWorks for MSP430, ARM, AVR and (soon) MAXQ processors