aboutsummaryrefslogtreecommitdiff
path: root/usr/math.c
diff options
context:
space:
mode:
authorChristian Cunningham <cc@localhost>2022-03-28 11:10:07 -0700
committerChristian Cunningham <cc@localhost>2022-03-28 11:10:07 -0700
commit79fa96514f59e8999bbb74d66313ca13c1b3572a (patch)
tree5adcdd5665e763880f71ecbf9d381014eb2c486e /usr/math.c
parent3e7833e4017c52ca54e6c8311d1aeb793796986c (diff)
Output s rather than s^2
Diffstat (limited to 'usr/math.c')
-rw-r--r--usr/math.c25
1 files changed, 25 insertions, 0 deletions
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;
+}