All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/1] int_sqrt() adjustments
@ 2019-01-27 17:06 Florian La Roche
  2019-01-27 17:31 ` Linus Torvalds
  0 siblings, 1 reply; 2+ messages in thread
From: Florian La Roche @ 2019-01-27 17:06 UTC (permalink / raw)
  To: linux-kernel
  Cc: Crt Mori, Joe Perches, Davidlohr Bueso, Will Deacon,
	Peter Zijlstra, Linus Torvalds, Florian La Roche

From: Florian La Roche <Florian.LaRoche@gmail.com>

int_sqrt() and int_sqrt64():
Add __attribute_const__ and adjust to use (signed) int for the right param of "<<".

Signed-off-by: Florian La Roche <Florian.LaRoche@gmail.com>
---
 include/linux/kernel.h | 4 ++--
 lib/int_sqrt.c         | 8 ++++----
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index 8f0e68e250a7..df03a3cca309 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -513,10 +513,10 @@ extern int __kernel_text_address(unsigned long addr);
 extern int kernel_text_address(unsigned long addr);
 extern int func_ptr_is_kernel_text(void *ptr);
 
-unsigned long int_sqrt(unsigned long);
+__attribute_const__ unsigned long int_sqrt(unsigned long);
 
 #if BITS_PER_LONG < 64
-u32 int_sqrt64(u64 x);
+__attribute_const__ u32 int_sqrt64(u64 x);
 #else
 static inline u32 int_sqrt64(u64 x)
 {
diff --git a/lib/int_sqrt.c b/lib/int_sqrt.c
index 30e0f9770f88..75d930dd1802 100644
--- a/lib/int_sqrt.c
+++ b/lib/int_sqrt.c
@@ -16,14 +16,14 @@
  *
  * Computes: floor(sqrt(x))
  */
-unsigned long int_sqrt(unsigned long x)
+__attribute_const__ unsigned long int_sqrt(unsigned long x)
 {
 	unsigned long b, m, y = 0;
 
 	if (x <= 1)
 		return x;
 
-	m = 1UL << (__fls(x) & ~1UL);
+	m = 1UL << ((int)__fls(x) & ~1);
 	while (m != 0) {
 		b = y + m;
 		y >>= 1;
@@ -45,14 +45,14 @@ EXPORT_SYMBOL(int_sqrt);
  * is expected.
  * @x: 64bit integer of which to calculate the sqrt
  */
-u32 int_sqrt64(u64 x)
+__attribute_const__ u32 int_sqrt64(u64 x)
 {
 	u64 b, m, y = 0;
 
 	if (x <= ULONG_MAX)
 		return int_sqrt((unsigned long) x);
 
-	m = 1ULL << ((fls64(x) - 1) & ~1ULL);
+	m = 1ULL << ((fls64(x) - 1) & ~1);
 	while (m != 0) {
 		b = y + m;
 		y >>= 1;
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH 1/1] int_sqrt() adjustments
  2019-01-27 17:06 [PATCH 1/1] int_sqrt() adjustments Florian La Roche
@ 2019-01-27 17:31 ` Linus Torvalds
  0 siblings, 0 replies; 2+ messages in thread
From: Linus Torvalds @ 2019-01-27 17:31 UTC (permalink / raw)
  To: Florian La Roche
  Cc: Linux List Kernel Mailing, Crt Mori, Joe Perches,
	Davidlohr Bueso, Will Deacon, Peter Zijlstra, Florian La Roche

On Sun, Jan 27, 2019 at 9:06 AM Florian La Roche
<florian.laroche@googlemail.com> wrote:
>
>
> -       m = 1UL << (__fls(x) & ~1UL);
> +       m = 1UL << ((int)__fls(x) & ~1);

No need to add the cast. It doesn't do anything and just makes the code uglier.

The type of a shift operator is the type of the left side of the shift
(with the usual int-promotion).

The type of the right side simply doesn't matter (the *value* does, of
course, but because the value range is limited, the cast is just
noise).

                Linus

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-01-27 17:32 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-01-27 17:06 [PATCH 1/1] int_sqrt() adjustments Florian La Roche
2019-01-27 17:31 ` Linus Torvalds

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.