From 52b899d60d8c61b8affe0206014173912de94940 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 24 Nov 2023 14:41:07 -0300 Subject: Simpler coding for new representation for arrays With the tags comming first in a cell, we can define the whole cell as a C type and let C do part of the address computations. --- ltable.h | 38 ++++++++------------------------------ 1 file changed, 8 insertions(+), 30 deletions(-) (limited to 'ltable.h') diff --git a/ltable.h b/ltable.h index d488a1f7..5581efb1 100644 --- a/ltable.h +++ b/ltable.h @@ -69,44 +69,22 @@ /* ** The array part of a table is represented by an array of cells. -** Each cell is composed of (NM + 1) elements, and each element has the -** type 'ArrayCell'. In each cell, only one element has the variant -** 'tag', while the other NM elements have the variant 'value'. The -** array in the 'tag' element holds the tags of the other elements in -** that cell. +** Each cell is composed of NM tags followed by NM values, so that +** no space is wasted in padding. */ -#define NM ((unsigned int)sizeof(Value)) +#define NM cast_uint(sizeof(Value)) -union ArrayCell { - unsigned char tag[NM]; - Value value; +struct ArrayCell { + lu_byte tag[NM]; + Value value[NM]; }; -/* -** 'NMTag' defines which cell element has the tags; that could be any -** value between 0 (tags come before all values) and NM (tags come after -** all values). -*/ -#define NMTag 0 - - -/* -** Computes the concrete index that holds the tag of abstract index 'i' -*/ -#define TagIndex(i) (((i)/NM * (NM + 1u)) + NMTag) - -/* -** Computes the concrete index that holds the value of abstract index 'i' -*/ -#define ValueIndex(i) ((i) + (((i) + (NM - NMTag))/NM)) - - /* Computes the address of the tag for the abstract index 'k' */ -#define getArrTag(t,k) (&(t)->array[TagIndex(k)].tag[(k)%NM]) +#define getArrTag(t,k) (&(t)->array[(k)/NM].tag[(k)%NM]) /* Computes the address of the value for the abstract index 'k' */ -#define getArrVal(t,k) (&(t)->array[ValueIndex(k)].value) +#define getArrVal(t,k) (&(t)->array[(k)/NM].value[(k)%NM]) /* -- cgit v1.2.3-55-g6feb