5 ms·
I don’t know if it could help in your case (java) but there’s a machine instruction for count leading zeros on x86 platform - LZCNT https://en.m.wikipedia.org/
by CraigJPerry 5y ago
I don’t know if it could help in your case (java) but there’s a machine instruction for count leading zeros on x86 platform - LZCNT https://en.m.wikipedia.org/wiki/X86_Bit_manipulation_instruction_set https://en.m.wikipedia.org/wiki/X86_Bit_manipulation_instruc... - i found out about this randomly years ago when i was doing some interfacing with a microcontroller from python and my solution was horrifically slow.
To exploit this i had to use cython from python to define a c function which used a gcc compiler intrinsic to allow me to write the asm instruction inline. It turned out about 7 lines of code so it wasn’t as bad as it sounds.
In your case, you’d need to use JNI i suppose so it might not be any less code but it should at least be a lot faster if that matters.
- marginalia_nu 5y agoJava has Integer.numberOfLeadingZeros(), which I suspect exposes LZCNT where available.
- Const-me 5y agoYeah, and where not available Java probably uses BSR instruction which does the same thing but introduced in i386 (1985): https://www.felixcloutier.com/x86/bsr https://www.felixcloutier.com/x86/bsr