diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2023-11-24 14:41:07 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2023-11-24 14:41:07 -0300 |
commit | 52b899d60d8c61b8affe0206014173912de94940 (patch) | |
tree | 5e6f9116b2c70a5ebd4c1bdea49f283cca5cb3fb /ltable.h | |
parent | 25cd3d377ec13176a6701d9d21a278ba8f2bc3d6 (diff) | |
download | lua-52b899d60d8c61b8affe0206014173912de94940.tar.gz lua-52b899d60d8c61b8affe0206014173912de94940.tar.bz2 lua-52b899d60d8c61b8affe0206014173912de94940.zip |
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.
Diffstat (limited to 'ltable.h')
-rw-r--r-- | ltable.h | 38 |
1 files changed, 8 insertions, 30 deletions
@@ -69,44 +69,22 @@ | |||
69 | 69 | ||
70 | /* | 70 | /* |
71 | ** The array part of a table is represented by an array of cells. | 71 | ** The array part of a table is represented by an array of cells. |
72 | ** Each cell is composed of (NM + 1) elements, and each element has the | 72 | ** Each cell is composed of NM tags followed by NM values, so that |
73 | ** type 'ArrayCell'. In each cell, only one element has the variant | 73 | ** no space is wasted in padding. |
74 | ** 'tag', while the other NM elements have the variant 'value'. The | ||
75 | ** array in the 'tag' element holds the tags of the other elements in | ||
76 | ** that cell. | ||
77 | */ | 74 | */ |
78 | #define NM ((unsigned int)sizeof(Value)) | 75 | #define NM cast_uint(sizeof(Value)) |
79 | 76 | ||
80 | union ArrayCell { | 77 | struct ArrayCell { |
81 | unsigned char tag[NM]; | 78 | lu_byte tag[NM]; |
82 | Value value; | 79 | Value value[NM]; |
83 | }; | 80 | }; |
84 | 81 | ||
85 | 82 | ||
86 | /* | ||
87 | ** 'NMTag' defines which cell element has the tags; that could be any | ||
88 | ** value between 0 (tags come before all values) and NM (tags come after | ||
89 | ** all values). | ||
90 | */ | ||
91 | #define NMTag 0 | ||
92 | |||
93 | |||
94 | /* | ||
95 | ** Computes the concrete index that holds the tag of abstract index 'i' | ||
96 | */ | ||
97 | #define TagIndex(i) (((i)/NM * (NM + 1u)) + NMTag) | ||
98 | |||
99 | /* | ||
100 | ** Computes the concrete index that holds the value of abstract index 'i' | ||
101 | */ | ||
102 | #define ValueIndex(i) ((i) + (((i) + (NM - NMTag))/NM)) | ||
103 | |||
104 | |||
105 | /* Computes the address of the tag for the abstract index 'k' */ | 83 | /* Computes the address of the tag for the abstract index 'k' */ |
106 | #define getArrTag(t,k) (&(t)->array[TagIndex(k)].tag[(k)%NM]) | 84 | #define getArrTag(t,k) (&(t)->array[(k)/NM].tag[(k)%NM]) |
107 | 85 | ||
108 | /* Computes the address of the value for the abstract index 'k' */ | 86 | /* Computes the address of the value for the abstract index 'k' */ |
109 | #define getArrVal(t,k) (&(t)->array[ValueIndex(k)].value) | 87 | #define getArrVal(t,k) (&(t)->array[(k)/NM].value[(k)%NM]) |
110 | 88 | ||
111 | 89 | ||
112 | /* | 90 | /* |