diff options
author | quou <quou@disroot.org> | 2024-09-30 22:12:53 +1000 |
---|---|---|
committer | quou <quou@disroot.org> | 2024-09-30 22:13:01 +1000 |
commit | de9a4fd93343d4654dfe7e2a566704644a9f306f (patch) | |
tree | 5c666430b8cd189c9a48c303dbe018e4cc69cb15 | |
parent | 3aa1183d3f9032d1d17c295e3f3a1dbeab57ab66 (diff) |
square root
-rw-r--r-- | maths.c | 19 | ||||
-rw-r--r-- | maths.h | 1 |
2 files changed, 20 insertions, 0 deletions
@@ -34,3 +34,22 @@ void init_maths(void) { cos_table[i] = sin_table[(i + hs) & sin_table_mask]; } } + +int fxsqrt(int n) { + int lo, hi, mid, i, mm; + if (n <= 0) { return -1; } + lo = mini(1, n); + hi = maxi(1, n); + while ((100 << fbits * lo * lo) >> fbits * 2) + lo *= (10 << fbits) >> fbits; + while ((5 * hi * hi) >> fbits * 2 > n) + hi *= (1 << fbits) / 10; + for (i = 0; i < 100; i++) { + mid = ((lo + hi) * (1 << fbits)) / (2 << fbits); + mm = mid * mid >> fbits; + if (mm == n) return mid; + if (mm > n) hi = mid; + else lo = mid; + } + return mid; +} @@ -18,5 +18,6 @@ extern int sin_table[sin_table_count]; extern int cos_table[sin_table_count]; void init_maths(void); +int fxsqrt(int n); #endif |