MISRA work on maths.ino

This commit is contained in:
Josh Stewart 2017-06-16 22:31:42 +10:00
parent 7fb7d83f5f
commit 8db54c9c22
1 changed files with 47 additions and 44 deletions

View File

@ -9,7 +9,7 @@ int fastMap(unsigned long x, int in_min, int in_max, int out_min, int out_max)
//return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; //return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
} }
//This is a dedicated function that specifically handles the case of mapping 0-1023 values into a 0 to X range //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) //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) //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 //removed ununsed variables, in_min and out_min is aways 0, in_max is aways 1023
@ -24,61 +24,63 @@ Ref: http://www.hackersdelight.org/divcMore.pdf
*/ */
//Unsigned divide by 10 //Unsigned divide by 10
unsigned int divu10(unsigned int n) { unsigned int divu10(unsigned int n)
unsigned long q, r; {
q = (n >> 1) + (n >> 2); unsigned long q, r;
q = q + (q >> 4); q = (n >> 1) + (n >> 2);
q = q + (q >> 8); q = q + (q >> 4);
q = q + (q >> 16); q = q + (q >> 8);
q = q >> 3; q = q + (q >> 16);
r = n - q*10; q = q >> 3;
return q + ((r + 6) >> 4); r = n - (q * 10);
// return q + (r > 9); return q + ((r + 6) >> 4);
} }
//Signed divide by 10 //Signed divide by 10
int divs10(long n) { int divs10(long n)
long q, r; {
n = n + (n>>31 & 9); long q, r, p;
q = (n >> 1) + (n >> 2); p = n + (n>>31 & 9);
q = q + (q >> 4); q = (p >> 1) + (p >> 2);
q = q + (q >> 8); q = q + (q >> 4);
q = q + (q >> 16); q = q + (q >> 8);
q = q >> 3; q = q + (q >> 16);
r = n - q*10; q = q >> 3;
return q + ((r + 6) >> 4); r = p - (q * 10);
// return q + (r > 9); return q + ((r + 6) >> 4);
} }
//Signed divide by 100 //Signed divide by 100
int divs100(long n) { int divs100(long n)
{
return (n / 100); // Amazingly, gcc is producing a better /divide by 100 function than this return (n / 100); // Amazingly, gcc is producing a better /divide by 100 function than this
long q, r; /*
n = n + (n>>31 & 99); long q, r;
q = (n >> 1) + (n >> 3) + (n >> 6) - (n >> 10) + n = n + (n>>31 & 99);
(n >> 12) + (n >> 13) - (n >> 16); q = (n >> 1) + (n >> 3) + (n >> 6) - (n >> 10) +
q = q + (q >> 20); (n >> 12) + (n >> 13) - (n >> 16);
q = q >> 6; q = q + (q >> 20);
r = n - q*100; q = q >> 6;
return q + ((r + 28) >> 7); r = n - q*100;
// return q + (r > 99); return q + ((r + 28) >> 7);
*/
} }
//Unsigned divide by 100 //Unsigned divide by 100
unsigned long divu100(unsigned long n) { unsigned long divu100(unsigned long n)
//return (n / 100); // No difference with this on/off {
unsigned long q, r; //return (n / 100);
q = (n >> 1) + (n >> 3) + (n >> 6) - (n >> 10) + unsigned long q, r;
(n >> 12) + (n >> 13) - (n >> 16); q = (n >> 1) + (n >> 3) + (n >> 6) - (n >> 10) +
q = q + (q >> 20); (n >> 12) + (n >> 13) - (n >> 16);
q = q >> 6; q = q + (q >> 20);
r = n - q*100; q = q >> 6;
return q + ((r + 28) >> 7); r = n - (q * 100);
// return q + (r > 99); return q + ((r + 28) >> 7);
} }
//Return x percent of y //Return x percent of y
//This is a relatively fast approximation of a percentage value. //This is a relatively fast approximation of a percentage value.
unsigned long percentage(byte x, unsigned long y) unsigned long percentage(byte x, unsigned long y)
{ {
return (y * x) / 100; //For some reason this is faster return (y * x) / 100; //For some reason this is faster
@ -91,7 +93,8 @@ unsigned long percentage(byte x, unsigned long y)
inline long powint(int factor, unsigned int exponent) inline long powint(int factor, unsigned int exponent)
{ {
long product = 1; long product = 1;
while (exponent--) unsigned int counter = exponent;
while ( (counter--) > 0)
product *= factor; product *= factor;
return product; return product;
} }