M
Modular•2w ago
Korneliusz

Using BMI (pext_u64 and pdep_u64) in Mojo

How can I use pext and pdep in my Mojo project? I need that Bit Manipulation Instructions to be efficient with some fancy lookups. Is there a way to use those instructions on x86 currently? Those instructions are supported by every CPU with BMI2. I can use them in Rust simply importing them "use std::arch::x86_64::{_pdep_u64, _pext_u64};" I can use them in Julia simply invoking them from C. If anyone knows, please tell me. And if it's not possible, can I commit to Mojo development and make BMI2 instructions available? I would really like to have it in Mojo 😉
4 Replies
Darkmatter
Darkmatter•2w ago
The LLVM intrinsic names are:
llvm.x86.bmi.bextr.32
llvm.x86.bmi.bextr.64
llvm.x86.bmi.bzhi.32
llvm.x86.bmi.bzhi.64
llvm.x86.bmi.pdep.32
llvm.x86.bmi.pdep.64
llvm.x86.bmi.pext.32
llvm.x86.bmi.pext.64
llvm.x86.bmi.bextr.32
llvm.x86.bmi.bextr.64
llvm.x86.bmi.bzhi.32
llvm.x86.bmi.bzhi.64
llvm.x86.bmi.pdep.32
llvm.x86.bmi.pdep.64
llvm.x86.bmi.pext.32
llvm.x86.bmi.pext.64
Use them with https://docs.modular.com/mojo/stdlib/sys/intrinsics/llvm_intrinsic
llvm_intrinsic | Modular
llvmintrinsicintrin AnyTrivialRegType, *types Bool = True -> type
Korneliusz
KorneliuszOP•2w ago
Yes, that's exactly what I need
Darkmatter
Darkmatter•2w ago
If you want to put them into the stdlib, they probably belong in the bit module, and PRs are welcome (https://github.com/modularml/mojo)
GitHub
GitHub - modularml/mojo: The Mojo Programming Language
The Mojo Programming Language. Contribute to modularml/mojo development by creating an account on GitHub.
Korneliusz
KorneliuszOP•2w ago
I didn't see this page, thanks It works, nice, for anyone having same question, here's example: from sys import llvm_intrinsic def main(): alias intrin: StringLiteral = "llvm.x86.bmi.pext.64" var x = llvm_intrinsicintrin, UInt64, UInt64(12309)) print(x) # 22

Did you find this page helpful?