diff options
| author | ramgear <ramgear@gmail.com> | 2013-09-03 14:25:04 +0700 |
|---|---|---|
| committer | Moyster <oysterized@gmail.com> | 2016-09-10 12:06:54 +0200 |
| commit | 3c3c4a97a2e92d835a2eb00b380ae3811c4be1a5 (patch) | |
| tree | da72358409f19ea68f0d79b8232bb9a46e8e1bea /lib | |
| parent | 5dd114b6780be9d616147d9999e431011795bdad (diff) | |
int_sqrt: correction square root algo with naming
Signed-off-by: engstk <eng.stk@sapo.pt>
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/int_sqrt.c | 30 |
1 files changed, 20 insertions, 10 deletions
diff --git a/lib/int_sqrt.c b/lib/int_sqrt.c index 1ef4cc344..0814f0762 100644 --- a/lib/int_sqrt.c +++ b/lib/int_sqrt.c @@ -16,23 +16,33 @@ */ unsigned long int_sqrt(unsigned long x) { - unsigned long b, m, y = 0; + unsigned long tmp; + unsigned long place; + unsigned long root; + unsigned long remainder; if (x <= 1) return x; - m = 1UL << (BITS_PER_LONG - 2); - while (m != 0) { - b = y + m; - y >>= 1; + root = 0; + remainder = x; + place = 1UL << (BITS_PER_LONG - 2); + + while (place > remainder) + place >>= 2; - if (x >= b) { - x -= b; - y += m; + while (place != 0) { + tmp = root + place; + + if (remainder >= tmp) + { + remainder -= tmp; + root += (place << 1); } - m >>= 2; + root >>= 1; + place >>= 2; } - return y; + return root; } EXPORT_SYMBOL(int_sqrt); |
