8
submitted 3 weeks ago by tracyspcy@lemmy.ml to c/rust@lemmy.ml

I've been building a bytecode VM in Rust and recently implemented NaN boxing for value representation. Sharing here for anyone interested.

I needed all VM values (booleans, integers, string pool indices, bytecode references) to fit in 64 bits (stack is Vec).

My implementation encodes 5 distinct types using a 3-bit tag and 32-bit payload, all within a single u64. It also has 15 unused bits, they may be used later for types expansion.

I'm using a 64-bit layout:

  • Bits 63-51: Quiet NaN signature (0x7FFC...)
  • Bits 50-18: 32-bit payload (integers, string pool indices, etc.)
  • Bits 17-3: Unused/ (15 bits)
  • Bits 2-0: 3-bit type tag

So it allows me to have 5 tagged types: TRUE_VAL, FALSE_VAL, STRING_VAL, CALLDATA_VAL, U32_VAL

no comments (yet)
sorted by: hot top controversial new old
there doesn't seem to be anything here
this post was submitted on 08 Jan 2026
8 points (100.0% liked)

Rust Programming

9122 readers
53 users here now

founded 6 years ago
MODERATORS