-
Notifications
You must be signed in to change notification settings - Fork 13
/
cha_core.c
418 lines (386 loc) · 11.1 KB
/
cha_core.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
// cha_core.c - CHAPRO core functions
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "chapro.h"
#include "version.h"
#define free_null(p) if(p){free(p);p=NULL;}
/***********************************************************/
FUNC(char *)
cha_version(void)
{
return (VER);
}
FUNC(void)
cha_prepare(CHA_PTR cp)
{
if (cp[_size] == NULL) {
cp[_size] = calloc(NPTR, sizeof(int));
cp[_ivar] = calloc(NVAR, sizeof(int));
cp[_dvar] = calloc(NVAR, sizeof(double));
((int *)cp[_size])[_size] = NPTR * sizeof(int);
((int *)cp[_size])[_ivar] = NVAR * sizeof(int);
((int *)cp[_size])[_dvar] = NVAR * sizeof(double);
}
}
FUNC(void)
cha_chunk_size(CHA_PTR cp, int cs)
{
cha_prepare(cp);
CHA_IVAR[_cs] = cs;
}
FUNC(void *)
cha_allocate(CHA_PTR cp, int cnt, int siz, int idx)
{
cha_prepare(cp);
assert(idx < NPTR);
free_null(cp[idx]);
cp[idx] = calloc(cnt, siz);
((int *)cp[_size])[idx] = cnt * siz;
return (cp[idx]);
}
FUNC(void)
cha_cleanup(CHA_PTR cp)
{
int i;
for (i = 0; i < NPTR; i++) {
free_null(cp[i]);
}
}
/***********************************************************/
static int32_t *
data_plan(CHA_PTR cp, int *psz, int *asz)
{
int i, ptsiz, arsiz;
int32_t *cpsiz;
// count pointers
ptsiz = 0;
for (i = 0; i < NPTR; i++) {
if (cp[i]) ptsiz = i + 1;
}
// sum array sizes
cpsiz = (int32_t *) cp[_size]; //altered 11/17/2021 from (int *) to (int32_t *) to better match data type and avoid compiler warnings
arsiz = 0;
if (cpsiz) {
for (i = 0; i < NPTR; i++) {
if (cpsiz[i] < 0) {
arsiz = 0;
break;
}
arsiz += cpsiz[i];
}
}
*psz = ptsiz;
*asz = arsiz;
return(cpsiz);
}
static void
state_make(CHA_STA *state, int32_t *cpsiz, void **cp, int ptsiz, int arsiz)
{
char *data;
int i;
void *mkdata;
void **mkcp;
// allocate memory
mkcp = (void **)calloc(NPTR, sizeof(void *));
mkdata = calloc(arsiz, 1);
// copy pointers
memcpy(mkcp, cp, NPTR * sizeof(void *));
// copy data
data = (char *)mkdata;
for (i = 0; i < NPTR; i++) {
if (cp[i]) {
memcpy(data, cp[i], cpsiz[i]);
mkcp[i] = data;
data += cpsiz[i];
}
}
// return state variables
state->ptsiz = ptsiz;
state->arsiz = arsiz;
state->cp = mkcp;
state->data = mkdata;
state->sr = CHA_DVAR[_fs] * 1000;
state->cs = CHA_IVAR[_cs];
}
/***********************************************************/
FUNC(int)
cha_data_gen(CHA_PTR cp, char *fn)
{
int ptsiz, arsiz, arlen, i, j = 0;
int32_t *cpsiz;
uint32_t *ulptr;
FILE *fp;
static char *head[] = {
"#ifndef CHA_DATA_H",
"#define CHA_DATA_H",
""
};
static char *tail[] = {
"",
"#endif // CHA_DATA_H"
};
static int hdsz = sizeof(head) / sizeof(char *);
static int tlsz = sizeof(tail) / sizeof(char *);
static int ptpl = 15;
static int szpl = 8;
static int ivpl = 8;
static int dvpl = 4;
static int arpl = 6;
static int cptr = 4; // number of cast pointers
cpsiz = data_plan(cp, &ptsiz, &arsiz);
if (arsiz == 0) {
return (1);
}
// open file
fp = fopen(fn, "wt");
if (fp == NULL) {
return (2);
}
// print header
fprintf(fp, "// cha_data.h - data size = %d bytes\n", arsiz);
for (i = 0; i < hdsz; i++) {
fprintf(fp, "%s\n", head[i]);
}
// initialize magic number
if (ptsiz > 0) {
fprintf(fp, "static CHA_DATA cha_magic[4] = ");
fprintf(fp, "{0x55530,0x68131,%d,%d};\n", ptsiz, arsiz);
}
// initialize ptr arrays
for (i = 0; i < ptsiz; i++) {
if (i == _size) {
arlen = cpsiz[i] / sizeof(int32_t);
arsiz = 0;
ulptr = (uint32_t *) cp[i];
if (ulptr) {
for (j = 0; j < arlen; j++) {
if (ulptr[j]) arsiz = j + 1;
}
}
fprintf(fp, "static CHA_DATA p%02d[%8d] = { // _size\n", i, arlen);
for (j = 0; j < arsiz; j++) {
if ((j % szpl) == 0) fprintf(fp, " ");
fprintf(fp, "%8u", ulptr[j]);
if (j < (arsiz - 1)) fprintf(fp, ",");
if ((j % szpl) == (szpl - 1)) fprintf(fp, "\n");
}
if ((j % szpl) != 0) fprintf(fp, "\n");
fprintf(fp, "};\n");
} else if (i == _ivar) {
arlen = cpsiz[i] / sizeof(int);
arsiz = 1;
for (j = 1; j < arlen; j++) {
if (CHA_IVAR[j]) arsiz = j + 1;
}
fprintf(fp, "static CHA_DATA p%02d[%8d] = { // _ivar\n", i, arlen);
for (j = 0; j < arsiz; j++) {
if ((j % ivpl) == 0) fprintf(fp, " ");
fprintf(fp, "%8d", CHA_IVAR[j]);
if (j < (arsiz - 1)) fprintf(fp, ",");
if ((j % ivpl) == (ivpl - 1)) fprintf(fp, "\n");
}
if ((j % ivpl) != 0) fprintf(fp, "\n");
fprintf(fp, "};\n");
} else if (i == _dvar) {
arlen = cpsiz[i] / sizeof(double);
arsiz = 1;
for (j = 1; j < arlen; j++) {
if (CHA_DVAR[j]) arsiz = j + 1;
}
fprintf(fp, "static double p%02d[%8d] = { // _dvar\n", i, arlen);
for (j = 0; j < arsiz; j++) {
if ((j % dvpl) == 0) fprintf(fp, " ");
fprintf(fp, "%15.9g", CHA_DVAR[j]);
if (j < (arsiz - 1)) fprintf(fp, ",");
if ((j % dvpl) == (dvpl - 1)) fprintf(fp, "\n");
}
if ((j % dvpl) != 0) fprintf(fp, "\n");
fprintf(fp, "};\n");
} else if (cpsiz[i] == 0) {
fprintf(fp, "// empty array: p%02d\n", i);
} else if ((cpsiz[i] % sizeof(int32_t)) == 0) {
arlen = cpsiz[i] / sizeof(int32_t);
arsiz = 0;
ulptr = (uint32_t *) cp[i];
if (ulptr) {
for (j = 0; j < arlen; j++) {
if (ulptr[j]) arsiz = j + 1;
}
}
if (arsiz < 2) {
fprintf(fp, "static CHA_DATA p%02d[%8d] = {%10u};\n",
i, arlen, ulptr[0]);
} else {
fprintf(fp, "static CHA_DATA p%02d[%8d] = {\n", i, arlen);
for (j = 0; j < arsiz; j++) {
if ((j % arpl) == 0) fprintf(fp, " ");
fprintf(fp, "0x%08X", ulptr[j]);
if (j < (arsiz - 1)) fprintf(fp, ",");
if ((j % arpl) == (arpl - 1)) fprintf(fp, "\n");
}
if ((j % arpl) != 0) fprintf(fp, "\n");
fprintf(fp, "};\n");
}
} else if ((cpsiz[i] % sizeof(int16_t)) == 0) {
arlen = cpsiz[i] / sizeof(int16_t);
fprintf(fp, "// NOTE: Only zero data implemented unless size%%4==0.\n");
fprintf(fp, "static uint16_t p%02d[%8d] = {0};\n", i, arlen);
} else {
arlen = cpsiz[i];
fprintf(fp, "// NOTEL Only zero data implemented unless size%%4==0.\n");
fprintf(fp, "static unsigned char p%02d[%8d] = {0};\n", i, arlen);
}
}
fprintf(fp, "\n");
// initialize ptr
if (ptsiz < 1) {
fprintf(fp, "static CHA_DATA *cha_data[1] = {cha_magic};\n");
} else {
fprintf(fp, "static CHA_DATA *cha_data[%d] = {\n", NPTR);
fprintf(fp, " ");
for (i = 0; i < cptr; i++) {
fprintf(fp, "(CHA_DATA *)p%02d,", i);
}
fprintf(fp, "\n");
for (i = cptr; i < ptsiz; i++) {
j = i - cptr;
if ((j % ptpl) == 0) fprintf(fp, " ");
if (cpsiz[i] == 0) {
fprintf(fp, "NULL");
} else {
fprintf(fp, " p%02d", i);
}
if (i < (ptsiz - 1)) fprintf(fp, ",");
if ((j % ptpl) == (ptpl - 1)) fprintf(fp, "\n");
}
if ((j % ptpl) != (ptpl - 1)) fprintf(fp, "\n");
fprintf(fp, "};\n");
}
// print trailer
for (i = 0; i < tlsz; i++) {
fprintf(fp, "%s\n", tail[i]);
}
fclose(fp);
return (0);
}
FUNC(int)
cha_data_save(CHA_PTR cp, char *fn)
{
int ptsiz, arsiz, dtsiz, i;
int32_t *cpsiz;
CHA_DATA cha_magic[4];
FILE *fp;
cpsiz = data_plan(cp, &ptsiz, &arsiz);
if (arsiz == 0) {
return (1);
}
// open file
fp = fopen(fn, "wb");
if (fp == NULL) {
return (2);
}
dtsiz = sizeof(CHA_DATA);
// write magic number
cha_magic[0] = 0x55530;
cha_magic[1] = 0x68131;
cha_magic[2] = ptsiz;
cha_magic[3] = arsiz;
fwrite(cha_magic, dtsiz, 4, fp);
// write ptr arrays
for (i = 0; i < ptsiz; i++) {
if (cpsiz[i]) {
arsiz = cpsiz[i] / dtsiz;
fwrite(cp[i], dtsiz, arsiz, fp);
}
}
fclose(fp);
return (0);
}
FUNC(int)
cha_data_load(CHA_PTR cp, char *fn)
{
int ptsiz, arsiz, dtsiz, i, rv;
CHA_DATA cha_magic[4], file_magic[4], *file_size, *file_data;
FILE *fp;
(void) data_plan(cp, &ptsiz, &arsiz);
if (arsiz == 0) {
return (1);
}
// open file
fp = fopen(fn, "rb");
if (fp == NULL) {
return (2);
}
dtsiz = sizeof(CHA_DATA);
rv = 0; // default return value
// read magic number
cha_magic[0] = 0x55530;
cha_magic[1] = 0x68131;
cha_magic[2] = ptsiz;
cha_magic[3] = arsiz;
rv = fread(file_magic, dtsiz, 4, fp);
// check magic number
for (i = 0; i < 4; i++) {
if (file_magic[i] != cha_magic[i]) {
break;
}
}
if (i < 4) {
fclose(fp);
return (4);
}
// read & check size array
ptsiz = file_magic[2];
arsiz = NPTR * dtsiz;
file_size = (CHA_DATA *) cha_allocate(cp, NPTR, dtsiz, 0);
rv = fread(file_size, NPTR, dtsiz, fp);
if (file_size[0] != arsiz) {
return (4);
}
// read data arrays
for (i = 1; i < ptsiz; i++) {
if (file_size[i]) {
arsiz = file_size[i] / dtsiz;
file_data = (CHA_DATA *) cha_allocate(cp, arsiz, dtsiz, i);
rv = fread(file_data, arsiz, dtsiz, fp);
}
}
fclose(fp);
return (rv);
}
FUNC(int)
cha_state_save(CHA_PTR cp, CHA_STA *state)
{
int ptsiz, arsiz;
int32_t *cpsiz;
cpsiz = data_plan(cp, &ptsiz, &arsiz);
if (arsiz == 0) {
return (1);
}
state_make(state, cpsiz, cp, ptsiz, arsiz);
return (0);
}
FUNC(int)
cha_state_copy(CHA_STA *new_state, CHA_STA *old_state)
{
int ptsiz, arsiz;
int32_t *cpsiz;
void **cp;
cp = old_state->cp;
cpsiz = (int32_t *) cp[_size]; //altered 11/17/2021 from (int *) to (int32_t *) to better match data type and avoid compiler warnings
ptsiz = old_state->ptsiz;
arsiz = old_state->arsiz;
state_make(new_state, cpsiz, cp, ptsiz, arsiz);
return (0);
}
FUNC(int)
cha_state_free(CHA_STA *state)
{
free(state->cp);
free(state->data);
state->cp = NULL;
state->data = NULL;
return (0);
}