Skip to contentSkip to conversation

SPARC: Better fls64(), fls() and __fls() on SPARCv9

PATCH In review George Spelvin 2 participants 3 messages Mar 28, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 SPARC: Better fls64(), fls() and __fls() on SPARCv9

For some reason, the asm implementations didn't take advantage of the popc instruction, which allows a very simple fls64() implementation.

fls() requires masking to avoid counting the high 32 bits, since popc is a 64-bit-only instruction.

__fls() requires an extra shift right. (Or it could be a subtract. Do you prefer __fls(0) to return 0 or -1?)

I don't have access to a Sparc machine to test this, but maybe someone would be willing? It's simple enough that any bugs should not be subtle.

---
 arch/sparc/include/asm/bitops_64.h |  3 +-
 arch/sparc/lib/fls.S               | 77 +++++++-----------------
 arch/sparc/lib/fls64.S             | 94 ++++++++++++++----------------
 3 files changed, 65 insertions(+), 109 deletions(-)
arch/sparc/include/asm/bitops_64.h +1 -2
index ca7ea5913494..888e4f786826 100644
@@ -24,12 +24,11 @@ void clear_bit(unsigned long nr, volatile unsigned long *addr);
 void change_bit(unsigned long nr, volatile unsigned long *addr);
 
 int fls(unsigned int word);
+int fls64(unsigned long word);
 int __fls(unsigned long word);
 
 #include <asm-generic/bitops/non-atomic.h>
 
-#include <asm-generic/bitops/fls64.h>
-
 #ifdef __KERNEL__
 
 int ffs(int x);
arch/sparc/lib/fls.S +20 -57

87 lines not on this page · raw message

arch/sparc/lib/fls64.S +44 -50

105 lines not on this page · raw message

Signature · 1 line
2.20.1
maintainer
#1 lore raw

You can't use popc in the default implementations, most sparc64 chips do not implement popc in hardware and it must be emulated.

You can only use it in the code that is patched in on T4 and later cpus.

See arch/sparc/lib/NG4fls*.S

#2 lore raw
In reply to George Spelvin
Quoted 2 lines

You can't use popc in the default implementations, most sparc64 chips do not implement popc in hardware and it must be emulated.

Mea culpa! I was reading the various architecture manuals (mostly about umulxhi support) and noticed that popc was in the oldest original SPARCv9 architecture manuals, so I assumed it had universal hardware support.

Damn manuals not mentioning a Really Important Detail like that! (They *do* document the lack of hardware support for quad floating point, but no such warning appears in the 2011 or 2015 architecture manuals.) With your prompting, I found mention of this in a Java feature request:
https://bugs.java.com/bugdatabase/view_bug.do?bug_idc78821

And of course, T4 has lzcnt and doesn't care.

Sorry for wasting your time. (And mine.)

P.S. I do think *some* earlier processors have hardware popc support; e.g. the T3 manual documents the instruction latency, which implies hardware support. But dealing with a feature flag is not nearly so simple a patch.

P.P.S. __NG4fls cn be simplified to
ENTRY(__NG4fls)

	LZCNT_O0_G2	!lzcnt  %o0, %g2
	retl
	 xor	%g2, 63, %o0

ENDPROC(__NG4fls)