public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Soumyadip Das Mahapatra <soumya.linux@yahoo.com>
To: linux-kernel@vger.kernel.org
Subject: [PATCH] : A better approach to compute int_sqrt in lib/int_sqrt.c
Date: Wed, 16 Jul 2008 13:19:31 -0700 (PDT)	[thread overview]
Message-ID: <551692.52350.qm@web59507.mail.ac4.yahoo.com> (raw)

Hello everybody !!
      The patch below is what i think is a better approach to
compute int_sqrt().

What about it ?

Thanks !!

---
--- a/lib/int_sqrt.c    2008-04-17 08:19:44.000000000 +0530
+++ b/lib/int_sqrt.c    2008-07-02 11:37:01.000000000 +0530
@@ -1,4 +1,3 @@
-
 #include <linux/kernel.h>
 #include <linux/module.h>
 
@@ -7,26 +6,21 @@
  * @x: integer of which to calculate the sqrt
  *
  * A very rough approximation to the sqrt() function.
+ * Improved version from the previous one.
  */
 unsigned long int_sqrt(unsigned long x)
 {
-    unsigned long op, res, one;
-
-    op = x;
-    res = 0;
-
-    one = 1UL << (BITS_PER_LONG - 2);
-    while (one > op)
-        one >>= 2;
-
-    while (one != 0) {
-        if (op >= res + one) {
-            op = op - (res + one);
-            res = res +  2 * one;
-        }
-        res /= 2;
-        one /= 4;
-    }
-    return res;
+    unsigned long ub, lb, m;
+    lb = 1;                /* lower bound */
+    ub = (x >> 5) + 8;        /* upper bound */
+    do {
+        m = (ub + lb) >>  1;    /* middle value */
+        if((m * m) > x)
+            ub = m - 1;
+        else
+            lb = m + 1;
+    } while(ub >= lb);
+    
+    return lb - 1;
 }
 EXPORT_SYMBOL(int_sqrt);



      


             reply	other threads:[~2008-07-16 20:25 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-07-16 20:19 Soumyadip Das Mahapatra [this message]
2008-07-16 20:51 ` [PATCH] : A better approach to compute int_sqrt in lib/int_sqrt.c Peter Zijlstra
  -- strict thread matches above, loose matches on Subject: below --
2008-07-16 21:35 Soumyadip Das Mahapatra
2008-07-16 22:05 ` Lennart Sorensen
2008-07-17 18:00   ` Lennart Sorensen
2008-07-17  4:08 ` Vadim Lobanov
2008-07-17 18:10 ` Lennart Sorensen
2008-07-17 18:26 ` Peter Zijlstra

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=551692.52350.qm@web59507.mail.ac4.yahoo.com \
    --to=soumya.linux@yahoo.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox