lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Em sex., 5 de nov. de 2021 às 12:10, Jonathan Goble <jcgoble3@gmail.com> escreveu:
On Fri, Nov 5, 2021 at 10:28 AM Ranier Vilela <ranier.vf@gmail.com> wrote:
What if we create a new TValue that only uses 7 bytes for the value and 1 byte for the type?

typedef struct TValue {
 lu_byte value_[7]; /* 7 bytes */
 lu_byte tt_; /* 1 byte */
} TValue;

Total Size: 8 bytes
It would have to be considered the big-endian and little-endian,
to store and read.

You can fit a pointer into 7 bytes (almost no architecture uses more than 48 bits for pointers), but you don't have room for a double-precision floating point number, which needs all 8 bytes and can't be reduced (at least without defining your own floating-point standard and losing the benefit of hardware support for your floating-point numbers). And single-precision floating point numbers are not appropriate for all use cases due to loss of precision.

The closest thing to what you're suggesting is NaN packing, using the approximately 50 unused bits of a double-precision floating point NaN to store a 48-bit pointer and a couple of type tags to fit it all into 8 bytes. You lose 64-bit integers if you do that, although 63-bit integers are possible by using the sign bit of the NaN to distinguish them, and the loss of one bit from a 64-bit integer is not likely to be a problem for the majority of applications.
Very nice, NaN packing seems to me a good solution.

regards,
Ranier Vilela