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

@ -24,35 +24,37 @@ Ref: http://www.hackersdelight.org/divcMore.pdf
*/
//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);
q = q + (q >> 4);
q = q + (q >> 8);
q = q + (q >> 16);
q = q >> 3;
r = n - q*10;
r = n - (q * 10);
return q + ((r + 6) >> 4);
// return q + (r > 9);
}
//Signed divide by 10
int divs10(long n) {
long q, r;
n = n + (n>>31 & 9);
q = (n >> 1) + (n >> 2);
int divs10(long n)
{
long q, r, p;
p = n + (n>>31 & 9);
q = (p >> 1) + (p >> 2);
q = q + (q >> 4);
q = q + (q >> 8);
q = q + (q >> 16);
q = q >> 3;
r = n - q*10;
r = p - (q * 10);
return q + ((r + 6) >> 4);
// return q + (r > 9);
}
//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
/*
long q, r;
n = n + (n>>31 & 99);
q = (n >> 1) + (n >> 3) + (n >> 6) - (n >> 10) +
@ -61,20 +63,20 @@ int divs100(long n) {
q = q >> 6;
r = n - q*100;
return q + ((r + 28) >> 7);
// return q + (r > 99);
*/
}
//Unsigned divide by 100
unsigned long divu100(unsigned long n) {
//return (n / 100); // No difference with this on/off
unsigned long divu100(unsigned long n)
{
//return (n / 100);
unsigned long q, 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;
r = n - (q * 100);
return q + ((r + 28) >> 7);
// return q + (r > 99);
}
//Return x percent of y
@ -91,7 +93,8 @@ unsigned long percentage(byte x, unsigned long y)
inline long powint(int factor, unsigned int exponent)
{
long product = 1;
while (exponent--)
unsigned int counter = exponent;
while ( (counter--) > 0)
product *= factor;
return product;
}