Debian Codegraph @

rts/c/values.h

raw
1 // Start of values.h.
2
3 //// Text I/O
4
5 typedef int (*writer)(FILE*, const void*);
6 typedef int (*bin_reader)(void*);
7 typedef int (*str_reader)(const char *, void*);
8
9 struct array_reader {
10 char* elems;
11 int64_t n_elems_space;
12 int64_t elem_size;
13 int64_t n_elems_used;
14 int64_t *shape;
15 str_reader elem_reader;
16 };
17
18 static void skipspaces(FILE *f) {
19 int c;
20 do {
21 c = getc(f);
22 } while (isspace(c));
23
24 if (c != EOF) {
25 ungetc(c, f);
26 }
27 }
28
29 static int constituent(char c) {
30 return isalnum(c) || c == '.' || c == '-' || c == '+' || c == '_';
31 }
32
33 // Produces an empty token only on EOF.
34 static void next_token(FILE *f, char *buf, int bufsize) {
35 start:
36 skipspaces(f);
37
38 int i = 0;
39 while (i < bufsize) {
40 int c = getc(f);
41 buf[i] = (char)c;
42
43 if (c == EOF) {
44 buf[i] = 0;
45 return;
46 } else if (c == '-' && i == 1 && buf[0] == '-') {
47 // Line comment, so skip to end of line and start over.
48 for (; c != '\n' && c != EOF; c = getc(f));
49 goto start;
50 } else if (!constituent((char)c)) {
51 if (i == 0) {
52 // We permit single-character tokens that are not
53 // constituents; this lets things like ']' and ',' be
54 // tokens.
55 buf[i+1] = 0;
56 return;
57 } else {
58 ungetc(c, f);
59 buf[i] = 0;
60 return;
61 }
62 }
63
64 i++;
65 }
66
67 buf[bufsize-1] = 0;
68 }
69
70 static int next_token_is(FILE *f, char *buf, int bufsize, const char* expected) {
71 next_token(f, buf, bufsize);
72 return strcmp(buf, expected) == 0;
73 }
74
75 static void remove_underscores(char *buf) {
76 char *w = buf;
77
78 for (char *r = buf; *r; r++) {
79 if (*r != '_') {
80 *w++ = *r;
81 }
82 }
83
84 *w++ = 0;
85 }
86
87 static int read_str_elem(char *buf, struct array_reader *reader) {
88 int ret;
89 if (reader->n_elems_used == reader->n_elems_space) {
90 reader->n_elems_space *= 2;
91 reader->elems = (char*) realloc(reader->elems,
92 (size_t)(reader->n_elems_space * reader->elem_size));
93 }
94
95 ret = reader->elem_reader(buf, reader->elems + reader->n_elems_used * reader->elem_size);
96
97 if (ret == 0) {
98 reader->n_elems_used++;
99 }
100
101 return ret;
102 }
103
104 static int read_str_array_elems(FILE *f,
105 char *buf, int bufsize,
106 struct array_reader *reader, int64_t dims) {
107 int ret = 1;
108 int expect_elem = 1;
109 char *knows_dimsize = (char*) calloc((size_t)dims, sizeof(char));
110 int cur_dim = (int)dims-1;
111 int64_t *elems_read_in_dim = (int64_t*) calloc((size_t)dims, sizeof(int64_t));
112
113 while (1) {
114 next_token(f, buf, bufsize);
115 if (strcmp(buf, "]") == 0) {
116 expect_elem = 0;
117 if (knows_dimsize[cur_dim]) {
118 if (reader->shape[cur_dim] != elems_read_in_dim[cur_dim]) {
119 ret = 1;
120 break;
121 }
122 } else {
123 knows_dimsize[cur_dim] = 1;
124 reader->shape[cur_dim] = elems_read_in_dim[cur_dim];
125 }
126 if (cur_dim == 0) {
127 ret = 0;
128 break;
129 } else {
130 cur_dim--;
131 elems_read_in_dim[cur_dim]++;
132 }
133 } else if (!expect_elem && strcmp(buf, ",") == 0) {
134 expect_elem = 1;
135 } else if (expect_elem) {
136 if (strcmp(buf, "[") == 0) {
137 if (cur_dim == dims - 1) {
138 ret = 1;
139 break;
140 }
141 cur_dim++;
142 elems_read_in_dim[cur_dim] = 0;
143 } else if (cur_dim == dims - 1) {
144 ret = read_str_elem(buf, reader);
145 if (ret != 0) {
146 break;
147 }
148 expect_elem = 0;
149 elems_read_in_dim[cur_dim]++;
150 } else {
151 ret = 1;
152 break;
153 }
154 } else {
155 ret = 1;
156 break;
157 }
158 }
159
160 free(knows_dimsize);
161 free(elems_read_in_dim);
162 return ret;
163 }
164
165 static int read_str_empty_array(FILE *f, char *buf, int bufsize,
166 const char *type_name, int64_t *shape, int64_t dims) {
167 if (strlen(buf) == 0) {
168 // EOF
169 return 1;
170 }
171
172 if (strcmp(buf, "empty") != 0) {
173 return 1;
174 }
175
176 if (!next_token_is(f, buf, bufsize, "(")) {
177 return 1;
178 }
179
180 for (int i = 0; i < dims; i++) {
181 if (!next_token_is(f, buf, bufsize, "[")) {
182 return 1;
183 }
184
185 next_token(f, buf, bufsize);
186
187 if (sscanf(buf, "%"SCNu64, (uint64_t*)&shape[i]) != 1) {
188 return 1;
189 }
190
191 if (!next_token_is(f, buf, bufsize, "]")) {
192 return 1;
193 }
194 }
195
196 if (!next_token_is(f, buf, bufsize, type_name)) {
197 return 1;
198 }
199
200
201 if (!next_token_is(f, buf, bufsize, ")")) {
202 return 1;
203 }
204
205 // Check whether the array really is empty.
206 for (int i = 0; i < dims; i++) {
207 if (shape[i] == 0) {
208 return 0;
209 }
210 }
211
212 // Not an empty array!
213 return 1;
214 }
215
216 static int read_str_array(FILE *f,
217 int64_t elem_size, str_reader elem_reader,
218 const char *type_name,
219 void **data, int64_t *shape, int64_t dims) {
220 int ret;
221 struct array_reader reader;
222 char buf[100];
223
224 int dims_seen;
225 for (dims_seen = 0; dims_seen < dims; dims_seen++) {
226 if (!next_token_is(f, buf, sizeof(buf), "[")) {
227 break;
228 }
229 }
230
231 if (dims_seen == 0) {
232 return read_str_empty_array(f, buf, sizeof(buf), type_name, shape, dims);
233 }
234
235 if (dims_seen != dims) {
236 return 1;
237 }
238
239 reader.shape = shape;
240 reader.n_elems_used = 0;
241 reader.elem_size = elem_size;
242 reader.n_elems_space = 16;
243 reader.elems = (char*) realloc(*data, (size_t)(elem_size*reader.n_elems_space));
244 reader.elem_reader = elem_reader;
245
246 ret = read_str_array_elems(f, buf, sizeof(buf), &reader, dims);
247
248 *data = reader.elems;
249
250 return ret;
251 }
252
253 #define READ_STR(MACRO, PTR, SUFFIX) \
254 remove_underscores(buf); \
255 int j; \
256 if (sscanf(buf, "%"MACRO"%n", (PTR*)dest, &j) == 1) { \
257 return !(strcmp(buf+j, "") == 0 || strcmp(buf+j, SUFFIX) == 0); \
258 } else { \
259 return 1; \
260 }
261
262 static int read_str_i8(char *buf, void* dest) {
263 // Some platforms (WINDOWS) does not support scanf %hhd or its
264 // cousin, %SCNi8. Read into int first to avoid corrupting
265 // memory.
266 //
267 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63417
268 remove_underscores(buf);
269 int j, x;
270 if (sscanf(buf, "%i%n", &x, &j) == 1) {
271 *(int8_t*)dest = (int8_t)x;
272 return !(strcmp(buf+j, "") == 0 || strcmp(buf+j, "i8") == 0);
273 } else {
274 return 1;
275 }
276 }
277
278 static int read_str_u8(char *buf, void* dest) {
279 // Some platforms (WINDOWS) does not support scanf %hhd or its
280 // cousin, %SCNu8. Read into int first to avoid corrupting
281 // memory.
282 //
283 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63417
284 remove_underscores(buf);
285 int j, x;
286 if (sscanf(buf, "%i%n", &x, &j) == 1) {
287 *(uint8_t*)dest = (uint8_t)x;
288 return !(strcmp(buf+j, "") == 0 || strcmp(buf+j, "u8") == 0);
289 } else {
290 return 1;
291 }
292 }
293
294 static int read_str_i16(char *buf, void* dest) {
295 READ_STR(SCNi16, int16_t, "i16");
296 }
297
298 static int read_str_u16(char *buf, void* dest) {
299 READ_STR(SCNi16, int16_t, "u16");
300 }
301
302 static int read_str_i32(char *buf, void* dest) {
303 READ_STR(SCNi32, int32_t, "i32");
304 }
305
306 static int read_str_u32(char *buf, void* dest) {
307 READ_STR(SCNi32, int32_t, "u32");
308 }
309
310 static int read_str_i64(char *buf, void* dest) {
311 READ_STR(SCNi64, int64_t, "i64");
312 }
313
314 static int read_str_u64(char *buf, void* dest) {
315 // FIXME: This is not correct, as SCNu64 only permits decimal
316 // literals. However, SCNi64 does not handle very large numbers
317 // correctly (it's really for signed numbers, so that's fair).
318 READ_STR(SCNu64, uint64_t, "u64");
319 }
320
321 static int read_str_f16(char *buf, void* dest) {
322 remove_underscores(buf);
323 if (strcmp(buf, "f16.nan") == 0) {
324 *(uint16_t*)dest = float2halfbits(NAN);
325 return 0;
326 } else if (strcmp(buf, "f16.inf") == 0) {
327 *(uint16_t*)dest = float2halfbits(INFINITY);
328 return 0;
329 } else if (strcmp(buf, "-f16.inf") == 0) {
330 *(uint16_t*)dest = float2halfbits(-INFINITY);
331 return 0;
332 } else {
333 int j;
334 float x;
335 if (sscanf(buf, "%f%n", &x, &j) == 1) {
336 if (strcmp(buf+j, "") == 0 || strcmp(buf+j, "f16") == 0) {
337 *(uint16_t*)dest = float2halfbits(x);
338 return 0;
339 }
340 }
341 return 1;
342 }
343 }
344
345 static int read_str_f32(char *buf, void* dest) {
346 remove_underscores(buf);
347 if (strcmp(buf, "f32.nan") == 0) {
348 *(float*)dest = (float)NAN;
349 return 0;
350 } else if (strcmp(buf, "f32.inf") == 0) {
351 *(float*)dest = (float)INFINITY;
352 return 0;
353 } else if (strcmp(buf, "-f32.inf") == 0) {
354 *(float*)dest = (float)-INFINITY;
355 return 0;
356 } else {
357 READ_STR("f", float, "f32");
358 }
359 }
360
361 static int read_str_f64(char *buf, void* dest) {
362 remove_underscores(buf);
363 if (strcmp(buf, "f64.nan") == 0) {
364 *(double*)dest = (double)NAN;
365 return 0;
366 } else if (strcmp(buf, "f64.inf") == 0) {
367 *(double*)dest = (double)INFINITY;
368 return 0;
369 } else if (strcmp(buf, "-f64.inf") == 0) {
370 *(double*)dest = (double)-INFINITY;
371 return 0;
372 } else {
373 READ_STR("lf", double, "f64");
374 }
375 }
376
377 static int read_str_bool(char *buf, void* dest) {
378 if (strcmp(buf, "true") == 0) {
379 *(char*)dest = 1;
380 return 0;
381 } else if (strcmp(buf, "false") == 0) {
382 *(char*)dest = 0;
383 return 0;
384 } else {
385 return 1;
386 }
387 }
388
389 static int write_str_i8(FILE *out, int8_t *src) {
390 return fprintf(out, "%hhdi8", *src);
391 }
392
393 static int write_str_u8(FILE *out, uint8_t *src) {
394 return fprintf(out, "%hhuu8", *src);
395 }
396
397 static int write_str_i16(FILE *out, int16_t *src) {
398 return fprintf(out, "%hdi16", *src);
399 }
400
401 static int write_str_u16(FILE *out, uint16_t *src) {
402 return fprintf(out, "%huu16", *src);
403 }
404
405 static int write_str_i32(FILE *out, int32_t *src) {
406 return fprintf(out, "%di32", *src);
407 }
408
409 static int write_str_u32(FILE *out, uint32_t *src) {
410 return fprintf(out, "%uu32", *src);
411 }
412
413 static int write_str_i64(FILE *out, int64_t *src) {
414 return fprintf(out, "%"PRIi64"i64", *src);
415 }
416
417 static int write_str_u64(FILE *out, uint64_t *src) {
418 return fprintf(out, "%"PRIu64"u64", *src);
419 }
420
421 static int write_str_f16(FILE *out, uint16_t *src) {
422 float x = halfbits2float(*src);
423 if (isnan(x)) {
424 return fprintf(out, "f16.nan");
425 } else if (isinf(x) && x >= 0) {
426 return fprintf(out, "f16.inf");
427 } else if (isinf(x)) {
428 return fprintf(out, "-f16.inf");
429 } else {
430 return fprintf(out, "%.*ff16", FLT_DIG, x);
431 }
432 }
433
434 static int write_str_f32(FILE *out, float *src) {
435 float x = *src;
436 if (isnan(x)) {
437 return fprintf(out, "f32.nan");
438 } else if (isinf(x) && x >= 0) {
439 return fprintf(out, "f32.inf");
440 } else if (isinf(x)) {
441 return fprintf(out, "-f32.inf");
442 } else {
443 return fprintf(out, "%.*ff32", FLT_DIG, x);
444 }
445 }
446
447 static int write_str_f64(FILE *out, double *src) {
448 double x = *src;
449 if (isnan(x)) {
450 return fprintf(out, "f64.nan");
451 } else if (isinf(x) && x >= 0) {
452 return fprintf(out, "f64.inf");
453 } else if (isinf(x)) {
454 return fprintf(out, "-f64.inf");
455 } else {
456 return fprintf(out, "%.*ff64", DBL_DIG, x);
457 }
458 }
459
460 static int write_str_bool(FILE *out, void *src) {
461 return fprintf(out, *(char*)src ? "true" : "false");
462 }
463
464 //// Binary I/O
465
466 #define BINARY_FORMAT_VERSION 2
467 #define IS_BIG_ENDIAN (!*(unsigned char *)&(uint16_t){1})
468
469 static void flip_bytes(size_t elem_size, unsigned char *elem) {
470 for (size_t j=0; j<elem_size/2; j++) {
471 unsigned char head = elem[j];
472 size_t tail_index = elem_size-1-j;
473 elem[j] = elem[tail_index];
474 elem[tail_index] = head;
475 }
476 }
477
478 // On Windows we need to explicitly set the file mode to not mangle
479 // newline characters. On *nix there is no difference.
480 #ifdef _WIN32
481 #include <io.h>
482 #include <fcntl.h>
483 static void set_binary_mode(FILE *f) {
484 setmode(fileno(f), O_BINARY);
485 }
486 #else
487 static void set_binary_mode(FILE *f) {
488 (void)f;
489 }
490 #endif
491
492 static int read_byte(FILE *f, void* dest) {
493 size_t num_elems_read = fread(dest, 1, 1, f);
494 return num_elems_read == 1 ? 0 : 1;
495 }
496
497 //// Types
498
499 struct primtype_info_t {
500 const char binname[4]; // Used for parsing binary data.
501 const char* type_name; // Same name as in Futhark.
502 const int64_t size; // in bytes
503 const writer write_str; // Write in text format.
504 const str_reader read_str; // Read in text format.
505 };
506
507 static const struct primtype_info_t i8_info =
508 {.binname = " i8", .type_name = "i8", .size = 1,
509 .write_str = (writer)write_str_i8, .read_str = (str_reader)read_str_i8};
510 static const struct primtype_info_t i16_info =
511 {.binname = " i16", .type_name = "i16", .size = 2,
512 .write_str = (writer)write_str_i16, .read_str = (str_reader)read_str_i16};
513 static const struct primtype_info_t i32_info =
514 {.binname = " i32", .type_name = "i32", .size = 4,
515 .write_str = (writer)write_str_i32, .read_str = (str_reader)read_str_i32};
516 static const struct primtype_info_t i64_info =
517 {.binname = " i64", .type_name = "i64", .size = 8,
518 .write_str = (writer)write_str_i64, .read_str = (str_reader)read_str_i64};
519 static const struct primtype_info_t u8_info =
520 {.binname = " u8", .type_name = "u8", .size = 1,
521 .write_str = (writer)write_str_u8, .read_str = (str_reader)read_str_u8};
522 static const struct primtype_info_t u16_info =
523 {.binname = " u16", .type_name = "u16", .size = 2,
524 .write_str = (writer)write_str_u16, .read_str = (str_reader)read_str_u16};
525 static const struct primtype_info_t u32_info =
526 {.binname = " u32", .type_name = "u32", .size = 4,
527 .write_str = (writer)write_str_u32, .read_str = (str_reader)read_str_u32};
528 static const struct primtype_info_t u64_info =
529 {.binname = " u64", .type_name = "u64", .size = 8,
530 .write_str = (writer)write_str_u64, .read_str = (str_reader)read_str_u64};
531 static const struct primtype_info_t f16_info =
532 {.binname = " f16", .type_name = "f16", .size = 2,
533 .write_str = (writer)write_str_f16, .read_str = (str_reader)read_str_f16};
534 static const struct primtype_info_t f32_info =
535 {.binname = " f32", .type_name = "f32", .size = 4,
536 .write_str = (writer)write_str_f32, .read_str = (str_reader)read_str_f32};
537 static const struct primtype_info_t f64_info =
538 {.binname = " f64", .type_name = "f64", .size = 8,
539 .write_str = (writer)write_str_f64, .read_str = (str_reader)read_str_f64};
540 static const struct primtype_info_t bool_info =
541 {.binname = "bool", .type_name = "bool", .size = 1,
542 .write_str = (writer)write_str_bool, .read_str = (str_reader)read_str_bool};
543
544 static const struct primtype_info_t* primtypes[] = {
545 &i8_info, &i16_info, &i32_info, &i64_info,
546 &u8_info, &u16_info, &u32_info, &u64_info,
547 &f16_info, &f32_info, &f64_info,
548 &bool_info,
549 NULL // NULL-terminated
550 };
551
552 // General value interface. All endian business taken care of at
553 // lower layers.
554
555 static int read_is_binary(FILE *f) {
556 skipspaces(f);
557 int c = getc(f);
558 if (c == 'b') {
559 int8_t bin_version;
560 int ret = read_byte(f, &bin_version);
561
562 if (ret != 0) { futhark_panic(1, "binary-input: could not read version.\n"); }
563
564 if (bin_version != BINARY_FORMAT_VERSION) {
565 futhark_panic(1, "binary-input: File uses version %i, but I only understand version %i.\n",
566 bin_version, BINARY_FORMAT_VERSION);
567 }
568
569 return 1;
570 }
571 ungetc(c, f);
572 return 0;
573 }
574
575 static const struct primtype_info_t* read_bin_read_type_enum(FILE *f) {
576 char read_binname[4];
577
578 int num_matched = fscanf(f, "%4c", read_binname);
579 if (num_matched != 1) { futhark_panic(1, "binary-input: Couldn't read element type.\n"); }
580
581 const struct primtype_info_t **type = primtypes;
582
583 for (; *type != NULL; type++) {
584 // I compare the 4 characters manually instead of using strncmp because
585 // this allows any value to be used, also NULL bytes
586 if (memcmp(read_binname, (*type)->binname, 4) == 0) {
587 return *type;
588 }
589 }
590 futhark_panic(1, "binary-input: Did not recognize the type '%s'.\n", read_binname);
591 return NULL;
592 }
593
594 static void read_bin_ensure_scalar(FILE *f, const struct primtype_info_t *expected_type) {
595 int8_t bin_dims;
596 int ret = read_byte(f, &bin_dims);
597 if (ret != 0) { futhark_panic(1, "binary-input: Couldn't get dims.\n"); }
598
599 if (bin_dims != 0) {
600 futhark_panic(1, "binary-input: Expected scalar (0 dimensions), but got array with %i dimensions.\n",
601 bin_dims);
602 }
603
604 const struct primtype_info_t *bin_type = read_bin_read_type_enum(f);
605 if (bin_type != expected_type) {
606 futhark_panic(1, "binary-input: Expected scalar of type %s but got scalar of type %s.\n",
607 expected_type->type_name,
608 bin_type->type_name);
609 }
610 }
611
612 //// High-level interface
613
614 static int read_bin_array(FILE *f,
615 const struct primtype_info_t *expected_type, void **data, int64_t *shape, int64_t dims) {
616 int ret;
617
618 int8_t bin_dims;
619 ret = read_byte(f, &bin_dims);
620 if (ret != 0) { futhark_panic(1, "binary-input: Couldn't get dims.\n"); }
621
622 if (bin_dims != dims) {
623 futhark_panic(1, "binary-input: Expected %i dimensions, but got array with %i dimensions.\n",
624 dims, bin_dims);
625 }
626
627 const struct primtype_info_t *bin_primtype = read_bin_read_type_enum(f);
628 if (expected_type != bin_primtype) {
629 futhark_panic(1, "binary-input: Expected %iD-array with element type '%s' but got %iD-array with element type '%s'.\n",
630 dims, expected_type->type_name, dims, bin_primtype->type_name);
631 }
632
633 int64_t elem_count = 1;
634 for (int i=0; i<dims; i++) {
635 int64_t bin_shape;
636 ret = (int)fread(&bin_shape, sizeof(bin_shape), 1, f);
637 if (ret != 1) {
638 futhark_panic(1, "binary-input: Couldn't read size for dimension %i of array.\n", i);
639 }
640 if (IS_BIG_ENDIAN) {
641 flip_bytes(sizeof(bin_shape), (unsigned char*) &bin_shape);
642 }
643 elem_count *= bin_shape;
644 shape[i] = bin_shape;
645 }
646
647 int64_t elem_size = expected_type->size;
648 void* tmp = realloc(*data, (size_t)(elem_count * elem_size));
649 if (tmp == NULL) {
650 futhark_panic(1, "binary-input: Failed to allocate array of size %i.\n",
651 elem_count * elem_size);
652 }
653 *data = tmp;
654
655 int64_t num_elems_read = (int64_t)fread(*data, (size_t)elem_size, (size_t)elem_count, f);
656 if (num_elems_read != elem_count) {
657 futhark_panic(1, "binary-input: tried to read %i elements of an array, but only got %i elements.\n",
658 elem_count, num_elems_read);
659 }
660
661 // If we're on big endian platform we must change all multibyte elements
662 // from using little endian to big endian
663 if (IS_BIG_ENDIAN && elem_size != 1) {
664 flip_bytes((size_t)elem_size, (unsigned char*) *data);
665 }
666
667 return 0;
668 }
669
670 static int read_array(FILE *f, const struct primtype_info_t *expected_type, void **data, int64_t *shape, int64_t dims) {
671 if (!read_is_binary(f)) {
672 return read_str_array(f, expected_type->size, (str_reader)expected_type->read_str, expected_type->type_name, data, shape, dims);
673 } else {
674 return read_bin_array(f, expected_type, data, shape, dims);
675 }
676 }
677
678 static int end_of_input(FILE *f) {
679 skipspaces(f);
680 char token[2];
681 next_token(f, token, sizeof(token));
682 if (strcmp(token, "") == 0) {
683 return 0;
684 } else {
685 return 1;
686 }
687 }
688
689 static int write_str_array(FILE *out,
690 const struct primtype_info_t *elem_type,
691 const unsigned char *data,
692 const int64_t *shape,
693 int8_t rank) {
694 if (rank==0) {
695 elem_type->write_str(out, (const void*)data);
696 } else {
697 int64_t len = (int64_t)shape[0];
698 int64_t slice_size = 1;
699
700 int64_t elem_size = elem_type->size;
701 for (int8_t i = 1; i < rank; i++) {
702 slice_size *= shape[i];
703 }
704
705 if (len*slice_size == 0) {
706 fprintf(out, "empty(");
707 for (int64_t i = 0; i < rank; i++) {
708 fprintf(out, "[%"PRIi64"]", shape[i]);
709 }
710 fprintf(out, "%s", elem_type->type_name);
711 fprintf(out, ")");
712 } else if (rank==1) {
713 fputc('[', out);
714 for (int64_t i = 0; i < len; i++) {
715 elem_type->write_str(out, (const void*) (data + i * elem_size));
716 if (i != len-1) {
717 fprintf(out, ", ");
718 }
719 }
720 fputc(']', out);
721 } else {
722 fputc('[', out);
723 for (int64_t i = 0; i < len; i++) {
724 write_str_array(out, elem_type, data + i * slice_size * elem_size, shape+1, rank-1);
725 if (i != len-1) {
726 fprintf(out, ", ");
727 }
728 }
729 fputc(']', out);
730 }
731 }
732 return 0;
733 }
734
735 static int write_bin_array(FILE *out,
736 const struct primtype_info_t *elem_type,
737 const unsigned char *data,
738 const int64_t *shape,
739 int8_t rank) {
740 int64_t num_elems = 1;
741 for (int64_t i = 0; i < rank; i++) {
742 num_elems *= shape[i];
743 }
744
745 fputc('b', out);
746 fputc((char)BINARY_FORMAT_VERSION, out);
747 fwrite(&rank, sizeof(int8_t), 1, out);
748 fwrite(elem_type->binname, 4, 1, out);
749 if (shape != NULL) {
750 fwrite(shape, sizeof(int64_t), (size_t)rank, out);
751 }
752
753 if (IS_BIG_ENDIAN) {
754 for (int64_t i = 0; i < num_elems; i++) {
755 const unsigned char *elem = data+i*elem_type->size;
756 for (int64_t j = 0; j < elem_type->size; j++) {
757 fwrite(&elem[elem_type->size-j], 1, 1, out);
758 }
759 }
760 } else {
761 fwrite(data, (size_t)elem_type->size, (size_t)num_elems, out);
762 }
763
764 return 0;
765 }
766
767 static int write_array(FILE *out, int write_binary,
768 const struct primtype_info_t *elem_type,
769 const void *data,
770 const int64_t *shape,
771 const int8_t rank) {
772 if (write_binary) {
773 return write_bin_array(out, elem_type, data, shape, rank);
774 } else {
775 return write_str_array(out, elem_type, data, shape, rank);
776 }
777 }
778
779 static int read_scalar(FILE *f,
780 const struct primtype_info_t *expected_type, void *dest) {
781 if (!read_is_binary(f)) {
782 char buf[100];
783 next_token(f, buf, sizeof(buf));
784 return expected_type->read_str(buf, dest);
785 } else {
786 read_bin_ensure_scalar(f, expected_type);
787 size_t elem_size = (size_t)expected_type->size;
788 size_t num_elems_read = fread(dest, elem_size, 1, f);
789 if (IS_BIG_ENDIAN) {
790 flip_bytes(elem_size, (unsigned char*) dest);
791 }
792 return num_elems_read == 1 ? 0 : 1;
793 }
794 }
795
796 static int write_scalar(FILE *out, int write_binary, const struct primtype_info_t *type, void *src) {
797 if (write_binary) {
798 return write_bin_array(out, type, src, NULL, 0);
799 } else {
800 return type->write_str(out, src);
801 }
802 }
803
804 // End of values.h.