summaryrefslogtreecommitdiff
path: root/inline.h
diff options
context:
space:
mode:
authorKarl Williamson <khw@cpan.org>2021-06-05 10:22:42 -0600
committerKarl Williamson <khw@cpan.org>2021-07-30 05:41:27 -0600
commitbf874180e9a7a8fbb74ab7eaa5c683ecc5a6ffc7 (patch)
treedcd3fbb669c5ffe256a551deac326fd1d66782df /inline.h
parent6e7a7813c390d2121876b9f8af4f37102e8d51fa (diff)
downloadperl-bf874180e9a7a8fbb74ab7eaa5c683ecc5a6ffc7.tar.gz
Create and use single_1bit_pos32()
This moves the code from regcomp.c to inline.h that calculates the position of the lone set bit in a U32. This is in preparation for use by other call sites.
Diffstat (limited to 'inline.h')
-rw-r--r--inline.h19
1 files changed, 19 insertions, 0 deletions
diff --git a/inline.h b/inline.h
index 55b26e0d78..9ec02265bd 100644
--- a/inline.h
+++ b/inline.h
@@ -664,6 +664,25 @@ Perl_is_utf8_invariant_string_loc(const U8* const s, STRLEN len, const U8 ** ep)
return TRUE;
}
+PERL_STATIC_INLINE unsigned
+Perl_single_1bit_pos32(U32 word)
+{
+ /* Given a 32-bit word known to contain all zero bits except one 1 bit,
+ * find and return the 1's position: 0..31 */
+
+#ifdef PERL_CORE /* macro not exported */
+ ASSUME(isPOWER_OF_2(word));
+#else
+ ASSUME(word && (word & (word-1)) == 0);
+#endif
+
+ /* The position of the only set bit in a word can be quickly calculated
+ * using deBruijn sequences. See for example
+ * https://en.wikipedia.org/wiki/De_Bruijn_sequence */
+ return PL_deBruijn_bitpos_tab32[(word * PERL_deBruijnMagic32_)
+ >> PERL_deBruijnShift32_];
+}
+
#ifndef EBCDIC
PERL_STATIC_INLINE unsigned int