Hasty Briefsbeta

NaN-Boxing in C

a day ago
  • #data-encoding
  • #C-programming
  • #NaN-boxing
  • Defines a type `nanbox_t` that can store multiple data types in 64 bits using NaN-boxing.
  • Supports storing doubles, 32-bit integers, pointers, booleans, null, and special values like 'undefined', 'empty', and 'deleted'.
  • Uses unused NaN-space in IEEE754 representation and unused pointer bits on 64-bit platforms.
  • Provides encoding functions like `nanbox_from_double`, `nanbox_from_int`, etc., and decoding functions like `nanbox_to_double`, `nanbox_to_int`, etc.
  • Includes type-checking functions such as `nanbox_is_double`, `nanbox_is_int`, etc.
  • Allows customization via macros like `NANBOX_PREFIX` and `NANBOX_POINTER_TYPE`.
  • Supports 'auxillary space' for storing additional 48-bit values.
  • Includes a demo for storing short strings (up to 6 bytes) in auxillary space.
  • Tested on x86-64 (Intel Core 2 Duo) and supports 32-bit and 64-bit modes.
  • Encourages testing on other architectures like ARM and SPARC.