]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - mood.c
mood.c: Fix documentation of int_sqrt().
[paraslash.git] / mood.c
diff --git a/mood.c b/mood.c
index 315ef0cbdc61b7de1a3bbbd8f8683d7ab81d9c81..bf6b3c9c9992afd04b791af9c281eaf417fc376f 100644 (file)
--- a/mood.c
+++ b/mood.c
@@ -84,12 +84,46 @@ struct mood {
  */
 static struct mood *current_mood;
 
-/**
- * Rough approximation to sqrt.
+/*
+ * Find the position of the most-significant set bit.
  *
- * \param x Integer of which to calculate the sqrt.
+ * Copied and slightly adapted from the linux source tree, version 4.9.39
+ * (2017-07).
+ */
+__a_const static uint32_t fls64(uint64_t v)
+{
+       int n = 63;
+       const uint64_t ones = ~(uint64_t)0U;
+
+       if ((v & (ones << 32)) == 0) {
+               n -= 32;
+               v <<= 32;
+       }
+       if ((v & (ones << (64 - 16))) == 0) {
+               n -= 16;
+               v <<= 16;
+       }
+       if ((v & (ones << (64 - 8))) == 0) {
+               n -= 8;
+               v <<= 8;
+       }
+       if ((v & (ones << (64 - 4))) == 0) {
+               n -= 4;
+               v <<= 4;
+       }
+       if ((v & (ones << (64 - 2))) == 0) {
+               n -= 2;
+               v <<= 2;
+       }
+       if ((v & (ones << (64 - 1))) == 0)
+               n -= 1;
+       return n;
+}
+
+/*
+ * Compute the integer square root floor(sqrt(x)).
  *
- * \return An integer res with res * res <= x.
+ * Taken 2007 from the linux source tree.
  */
 __a_const static uint64_t int_sqrt(uint64_t x)
 {
@@ -97,10 +131,7 @@ __a_const static uint64_t int_sqrt(uint64_t x)
        op = x;
        res = 0;
 
-       one = one << 62;
-       while (one > op)
-               one >>= 2;
-
+       one = one << (fls64(x) & ~one);
        while (one != 0) {
                if (op >= res + one) {
                        op = op - (res + one);