2013-09-23 05:23:34 -07:00
|
|
|
|
2014-05-08 06:01:36 -07:00
|
|
|
//Replace the standard arduino map() function to use the div function instead
|
2015-01-28 00:31:09 -08:00
|
|
|
int fastMap(int x, int in_min, int in_max, int out_min, int out_max)
|
2013-09-23 05:23:34 -07:00
|
|
|
{
|
2015-01-28 00:31:09 -08:00
|
|
|
return div( ((x - in_min) * (out_max - out_min)) , (in_max - in_min) ).quot + out_min;
|
2013-09-23 05:23:34 -07:00
|
|
|
}
|
|
|
|
|
2015-01-28 00:31:09 -08:00
|
|
|
/*
|
|
|
|
The following are all fast versions of specific divisions
|
2015-01-30 04:51:01 -08:00
|
|
|
Ref: http://www.hackersdelight.org/divcMore.pdf
|
2015-01-28 00:31:09 -08:00
|
|
|
*/
|
|
|
|
|
|
|
|
//Unsigned divide by 10
|
2015-02-03 16:52:01 -08:00
|
|
|
unsigned int divu10(unsigned int n) {
|
|
|
|
unsigned int q, r;
|
2015-01-28 00:31:09 -08:00
|
|
|
q = (n >> 1) + (n >> 2);
|
|
|
|
q = q + (q >> 4);
|
|
|
|
q = q + (q >> 8);
|
|
|
|
q = q + (q >> 16);
|
|
|
|
q = q >> 3;
|
|
|
|
r = n - q*10;
|
|
|
|
return q + ((r + 6) >> 4);
|
|
|
|
// return q + (r > 9);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Signed divide by 10
|
|
|
|
int divs10(int n) {
|
|
|
|
int q, 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;
|
|
|
|
return q + ((r + 6) >> 4);
|
|
|
|
// return q + (r > 9);
|
|
|
|
}
|
2015-02-03 16:52:01 -08:00
|
|
|
|
|
|
|
//Signed divide by 100
|
|
|
|
int divs100(int n) {
|
|
|
|
int q, 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;
|
|
|
|
return q + ((r + 28) >> 7);
|
|
|
|
// return q + (r > 99);
|
|
|
|
}
|