forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.c
417 lines (385 loc) · 11.3 KB
/
array.c
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
array constructors and primitives
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <assert.h>
#include <sys/types.h>
#include <limits.h>
#include <errno.h>
#include <math.h>
#include "llt.h"
#include "julia.h"
// array constructors ---------------------------------------------------------
static void *alloc_array_buffer(size_t nbytes, int isunboxed)
{
void *data;
if (nbytes > 0) {
if (isunboxed) {
data = alloc_pod(nbytes);
}
else {
data = allocb(nbytes);
memset(data, 0, nbytes);
}
}
else {
data = NULL;
}
return data;
}
static jl_array_t *_new_array(jl_type_t *atype, jl_tuple_t *dimst,
uint32_t ndims, size_t *dims)
{
size_t i, tot;
size_t nel=1;
int isbytes=0, isunboxed=0, elsz;
void *data;
jl_array_t *a;
if (ndims == 0) nel = 0;
for(i=0; i < ndims; i++) {
nel *= dims[i];
}
jl_type_t *el_type = (jl_type_t*)jl_tparam0(atype);
isunboxed = jl_is_bits_type(el_type);
if (isunboxed) {
elsz = jl_bitstype_nbits(el_type)/8;
tot = elsz * nel;
if (elsz==1 && ndims==1) {
// hidden 0 terminator for all byte arrays
isbytes = 1;
tot++;
}
}
else {
elsz = sizeof(void*);
tot = sizeof(void*) * nel;
}
int ndimwords = (ndims > 3 ? (ndims-3) : 0);
if (tot <= ARRAY_INLINE_NBYTES) {
a = allocobj(sizeof(jl_array_t) + tot + (ndimwords-1)*sizeof(size_t));
a->type = atype;
a->dims = dimst;
data = (&a->_space[0] + ndimwords*sizeof(size_t));
if (tot > 0 && !isunboxed) {
memset(data, 0, tot);
}
}
else {
a = allocobj(sizeof(jl_array_t) + (ndimwords-1)*sizeof(size_t));
jl_gc_preserve((jl_value_t*)a);
a->type = atype;
a->dims = dimst;
// temporarily initialize to make gc-safe
a->data = NULL;
a->length = 0;
data = alloc_array_buffer(tot, isunboxed);
jl_gc_unpreserve();
}
a->data = data;
if (isbytes) ((char*)data)[tot-1] = '\0';
a->length = nel;
a->ndims = ndims;
a->elsize = elsz;
size_t *adims = &a->nrows;
if (ndims == 1) {
a->nrows = nel;
a->maxsize = tot;
a->offset = 0;
}
else {
for(i=0; i < ndims; i++)
adims[i] = dims[i];
}
return a;
}
jl_array_t *jl_new_array(jl_type_t *atype, jl_tuple_t *dims)
{
size_t ndims = dims->length;
size_t *adims = alloca(ndims*sizeof(size_t));
size_t i;
for(i=0; i < ndims; i++)
adims[i] = jl_unbox_int32(jl_tupleref(dims,i));
return _new_array(atype, dims, ndims, adims);
}
jl_array_t *jl_alloc_array_1d(jl_type_t *atype, size_t nr)
{
return _new_array(atype, NULL, 1, &nr);
}
JL_CALLABLE(jl_new_array_internal)
{
jl_struct_type_t *atype = (jl_struct_type_t*)env;
jl_value_t *ndims = jl_tupleref(atype->parameters,1);
size_t nd = jl_unbox_int32(ndims);
JL_NARGS(Array, 1, 1);
JL_TYPECHK(Array, tuple, args[0]);
jl_tuple_t *d = (jl_tuple_t*)args[0];
if (d->length != nd) {
jl_error("Array: wrong number of dimensions");
}
size_t i;
for(i=0; i < d->length; i++) {
JL_TYPECHK(Array, int32, jl_tupleref(d,i));
}
return (jl_value_t*)jl_new_array((jl_type_t*)atype, d);
}
jl_array_t *jl_pchar_to_array(char *str, size_t len)
{
jl_array_t *a = jl_alloc_array_1d(jl_array_uint8_type, len);
memcpy(a->data, str, len);
return a;
}
jl_array_t *jl_cstr_to_array(char *str)
{
size_t len = strlen(str);
return jl_pchar_to_array(str, len);
}
jl_value_t *jl_pchar_to_string(char *str, size_t len)
{
jl_array_t *a = jl_pchar_to_array(str, len);
JL_GC_PUSH(&a);
jl_struct_type_t* string_type = u8_isvalid(a->data, len) == 1 ? // ASCII
jl_ascii_string_type : jl_utf8_string_type;
jl_value_t *s = jl_apply((jl_function_t*)string_type, (jl_value_t**)&a, 1);
JL_GC_POP();
return s;
}
jl_value_t *jl_cstr_to_string(char *str)
{
size_t len = strlen(str);
return jl_pchar_to_string(str, len);
}
jl_array_t *jl_alloc_cell_1d(size_t n)
{
return jl_alloc_array_1d(jl_array_any_type, n);
}
// array primitives -----------------------------------------------------------
JL_CALLABLE(jl_f_arraylen)
{
JL_NARGS(arraylen, 1, 1);
JL_TYPECHK(arraylen, array, args[0]);
return jl_box_int32(((jl_array_t*)args[0])->length);
}
jl_tuple_t *jl_construct_array_size(jl_array_t *a, size_t nd)
{
if (a->dims) return a->dims;
jl_tuple_t *d = jl_alloc_tuple(nd);
a->dims = d;
size_t i;
for(i=0; i < nd; i++)
jl_tupleset(d, i, jl_box_int32((&a->nrows)[i]));
return d;
}
JL_CALLABLE(jl_f_arraysize)
{
JL_TYPECHK(arraysize, array, args[0]);
jl_array_t *a = (jl_array_t*)args[0];
size_t nd = jl_array_ndims(a);
if (nargs == 2) {
JL_TYPECHK(arraysize, int32, args[1]);
int dno = jl_unbox_int32(args[1]);
if (dno < 1 || dno > nd)
jl_error("arraysize: dimension out of range");
return jl_box_int32((&a->nrows)[dno-1]);
}
else {
JL_NARGS(arraysize, 1, 1);
}
if (a->dims) return (jl_value_t*)a->dims;
return (jl_value_t*)jl_construct_array_size(a, nd);
}
static jl_value_t *new_scalar(jl_bits_type_t *bt)
{
size_t nb = jl_bitstype_nbits(bt)/8;
jl_value_t *v =
(jl_value_t*)allocobj((NWORDS(LLT_ALIGN(nb,sizeof(void*)))+1)*
sizeof(void*));
v->type = (jl_type_t*)bt;
return v;
}
jl_value_t *jl_arrayref(jl_array_t *a, size_t i)
{
jl_type_t *el_type = (jl_type_t*)jl_tparam0(jl_typeof(a));
jl_value_t *elt;
if (jl_is_bits_type(el_type)) {
if (el_type == (jl_type_t*)jl_bool_type) {
if (((int8_t*)a->data)[i] != 0)
return jl_true;
return jl_false;
}
elt = new_scalar((jl_bits_type_t*)el_type);
size_t nb = a->elsize;
switch (nb) {
case 1:
*(int8_t*)jl_bits_data(elt) = ((int8_t*)a->data)[i]; break;
case 2:
*(int16_t*)jl_bits_data(elt) = ((int16_t*)a->data)[i]; break;
case 4:
*(int32_t*)jl_bits_data(elt) = ((int32_t*)a->data)[i]; break;
case 8:
*(int64_t*)jl_bits_data(elt) = ((int64_t*)a->data)[i]; break;
default:
memcpy(jl_bits_data(elt), &((char*)a->data)[i*nb], nb);
}
}
else {
elt = ((jl_value_t**)a->data)[i];
if (elt == NULL) {
jl_errorf("array[%d]: uninitialized reference error", i+1);
}
}
return elt;
}
JL_CALLABLE(jl_f_arrayref)
{
JL_NARGS(arrayref, 2, 2);
JL_TYPECHK(arrayref, array, args[0]);
JL_TYPECHK(arrayref, int32, args[1]);
jl_array_t *a = (jl_array_t*)args[0];
size_t i = jl_unbox_int32(args[1])-1;
if (i >= a->length) {
jl_errorf("ref array[%d]: index out of range", i+1);
}
return jl_arrayref(a, i);
}
void jl_arrayset(jl_array_t *a, size_t i, jl_value_t *rhs)
{
jl_value_t *el_type = jl_tparam0(jl_typeof(a));
if (el_type != (jl_value_t*)jl_any_type) {
if (!jl_subtype(rhs, el_type, 1))
jl_type_error("arrayset", el_type, rhs);
}
if (jl_is_bits_type(el_type)) {
size_t nb = a->elsize;
switch (nb) {
case 1:
((int8_t*)a->data)[i] = *(int8_t*)jl_bits_data(rhs); break;
case 2:
((int16_t*)a->data)[i] = *(int16_t*)jl_bits_data(rhs); break;
case 4:
((int32_t*)a->data)[i] = *(int32_t*)jl_bits_data(rhs); break;
case 8:
((int64_t*)a->data)[i] = *(int64_t*)jl_bits_data(rhs); break;
default:
memcpy(&((char*)a->data)[i*nb], jl_bits_data(rhs), nb);
}
}
else {
((jl_value_t**)a->data)[i] = rhs;
}
}
JL_CALLABLE(jl_f_arrayset)
{
JL_NARGS(arrayset, 3, 3);
JL_TYPECHK(arrayset, array, args[0]);
JL_TYPECHK(arrayset, int32, args[1]);
jl_array_t *b = (jl_array_t*)args[0];
size_t i = jl_unbox_int32(args[1])-1;
if (i >= b->length) {
jl_errorf("assign array[%d]: index out of range", i+1);
}
jl_arrayset(b, i, args[2]);
return args[0];
}
static void *array_new_buffer(jl_array_t *a, size_t newsize)
{
size_t nbytes = newsize * a->elsize;
int isbytes = 0;
if (a->elsize == 1) {
isbytes = 1;
nbytes++;
}
int isunboxed = jl_is_bits_type(jl_tparam0(jl_typeof(a)));
char *newdata = alloc_array_buffer(nbytes, isunboxed);
if (isbytes) newdata[nbytes-1] = '\0';
return newdata;
}
void jl_array_grow_end(jl_array_t *a, size_t inc)
{
// optimized for the case of only growing and shrinking at the end
if (inc == 0)
return;
size_t es = a->elsize;
if (es*(a->length + inc) > a->maxsize - a->offset) {
size_t newsize = a->maxsize==0 ? inc*es : a->maxsize*2;
while (es*(a->length+inc) > newsize-a->offset)
newsize *= 2;
char *newdata = array_new_buffer(a, newsize);
newdata += a->offset;
memcpy(newdata, (char*)a->data, a->length*es);
a->maxsize = newsize;
a->data = newdata;
}
a->length += inc; a->nrows += inc;
a->dims = NULL;
}
void jl_array_del_end(jl_array_t *a, size_t dec)
{
if (dec == 0)
return;
if (dec > a->length)
jl_error("array_del_end: index out of range");
memset((char*)a->data + (a->length-dec)*a->elsize, 0, dec*a->elsize);
a->length -= dec; a->nrows -= dec;
a->dims = NULL;
}
void jl_array_grow_beg(jl_array_t *a, size_t inc)
{
// designed to handle the case of growing and shrinking at both ends
if (inc == 0)
return;
size_t es = a->elsize;
size_t nb = inc*es;
if (a->offset >= nb) {
a->data = (char*)a->data - nb;
a->offset -= nb;
}
else {
size_t anb = a->length*es;
char *newdata;
if (inc > (a->maxsize-anb)/2 - (a->maxsize-anb)/20) {
size_t newsize = a->maxsize==0 ? 2*nb : a->maxsize*2;
while (anb+2*nb > newsize-a->offset)
newsize *= 2;
newdata = array_new_buffer(a, newsize);
size_t center = (newsize - (anb + nb))/2;
newdata += center;
a->maxsize = newsize;
a->offset = center;
}
else {
size_t center = (a->maxsize - (anb + nb))/2;
newdata = (char*)a->data - a->offset + center;
a->offset = center;
}
memmove(&newdata[nb], a->data, anb);
a->data = newdata;
}
a->length += inc; a->nrows += inc;
a->dims = NULL;
}
void jl_array_del_beg(jl_array_t *a, size_t dec)
{
if (dec == 0)
return;
if (dec > a->length)
jl_error("array_del_beg: index out of range");
size_t nb = dec*a->elsize;
memset(a->data, 0, nb);
a->offset += nb;
a->data = (char*)a->data + nb;
a->length -= dec; a->nrows -= dec;
a->dims = NULL;
// make sure offset doesn't grow forever due to deleting at beginning
// and growing at end
if (a->offset >= 13*a->maxsize/20) {
size_t anb = a->length*a->elsize;
size_t newoffs = 17*(a->maxsize - anb)/100;
size_t delta = (a->offset - newoffs);
a->data = (char*)a->data - delta;
memmove(a->data, (char*)a->data + delta, anb);
a->offset = newoffs;
}
}