Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the new x86 bit-manipulation instructions from Common Lisp?

I want to use the new bit manipulation instructions available in the latest Intel and AMD chips, in particular the "Parallel Bits Extract" PEXT instruction (see here and here). How can I access these instructions from within Common Lisp? (specifically SBCL).

Ideally, I would like to access these instructions through a library that detects whether the CPU provides them and, if it doesn't, it emulates them in software. Compilers for other languages provide such functions (e.g., GCC provides the function _pext_u32; see here).

like image 615
scaramouche Avatar asked Jan 02 '23 03:01

scaramouche


1 Answers

I think that to actually add support for it, you need to modify the compiler itself. This might involve something like adding a new VOP (see vop.lisp in the sbcl sources) defining a new function that compiles to it (maybe in the sb-ext package) and wiring it up. I can't actually tell you how to do that, my understanding of that is only superficial.

The other way, which might be more portable, is to create a C library containing functions that use the new primitive in assembly, then to wrap it in a CFFI binding.

like image 77
Svante Avatar answered Jan 05 '23 17:01

Svante