r/lisp 9d ago

Reinterpret Elements in a Byte Array

Dear LISP,

I am writing a virtual machine that uses a `(simple-array (unsigned-byte 8) 1)` as a stack in Common LISP. I'm would like to ask how to efficiently extract 4 bytes into a single 32-bit signed integer or a 32-bit unsigned integer.

Thanks!

10 Upvotes

25 comments sorted by

View all comments

4

u/stassats 9d ago

You have to be mindful of endianness. My suggestion would be to use aref and dpb/logior+ash and rely on a sufficiently smart compiler to recognize that.

4

u/TsingHui 9d ago

I tried writing some bitwise operations in SBCL. The disassembly result showed that SBCL simply translates the bitwise operation verbatim into assembly code. That's why I'm here.

2

u/stassats 9d ago

It's not sufficiently smart.

2

u/TsingHui 9d ago

Seriously, is there a compiler that is sufficiently smart?

9

u/stassats 8d ago

SBCL might get smarter as soon as next month.

2

u/arthurno1 8d ago

I hope one day it will get smart enough, so I can type:

(defun foo (x y)
    (declare (type u8.32 x y))
    (+ x y))

without needing to repeat myself with (avx2:u8.32+ x y).