You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

isqrt.c 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. /** @file
  21. *
  22. * Integer square root
  23. *
  24. */
  25. #include <ipxe/isqrt.h>
  26. /**
  27. * Find integer square root
  28. *
  29. * @v value Value
  30. * @v isqrt Integer square root of value
  31. */
  32. unsigned long isqrt ( unsigned long value ) {
  33. unsigned long result = 0;
  34. unsigned long bit = ( 1UL << ( ( 8 * sizeof ( bit ) ) - 2 ) );
  35. while ( bit > value )
  36. bit >>= 2;
  37. while ( bit ) {
  38. if ( value >= ( result + bit ) ) {
  39. value -= ( result + bit );
  40. result = ( ( result >> 1 ) + bit );
  41. } else {
  42. result >>= 1;
  43. }
  44. bit >>= 2;
  45. }
  46. return result;
  47. }