Back to home page

LXR

 
 

    


0001 /*
0002  * Copyright (C) 2013 Davidlohr Bueso <davidlohr.bueso@hp.com>
0003  *
0004  *  Based on the shift-and-subtract algorithm for computing integer
0005  *  square root from Guy L. Steele.
0006  */
0007 
0008 #include <linux/kernel.h>
0009 #include <linux/export.h>
0010 
0011 /**
0012  * int_sqrt - rough approximation to sqrt
0013  * @x: integer of which to calculate the sqrt
0014  *
0015  * A very rough approximation to the sqrt() function.
0016  */
0017 unsigned long int_sqrt(unsigned long x)
0018 {
0019     unsigned long b, m, y = 0;
0020 
0021     if (x <= 1)
0022         return x;
0023 
0024     m = 1UL << (BITS_PER_LONG - 2);
0025     while (m != 0) {
0026         b = y + m;
0027         y >>= 1;
0028 
0029         if (x >= b) {
0030             x -= b;
0031             y += m;
0032         }
0033         m >>= 2;
0034     }
0035 
0036     return y;
0037 }
0038 EXPORT_SYMBOL(int_sqrt);