1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
use math;
use types;
// premature optimization ftw!
export def DIRECT: u8 = 0;
export def CONSTANT: u8 = 1;
export def INLINE_MAX: u8 = ((size(*opaque) * 2 - 1) / 2): u8;
export type Array = union {
// (2024 note): this used to use duplicate struct member names, which i guess
// was a compiler bug and doesn't work anymore.
struct { // direct
palette_size: u8, // == DIRECT
data_direct: *[*]u16,
},
struct { // constant
palette_size_const: u8, // == CONSTANT
data_constant: u16,
},
struct { // indirect inline
palette_size_indirect_inline: u8, // <= INLINE_MAX, != DIRECT, != CONSTANT
palette_inline: [INLINE_MAX]u16,
data_indirect: *[*]u8,
},
struct { // indirect ptr
palette_size_indirect_ptr: u8, // > INLINE_MAX
palette_ptr: *[*]u16,
// data_indirect: *[*]u8,
// (though it would result in an incorrect offset... yeah,
// i don't know what i was thinking here at all.)
},
};
export fn new(palette_size: u8, arraylen: size) Array = {
let array = Array {
palette_size = palette_size,
};
if (palette_size == DIRECT) {
array.data_direct =
alloc([]: [0]u16, arraylen): *[*]u16;
} else if (palette_size != CONSTANT) {
array.data_indirect = alloc([]: [0]u8,
indirect_size(palette_size, arraylen)): *[*]u8;
if (palette_size > INLINE_MAX) {
array.palette_ptr =
alloc([]: [0]u16, palette_size): *[*]u16;
};
};
return array;
};
export fn clear(array: *Array, fill_value: u16) void = {
if (array.palette_size == DIRECT) {
free(array.data_direct);
} else if (array.palette_size != CONSTANT) {
free(array.data_indirect);
if (array.palette_size > INLINE_MAX)
free(array.palette_ptr);
};
array.palette_size = CONSTANT;
array.data_constant = fill_value;
};
// XXX: should be @inline
export fn get(array: *Array, i: size) u16 = {
if (array.palette_size == DIRECT) {
return array.data_direct[i];
} else if (array.palette_size == CONSTANT) {
return 0;
} else {
const log2nbits = palette_log2nbits(array.palette_size);
const nbits = 1 << log2nbits;
const valuemask = (1 << nbits) - 1;
const log2nperbyte = 3 - log2nbits;
const nperbyte = 1 << log2nperbyte;
const indexlow = nperbyte - 1;
const byte = array.data_indirect[i >> log2nperbyte];
const shift = (i: u8 & indexlow) << log2nbits;
return (byte >> shift) & valuemask;
};
};
// XXX: should be @inline
export fn set(array: *Array, i: size, ref: u16) void = {
if (array.palette_size == DIRECT) {
array.data_direct[i] = ref;
} else if (array.palette_size == CONSTANT) {
assert(ref == 0);
} else {
const log2nbits = palette_log2nbits(array.palette_size);
const nbits = 1 << log2nbits;
const valuemask = (1 << nbits) - 1;
const log2nperbyte = 3 - log2nbits;
const nperbyte = 1 << log2nperbyte;
const indexlow = nperbyte - 1;
const byte = &array.data_indirect[i >> log2nperbyte];
const shift = (i: u8 & indexlow) << log2nbits;
*byte = *byte & ~(valuemask << shift) | ref: u8 << shift;
};
};
// XXX: should be @inline
export fn get_palette(array: *Array) []u16 = {
if (array.palette_size == DIRECT) {
abort();
} else if (array.palette_size == CONSTANT) {
return &array.data_constant: *[1]u16;
} else if (array.palette_size <= INLINE_MAX) {
return array.palette_inline[..array.palette_size];
} else {
return array.palette_ptr[..array.palette_size];
};
};
// XXX: should be @inline
export fn lookup(array: *Array, ref: u16) u16 = {
if (array.palette_size == DIRECT) {
return ref;
};
return get_palette(array)[ref];
};
// XXX: should be @inline
export fn indirect_size(palette_size: u8, arraylen: size) size =
arraylen << palette_log2nbits(palette_size) >> 3;
// XXX: should be @inline
export fn palette_log2nbits(palette_size: u8) u8 = {
assert(palette_size != DIRECT);
const nbits = math::bit_size_u8(palette_size - 1);
return math::bit_size_u8(nbits - 1);
};
export fn grow_palette(array: *Array, arraylen: size, newsize: u8) void = {
if (array.palette_size == newsize) return;
assert(
(array.palette_size - 1) < (newsize - 1),
"new size must be >= old size"
);
let newarray = *array;
newarray.palette_size = newsize;
defer *array = newarray;
// By this point we know that the palette has grown in size, so it
// always needs to be reallocated unless the new size is DIRECT,
// in which case there will be no palette.
const palette = get_palette(array);
if (newsize == DIRECT) {
void;
} else if (newsize <= INLINE_MAX) {
newarray.palette_inline[..len(palette)] = palette;
} else {
let newpalette = alloc(palette, newsize);
newarray.palette_ptr = newpalette: *[*]u16;
};
defer if (array.palette_size > INLINE_MAX) {
free(array.palette_ptr);
};
if (array.palette_size == CONSTANT) {
if (newsize == DIRECT) {
newarray.data_direct = alloc([array.data_constant...],
arraylen): *[*]u16;
} else {
newarray.data_indirect = alloc([0...],
indirect_size(newsize, arraylen)): *[*]u8;
};
return;
};
if (newsize == DIRECT) {
newarray.data_direct = alloc([]: [0]u16, arraylen): *[*]u16;
for (let i = 0z; i < arraylen; i += 1) {
newarray.data_direct[i] = palette[get(array, i)];
};
free(array.data_indirect);
return;
};
// We need to reallocate the data array if the number of bits per item
// increased.
const log2nbits = palette_log2nbits(array.palette_size);
const log2newnbits = palette_log2nbits(newsize);
if (log2nbits != log2newnbits) {
newarray.data_indirect = alloc([]: [0]u8,
indirect_size(newsize, arraylen)): *[*]u8;
for (let i = 0z; i < arraylen; i += 1) {
set(&newarray, i, get(array, i));
};
free(array.data_indirect);
};
};
|