int log2_96(unsigned int x){ if(x<2)return 0; double dx2=x*1.00361666597546291349; //save x * (2^(1/192) rounding factor int x96=-1; do //find power of 2 less than x { x>>=1; x96+=1; } while(x>0); double dx1=1U<<x96; //same as 2^x96 -- must use 1U, not 1 x96 *= 96; // why the function has 96 in the name while(dx1<dx2) //finds log2 value within 96 times through loop { dx1 *= 1.00724641222370389809; //2^(1/96) x96+=1; } return x96; }unsigned int pow2_96(int x){ //will add code here}

CHDK already has a log2 function built in so why not use (int)(log2(x)*96.0+0.5)

int log2_96(unsigned int x){ if(x<2)return 0; double dx2=x*0.99639636716081462905; //save: x / 2^(1/192) rounding factor int x96=-1; do //find power of 2 less than x { x>>=1; x96+=1; } while(x>0); double dx1=1U<<x96; //same as 2^x96 -- must use 1U, not 1 x96 *= 96; // why the function has 96 in the name while(dx1<dx2) //finds log2 value within 96 times through loop { dx1 *= 1.00724641222370389809; //2^(1/96) x96+=1; } return x96; }

The implementation of log2 in the code is done as log10(x)/log10(2) where the value for log10(2) is stored as a constant in the code.If you want an even faster version use (int)(log10(x)*318.9050971+0.5)where 318.9050971 = 96 / log10(2)This averages 16.6 microseconds per call.

Started by kis17d General Help and Assistance on using CHDK stable releases

Started by Gran Canaria General Help and Assistance on using CHDK stable releases

Started by totalz General Discussion and Assistance

Started by trackuino LUA Scripting

Started by msl « 1 2 » General Discussion and Assistance