All, The division by multiplication technique is well known, but does not generate a remainder--you need to compute the remainder by multiplying and subtracting. In essence, you're multiplying by the reciprocal of 10 expressed (for instance) as 2^32/10 or 2^16/10, but it *does* require that you can multiply two n-bit numbers to produce a 2n-bit product and then take the high n bits of that product--and you need to account for the error term. Some processors don't do this in hardware. Alternatively, Karl, try using the types div_t, ldiv_t and the functions div and ldiv that deliver both quotient and remainder for you. Note that the operation of div_t is consistent across all implementations of C, whereas / and % are *not*. -- Paul. > -----Original Message----- > From: Karl Olsen [mailto:kro@...] > Sent: 30 November 2004 08:01 > To: lpc2000@yahoogroups.com > Subject: [lpc2000] Re: Slightly OT -- Fast Divide By 10 > > > > > --- In lpc2000@yahoogroups.com, "James Dabbs" <jdabbs@t...> wrote: > > > > I'm trying to squeeze a few more cycles out of one my LPC, and I'm > > looking for an optimized way to divide by 10 and leave the quotient > and > > remainder. I.e., a faster way to do this: > > > > #define DIVIDE_BY_10(D, Q, R) Q=D/10; R=D%10; // D is unsigned long > > > Check out "How to optimize for the Pentium family of microprocessors" > http://www.agner.org/assem/pentopt.pdf > > The section "Division (all processors)" has a smart algorithm for > converting division by a constant to a cheaper multiplication and > shift. > > Karl Olsen > > > > > > > ------------------------ Yahoo! Groups Sponsor > --------------------~--> > $9.95 domain names from Yahoo!. Register anything. > http://us.click.yahoo.com/J8kdrA/y20IAA/yQLSAA/dN_tlB/TM > -------------------------------------------------------------- > ------~-> > > > Yahoo! Groups Links > > > > > > > >
Message
RE: [lpc2000] Re: Slightly OT -- Fast Divide By 10
2004-11-30 by Paul Curtis
Attachments
- No local attachments were found for this message.