Issue 98217
Summary how to express pext without intrinsics or inline assembly
Labels new issue
Assignees
Reporter ImpleLee
    I want to use x86-64 `pext`, but I also want my function to look platform-agnostic, so I don't want to use intrinsics or inline assembly.
I tried some semantic-equivalent implementations of `pext` I found online, but none of them can be compiled to `pext`.
Is it really possible to make the compiler generate the `pext` instruction? Or will there be any plan to support this behavior?

Here are the `pext` implementations I tried. ( https://godbolt.org/z/f3vMTf8ob )
```c++
// from https://stackoverflow.com/questions/21144237
int pext1(int x, int mask) {
  int res = 0;
  for(int bb = 1; mask != 0; bb += bb) {
 if(x & mask & -mask) {
      res |= bb;
    }
    mask &= (mask - 1);
  }
  return res;
}

// from https://www.felixcloutier.com/x86/pext
int pext2(int x, int mask) {
 int res = 0;
  int k = 0;
  for (int m = 0; m < 32; ++m) {
    if (mask & (1 << m)) {
        res |= ((x >> m) & 1) << k;
        k += 1;
    }
  }
  return res;
}
```
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to