blob: 1ef4cc344977cda2886ec1033a0868cec4a8ec41 [file] [log] [blame]
Davidlohr Bueso30493cc2013-04-29 16:18:09 -07001/*
2 * Copyright (C) 2013 Davidlohr Bueso <davidlohr.bueso@hp.com>
3 *
4 * Based on the shift-and-subtract algorithm for computing integer
5 * square root from Guy L. Steele.
6 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007
8#include <linux/kernel.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -05009#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
11/**
12 * int_sqrt - rough approximation to sqrt
13 * @x: integer of which to calculate the sqrt
14 *
15 * A very rough approximation to the sqrt() function.
16 */
17unsigned long int_sqrt(unsigned long x)
18{
Davidlohr Bueso30493cc2013-04-29 16:18:09 -070019 unsigned long b, m, y = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Davidlohr Bueso30493cc2013-04-29 16:18:09 -070021 if (x <= 1)
22 return x;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Davidlohr Bueso30493cc2013-04-29 16:18:09 -070024 m = 1UL << (BITS_PER_LONG - 2);
25 while (m != 0) {
26 b = y + m;
27 y >>= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Davidlohr Bueso30493cc2013-04-29 16:18:09 -070029 if (x >= b) {
30 x -= b;
31 y += m;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 }
Davidlohr Bueso30493cc2013-04-29 16:18:09 -070033 m >>= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 }
Davidlohr Bueso30493cc2013-04-29 16:18:09 -070035
36 return y;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037}
38EXPORT_SYMBOL(int_sqrt);