diff options
author | Crt Mori <cmo@melexis.com> | 2018-01-11 05:19:57 -0500 |
---|---|---|
committer | Jonathan Cameron <Jonathan.Cameron@huawei.com> | 2018-02-04 05:17:21 -0500 |
commit | 47a361634821dc66cefbfa70b9d10a91269d7f7d (patch) | |
tree | ceba4c0edea64792e26e4d1a2363b64e374fa5cd | |
parent | 213451076bd370e55a70ff07f6575b1451ba1a9f (diff) |
lib: Add strongly typed 64bit int_sqrt
There is no option to perform 64bit integer sqrt on 32bit platform.
Added stronger typed int_sqrt64 enables the 64bit calculations to
be performed on 32bit platforms. Using same algorithm as int_sqrt()
with strong typing provides enough precision also on 32bit platforms,
but it sacrifices some performance. In case values are smaller than
ULONG_MAX the standard int_sqrt is used for calculation to maximize the
performance due to more native calculations.
Signed-off-by: Crt Mori <cmo@melexis.com>
Acked-by: Joe Perches <joe@perches.com>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
-rw-r--r-- | include/linux/kernel.h | 9 | ||||
-rw-r--r-- | lib/int_sqrt.c | 30 |
2 files changed, 39 insertions, 0 deletions
diff --git a/include/linux/kernel.h b/include/linux/kernel.h index ce51455e2adf..2da80e079d56 100644 --- a/include/linux/kernel.h +++ b/include/linux/kernel.h | |||
@@ -479,6 +479,15 @@ extern int func_ptr_is_kernel_text(void *ptr); | |||
479 | 479 | ||
480 | unsigned long int_sqrt(unsigned long); | 480 | unsigned long int_sqrt(unsigned long); |
481 | 481 | ||
482 | #if BITS_PER_LONG < 64 | ||
483 | u32 int_sqrt64(u64 x); | ||
484 | #else | ||
485 | static inline u32 int_sqrt64(u64 x) | ||
486 | { | ||
487 | return (u32)int_sqrt(x); | ||
488 | } | ||
489 | #endif | ||
490 | |||
482 | extern void bust_spinlocks(int yes); | 491 | extern void bust_spinlocks(int yes); |
483 | extern int oops_in_progress; /* If set, an oops, panic(), BUG() or die() is in progress */ | 492 | extern int oops_in_progress; /* If set, an oops, panic(), BUG() or die() is in progress */ |
484 | extern int panic_timeout; | 493 | extern int panic_timeout; |
diff --git a/lib/int_sqrt.c b/lib/int_sqrt.c index e2d329099bf7..14436f4ca6bd 100644 --- a/lib/int_sqrt.c +++ b/lib/int_sqrt.c | |||
@@ -38,3 +38,33 @@ unsigned long int_sqrt(unsigned long x) | |||
38 | return y; | 38 | return y; |
39 | } | 39 | } |
40 | EXPORT_SYMBOL(int_sqrt); | 40 | EXPORT_SYMBOL(int_sqrt); |
41 | |||
42 | #if BITS_PER_LONG < 64 | ||
43 | /** | ||
44 | * int_sqrt64 - strongly typed int_sqrt function when minimum 64 bit input | ||
45 | * is expected. | ||
46 | * @x: 64bit integer of which to calculate the sqrt | ||
47 | */ | ||
48 | u32 int_sqrt64(u64 x) | ||
49 | { | ||
50 | u64 b, m, y = 0; | ||
51 | |||
52 | if (x <= ULONG_MAX) | ||
53 | return int_sqrt((unsigned long) x); | ||
54 | |||
55 | m = 1ULL << (fls64(x) & ~1ULL); | ||
56 | while (m != 0) { | ||
57 | b = y + m; | ||
58 | y >>= 1; | ||
59 | |||
60 | if (x >= b) { | ||
61 | x -= b; | ||
62 | y += m; | ||
63 | } | ||
64 | m >>= 2; | ||
65 | } | ||
66 | |||
67 | return y; | ||
68 | } | ||
69 | EXPORT_SYMBOL(int_sqrt64); | ||
70 | #endif | ||