forked from xnd-project/libndtypes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ndtypes.h.in
811 lines (640 loc) · 25.4 KB
/
ndtypes.h.in
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
/*
* BSD 3-Clause License
*
* Copyright (c) 2017-2018, plures
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef NDTYPES_H
#define NDTYPES_H
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
#include <stdbool.h>
#include <stddef.h>
#include <complex.h>
#if SIZE_MAX > ULLONG_MAX
#error "need SIZE_MAX <= ULLONG_MAX"
#endif
#if SIZE_MAX == UINT32_MAX
typedef int32_t ndt_ssize_t;
#define PRI_ndt_ssize PRIi32
#elif SIZE_MAX == UINT64_MAX
typedef int64_t ndt_ssize_t;
#define PRI_ndt_ssize PRIi64
#else
#error "need SIZE_MAX == UINT32_MAX or SIZE_MAX == UINT64_MAX"
#endif
#ifdef _MSC_VER
#if defined (EXPORT)
#define NDTYPES_API __declspec(dllexport)
#elif defined(IMPORT)
#define NDTYPES_API __declspec(dllimport)
#else
#define NDTYPES_API
#endif
typedef _Dcomplex ndt_complex128_t;
typedef _Fcomplex ndt_complex64_t;
#define alignof __alignof
#define alignas(n) __declspec(align(n))
#define MAX_ALIGN 8
#define NDT_SYS_BIG_ENDIAN 0
#else
#define NDTYPES_API
#if !defined(__APPLE__) && !defined(__STDC_IEC_559__)
#error "ndtypes requires IEEE floating point arithmetic"
#endif
#include <stdalign.h>
typedef double complex ndt_complex128_t;
typedef float complex ndt_complex64_t;
#define MAX_ALIGN (alignof(max_align_t))
#define NDT_SYS_BIG_ENDIAN @NDT_SYS_BIG_ENDIAN@
#endif
#if (defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__)) && \
defined(__GNUC__) && __GNUC__ >= 4 && !defined(__INTEL_COMPILER)
#define NDT_PRAGMA(x) _Pragma(x)
#define NDT_HIDE_SYMBOLS_START "GCC visibility push(hidden)"
#define NDT_HIDE_SYMBOLS_END "GCC visibility pop"
#else
#define NDT_PRAGMA(x)
#define NDT_HIDE_SYMBOLS_START
#define NDT_HIDE_SYMBOLS_END
#endif
/*****************************************************************************/
/* Datashape */
/*****************************************************************************/
#define NDT_MAX_DIM 128
#define NDT_MAX_ARGS 128
#define NDT_LITTLE_ENDIAN 0x00000001U
#define NDT_BIG_ENDIAN 0x00000002U
#define NDT_OPTION 0x00000004U
#define NDT_SUBTREE_OPTION 0x00000008U
#define NDT_ELLIPSIS 0x00000010U
/* Types: ndt_t */
typedef struct _ndt ndt_t;
typedef struct _ndt_context ndt_context_t;
/* Internal option type */
enum ndt_option {
None,
Some
};
typedef struct {
enum ndt_option tag;
char Some;
} char_opt_t;
typedef struct {
enum ndt_option tag;
int64_t Some;
} int64_opt_t;
typedef struct {
enum ndt_option tag;
uint16_t Some;
} uint16_opt_t;
/* Flag for variadic tuples and records */
enum ndt_variadic {
Nonvariadic,
Variadic
};
/* Ownership flag for var dim offsets */
enum ndt_offsets {
InternalOffsets,
ExternalOffsets,
};
/*
* Collect offsets during parsing for transferring ownership to an external
* resource manager later.
*/
typedef struct {
int num_offset_arrays; /* number of offset arrays */
int32_t num_offsets[NDT_MAX_DIM]; /* lengths of the offset arrays */
int32_t *offset_arrays[NDT_MAX_DIM]; /* offset arrays */
} ndt_meta_t;
/* Encoding for characters and strings */
enum ndt_encoding {
Ascii,
Utf8,
Utf16,
Utf32,
Ucs2,
};
/* Datashape kinds */
enum ndt {
/* Name space */
Module,
/* Function */
Function,
/* Any */
AnyKind,
FixedDim,
VarDim,
SymbolicDim,
EllipsisDim,
/* Dtype */
Tuple,
Record,
Ref,
Constr,
Nominal,
/* Scalar */
ScalarKind,
Categorical,
FixedStringKind,
FixedString,
FixedBytesKind,
FixedBytes,
String,
Bytes,
Char,
/* Primitive */
Bool,
SignedKind,
Int8,
Int16,
Int32,
Int64,
UnsignedKind,
Uint8,
Uint16,
Uint32,
Uint64,
FloatKind,
Float16,
Float32,
Float64,
ComplexKind,
Complex32,
Complex64,
Complex128,
/* Dtype variable */
Typevar,
};
enum ndt_alias {
Size,
Intptr,
Uintptr
};
/* Protect access to concrete type fields. */
enum ndt_access {
Abstract,
Concrete
};
/* Tuple or record field, used in the parser. */
typedef struct {
enum ndt_access access;
char *name;
ndt_t *type;
struct {
uint16_t align;
bool explicit_align;
uint16_t pad;
bool explicit_pad;
} Concrete;
} ndt_field_t;
/* Selected values for the categorical type. */
enum ndt_value {
ValBool,
ValInt64,
ValFloat64,
ValString,
ValNA,
};
typedef struct {
enum ndt_value tag;
union {
bool ValBool;
int64_t ValInt64;
double ValFloat64;
char *ValString;
};
} ndt_value_t;
typedef struct {
int64_t start;
int64_t stop;
int64_t step;
} ndt_slice_t;
/* Datashape type */
struct _ndt {
/* Always defined */
enum ndt tag;
enum ndt_access access;
uint32_t flags;
int ndim;
/* Undefined if the type is abstract */
int64_t datasize;
uint16_t align;
/* Abstract */
union {
struct {
char *name;
ndt_t *type;
} Module;
struct {
int64_t nin;
int64_t nout;
int64_t nargs;
ndt_t **types;
} Function;
struct {
int64_t shape;
ndt_t *type;
} FixedDim;
struct {
ndt_t *type;
} VarDim;
struct {
char *name;
ndt_t *type;
} SymbolicDim;
struct {
char *name;
ndt_t *type;
} EllipsisDim;
struct {
enum ndt_variadic flag;
int64_t shape;
ndt_t **types;
} Tuple;
struct {
enum ndt_variadic flag;
int64_t shape;
char **names;
ndt_t **types;
} Record;
struct {
ndt_t *type;
} Ref;
struct {
char *name;
ndt_t *type;
} Constr;
struct {
char *name;
ndt_t *type;
bool (* constraint)(void *, ndt_context_t *);
} Nominal;
struct {
int64_t ntypes;
ndt_value_t *types;
} Categorical;
struct {
int64_t size;
enum ndt_encoding encoding;
} FixedString;
struct {
int64_t size;
uint16_t align;
} FixedBytes;
struct {
uint16_t target_align;
} Bytes;
struct {
enum ndt_encoding encoding;
} Char;
struct {
char *name;
} Typevar;
};
/* Concrete */
struct {
union {
struct {
int64_t itemsize;
int64_t step;
} FixedDim;
struct {
enum ndt_offsets flag;
int64_t itemsize;
int32_t noffsets;
const int32_t *offsets;
int nslices;
ndt_slice_t *slices;
} VarDim;
struct {
int64_t *offset;
uint16_t *align;
uint16_t *pad;
} Tuple;
struct {
int64_t *offset;
uint16_t *align;
uint16_t *pad;
} Record;
};
} Concrete;
alignas(MAX_ALIGN) char extra[];
};
/*****************************************************************************/
/* Context and error handling */
/*****************************************************************************/
#define NDT_Dynamic 0x00000001U
#define NDT_STATIC_CONTEXT(name) \
ndt_context_t name = { .flags=0, .err=NDT_Success, .msg=ConstMsg, .ConstMsg="Success" }
enum ndt_error {
NDT_Success,
NDT_ValueError,
NDT_TypeError,
NDT_InvalidArgumentError,
NDT_NotImplementedError,
NDT_LexError,
NDT_ParseError,
NDT_OSError,
NDT_RuntimeError,
NDT_MemoryError
};
enum ndt_msg {
ConstMsg,
DynamicMsg
};
struct _ndt_context {
uint32_t flags;
enum ndt_error err;
enum ndt_msg msg;
union {
const char *ConstMsg;
char *DynamicMsg;
};
};
NDTYPES_API ndt_context_t *ndt_context_new(void);
NDTYPES_API void ndt_context_del(ndt_context_t *ctx);
NDTYPES_API void ndt_err_format(ndt_context_t *ctx, enum ndt_error err, const char *fmt, ...);
NDTYPES_API int ndt_err_occurred(const ndt_context_t *ctx);
NDTYPES_API void ndt_err_clear(ndt_context_t *ctx);
NDTYPES_API void *ndt_memory_error(ndt_context_t *ctx);
NDTYPES_API const char *ndt_err_as_string(enum ndt_error err);
NDTYPES_API const char *ndt_context_msg(ndt_context_t *ctx);
NDTYPES_API void ndt_err_fprint(FILE *fp, ndt_context_t *ctx);
/******************************************************************************/
/* Array conversions and properties */
/******************************************************************************/
/* This may go into xnd at some point. */
typedef struct {
int ndim;
int64_t itemsize;
int64_t shape[NDT_MAX_DIM];
int64_t strides[NDT_MAX_DIM];
int64_t steps[NDT_MAX_DIM];
} ndt_ndarray_t;
/* This may go into gumath at some point. */
enum ndt_apply {
C,
Fortran,
Strided,
Xnd
};
typedef struct {
enum ndt_apply tag;
int nout;
int nbroadcast;
int outer_dims;
ndt_t *out[NDT_MAX_ARGS];
ndt_t *broadcast[NDT_MAX_ARGS];
} ndt_apply_spec_t;
NDTYPES_API extern const ndt_apply_spec_t ndt_apply_spec_empty;
NDTYPES_API ndt_apply_spec_t *ndt_apply_spec_new(ndt_context_t *ctx);
NDTYPES_API void ndt_apply_spec_clear(ndt_apply_spec_t *spec);
NDTYPES_API void ndt_apply_spec_del(ndt_apply_spec_t *spec);
NDTYPES_API const char *ndt_apply_tag_as_string(ndt_apply_spec_t *spec);
NDTYPES_API int ndt_select_kernel_strategy(ndt_apply_spec_t *spec, const ndt_t *sig,
const ndt_t *in[], int nin,
ndt_context_t *ctx);
/*****************************************************************************/
/* Utilities */
/*****************************************************************************/
/* String and formatting utilities */
NDTYPES_API char *ndt_strdup(const char *s, ndt_context_t *ctx);
NDTYPES_API char *ndt_asprintf(ndt_context_t *ctx, const char *fmt, ...);
/* Type functions (unstable API) */
NDTYPES_API const ndt_t *ndt_dtype(const ndt_t *t);
NDTYPES_API const ndt_t *ndt_hidden_dtype(const ndt_t *t);
NDTYPES_API int ndt_dims_dtype(const ndt_t *dims[NDT_MAX_DIM], const ndt_t **dtype, const ndt_t *t);
NDTYPES_API int ndt_as_ndarray(ndt_ndarray_t *a, const ndt_t *t, ndt_context_t *ctx);
NDTYPES_API ndt_ssize_t ndt_hash(ndt_t *t, ndt_context_t *ctx);
/*****************************************************************************/
/* Fields and values */
/*****************************************************************************/
/* Fields */
NDTYPES_API ndt_field_t *ndt_field(char *name, ndt_t *type, uint16_opt_t align,
uint16_opt_t pack, uint16_opt_t pad, ndt_context_t *ctx);
NDTYPES_API void ndt_field_del(ndt_field_t *field);
NDTYPES_API void ndt_field_array_del(ndt_field_t *fields, int64_t shape);
/* Typed values */
NDTYPES_API void ndt_value_del(ndt_value_t *mem);
NDTYPES_API void ndt_value_array_del(ndt_value_t *types, int64_t ntypes);
NDTYPES_API ndt_value_t *ndt_value_from_number(enum ndt_value tag, char *v, ndt_context_t *ctx);
NDTYPES_API ndt_value_t *ndt_value_from_string(char *v, ndt_context_t *ctx);
NDTYPES_API ndt_value_t *ndt_value_na(ndt_context_t *ctx);
NDTYPES_API int ndt_value_equal(const ndt_value_t *x, const ndt_value_t *y);
NDTYPES_API int ndt_value_mem_equal(const ndt_value_t *x, const ndt_value_t *y);
NDTYPES_API int ndt_value_compare(const ndt_value_t *x, const ndt_value_t *y);
/* Type array */
NDTYPES_API void ndt_type_array_clear(ndt_t **types, int64_t shape);
NDTYPES_API void ndt_type_array_del(ndt_t **types, int64_t shape);
/*****************************************************************************/
/* Encodings */
/*****************************************************************************/
NDTYPES_API enum ndt_encoding ndt_encoding_from_string(const char *s, ndt_context_t *ctx);
NDTYPES_API const char *ndt_encoding_as_string(enum ndt_encoding encoding);
NDTYPES_API size_t ndt_sizeof_encoding(enum ndt_encoding encoding);
NDTYPES_API uint16_t ndt_alignof_encoding(enum ndt_encoding encoding);
/*****************************************************************************/
/* Predicates */
/*****************************************************************************/
NDTYPES_API int ndt_is_abstract(const ndt_t *t);
NDTYPES_API int ndt_is_concrete(const ndt_t *t);
NDTYPES_API int ndt_is_optional(const ndt_t *t);
NDTYPES_API int ndt_subtree_is_optional(const ndt_t *t);
NDTYPES_API int ndt_is_ndarray(const ndt_t *t);
NDTYPES_API int ndt_is_c_contiguous(const ndt_t *t);
NDTYPES_API int ndt_is_f_contiguous(const ndt_t *t);
NDTYPES_API int ndt_really_fortran(const ndt_t *t);
NDTYPES_API int ndt_is_scalar(const ndt_t *t);
NDTYPES_API int ndt_is_signed(const ndt_t *t);
NDTYPES_API int ndt_is_unsigned(const ndt_t *t);
NDTYPES_API int ndt_is_float(const ndt_t *t);
NDTYPES_API int ndt_is_complex(const ndt_t *t);
NDTYPES_API int ndt_endian_is_set(const ndt_t *t);
NDTYPES_API int ndt_is_little_endian(const ndt_t *t);
NDTYPES_API int ndt_is_big_endian(const ndt_t *t);
/*****************************************************************************/
/* Functions */
/*****************************************************************************/
NDTYPES_API ndt_t *ndt_copy(const ndt_t *t, ndt_context_t *ctx);
NDTYPES_API int ndt_equal(const ndt_t *t, const ndt_t *u);
NDTYPES_API int ndt_match(const ndt_t *p, const ndt_t *c, ndt_context_t *ctx);
NDTYPES_API int ndt_typecheck(ndt_apply_spec_t *spec, const ndt_t *sig,
const ndt_t *in[], const int nin, ndt_context_t *ctx);
NDTYPES_API int64_t ndt_itemsize(const ndt_t *t);
/*****************************************************************************/
/* Input/Output */
/*****************************************************************************/
/*** String to primitive conversion ***/
NDTYPES_API bool ndt_strtobool(const char *v, ndt_context_t *ctx);
NDTYPES_API char ndt_strtochar(const char *v, ndt_context_t *ctx);
NDTYPES_API long ndt_strtol(const char *v, long min, long max, ndt_context_t *ctx);
NDTYPES_API long long ndt_strtoll(const char *v, long long min, long long max, ndt_context_t *ctx);
NDTYPES_API unsigned long ndt_strtoul(const char *v, unsigned long max, ndt_context_t *ctx);
NDTYPES_API unsigned long long ndt_strtoull(const char *v, unsigned long long max, ndt_context_t *ctx);
NDTYPES_API float ndt_strtof(const char *v, ndt_context_t *ctx);
NDTYPES_API double ndt_strtod(const char *v, ndt_context_t *ctx);
/*** Type to string conversion ***/
NDTYPES_API char *ndt_as_string(const ndt_t *t, ndt_context_t *ctx);
NDTYPES_API char *ndt_indent(const ndt_t *t, ndt_context_t *ctx);
NDTYPES_API char *ndt_ast_repr(const ndt_t *t, ndt_context_t *ctx);
/*****************************************************************************/
/* Typedef */
/*****************************************************************************/
typedef bool (* ndt_constraint_t)(void *, ndt_context_t *);
typedef struct {
const ndt_t *type;
ndt_constraint_t constraint;
} ndt_typedef_t;
/* Typedef for nominal types */
NDTYPES_API int ndt_typedef_add(const char *name, ndt_t *type, ndt_constraint_t f, ndt_context_t *ctx);
NDTYPES_API const ndt_typedef_t *ndt_typedef_find(const char *name, ndt_context_t *ctx);
NDTYPES_API int ndt_typedef(const char *name, ndt_t *type, ndt_constraint_t f, ndt_context_t *ctx);
NDTYPES_API int ndt_typedef_from_string(const char *name, const char *type, ndt_constraint_t f, ndt_context_t *ctx);
/*****************************************************************************/
/* Allocate types */
/*****************************************************************************/
NDTYPES_API ndt_t *ndt_new(enum ndt tag, ndt_context_t *ctx);
NDTYPES_API ndt_t *ndt_function_new(int64_t nargs, ndt_context_t *ctx);
NDTYPES_API ndt_t *ndt_tuple_new(enum ndt_variadic flag, int64_t shape, ndt_context_t *ctx);
NDTYPES_API ndt_t *ndt_record_new(enum ndt_variadic flag, int64_t shape, ndt_context_t *ctx);
NDTYPES_API void ndt_del(ndt_t *t);
/*****************************************************************************/
/* Construct types */
/*****************************************************************************/
/* Special types */
NDTYPES_API ndt_t *ndt_option(ndt_t *type);
NDTYPES_API ndt_t *ndt_module(char *name, ndt_t *type, ndt_context_t *ctx);
NDTYPES_API ndt_t *ndt_function(ndt_t * const *types, int64_t nargs, int64_t nin, int64_t nout, ndt_context_t *ctx);
/* Any */
NDTYPES_API ndt_t *ndt_any_kind(ndt_context_t *ctx);
/* Dimensions */
NDTYPES_API ndt_t *ndt_to_fortran(const ndt_t *type, ndt_context_t *ctx);
NDTYPES_API ndt_t *ndt_fixed_dim(ndt_t *type, int64_t shape, int64_t step, ndt_context_t *ctx);
NDTYPES_API ndt_t *ndt_abstract_var_dim(ndt_t *type, ndt_context_t *ctx);
NDTYPES_API int64_t ndt_var_indices(int64_t *res_start, int64_t *res_step, const ndt_t *t,
int64_t index, ndt_context_t *ctx);
NDTYPES_API ndt_slice_t *ndt_var_add_slice(int32_t *nslices, const ndt_t *t,
int64_t start, int64_t stop, int64_t step,
ndt_context_t *ctx);
NDTYPES_API ndt_t *ndt_var_dim(ndt_t *type,
enum ndt_offsets flag, int32_t noffsets, const int32_t *offsets,
int32_t nslices, ndt_slice_t *slices,
ndt_context_t *ctx);
NDTYPES_API ndt_t *ndt_symbolic_dim(char *name, ndt_t *type, ndt_context_t *ctx);
NDTYPES_API ndt_t *ndt_ellipsis_dim(char *name, ndt_t *type, ndt_context_t *ctx);
/* Dtypes */
NDTYPES_API ndt_t *ndt_tuple(enum ndt_variadic flag, ndt_field_t *fields, int64_t shape,
uint16_opt_t align, uint16_opt_t pack, ndt_context_t *ctx);
NDTYPES_API ndt_t *ndt_record(enum ndt_variadic flag, ndt_field_t *fields, int64_t shape,
uint16_opt_t align, uint16_opt_t pack, ndt_context_t *ctx);
NDTYPES_API ndt_t *ndt_ref(ndt_t *type, ndt_context_t *ctx);
NDTYPES_API ndt_t *ndt_constr(char *name, ndt_t *type, ndt_context_t *ctx);
NDTYPES_API ndt_t *ndt_nominal(char *name, ndt_t *type, ndt_context_t *ctx);
/* Scalars */
NDTYPES_API ndt_t *ndt_scalar_kind(ndt_context_t *ctx);
NDTYPES_API ndt_t *ndt_categorical(ndt_value_t *types, int64_t ntypes, ndt_context_t *ctx);
NDTYPES_API ndt_t *ndt_fixed_string_kind(ndt_context_t *ctx);
NDTYPES_API ndt_t *ndt_fixed_string(int64_t size, enum ndt_encoding encoding, ndt_context_t *ctx);
NDTYPES_API ndt_t *ndt_fixed_bytes_kind(ndt_context_t *ctx);
NDTYPES_API ndt_t *ndt_fixed_bytes(int64_t size, uint16_opt_t align, ndt_context_t *ctx);
NDTYPES_API ndt_t *ndt_string(ndt_context_t *ctx);
NDTYPES_API ndt_t *ndt_bytes(uint16_opt_t target_align, ndt_context_t *ctx);
NDTYPES_API ndt_t *ndt_char(enum ndt_encoding encoding, ndt_context_t *ctx);
NDTYPES_API ndt_t *ndt_signed_kind(ndt_context_t *ctx);
NDTYPES_API ndt_t *ndt_unsigned_kind(ndt_context_t *ctx);
NDTYPES_API ndt_t *ndt_float_kind(ndt_context_t *ctx);
NDTYPES_API ndt_t *ndt_complex_kind(ndt_context_t *ctx);
NDTYPES_API ndt_t *ndt_primitive(enum ndt tag, uint32_t flags, ndt_context_t *ctx);
NDTYPES_API ndt_t *ndt_signed(int size, uint32_t flags, ndt_context_t *ctx);
NDTYPES_API ndt_t *ndt_unsigned(int size, uint32_t flags, ndt_context_t *ctx);
NDTYPES_API ndt_t *ndt_from_alias(enum ndt_alias tag, uint32_t flags, ndt_context_t *ctx);
/* Type variable */
NDTYPES_API ndt_t *ndt_typevar(char *name, ndt_context_t *ctx);
/******************************************************************************/
/* Parsing */
/******************************************************************************/
/* Metadata is currently limited to var-dimension offsets. */
/*
* Metadata is read from the type string and managed by the type. This is
* convenient but can waste a lot of space when offset arrays are large.
*/
NDTYPES_API ndt_t *ndt_from_file(const char *name, ndt_context_t *ctx);
NDTYPES_API ndt_t *ndt_from_string(const char *input, ndt_context_t *ctx);
NDTYPES_API ndt_t *ndt_from_bpformat(const char *input, ndt_context_t *ctx);
/*
* Metadata is read from the type string and extracted for external management.
* The type still has pointers to the metadata. This scheme is used for sharing
* offsets between copies or subtypes of a type.
*/
NDTYPES_API ndt_t *ndt_from_file_fill_meta(ndt_meta_t *m, const char *name, ndt_context_t *ctx);
NDTYPES_API ndt_t *ndt_from_string_fill_meta(ndt_meta_t *m, const char *input, ndt_context_t *ctx);
/* Metadata is provided and managed by an external source. */
NDTYPES_API ndt_t *ndt_from_metadata_and_dtype(const ndt_meta_t *m, const char *dtype, ndt_context_t *ctx);
/******************************************************************************/
/* Library initialization and tables */
/******************************************************************************/
NDTYPES_API int ndt_init(ndt_context_t *ctx);
NDTYPES_API void ndt_finalize(void);
/******************************************************************************/
/* Error Macros */
/******************************************************************************/
#define ndt_internal_error(msg) \
do { \
fprintf(stderr, "%s:%d: internal error: %s\n", __FILE__, __LINE__, msg); \
abort(); \
} while (0)
#define ndt_warn(msg) \
do { \
fprintf(stderr, "%s:%d: warning: %s\n", __FILE__, __LINE__, msg); \
} while (0)
/******************************************************************************/
/* Memory handling */
/******************************************************************************/
NDTYPES_API extern void *(* ndt_mallocfunc)(size_t size);
NDTYPES_API extern void *(* ndt_callocfunc)(size_t nmemb, size_t size);
NDTYPES_API extern void *(* ndt_reallocfunc)(void *ptr, size_t size);
NDTYPES_API extern void (* ndt_freefunc)(void *ptr);
NDTYPES_API void *ndt_alloc(int64_t nmemb, int64_t size);
NDTYPES_API void *ndt_alloc_size(size_t size);
NDTYPES_API void *ndt_calloc(int64_t nmemb, int64_t size);
NDTYPES_API void *ndt_realloc(void *ptr, int64_t nmemb, int64_t size);
NDTYPES_API void ndt_free(void *ptr);
NDTYPES_API void *ndt_aligned_calloc(uint16_t alignment, int64_t size);
NDTYPES_API void ndt_aligned_free(void *ptr);
/******************************************************************************/
/* Low level details */
/******************************************************************************/
typedef struct {
int64_t size;
uint8_t *data;
} ndt_bytes_t;
typedef int64_t ndt_categorical_t;
#endif /* NDTYPES_H */