<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/98217>98217</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            how to express pext without intrinsics or inline assembly
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          ImpleLee
      </td>
    </tr>
</table>

<pre>
    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;
}
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyMVNuSozYQ_Zr2S5dd0JjbAw87O0vFVUnlJT8goLEVC4mVhC_5-pTA9oxnZ7OhKFuo-5w-nBYtnJN7zVxB-gLp60pM_mBstRtGxb8zrxrTXasdnoX26A1OjvFSZOtsi5BFI188ZBHQV2wmjzsUypkld7hiP-nWS6MDThlzxFEJ3xs7rMVeG-dlG4DO4A47o4Fy_1RGam-ldrJ1aCxKraRmFM7x0KjrBqJXiL7s0FvJHTozMDoehPayXfP3SZ6EYu1RhvcYWHsRlDg0_TvduMPeTLpDM5PfX0MbzSHRH3jAVmhsGFszjFJxF8S9EdxVO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