M7350/kernel/lib/int_sqrt.c

39 lines
652 B
C
Raw Permalink Normal View History

2024-09-09 08:57:42 +00:00
/*
* Copyright (C) 2013 Davidlohr Bueso <davidlohr.bueso@hp.com>
*
* Based on the shift-and-subtract algorithm for computing integer
* square root from Guy L. Steele.
*/
2024-09-09 08:52:07 +00:00
#include <linux/kernel.h>
#include <linux/export.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)
{
2024-09-09 08:57:42 +00:00
unsigned long b, m, y = 0;
2024-09-09 08:52:07 +00:00
2024-09-09 08:57:42 +00:00
if (x <= 1)
return x;
2024-09-09 08:52:07 +00:00
2024-09-09 08:57:42 +00:00
m = 1UL << (BITS_PER_LONG - 2);
while (m != 0) {
b = y + m;
y >>= 1;
2024-09-09 08:52:07 +00:00
2024-09-09 08:57:42 +00:00
if (x >= b) {
x -= b;
y += m;
2024-09-09 08:52:07 +00:00
}
2024-09-09 08:57:42 +00:00
m >>= 2;
2024-09-09 08:52:07 +00:00
}
2024-09-09 08:57:42 +00:00
return y;
2024-09-09 08:52:07 +00:00
}
EXPORT_SYMBOL(int_sqrt);