aboutsummaryrefslogtreecommitdiff
path: root/src/misc.c
diff options
context:
space:
mode:
authortlatorre <tlatorre@uchicago.edu>2019-06-02 13:28:36 -0400
committertlatorre <tlatorre@uchicago.edu>2019-06-02 13:28:36 -0400
commit98c663f379d5acec1656924e26cd3e5c9c9984d5 (patch)
treeece8d4ce1eefc442ebe256345301b756fdd5eb68 /src/misc.c
parent2f2cd1797190335468e956f4c286c6e4de27518e (diff)
downloadsddm-98c663f379d5acec1656924e26cd3e5c9c9984d5.tar.gz
sddm-98c663f379d5acec1656924e26cd3e5c9c9984d5.tar.bz2
sddm-98c663f379d5acec1656924e26cd3e5c9c9984d5.zip
add a fast sqrt function for values in between 0 and 1
Diffstat (limited to 'src/misc.c')
-rw-r--r--src/misc.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/misc.c b/src/misc.c
index f705c74..003b170 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -881,3 +881,25 @@ double fast_acos(double x)
return interp1d(x,xs,ys,LEN(xs));
}
+
+/* Fast version of sqrt() for `x` between 0 and 1 which uses a lookup table
+ * computed on the first call. */
+double fast_sqrt(double x)
+{
+ size_t i;
+ static int initialized = 0;
+ static double xs[N_SQRT];
+ static double ys[N_SQRT];
+
+ if (!initialized) {
+ for (i = 0; i < LEN(xs); i++) {
+ xs[i] = 1.0*i/(LEN(xs)-1);
+ ys[i] = sqrt(xs[i]);
+ }
+ initialized = 1;
+ }
+
+ if (x > 1.0) return sqrt(x);
+
+ return interp1d(x,xs,ys,LEN(xs));
+}