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);
--
| Ingo Molnar | Re: containers (was Re: -mm merge plans for 2.6.23) |
| Greg Kroah-Hartman | [PATCH 009/196] Chinese: add translation of sparse.txt |
| holzheu | Re: [RFC/PATCH] Documentation of kernel messages |
| Vladislav Bolkhovitin | Re: Integration of SCST in the mainstream Linux kernel |
git: | |
| Jarek Poplawski | [PATCH] pkt_sched: Destroy gen estimators under rtnl_lock(). |
| Gerrit Renker | [PATCH 27/37] dccp: Integration of dynamic feature activation - part 2 (server side) |
| David Miller | [GIT]: Networking |
| Antonio Almeida | HTB accuracy for high speed |
