
Rockbox mail archiveSubject: Re: mathRe: math
From: Ray Lambert <listlizard_at_interthingy.net>
Date: Thu, 7 Jul 2005 11:41:24 0400 (EDT) Dave Wiard wrote: > Ray Lambert wrote: >> BlueChip wrote: >> >>> while (x>0) r = (r<<3)+(r<<1); // r*8 + r*2 >>> ^whoops >>> >>> what values of 'x' ? all positive? include 0? >>> >>> if (x<0) return(0); // cannot handle negative values of x >>> if (x>9) return(1); // result will be > (2^32)1 >>> unsigned long r = 1; >>> while (x>0) r = (r<<4)+(r<<1); // r*8 + r*2 >>> return (r); >> >> It might be a good idea to decrement x at some point too.... :) >> >>> while (x>0) r = (r<<3)+(r<<1); // r*8 + r*2 >> >> perhaps: >> >> for (; x>0; x) r = (r<<3)+(r<<1); // r*8 + r*2 >> >> ~ray > > And catch x=0. And set your base at 10 not 1 (remember, this is exp10). > > if (x == 0) return 1; // 10^0 is always 1 > if (x < 0) return 0; // cannot handle negative values of x > if (x > 9) return 1; // result will be > (2^32)1 > unsigned long r = 10; > while (x) r = (r << 3) + (r << 1); // r*8 + r*2 > return r; > > Dave Actually, his code did catch that. If x==0 on entry the loop doesn't execute and r is returned (with 1). If x==1 on entry the loop executes once and r is multplied by 10 (making 10) and returned. Your code is right also. You code optimizes for speed (one less multiply) but is longer. His code has slightly smaller code size at the expense of one additional multiply. If the possible values of x are really as limited as someone suggested they were, then the lookup table approach is the best for both code size and speed. ~ray  Politicians and diapers have one thing in common. They should both be changed regularly, and for the same reason. _______________________________________________ http://cool.haxx.se/mailman/listinfo/rockboxReceived on 20050707 Page template was last modified "Mon Jun 14 16:30:06 2021" The Rockbox Crew  Privacy Policy 