mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2024-08-29 20:30:35 +00:00
![Peter Williams](/assets/img/avatar_default.png)
The implementation of int_sqrt() assumes that longs have 32 bits. On systems that have 64 bit longs this will result in gross errors when the argument to the function is greater than 2^32 - 1 on such systems. I doubt whether any such use is currently made of int_sqrt() but the attached patch fixes the problem anyway. Signed-off-by: Peter Williams <pwil3058@bigpond.com.au> Cc: Dave Jones <davej@codemonkey.org.uk> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
32 lines
533 B
C
32 lines
533 B
C
|
|
#include <linux/kernel.h>
|
|
#include <linux/module.h>
|
|
|
|
/**
|
|
* int_sqrt - rough approximation to sqrt
|
|
* @x: integer of which to calculate the sqrt
|
|
*
|
|
* A very rough approximation to the sqrt() function.
|
|
*/
|
|
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;
|
|
}
|
|
EXPORT_SYMBOL(int_sqrt);
|