//This is a dedicated function that specifically handles the case of mapping 0-1023 values into a 0 to X range
//This is a common case because it means converting from a standard 10-bit analog input to a byte or 10-bit analog into 0-511 (Eg the temperature readings)
//int fastMap1023toX(unsigned long x, int in_min, int in_max, int out_min, int out_max)
//removed ununsed variables, in_min and out_min is aways 0, in_max is aways 1023
intfastMap1023toX(unsignedlongx,intout_max)
{
return(x*out_max)>>10;
}
/*
Thefollowingareallfastversionsofspecificdivisions
Ref:http://www.hackersdelight.org/divcMore.pdf
*/
//Unsigned divide by 10
unsignedintdivu10(unsignedintn){
unsignedlongq,r;
q=(n>>1)+(n>>2);
q=q+(q>>4);
q=q+(q>>8);
q=q+(q>>16);
q=q>>3;
r=n-q*10;
returnq+((r+6)>>4);
// return q + (r > 9);
}
//Signed divide by 10
intdivs10(longn){
longq,r;
n=n+(n>>31&9);
q=(n>>1)+(n>>2);
q=q+(q>>4);
q=q+(q>>8);
q=q+(q>>16);
q=q>>3;
r=n-q*10;
returnq+((r+6)>>4);
// return q + (r > 9);
}
//Signed divide by 100
intdivs100(longn){
return(n/100);// Amazingly, gcc is producing a better /divide by 100 function than this
longq,r;
n=n+(n>>31&99);
q=(n>>1)+(n>>3)+(n>>6)-(n>>10)+
(n>>12)+(n>>13)-(n>>16);
q=q+(q>>20);
q=q>>6;
r=n-q*100;
returnq+((r+28)>>7);
// return q + (r > 99);
}
//Unsigned divide by 100
unsignedlongdivu100(unsignedlongn){
//return (n / 100); // No difference with this on/off
unsignedlongq,r;
q=(n>>1)+(n>>3)+(n>>6)-(n>>10)+
(n>>12)+(n>>13)-(n>>16);
q=q+(q>>20);
q=q>>6;
r=n-q*100;
returnq+((r+28)>>7);
// return q + (r > 99);
}
//Return x percent of y
//This is a relatively fast approximation of a percentage value.