linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* int_sqrt() adjustments
@ 2019-01-24 18:58 Florian La Roche
  2019-01-24 20:02 ` Linus Torvalds
  2019-01-26 16:18 ` Florian La Roche
  0 siblings, 2 replies; 4+ messages in thread
From: Florian La Roche @ 2019-01-24 18:58 UTC (permalink / raw)
  To: linux-kernel
  Cc: Crt Mori, Joe Perches, Davidlohr Bueso, Will Deacon,
	Peter Zijlstra, Linus Torvalds, Florian La Roche


__fls() is returning an unsigned long, but fls() and fls64() are
both returning a (signed) int.
As we need a signed int as right operand of "<<" (as Linus pointed out),
change __fls() to fls() for 32bit and also adjust masking the lowest bit
to be a signed int.
Now the 32bit and the 64bit version are again similar.

best regards,

Florian La Roche



Signed-off-by: Florian La Roche <Florian.LaRoche@googlemail.com>
---
 lib/int_sqrt.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/int_sqrt.c b/lib/int_sqrt.c
index 30e0f9770f88..66eb93105812 100644
--- a/lib/int_sqrt.c
+++ b/lib/int_sqrt.c
@@ -23,7 +23,7 @@ unsigned long int_sqrt(unsigned long x)
 	if (x <= 1)
 		return x;
 
-	m = 1UL << (__fls(x) & ~1UL);
+	m = 1UL << ((fls(x) - 1) & ~1);
 	while (m != 0) {
 		b = y + m;
 		y >>= 1;
@@ -52,7 +52,7 @@ u32 int_sqrt64(u64 x)
 	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] 4+ messages in thread

* Re: int_sqrt() adjustments
  2019-01-24 18:58 int_sqrt() adjustments Florian La Roche
@ 2019-01-24 20:02 ` Linus Torvalds
  2019-01-26 16:18 ` Florian La Roche
  1 sibling, 0 replies; 4+ messages in thread
From: Linus Torvalds @ 2019-01-24 20:02 UTC (permalink / raw)
  To: Florian La Roche
  Cc: Linux List Kernel Mailing, Crt Mori, Joe Perches,
	Davidlohr Bueso, Will Deacon, Peter Zijlstra

On Fri, Jan 25, 2019 at 7:58 AM Florian La Roche
<florian.laroche@googlemail.com> wrote:
>
> __fls() is returning an unsigned long, but fls() and fls64() are
> both returning a (signed) int.
> As we need a signed int as right operand of "<<" (as Linus pointed out),

It's not that the "signed" part is all that important, it's that using
another type is unnecessary and maybe misleading, when just the
default plain regular integer constant works right.

So I think this:

> -       m = 1UL << (__fls(x) & ~1UL);
> +       m = 1UL << ((fls(x) - 1) & ~1);
..
> -       m = 1ULL << ((fls64(x) - 1) & ~1ULL);
> +       m = 1ULL << ((fls64(x) - 1) & ~1);

is all good and simplifies the code to not have suffixes that don't
really matter or help.

Thanks,

            Linus

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

* Re: int_sqrt() adjustments
  2019-01-24 18:58 int_sqrt() adjustments Florian La Roche
  2019-01-24 20:02 ` Linus Torvalds
@ 2019-01-26 16:18 ` Florian La Roche
  1 sibling, 0 replies; 4+ messages in thread
From: Florian La Roche @ 2019-01-26 16:18 UTC (permalink / raw)
  To: linux-kernel
  Cc: Crt Mori, Joe Perches, Davidlohr Bueso, Will Deacon,
	Peter Zijlstra, Linus Torvalds

Hello all,

The first part of this patch is wrong: it changes from an unsigned long
param to __fls() to an unsigned int param in fls(). One option would be
to add another implementation of flsl() or given the minimalistic usage
of int_sqrt() in the kernel to keep the current code with __fls() and only
change masking the lowest bit.

The only other nitpick I have come up with: Adding __attribute_const__
could be possible.

best regards,

Florian La Roche

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

* int_sqrt() adjustments
@ 2019-01-24 18:21 Florian La Roche
  0 siblings, 0 replies; 4+ messages in thread
From: Florian La Roche @ 2019-01-24 18:21 UTC (permalink / raw)
  To: linux-kernel
  Cc: Crt Mori, Joe Perches, Davidlohr Bueso, Will Deacon,
	Peter Zijlstra, Linus Torvalds, Florian La Roche


__fls() is returning an unsigned long, but fls() and fls64() are
both returning a (signed) int.
As we need a signed int as right operand of "<<" (as Linus pointed out),
change __fls() to fls() for 32bit and also adjust masking the lowest bit
to be a signed int.
Now the 32bit and the 64bit version are again similar.

best regards,

Florian La Roche



Signed-off-by: Florian La Roche <Florian.LaRoche@googlemail.com>
---
 lib/int_sqrt.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/int_sqrt.c b/lib/int_sqrt.c
index 30e0f9770f88..66eb93105812 100644
--- a/lib/int_sqrt.c
+++ b/lib/int_sqrt.c
@@ -23,7 +23,7 @@ unsigned long int_sqrt(unsigned long x)
 	if (x <= 1)
 		return x;
 
-	m = 1UL << (__fls(x) & ~1UL);
+	m = 1UL << ((fls(x) - 1) & ~1L);
 	while (m != 0) {
 		b = y + m;
 		y >>= 1;
@@ -52,7 +52,7 @@ u32 int_sqrt64(u64 x)
 	if (x <= ULONG_MAX)
 		return int_sqrt((unsigned long) x);
 
-	m = 1ULL << ((fls64(x) - 1) & ~1ULL);
+	m = 1ULL << ((fls64(x) - 1) & ~1LL);
 	while (m != 0) {
 		b = y + m;
 		y >>= 1;
-- 
2.17.1


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

end of thread, other threads:[~2019-01-26 16:19 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-01-24 18:58 int_sqrt() adjustments Florian La Roche
2019-01-24 20:02 ` Linus Torvalds
2019-01-26 16:18 ` Florian La Roche
  -- strict thread matches above, loose matches on Subject: below --
2019-01-24 18:21 Florian La Roche

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).