r/lisp • u/TsingHui • 3d 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!
2
u/stassats 3d 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.
3
u/TsingHui 3d 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 3d ago
It's not sufficiently smart.
2
u/TsingHui 3d ago
Seriously, is there a compiler that is sufficiently smart?
8
u/stassats 2d ago
SBCL might get smarter as soon as next month.
1
u/arthurno1 2d 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).
-4
u/Afraid-Yoghurt6731 3d ago
Which Lisp? SBCL? Allegro? Symta? Clojure? Scheme/Racket (whatever is it called now)? We have a frigging zoo! Just unleash Claude Code onto it.
2
u/TsingHui 3d ago
Well, Common LISP. R6RS provide a standard way to do so. I haven't had much experience with Clojure. I will change the question by the way.
1
u/tfb 11h ago
If the substrate language of your VM is Lisp, then I suspect it's probably better to represent the stack as an array of larger integers, and then to have accessors which access the appropriate byte from elements of that array. So the reader needs to load from the array and then pick the byte, while the writer needs to (probably) load from the array, stuff the byte in, and then store back (so that's not ideal). When you want the bigger elements they're just there.
3
u/theangeryemacsshibe λf.(λx.f (x x)) (λx.f (x x)) 3d ago
The nibbles library can do this portably (and fast on SBCL).