forked from dzaima/CBQN
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathffiTest.c
More file actions
367 lines (310 loc) · 8.72 KB
/
ffiTest.c
File metadata and controls
367 lines (310 loc) · 8.72 KB
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
#include <stdlib.h>
#include <stdio.h>
#include "../../include/bqnffi.h"
void do_nothing() { }
int32_t getType(BQNV v) {
int32_t i = bqn_type(v);
bqn_free(v);
return i;
}
BQNV timesTen(BQNV v) {
size_t len = bqn_bound(v);
int32_t* buf = malloc(len*4);
bqn_readI32Arr(v, buf);
for (int i = 0; i < len; i++) buf[i] = buf[i] * 10;
BQNV res = bqn_makeI32Vec(len, buf);
free(buf);
bqn_free(v);
return res;
}
BQNV readAtoms(BQNV num, BQNV chr) {
uint32_t c1 = bqn_readChar(chr); uint32_t c2 = bqn_toChar(chr);
double f1 = bqn_readF64 (num); double f2 = bqn_toF64 (num);
printf("%u %u %.18f %.18f\n", c1, c2, f1, f2);
return bqn_makeChar(c1+1);
}
BQNV readTyped(BQNV x) {
BQNV objs[7];
bqn_readObjArr(x, objs);
BQNV pick3 = bqn_pick(x, 3);
BQNV equal = bqn_evalCStr("≡");
if (!bqn_toF64(bqn_call2(equal, pick3, objs[3]))) printf("bqn_pick didn't give the same element as bqn_readObjArr!\n");
bqn_free(equal);
bqn_free(pick3);
double res = 0;
int8_t bufI8 [10]; bqn_readI8Arr (objs[0], bufI8 ); bqn_free(objs[0]); for (int i=0; i<10; i++) res+= bufI8 [i];
int16_t bufI16[10]; bqn_readI16Arr(objs[1], bufI16); bqn_free(objs[1]); for (int i=0; i<10; i++) res+= bufI16[i];
int32_t bufI32[10]; bqn_readI32Arr(objs[2], bufI32); bqn_free(objs[2]); for (int i=0; i<10; i++) res+= bufI32[i];
double bufF64[10]; bqn_readF64Arr(objs[3], bufF64); bqn_free(objs[3]); for (int i=0; i<10; i++) res+= bufF64[i];
uint8_t bufC8 [10]; bqn_readC8Arr (objs[4], bufC8 ); bqn_free(objs[4]); for (int i=0; i<10; i++) res+= bufC8 [i];
uint16_t bufC16[10]; bqn_readC16Arr(objs[5], bufC16); bqn_free(objs[5]); for (int i=0; i<10; i++) res+= bufC16[i];
uint32_t bufC32[10]; bqn_readC32Arr(objs[6], bufC32); bqn_free(objs[6]); for (int i=0; i<10; i++) res+= bufC32[i];
bqn_free(x);
return bqn_makeF64(res);
}
BQNV getField(BQNV ns, BQNV name, BQNV def) {
BQNV res;
if (bqn_hasField(ns, name)) {
res = bqn_getField(ns, name);
bqn_free(def);
} else {
res = def;
}
bqn_free(ns);
bqn_free(name);
return res;
}
BQNV getShape(BQNV x) {
size_t n = bqn_rank(x);
size_t sh[n];
bqn_shape(x, sh);
bqn_free(x);
int16_t res[n];
for (int i = 0; i < n; i++) res[i] = sh[i];
return bqn_makeI16Vec(n, res);
}
BQNV evalBQN(BQNV x) {
BQNV r = bqn_eval(x);
bqn_free(x);
return r;
}
BQNV makeArrays() {
BQNV res[16];
size_t shape[3] = {2,1,3};
res[ 0] = bqn_makeI8Arr (3, shape, (int8_t []){1,2,3,4,5,6});
res[ 1] = bqn_makeI16Arr(3, shape, (int16_t []){1,2,3,4,500,-600});
res[ 2] = bqn_makeI32Arr(3, shape, (int32_t []){1,2,3,4,500000000,-600000000});
res[ 3] = bqn_makeF64Arr(3, shape, (double []){1,2,3,4,5.25,6.9765625});
res[ 4] = bqn_makeC8Arr (3, shape, (uint8_t []){48,49,50,51,52,53});
res[ 5] = bqn_makeC16Arr(3, shape, (uint16_t[]){48,49,50,51,52,9033});
res[ 6] = bqn_makeC32Arr(3, shape, (uint32_t[]){48,49,50,51,52,120169});
res[ 7] = bqn_makeObjArr(2, (size_t[]){2,2}, (BQNV[]){
bqn_makeChar(120169),
bqn_makeC8Arr(2, shape, (uint8_t[]){48,49,50}),
bqn_makeC8Arr(1, shape, (uint8_t[]){48,49,50}),
bqn_makeC8Arr(0, shape, (uint8_t[]){48})
});
res[ 8] = bqn_makeI8Vec (7, (int8_t []){3,1,4,1,5,9,7});
res[ 9] = bqn_makeI16Vec(7, (int16_t []){3,1,4,1,5,9,7});
res[10] = bqn_makeI32Vec(7, (int32_t []){3,1,4,1,5,9,7});
res[11] = bqn_makeF64Vec(7, (double []){3,1,4,1,5,9,7});
res[12] = bqn_makeC8Vec (7, (uint8_t []){97,98,99,65,66,67,59});
res[13] = bqn_makeC16Vec(7, (uint16_t[]){97,98,99,65,66,67,59});
res[14] = bqn_makeC32Vec(7, (uint32_t[]){97,98,99,65,66,67,59});
res[15] = bqn_makeUTF8Str(11, "{𝕨+𝕩}");
return bqn_makeObjVec(16, res);
}
BQNV makeThree(BQNV x) {
return bqn_makeObjVec(3, (BQNV[]){bqn_copy(x), bqn_copy(x), x});
}
int32_t directAccess(BQNV x) {
BQNElType e = bqn_directArrType(x);
uint32_t res = 0;
if (e==elt_i32) {
size_t bound = bqn_bound(x);
const int32_t* els = bqn_directI32(x);
for (size_t i = 0; i < bound; i++) res = res*31 + (uint32_t)els[i];
} else {
printf("not elt_i32!\n");
}
bqn_free(x);
return res;
}
static BQNV add_args(BQNV t, BQNV x) {
return bqn_makeF64(bqn_toF64(t) + bqn_toF64(x));
}
BQNV bindAdd(BQNV x) {
BQNV r = bqn_makeBoundFn1(add_args, x);
bqn_free(x);
return r;
}
void printArgs(int8_t i8, int16_t i16, int32_t i32, uint8_t u8, uint16_t u16, uint32_t u32, float f, double d) {
printf("args: %d %d %d %u %u %u %.18f %.18f\n", i8, i16, i32, u8, u16, u32, f, d);
}
void noopArgs(int8_t i8, int16_t i16, int32_t i32, uint8_t u8, uint16_t u16, uint32_t u32, float f, double d) { }
void printPtrArgs(int8_t* i8, int16_t* i16, int32_t* i32, uint8_t* u8, uint16_t* u16, uint32_t* u32, float* f, double* d) {
printf("args: %d %d %d %u %u %u %.18f %.18f\n", *i8, *i16, *i32, *u8, *u16, *u32, *f, *d);
}
float printU64s(uint64_t a, uint64_t* b) {
printf("%llx %llx %llx %llx\n", (long long)a, (long long)b[0], (long long)b[1], (long long)b[2]);
return 12345678;
}
int32_t multiplyI32Ptrs(int32_t* a, int32_t* b, int32_t len) {
int32_t r = 0;
for (int i = 0; i < len; i++) {
r+= a[i]*b[i];
}
return r;
}
float sumF32Arr(float* f, int len) {
float r = 0;
for (int i = 0; i < len; i++) r+= f[i];
return r;
}
void incI32s(int32_t* a, int32_t len) {
for (int i = 0; i < len; i++) a[i]++;
}
void incInts(int32_t* a, int16_t* b, int8_t* c) {
printf("%d %d %d\n", *a, *b, *c);
(*a)++;
(*b)++;
(*c)++;
}
void incNativeElements(
int8_t* pi8, int16_t* pi16, int32_t* pi32,
uint8_t* pu8, uint16_t* pu16, uint32_t* pu32,
float* pf64, double* pf32
) {
printf("%d %d %d\n", *pi8, *pi16, *pi32);
printf("%u %u %u\n", *pu8, *pu16, *pu32);
(*pi8 )++; (*pu8 )++;
(*pi16)++; (*pu16)++;
(*pi32)++; (*pu32)++;
(*pf32)++; (*pf64)++;
}
void incInt64(int64_t* pi64, uint64_t* pu64) {
printf("%llx %llx\n", (long long)*pi64, (long long)*pu64);
(*pi64)++;
(*pu64)++;
}
uint64_t ident_u64(uint64_t x) { return x; }
int64_t ident_i64(int64_t x) { return x; }
double ident_f64(double a) { return a; }
void* pick_ptr(void** arr, int idx) {
return arr[idx];
}
uint64_t pick_u64(uint64_t* arr, int idx) {
return arr[idx];
}
typedef struct {
uint8_t u8v;
int32_t i32v;
int16_t u16v;
uint64_t u64v;
double f64v;
} S1;
int16_t thirdMember(S1 s) {
return s.u16v;
}
S1 incMembers(S1 s) {
s.u8v++;
s.u16v++;
s.i32v++;
s.u64v++;
s.f64v++;
return s;
}
typedef struct {
S1 a, b;
} S2;
S1 printAndInc(S1 s) {
s = incMembers(s);
printf("%u %d %d %lx %.17g\n", s.u8v, s.i32v, s.u16v, s.u64v, s.f64v);
return s;
}
void incMany(S2* list, uint64_t count) {
for (uint64_t i = 0; i < count; i++) {
list[i].a = printAndInc(list[i].a);
list[i].b = printAndInc(printAndInc(list[i].b));
}
}
typedef struct {
uint32_t* arr;
uint64_t sz;
} U32Arr;
uint32_t arrayOp(U32Arr a) {
uint32_t r = 0;
for (uint64_t i = 0; i < a.sz; i++) {
r+= a.arr[i];
a.arr[i]++;
}
return r;
}
uint32_t arrayRefOp(U32Arr* a) {
uint32_t res = arrayOp(*a);
a->arr++;
return res;
}
int plusone(int x) {
return x + 1;
}
int32_t arrarg(int32_t a[3]) {
return a[0] + a[1] + a[2];
}
typedef struct {
int32_t a[1];
} ArrStruct1;
int32_t arrstruct1(ArrStruct1 a, ArrStruct1* b) {
return a.a[0] + b->a[0];
}
int32_t ptrToArr(int32_t (*a)[2]) {
return a[3][0]+a[4][1];
}
typedef struct {
int32_t a[1];
ArrStruct1 b[2];
double c[3];
} ArrStruct2;
ArrStruct2 arrstruct2(ArrStruct2 a, ArrStruct2* b) {
return (ArrStruct2){
.a = {a.a[0] + b->a[0]},
.b = {{a.b[0].a[0] + b->b[1].a[0]}, {a.b[1].a[0] + b->b[0].a[0]}},
.c = {1.0, 2.0, a.c[0] + a.c[2] + b->c[1] + b->c[2]}
};
}
void arrstruct2Inc(ArrStruct2* b) {
for (int i = 0; i < 2; i++) {
b[i].a[0]++;
b[i].b[1].a[0]++;
b[i].c[1]++;
b[i].c[2]++;
}
}
int32_t callWithinMutated(BQNV v, int32_t* data) {
data[3] = 30;
BQNV r = bqn_call1(v, v);
bqn_free(v);
bqn_free(r);
return 123;
}
typedef struct {
float a0_0;
int8_t* a0_1;
int8_t a0_2[5];
} ManyArgs0;
void manyargs(ManyArgs0 a0, int8_t (*a1)[7], int8_t a2[4], int8_t* mut) {
mut++;
*mut++ = (int8_t) a0.a0_0;
*mut++ = a0.a0_1[0];
*mut++ = a0.a0_1[9];
*mut++ = a0.a0_2[0];
*mut++ = a0.a0_2[4];
*mut++ = a1[0][0];
*mut++ = a1[0][6];
*mut++ = a1[1][0];
*mut++ = a1[1][6];
*mut++ = a2[0];
*mut++ = a2[3];
}
typedef struct StructOfPtrs {
void* ptr1;
int32_t* ptr2;
int32_t* ptrs[2];
} StructOfPtrs;
StructOfPtrs operateOnStructOfPtrs(StructOfPtrs arg, StructOfPtrs ptr[2]) {
return (StructOfPtrs) {
.ptr1 = arg.ptr2,
.ptr2 = ptr[0].ptr1,
.ptrs = {arg.ptrs[0], ptr[1].ptrs[1]}
};
}
typedef struct PtrWrapper {
int32_t x;
char* ptr;
} PtrWrapper;
void updatePointerWithinPointer(PtrWrapper* ptr) {
for (int i = 0; i < 5; i++) {
ptr[i].x+= i*10;
ptr[i].ptr+= i;
}
}