diff options
author | Christian Cunningham <cc@localhost> | 2022-03-28 11:10:07 -0700 |
---|---|---|
committer | Christian Cunningham <cc@localhost> | 2022-03-28 11:10:07 -0700 |
commit | 79fa96514f59e8999bbb74d66313ca13c1b3572a (patch) | |
tree | 5adcdd5665e763880f71ecbf9d381014eb2c486e | |
parent | 3e7833e4017c52ca54e6c8311d1aeb793796986c (diff) |
Output s rather than s^2
-rw-r--r-- | include/usr/math.h | 6 | ||||
-rw-r--r-- | usr/math.c | 25 | ||||
-rw-r--r-- | usr/string.c | 2 | ||||
-rw-r--r-- | usr/test.c | 11 |
4 files changed, 39 insertions, 5 deletions
diff --git a/include/usr/math.h b/include/usr/math.h new file mode 100644 index 0000000..d3f9591 --- /dev/null +++ b/include/usr/math.h @@ -0,0 +1,6 @@ +#ifndef USR_MATH_H +#define USR_MATH_H + +unsigned long sqrt_rnd(unsigned long square); + +#endif diff --git a/usr/math.c b/usr/math.c new file mode 100644 index 0000000..4d50487 --- /dev/null +++ b/usr/math.c @@ -0,0 +1,25 @@ +unsigned long sqrt_rnd(unsigned long square) +{ + unsigned long op = square; + unsigned long res = 0; + unsigned long one = 1uL << 30; + + while (one > op) + one >>= 2; + + while (one != 0) + { + if (op >= res + one) + { + op = op - (res + one); + res = res + 2 * one; + } + res >>= 1; + one >>= 2; + } + + if (one > res) + res++; + + return res; +} diff --git a/usr/string.c b/usr/string.c index 8c94900..6129d38 100644 --- a/usr/string.c +++ b/usr/string.c @@ -10,7 +10,7 @@ char* ulong_to_string(unsigned long value, char* data) if (t==0) break; dptr -= 1; - if (i == 5) { + if (i == 2) { *dptr = '.'; dptr -= 1; } @@ -1,6 +1,7 @@ #include <cpu.h> #include <graphics/lfb.h> #include <sys/schedule.h> +#include <usr/math.h> #include <usr/string.h> #include <util/mutex.h> @@ -11,6 +12,7 @@ static unsigned long ti, tf; static unsigned long times[MAX_ITER]; static unsigned long idx = 0; + void test_results(unsigned long off) { unsigned long mean=0, stdev=0, max=0; @@ -24,14 +26,15 @@ void test_results(unsigned long off) unsigned long term = (times[i]-mean)*(times[i]-mean)/MAX_ITER; stdev += term; } - static char str[13]; + stdev = sqrt_rnd(stdev); + char str[] = " ns\0"; char* start; start = ulong_to_string(mean, str); - draw_string(off*13, 12, start); + draw_string(off*15, 12, start); start = ulong_to_string(stdev, str); - draw_string(off*13, 13, start); + draw_string(off*15, 13, start); start = ulong_to_string(max, str); - draw_string(off*13, 14, start); + draw_string(off*15, 14, start); } void nopfxn(void) {} |