14

cross-posted from: https://lemmy.ml/post/41409887

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
14 points (93.8% liked)

Programming

24410 readers
353 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



founded 2 years ago
MODERATORS