alamb commented on code in PR #10968:
URL: https://github.com/apache/arrow-rs/pull/10968#discussion_r4115925246


##########
arrow-buffer/src/util/bit_util.rs:
##########
@@ -113,29 +116,40 @@ pub fn compress(value: u64, mask: u64) -> u64 {
 /// assert_eq!(expand(compress(value, mask), mask), value & mask);
 /// ```
 #[inline]
-pub fn expand(mut value: u64, mask: u64) -> u64 {
-    if value == 0 {
-        return 0;
-    }
-
-    let mut zeros = !mask;
-    if zeros.count_ones() <= 32 {
-        // Insert zeros from low to high; excess input bits shift out.
-        while zeros != 0 {
-            let lower = (1_u64 << zeros.trailing_zeros()) - 1;
-            value = (value & lower) | ((value & !lower) << 1);
-            zeros &= zeros - 1;
+pub fn expand(value: u64, mask: u64) -> u64 {
+    #[cfg(all(target_arch = "x86_64", target_feature = "bmi2"))]
+    {
+        // SAFETY: the `bmi2` target feature is statically enabled for this
+        // build, so the `pdep` instruction is guaranteed to be available.
+        unsafe { std::arch::x86_64::_pdep_u64(value, mask) }

Review Comment:
   🚀 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to