rdentato / val

A small library to bring NaNboxing to C

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

val C Library: NaNboxing Made Accessible in C

Welcome to val, a C library designed to bring the wonders of NaNboxing to the fingertips of C programmers, enabling the efficient storage of multiple data types in a single variable.

🧰 What is NaNboxing?

NaNboxing is a technique that leverages the NaN (Not a Number) space in IEEE 754 floating-point numbers to encode and represent various data types, such as integers, pointers, and floating-point numbers, in a single 64-bit float. This approach has been vital for languages like JavaScript, providing them with a way to optimize memory usage and enhance performance. val aims to bestow these advantages upon C, a language that doesn’t natively offer NaNboxing.

πŸš€ Features

  • Versatile Storage: Handle signed/unsigned integers (up to 48 bits), double-precision floats, generic pointers, and string pointers all within a single val_t variable.
  • Type Discovery: Identify stored data types effortlessly.
  • Smooth Conversion: Retrieve your original data seamlessly with type conversion functions.
  • Helpful Constants: Utilize predefined constants for common values like true, false, and nil.

πŸ’» Usage

Storing a Value

Store various types of data using the val() function. Example:

val_t f = val(3.2);          // Stores a double
val_t s = val("a string");   // Stores a pointer to a string

Retrieving Values

Retrieve stored data by converting val_t variable using the appropriate function:

double num = valtodouble(f);
char * str = valtostring(s);

Type Checking

Check the type of data stored in val_t using:

int type = valtype(f);

Or utilize the provided helper functions for context-specific checks:

valisinteger(x);
valisdouble(x);
// ... and more

πŸ§ͺ Constants

The library defines several handy constants of type val_t:

  • valfalse
  • valtrue
  • valnil
  • valnilpointer
  • valnilstr

πŸ™‹β€β™‚οΈ Contributing

Contributions, issues, and feature requests are welcome!

πŸ“œ License

Distributed under the MIT License. See LICENSE for more information.

About

A small library to bring NaNboxing to C

License:MIT License


Languages

Language:C 94.9%Language:Shell 4.0%Language:Makefile 1.1%