blob: a8170bb9142f396263a4164db4d83ba56665bdef [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Davidlohr Bueso30493cc2013-04-29 16:18:09 -07002/*
3 * Copyright (C) 2013 Davidlohr Bueso <davidlohr.bueso@hp.com>
4 *
5 * Based on the shift-and-subtract algorithm for computing integer
6 * square root from Guy L. Steele.
7 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -05009#include <linux/export.h>
Peter Zijlstraf8ae1072017-11-17 15:28:08 -080010#include <linux/bitops.h>
Andy Shevchenkoaa6159a2020-12-15 20:42:48 -080011#include <linux/limits.h>
12#include <linux/math.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
14/**
Peter Zijlstrae813a612017-11-17 15:28:12 -080015 * int_sqrt - computes the integer square root
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * @x: integer of which to calculate the sqrt
17 *
Peter Zijlstrae813a612017-11-17 15:28:12 -080018 * Computes: floor(sqrt(x))
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 */
20unsigned long int_sqrt(unsigned long x)
21{
Davidlohr Bueso30493cc2013-04-29 16:18:09 -070022 unsigned long b, m, y = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Davidlohr Bueso30493cc2013-04-29 16:18:09 -070024 if (x <= 1)
25 return x;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Peter Zijlstraf8ae1072017-11-17 15:28:08 -080027 m = 1UL << (__fls(x) & ~1UL);
Davidlohr Bueso30493cc2013-04-29 16:18:09 -070028 while (m != 0) {
29 b = y + m;
30 y >>= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Davidlohr Bueso30493cc2013-04-29 16:18:09 -070032 if (x >= b) {
33 x -= b;
34 y += m;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 }
Davidlohr Bueso30493cc2013-04-29 16:18:09 -070036 m >>= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 }
Davidlohr Bueso30493cc2013-04-29 16:18:09 -070038
39 return y;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040}
41EXPORT_SYMBOL(int_sqrt);
Crt Mori47a36162018-01-11 11:19:57 +010042
43#if BITS_PER_LONG < 64
44/**
45 * int_sqrt64 - strongly typed int_sqrt function when minimum 64 bit input
46 * is expected.
47 * @x: 64bit integer of which to calculate the sqrt
48 */
49u32 int_sqrt64(u64 x)
50{
51 u64 b, m, y = 0;
52
53 if (x <= ULONG_MAX)
54 return int_sqrt((unsigned long) x);
55
Florian La Rochefbfaf852019-01-19 16:14:50 +010056 m = 1ULL << ((fls64(x) - 1) & ~1ULL);
Crt Mori47a36162018-01-11 11:19:57 +010057 while (m != 0) {
58 b = y + m;
59 y >>= 1;
60
61 if (x >= b) {
62 x -= b;
63 y += m;
64 }
65 m >>= 2;
66 }
67
68 return y;
69}
70EXPORT_SYMBOL(int_sqrt64);
71#endif