int_sqrt.c 533 B

1234567891011121314151617181920212223242526272829303132
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. /**
  4. * int_sqrt - rough approximation to sqrt
  5. * @x: integer of which to calculate the sqrt
  6. *
  7. * A very rough approximation to the sqrt() function.
  8. */
  9. unsigned long int_sqrt(unsigned long x)
  10. {
  11. unsigned long op, res, one;
  12. op = x;
  13. res = 0;
  14. one = 1UL << (BITS_PER_LONG - 2);
  15. while (one > op)
  16. one >>= 2;
  17. while (one != 0) {
  18. if (op >= res + one) {
  19. op = op - (res + one);
  20. res = res + 2 * one;
  21. }
  22. res /= 2;
  23. one /= 4;
  24. }
  25. return res;
  26. }
  27. EXPORT_SYMBOL(int_sqrt);