Blame view
lib/int_sqrt.c
652 Bytes
30493cc9d
|
1 2 3 4 5 6 |
/* * 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. */ |
1da177e4c
|
7 8 |
#include <linux/kernel.h> |
8bc3bcc93
|
9 |
#include <linux/export.h> |
1da177e4c
|
10 11 12 13 14 15 16 17 18 |
/** * 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) { |
30493cc9d
|
19 |
unsigned long b, m, y = 0; |
1da177e4c
|
20 |
|
30493cc9d
|
21 22 |
if (x <= 1) return x; |
1da177e4c
|
23 |
|
30493cc9d
|
24 25 26 27 |
m = 1UL << (BITS_PER_LONG - 2); while (m != 0) { b = y + m; y >>= 1; |
1da177e4c
|
28 |
|
30493cc9d
|
29 30 31 |
if (x >= b) { x -= b; y += m; |
1da177e4c
|
32 |
} |
30493cc9d
|
33 |
m >>= 2; |
1da177e4c
|
34 |
} |
30493cc9d
|
35 36 |
return y; |
1da177e4c
|
37 38 |
} EXPORT_SYMBOL(int_sqrt); |