aboutsummaryrefslogtreecommitdiff
path: root/usr/math.c
blob: 4d5048754a8aa8e3bd151ad8719edce4e82089dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
unsigned long sqrt_rnd(unsigned long square)
{
	unsigned long op = square;
	unsigned long res = 0;
	unsigned long one = 1uL << 30;

	while (one > op)
		one >>= 2;

	while (one != 0)
	{
		if (op >= res + one)
		{
			op = op - (res + one);
			res = res + 2 * one;
		}
		res >>= 1;
		one >>= 2;
	}

	if (one > res)
		res++;

	return res;
}