直接从 Numba 使用 CPU 指令

Using CPU instruction directly from Numba

我想在 Numba 编译函数中使用我的 CPU 的内置指令,但我无法弄清楚如何引用它们。例如,来自 SSE4 指令集的 popcnt 指令,我可以确认我有它使用 llvmlite.binding.get_host_cpu_features(),但无法调用函数本身。

我需要能够从其他 nopython 编译函数中调用这些函数(指令)。

理想情况下,这将尽可能接近 Python,但在这种情况下,速度比可读性更重要。

您可以使用 Cython 调用 SSE 内部函数,但不能使用 Numba 来调用。通过 Cython 做你想做的代码在这里:https://gist.github.com/aldro61/f604a3fa79b3dec5436a and here: https://gist.github.com/craffel/e470421958cad33df550

您可以制作一个小型汇编语言 DLL 并通过 ctypes 调用它,根据我的经验,当从 Numba nopython 代码中使用时,它没有任何开销。或者你也可以像这样直接使用指令代码 blog post on jit in Python Piston JavaScript assembler might be used to obtain machine codes for a small asm routine. Numba allows making small functions in LLVM ir as described in this thread 当然也可以使用 llvmlite。